Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

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:

  1. range(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(range(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. 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:

  1. range(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(range(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. 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:

  1. range(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(range(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. PEP 202: list comprehensions.

The hope is that the above code will be fairly readable to someone nominally proficient in Python.

Remove xrange due to Python 3.
Source Link

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:

  1. xrangerange(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(xrangerange(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. 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:

  1. xrange(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(xrange(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. 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:

  1. range(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(range(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. PEP 202: list comprehensions.

The hope is that the above code will be fairly readable to someone nominally proficient in Python.

Source Link

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:

  1. xrange(len(string)): Create an iterator object which iterates from 0 to len(string) - 1 (ie: the parameter is non-inclusive).
  2. reversed(xrange(len(string))): Reverse this iterator so you're starting at len(string) - 1 and going backward one-by-one to 0.
  3. [string[i] for i in ...]: This is a list comprehension, basically a way to create a list with a single line.
  4. ''.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:

  1. PEP 281: loop counters with range and xrange.
  2. PEP 322: reversed iteration.
  3. PEP 202: list comprehensions.

The hope is that the above code will be fairly readable to someone nominally proficient in Python.

lang-py

AltStyle によって変換されたページ (->オリジナル) /