Reverse a string without using reversed() or [::-1]?

前端 未结 30 2520
南旧
南旧 2020-11-30 19:44

I came across a strange Codecademy exercise that required a function that would take a string as input and return it in reverse order. The only problem was you could not use

相关标签:
30条回答
  • 2020-11-30 20:06

    Only been coding Python for a few days, but I feel like this was a fairly clean solution. Create an empty list, loop through each letter in the string and append it to the front of the list, return the joined list as a string.

    def reverse(text):
    backwardstext = []
    for letter in text:
        backwardstext.insert(0, letter)
    return ''.join(backwardstext)
    
    0 讨论(0)
  • 2020-11-30 20:06

    I used this:

    def reverse(text):
    s=""
    l=len(text)
    for i in range(l):
        s+=text[l-1-i]
    return s
    
    0 讨论(0)
  • 2020-11-30 20:07

    I have also just solved the coresponding exercise on codeacademy and wanted to compare my approach to others. I have not found the solution I used so far, so I thought that I sign up here and provide my solution to others. And maybe I get a suggestion or a helpful comment on how to improve the code.

    Ok here it goes, I did not use any list to store the string, instead I have just accessed the string index. It took me a bit at first to deal with the len() and index number, but in the end it worked :).

    def reverse(x):
    reversestring = ""
    for n in range(len(str(x))-1,-1, -1):
        reversestring += x[n]
    return reversestring 
    

    I am still wondering if the reversestring = "" could be solved in a more elegant way, or if it is "bad style" even, but i couldn't find an answer so far.

    0 讨论(0)
  • 2020-11-30 20:08

    This is my solution using the for i in range loop:

    def reverse(string):
        tmp = ""
        for i in range(1,len(string)+1):
            tmp += string[len(string)-i]            
        return tmp
    

    It's pretty easy to understand. I start from 1 to avoid index out of bound.

    0 讨论(0)
  • 2020-11-30 20:09

    The way I can think of without using any built-in functions:

    a = 'word'
    count = 0
    for letter in a:
        count += 1
    
    b = ''
    for letter in a:
        b += a[count-1]
        count -= 1
    

    And if you print b:

    print b
    drow
    
    0 讨论(0)
  • 2020-11-30 20:11

    I created different versions of how to reverse a string in python in my repo: https://github.com/fedmich/Python-Codes/tree/master/Reverse%20a%20String

    You can do it by using list-comprehension or lambda technique:

    # Reverse a string without using reverse() function
    s = 'Federico';
    li = list( s )  #convert string to list
    
    ret = [ li[i-1] for i in xrange(len(li),0,-1)  ]    #1 liner lambda
    print ( "".join( ret ) )
    

    or by doing a backward for loop

    # Reverse a string without using reverse() function
    s = 'Federico';
    r = []
    
    length = len(s)
    for i in xrange(length,0,-1):
        r.append( s[ i - 1] )
    
    print ( "".join(r) )
    
    0 讨论(0)
提交回复
热议问题