Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
871 views
in Technique[技术] by (71.8m points)

pseudocode - How can I write a pseudo code for concatenating string from a list of strings?

Suppose you have a list of strings.

List = {"A","AB","ABCD"}

Now I want to concatenate each elements as a string. But only if the element's string length is perfect square of an integer 1,2,3,4,5... N (1,4,9,16,25... n^2). If all of the elements are not perfect square of an integer, than return ? (epsilon) to denote no elements were concatenated.

First element on the List, since the element length is square of 1, A, concatenate.

List' = {A}

Second element on the List, since the element length is 2, AB, no concatenation.

List' = {A}

Third element on the List, since the element length is 4, ABCE, concatenate.

List' = {A,ABCD}

Suppose the the input list was

List2 = {"AB", ""ABC, "AABCC"}

Then I would return ? (epsilon), since all the elements in the list are not perfect square of an integer.

Now consider more general form, let 'a' be the first index of the list and 'b' be the last index of the list.

List3 = {a th . . . b th}

How can I write a recursive algorithm pseudo code to do this?

Here is what I attempted, but I felt that I was heading in the wrong direction.

Concat(A[a...b]){
     //Base case .. ?
     if(  ){
          
     }
     
     //Recursive step
     else{
          if(A[a]--> peftectSquare{
               //if last element, concatenate.
               if(a = b){
                    return A[a]
               }
               //if not last element, concatenate and recursive call.
               else{
                    return A[a] (concatenate) Concat(A[a+1...b])
               }
          }
          else{
               //If last element and is not perfect square, do nothing.
               if(a = b){
                    return
               }
               //If the element is not a perfect square recursive call.
               else{
                    return Concat(A[a+1...b])
               }
          }
     }
}
question from:https://stackoverflow.com/questions/66057061/how-can-i-write-a-pseudo-code-for-concatenating-string-from-a-list-of-strings

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...