mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Now I am happy
This commit is contained in:
parent
7b79f5816a
commit
ac7a68da17
@ -1,18 +1,63 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
# Mari von Steinkirch @ 2013
|
||||||
|
# mari.wahl9@gmail.com
|
||||||
reverse_string = lambda s: s[::-1]
|
|
||||||
|
# Bernardo Sulzbach (mafagafo) @ 2014
|
||||||
|
# 1449441@gmail.com
|
||||||
|
|
||||||
|
# This file defines and compares four different functions that reverse a string.
|
||||||
|
|
||||||
|
# timeit is used for benchmarking.
|
||||||
|
from timeit import timeit
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def reverse_1(string):
|
||||||
s1 = 'abcdefg'
|
"""
|
||||||
s2 = 'buffy'
|
Slowest function. Use a list and str.join to reverse the string.
|
||||||
s3 = ''
|
:param string: the string to be reversed.
|
||||||
print(reverse_string(s1))
|
:return: a reversed string.
|
||||||
print(reverse_string(s2))
|
"""
|
||||||
print(reverse_string(s3))
|
reversed_string = []
|
||||||
|
# Iterates from the last to the first character.
|
||||||
if __name__ == '__main__':
|
for i in range(len(string) - 1, -1, -1):
|
||||||
main()
|
# Appends the character to the list.
|
||||||
|
reversed_string.append(string[i])
|
||||||
|
return ''.join(reversed_string)
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_2(string):
|
||||||
|
"""
|
||||||
|
Same principle as reverse_1. One-liner cousin.
|
||||||
|
:param string: the string to be reversed.
|
||||||
|
:return: a reversed string.
|
||||||
|
"""
|
||||||
|
return ''.join([character for character in [string[i] for i in range(len(string) - 1, -1, -1)]])
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_3(string):
|
||||||
|
"""
|
||||||
|
Another one-liner. We make a list from the characters of the string and reverse it.
|
||||||
|
:param string: the string to be reversed.
|
||||||
|
:return: a reversed string.
|
||||||
|
"""
|
||||||
|
return ''.join([character for character in string][::-1])
|
||||||
|
|
||||||
|
# Overkill of elegance. A bit too passionate but it defines this lambda function well.
|
||||||
|
# Simply returns the string backwards. As fast as concise.
|
||||||
|
reverse_lambda = lambda s: s[::-1]
|
||||||
|
|
||||||
|
# Import a lowercase ASCII alphabet from the string module.
|
||||||
|
from string import ascii_lowercase
|
||||||
|
# We define some short strings to test our functions.
|
||||||
|
strings = (ascii_lowercase, 'buffy', 'foo', 'bar', 'SPAM')
|
||||||
|
# We announce what we are doing.
|
||||||
|
print(', '.join(strings), ' should appear reversed four times if all the functions are working.\n')
|
||||||
|
print('{:<30}:'.format('Function name'), 'benchmarking result (lower is better):')
|
||||||
|
# Iterate over a tuple of functions.
|
||||||
|
for function in (reverse_1, reverse_2, reverse_3, reverse_lambda):
|
||||||
|
name = function.__name__ if function.__name__ != "<lambda>" else 'reverse_lambda'
|
||||||
|
# We print the function's name and its benchmark result.
|
||||||
|
print("{:<30}:".format(name), timeit(name + "('string')", setup='from __main__ import ' + name))
|
||||||
|
# We print the output so that we can check if the function is working as expected.
|
||||||
|
print(', '.join(map(function, strings)), '\n')
|
Loading…
x
Reference in New Issue
Block a user