Home > Back-end >  python question about loops comparing string to string
python question about loops comparing string to string

Time:10-28

I am trying to emulate a circumstance where i send information and only get a true or false as a return. So i can check each character and if it is true, that means that character is in the string. I would know there would be a position 0 to some number x. I would receive a true result and eventually only receive false result and then I would know the string has been solved. In my circumstance i would not know the target string.

I am trying to iterate through all characters and see if it matches the string character. if it does, I add the character to a list until the list contains all the characters of the string. but for some reason, this isn't working.

import string

hi = list()
swoll = "dkjfksjdfksjdkfjksdjfsjkdfjsjreuvnslei"
characters = string.ascii_lowercase   string.ascii_uppercase   string.digits
for ch in characters:
    print(''.join(hi)   ch)
    for i in swoll:
        if i == ch:
            hi.append(ch)
            print(''.join(hi))
            break
        else:
            continue

results:

a
b
c
d
d
de
de
def
def
defg
defh
defi
defi
defij
defij
defijk
defijk
defijkl
defijkl
defijklm
defijkln
defijkln
defijklno
defijklnp
defijklnq
defijklnr
defijklnr
defijklnrs
defijklnrs
defijklnrst
defijklnrsu
defijklnrsu
defijklnrsuv
defijklnrsuv
defijklnrsuvw
defijklnrsuvx
defijklnrsuvy
defijklnrsuvz
defijklnrsuvA
defijklnrsuvB
defijklnrsuvC
defijklnrsuvD
defijklnrsuvE
defijklnrsuvF
defijklnrsuvG
defijklnrsuvH
defijklnrsuvI
defijklnrsuvJ
defijklnrsuvK
defijklnrsuvL
defijklnrsuvM`

As you can see, it does not match the string

When I tried the code above, I was expecting the string to come out the same as the other string.

CodePudding user response:

Based on my understanding of the question, I've implemented a function which I believe emulates the interface you are talking to:

spos = 0
def in_swoll(ch):
    global spos
    
    if spos == len(swoll) or ch != swoll[spos]:
        return False
    spos  = 1
    return True

This will return True and increment the counter into swoll when a character matches, otherwise it will return False.

You can then use this function in a loop which iterates until False is returned for all characters in characters. Inside the loop characters is iterated until a match is found, at which point it is added to hi:

hi = []
while True:
    for ch in characters:
        if in_swoll(ch):
            hi.append(ch)
            print(''.join(hi))
            break
    else:
        # no matches, we're done
        break

Output for your sample data:

d
dk
dkj
dkjf
dkjfk
dkjfks
dkjfksj
dkjfksjd
dkjfksjdf
dkjfksjdfk
dkjfksjdfks
dkjfksjdfksj
dkjfksjdfksjd
dkjfksjdfksjdk
dkjfksjdfksjdkf
dkjfksjdfksjdkfj
dkjfksjdfksjdkfjk
dkjfksjdfksjdkfjks
dkjfksjdfksjdkfjksd
dkjfksjdfksjdkfjksdj
dkjfksjdfksjdkfjksdjf
dkjfksjdfksjdkfjksdjfs
dkjfksjdfksjdkfjksdjfsj
dkjfksjdfksjdkfjksdjfsjk
dkjfksjdfksjdkfjksdjfsjkd
dkjfksjdfksjdkfjksdjfsjkdf
dkjfksjdfksjdkfjksdjfsjkdfj
dkjfksjdfksjdkfjksdjfsjkdfjs
dkjfksjdfksjdkfjksdjfsjkdfjsj
dkjfksjdfksjdkfjksdjfsjkdfjsjr
dkjfksjdfksjdkfjksdjfsjkdfjsjre
dkjfksjdfksjdkfjksdjfsjkdfjsjreu
dkjfksjdfksjdkfjksdjfsjkdfjsjreuv
dkjfksjdfksjdkfjksdjfsjkdfjsjreuvn
dkjfksjdfksjdkfjksdjfsjkdfjsjreuvns
dkjfksjdfksjdkfjksdjfsjkdfjsjreuvnsl
dkjfksjdfksjdkfjksdjfsjkdfjsjreuvnsle
dkjfksjdfksjdkfjksdjfsjkdfjsjreuvnslei
  • Related