Building on Caridorc's answer Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(range(len(string))
Here's an explanation from furthest depth outward:
range(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(range(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.
Building on Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(range(len(string))
Here's an explanation from furthest depth outward:
range(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(range(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.
Building on Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(range(len(string))
Here's an explanation from furthest depth outward:
range(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(range(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.
Building on Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(xrangerange(len(string))
Here's an explanation from furthest depth outward:
xrangerange(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(xrangerange(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.
Building on Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(xrange(len(string))
Here's an explanation from furthest depth outward:
xrange(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(xrange(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.
Building on Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(range(len(string))
Here's an explanation from furthest depth outward:
range(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(range(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.
Building on Caridorc's answer, there's a more Pythonic way to approach the problem which has an even better readability:
def reverse(string):
return ''.join(string[i] for i in reversed(xrange(len(string))
Here's an explanation from furthest depth outward:
xrange(len(string))
: Create an iterator object which iterates from 0 tolen(string) - 1
(ie: the parameter is non-inclusive).reversed(xrange(len(string)))
: Reverse this iterator so you're starting atlen(string) - 1
and going backward one-by-one to 0.[string[i] for i in ...]
: This is a list comprehension, basically a way to create a list with a single line.''.join(...)
: Join each element together into a string from the list, using an empty string as a separator.
Documentation can be found for each of these concepts here:
- PEP 281: loop counters with range and xrange.
- PEP 322: reversed iteration.
- PEP 202: list comprehensions.
The hope is that the above code will be fairly readable to someone nominally proficient in Python.