I got the question:
A special text code is invented to jump from one letter to another in
some text.
In this simplified form, text will only use letters A - F and each
character has the following 'jump' meaning.
A = jump back 3 characters
B = jump back 2 characters
C = jump back 1 character
D = jump forward 1 character
E = jump forward 2 characters
F = jump forward 3 characters
You always start on the first letter of the input text and follow the
instructions.
For example consider the input DEACFB
We start on the first letter which is D
DEACFB
^
DEACFB (after hop 1)
^
DEACFB (after hop 2)
^
DEACFB (after hop 3)
^
DEACFB (after hop 4)
^
By following the instructions, sooner or later you must either jump
off of the word (either to the left or to the right) or you must get
stuck in an infinite loop. Note that the latter letters FB were never
reached.
Input some text and output the number of hops required to jump off of
the text or output infinite if it would never do so.
Input Format:
A single line of text
Output Format:
A positive whole number or the single lower-case word infinite
Constraints:
The word will be less than 100 characters long.
My code:
inp = input()
if len(inp) < 101:
place = 0
list_of_letters = []
inf = False
while not inf:
if place >= len(inp) or place < 0:
print(len(list_of_letters))
inf = True #even though its not inf!
elif place in list_of_letters:
print('infinite')
inf = True
elif inp[place] == 'A':
place -= 3
list_of_letters.append(place)
elif inp[place] == 'B':
place -= 2
list_of_letters.append(place)
elif inp[place] == 'C':
place -= 1
list_of_letters.append(place)
elif inp[place] == 'D':
place += 1
list_of_letters.append(place)
elif inp[place] == 'E':
place += 2
list_of_letters.append(place)
elif inp[place] == 'F':
place += 3
list_of_letters.append(place)
for some reason, the output is never what I desire:
It has to either print the correct number or 'infinite' when it's stuck in a loop.
Can you please help me?
question from:
https://stackoverflow.com/questions/65882825/the-text-hopping-python-problem-why-does-my-code-disfunction 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…