Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

using

while
.pop()
insert()

pop() the first item in the list and add to the beginning of a new string that will be reversed

# [ ] Challenge: write the code for "reverse a string" reversing some_numbers

some_numbers =[1,2,3,4,5,6,7,8,9,11,22,33,44,55,66,77]

rev_string = []

while len(some_numbers):
    rev = some_numbers.pop()
    rev_string.insert(0,rev)
print(rev)

print(some_numbers)
print(rev_string)

My first question is that:

  1. As i am printing rev in line 7, i am getting 1 as the answer but we know that an empty pop is used to denote the last element then why i am getting 1 instead of 77.
  2. I am getting the right answer by putting 0 inside the pop in line 5. How ?
See Question&Answers more detail:os

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

1 Answer

Your assumption pop() the first item in the list is wrong. When you call some_numbers.pop, you get the last element of the list popped, check the docs. This should answer both of your questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...