Home > OS >  Recreate a string from list of words
Recreate a string from list of words

Time:02-15

Given the following string:

mystring = "today is a very boring day" 

And the list of words:

mylist1 = ["today", "is", "very boring", "today is a", "very boring day"]
mylist2 = ["today", "is", "today is a", "very boring" "very boring day"]
mylist3 = ["is", "a", "today is", "very boring", "very boring day"]

How can I create a function that helps me to .join() elements from any list in order to recreate mystring?

def string_from_list(mylist, mystring):
  return the_matching_string

matching_string1 = string_from_list(mylist1, mystring)
matching_string2 = string_from_list(mylist2, mystring)
matching_string3 = string_from_list(mylist3, mystring)

print(matching_string1)
# Today is a very boring day
print(matching_string2)
# Today is a very boring day
print(matching_string3)
# Today is a very boring day

CodePudding user response:

The solution to your problem as stated is simply:

mystring = "Today is a very boring day"
mylist = ["Today", "is", "very boring", "today is a", "very boring day"]

def string_from_list(mylist, mystring):
    return mystring

matching_string = string_from_list(mylist, mystring)
print(mystring == matching_string)  # True

i.e. passing the original mystring argument to string_from_list makes the problem of returning a matching_string such that mystring == matching_string trivial -- just ignore mylist and return mystring.

The more interesting problem is reconstructing mystring given only mylist. This can be done by combining strings that are subsets of other strings, and joining what remains.

Since you need to combine strings case-insensitively (e.g. to combine Today with today is a), you can then loop back and use non-lowercase strings from the original list to "fix" the case-normalized combined string, yielding the original string.

def string_from_list(mylist: list[str]) -> str:
    # Reconstruct the string, normalized to lowercase.
    mystring = ' '.join(
        s for s in mylist if not any(
            s != t and s.lower() in t.lower() for t in mylist
        )
    )
    # Correct case using hints from original list.
    for s in mylist:
        mystring = mystring.replace(s.lower(), s)
    return mystring


mystring = "Today is a very boring day"
mylist = ["Today", "is", "very boring", "today is a", "very boring day"]

print(string_from_list(mylist))     # Today is a very boring day
matching_string = string_from_list(mylist)
print(mystring == matching_string)  # True

CodePudding user response:

Here's a code which works

mystring = "today is a very boring day"
a="A safe life is not worth living"
list_a= ["A", 'safe',"life","is", "a", "life", "not worth living"]

mylist1 = ["today", "is", "very boring", "today is a", "very boring day"]
mylist2 = ["today", "is", "today is a", "very boring" "very boring day"]
mylist3 = ["is", "a", "today is", "very boring", "very boring day"]


def string_from_list(mylist, mystring):
    mystring_list=mystring.split(" ")
    mylist_mod=[]
    for i in mylist:
        i=i.split(" ")
        if len(i)>1:
            for j in i:
                mylist_mod.append(j)
        else:
            mylist_mod.append(i[0])
    string_return=""
    for i in mystring_list:
        if i in mylist_mod:
            if mystring_list[len(mystring_list)-1]==i:
                string_return =f"{i}"
            else:
                string_return =f"{i} "
        else:
            return "Not possible to covert"
        if string_return==mystring:
            return string_return
        




matching_string1 = string_from_list(mylist1, mystring)
matching_string2 = string_from_list(mylist2, mystring)
matching_string3 = string_from_list(mylist3, mystring)
matching_string4 = string_from_list(list_a, a)

print(matching_string1)
#Today is a very boring day
print(matching_string2)
# Today is a very boring day
print(matching_string3)
# Today is a very boring day
print(matching_string3)

What i have done is:

  1. Converted mystring into a list.
  2. Using a For loop, Splitted the mylist in string_from_list after every instance of " ".
  3. Using a For loop, check if mystring_list indexes exits in mylist_mod, if any single text is missing, then it would return "not possible to convert".
  • Related