Prolog:有没有办法在列表中找到最长的连续数序列并获得其长度?

发布于 2025-01-19 12:22:00 字数 97 浏览 0 评论 0原文

给定5个随机数字的列表(数字范围从1到13),即[1,6,11,12,13],我如何找到连续最长的数字序列(即[11,12,13 ])它的长度必须更大或等于3,并且还必须获得长度?

Given a list of 5 random natural numbers (the number's range is from 1 to 13), i.e. [1,6,11,12,13], how can I find out the longest consecutive number sequence (i.e. [11,12,13]) that its length must be larger or equal to 3, and also get its length?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苏大泽ㄣ 2025-01-26 12:22:00

您可以遍历列表,跟踪当前序列及其长度以及迄今为止找到的最大序列和长度。

处理完所有列表后,您可以检查最大长度是否 >= 3 并选择正确的输出:

longest_sequence([First|L], Len1, Seq) :-
    longest_sequence(L, 1, [First], 0, [], Len, RSeq),
    (   Len>=3
    ->  reverse(RSeq, Seq),
        Len1=Len
    ;   Seq=[],
        Len1=0
    ).
  
longest_sequence([], CurLen, CurSeq, MaxLen, MaxSeq, Len, Seq):-
  (CurLen > MaxLen -> Len-Seq=CurLen-CurSeq ; Len-Seq=MaxLen-MaxSeq).
longest_sequence([CurItem|L], CurLen, [LastItem|CurSeq], MaxLen, MaxSeq, Len, Seq):-
  (
   succ(LastItem, CurItem) ->
    (
      succ(CurLen, CurLen1),
      CurSeq1=[CurItem,LastItem|CurSeq],
      MaxLen1-MaxSeq1=MaxLen-MaxSeq
    ) ;
    (
      (CurLen > MaxLen -> MaxLen1-MaxSeq1=CurLen-[LastItem|CurSeq] ; MaxLen1-MaxSeq1=MaxLen-MaxSeq),
      CurLen1=1,
      CurSeq1=[CurItem]
    )
   ),
   longest_sequence(L, CurLen1, CurSeq1, MaxLen1, MaxSeq1, Len, Seq).

示例运行:

?- longest_sequence([1,6,11,12,13], Len, Seq).
Len = 3,
Seq = [11, 12, 13].

?- longest_sequence([1,6,11,12,13,2,3,4,5], Len, Seq).
Len = 4,
Seq = [2, 3, 4, 5].

?- longest_sequence([1,2], Len, Seq).
Len = 0,
Seq = [].

You can traverse the list keeping track of the current sequence and its length along with the maximum sequence and length found so far.

After processing all the list you can check whether the maximum length is >= 3 and select the proper output:

longest_sequence([First|L], Len1, Seq) :-
    longest_sequence(L, 1, [First], 0, [], Len, RSeq),
    (   Len>=3
    ->  reverse(RSeq, Seq),
        Len1=Len
    ;   Seq=[],
        Len1=0
    ).
  
longest_sequence([], CurLen, CurSeq, MaxLen, MaxSeq, Len, Seq):-
  (CurLen > MaxLen -> Len-Seq=CurLen-CurSeq ; Len-Seq=MaxLen-MaxSeq).
longest_sequence([CurItem|L], CurLen, [LastItem|CurSeq], MaxLen, MaxSeq, Len, Seq):-
  (
   succ(LastItem, CurItem) ->
    (
      succ(CurLen, CurLen1),
      CurSeq1=[CurItem,LastItem|CurSeq],
      MaxLen1-MaxSeq1=MaxLen-MaxSeq
    ) ;
    (
      (CurLen > MaxLen -> MaxLen1-MaxSeq1=CurLen-[LastItem|CurSeq] ; MaxLen1-MaxSeq1=MaxLen-MaxSeq),
      CurLen1=1,
      CurSeq1=[CurItem]
    )
   ),
   longest_sequence(L, CurLen1, CurSeq1, MaxLen1, MaxSeq1, Len, Seq).

Sample runs:

?- longest_sequence([1,6,11,12,13], Len, Seq).
Len = 3,
Seq = [11, 12, 13].

?- longest_sequence([1,6,11,12,13,2,3,4,5], Len, Seq).
Len = 4,
Seq = [2, 3, 4, 5].

?- longest_sequence([1,2], Len, Seq).
Len = 0,
Seq = [].
瘫痪情歌 2025-01-26 12:22:00
def longestSeq(n):
    seqMax = 0
    counter = 0
    i = 0
    while i+1 < len(n):
        curr = n[i+1]
        last = n[i]

        
        sub = curr - last

        if sub == 1:
            if counter == 0:
                counter = 2 
            else:
                counter += 1 
        else:
            if counter > 0:
               if seqMax < counter:
                   seqMax = counter
                   counter = 0 
        i += 1 

    if seqMax < counter:
        seqMax = counter

    if seqMax >= 3:
        return seqMax
    else:
        return 0 
def longestSeq(n):
    seqMax = 0
    counter = 0
    i = 0
    while i+1 < len(n):
        curr = n[i+1]
        last = n[i]

        
        sub = curr - last

        if sub == 1:
            if counter == 0:
                counter = 2 
            else:
                counter += 1 
        else:
            if counter > 0:
               if seqMax < counter:
                   seqMax = counter
                   counter = 0 
        i += 1 

    if seqMax < counter:
        seqMax = counter

    if seqMax >= 3:
        return seqMax
    else:
        return 0 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文