Alternate Python List Reverse Solution Needed

后端 未结 7 648
清酒与你
清酒与你 2021-01-22 09:54

I had a job interview today. During it I was asked to write down an algorithm that will reverse a list. First I offered the answer using the reversed() method:

          


        
7条回答
  •  感情败类
    2021-01-22 10:38

    I'm assuming your interviewer didn't want you to use built-in Python methods, but an algorithm that's more language-agnostic. Something like:

    lst = [1,2,3,4,5]
    lst2 = []
    
    while len(lst) > 0:
        lst2.append(lst.pop())
    

    or

    lst2 = [lst.pop() for _ in lst[:]]
    

提交回复
热议问题