【问题标题】:Detect all k-length words of string in prolog检测prolog中字符串的所有k长度单词
【发布时间】:2017-03-05 11:58:45
【问题描述】:

单词是由空格或字符串的开始/结束点分隔的任何符号字符。例如。 [w,o,r,d,1,' ',w,o,r,d,2].

我需要找到给定字符串的所有 k 长度单词并将它们附加到结果字符串中(用空格分隔)。 这就是我所期望的,例如在 k = 5 的情况下:

?- kthWords([w,o,r,d,1,'',w,r,d,'',w,o,r,d,2], 5, X).
X = [w,o,r,d,1,'',w,o,r,d,2].

【问题讨论】:

    标签: string recursion prolog


    【解决方案1】:

    你可以写:

    final_kthWords(L,K,Outlist):-
             kthWords(L,K,L1),
             reverse(L1,[_|T]),
             reverse(T,Outlist).
    
    kthWords([],_,[]):-!.
    kthWords(L,K,L1):-
         find_word(L,Word,L2),
         length(Word,N),
         (N=:=K-> append(Word,[' '|T],L1),kthWords(L2,K,T);
         kthWords(L2,K,L1)).
    
    find_word([],[],[]).
    find_word([H|T],[H|T1],L):-dif(H,' '),find_word(T,T1,L).
    find_word([H|T],[],T):- H = ' '.
    

    其中kthWords/3 调用find_word/2 找到单词,最后kthWords 返回输出列表,但最后添加了' 'final_kthWords(L,K,Outlist)/3 唯一做的就是删除列表末尾多余的 ' ' 并返回正确的列表:

    ?- final_kthWords([w,o,r,d,1,' ',w,r,d,' ',w,o,r,d,2], 5, X).
    X = [w, o, r, d, 1, ' ', w, o, r, d, 2] ;
    false.
    

    【讨论】:

      【解决方案2】:

      希望其他人可以提出一个更简单的解决方案...以下似乎可行

      kthWordsH([], 0, _, R0, R0).
      
      kthWordsH([], N, _, _, []) :-
        N \= 0.
      
      kthWordsH([' ' | Tl], 0, Len, W, Revult) :-
        kthWordsH(Tl, Len, Len, [], Res0),
        append(Res0, [' ' | W], Revult).
      
      kthWordsH([' ' | Tl], N, Len, _, Revult) :-
        N \= 0,
        kthWordsH(Tl, Len, Len, [], Revult).
      
      kthWordsH([H | Tl], 0, Len, _, Revult) :-
        H \= ' ',
        kthWordsH(Tl, Len, Len, [], Revult).
      
      kthWordsH([H | Tl], N, Len, Tw, Revult) :-
        H \= ' ',
        N \= 0,
        Nm1 is N-1,
        kthWordsH(Tl, Nm1, Len, [H | Tw], Revult).
      
      kthWords(List, Len, Result) :-
        kthWordsH(List, Len, Len, [], Revult),
        reverse(Revult, Result).
      

      【讨论】:

      • 嗯,有趣的解决方案:) +1
      【解决方案3】:

      没有反向的解决方案。

      % return a word of k length, or return [] otherwise
      kword(K,L,W):- 
          length(L,K) -> append(L,[' '],W); W=[].
      
      % if no more chars, then check final word in L and
      % append to word list Ls to return Lw
      kwords(K,[],L,Ls,Lw):- 
          kword(K,L,W),
          append(Ls,W,Lw).
      
      % if char is space, then append to Ls if word of length K
      % if not space, append char to "in progress" work list L
      kwords(K,[C|Cs],L,Ls,Lw):- 
          (   C=' ' -> 
              (   kword(K,L,W),
                  append(Ls,W,Ls0),
                  L2 = []
              );
              (   append(L,[C],L2),
                  Ls0 = Ls
              )
          ),
          kwords(K,Cs,L2,Ls0,Lw).
      
      % intialise predicate call with empty word and empty result
      kthWords(Cs,K,L):- kwords(K,Cs,[],[],L).
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-22
        • 2021-02-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多