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
1.1k views
in Technique[技术] by (71.8m points)

the Text Hopping python problem -- why does my code disfunction?

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

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

1 Reply

0 votes
by (71.8m points)

You are appending the future place instead of the current place to list_of_letters, which is not correct, other than that it works ok.

Your code with a simple fix:

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':
      list_of_letters.append(place)
      place -= 3
    elif inp[place] == 'B':
      list_of_letters.append(place)
      place -= 2
    elif inp[place] == 'C':
      list_of_letters.append(place)
      place -= 1
    elif inp[place] == 'D':
      list_of_letters.append(place)
      place += 1
    elif inp[place] == 'E':
      list_of_letters.append(place)
      place += 2
    elif inp[place] == 'F':
      list_of_letters.append(place)
      place += 3

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

...