mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Merge branch 'master' of https://github.com/mariwahl/Over-100-Exercises-Python-and-Algorithms
This commit is contained in:
commit
895214fa59
3
.gitignore
vendored
3
.gitignore
vendored
@ -34,3 +34,6 @@ nosetests.xml
|
|||||||
.mr.developer.cfg
|
.mr.developer.cfg
|
||||||
.project
|
.project
|
||||||
.pydevproject
|
.pydevproject
|
||||||
|
|
||||||
|
# PyCharm
|
||||||
|
.idea
|
@ -1,10 +1,19 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
# Mari von Steinkirch @ 2013
|
||||||
|
# mari.wahl9@gmail.com
|
||||||
|
|
||||||
|
# Bernardo Sulzbach (mafagafo) @ 2014
|
||||||
|
# 1449441@gmail.com
|
||||||
|
|
||||||
|
|
||||||
def bubble_sort(seq):
|
def bubble_sort(seq):
|
||||||
''' implement bubble sort, a O(n^2) not efficient algorithm'''
|
"""
|
||||||
|
Implementation of bubble sort.
|
||||||
|
O(n²) and thus highly ineffective.
|
||||||
|
:param seq: the sequence to be sorted.
|
||||||
|
:return: the sorted sequence.
|
||||||
|
"""
|
||||||
size = len(seq) -1
|
size = len(seq) -1
|
||||||
for num in range(size, 0, -1):
|
for num in range(size, 0, -1):
|
||||||
for i in range(num):
|
for i in range(num):
|
||||||
@ -13,7 +22,6 @@ def bubble_sort(seq):
|
|||||||
seq[i] = seq[i+1]
|
seq[i] = seq[i+1]
|
||||||
seq[i+1] = temp
|
seq[i+1] = temp
|
||||||
return seq
|
return seq
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_bubble_sort(module_name='this module'):
|
def test_bubble_sort(module_name='this module'):
|
||||||
|
@ -1,26 +1,63 @@
|
|||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
|
||||||
|
|
||||||
|
# Mari von Steinkirch @ 2013
|
||||||
def reverse_str(s):
|
# mari.wahl9@gmail.com
|
||||||
''' in place '''
|
|
||||||
sr_ls = []
|
# Bernardo Sulzbach (mafagafo) @ 2014
|
||||||
for i in range(len(s)-1, -1, -1):
|
# 1449441@gmail.com
|
||||||
sr_ls.append(s[i])
|
|
||||||
return ''.join(sr_ls)
|
# This file defines and compares four different functions that reverse a string.
|
||||||
|
|
||||||
|
# timeit is used for benchmarking.
|
||||||
|
from timeit import timeit
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_1(string):
|
||||||
|
"""
|
||||||
|
Slowest function. Use a list and str.join to reverse the string.
|
||||||
|
:param string: the string to be reversed.
|
||||||
|
:return: a reversed string.
|
||||||
|
"""
|
||||||
|
reversed_string = []
|
||||||
|
# Iterates from the last to the first character.
|
||||||
|
for i in range(len(string) - 1, -1, -1):
|
||||||
|
# Appends the character to the list.
|
||||||
|
reversed_string.append(string[i])
|
||||||
|
return ''.join(reversed_string)
|
||||||
|
|
||||||
def main():
|
|
||||||
s1 = 'abcdefg'
|
|
||||||
s2 = 'buffy'
|
|
||||||
s3 = ''
|
|
||||||
print(reverse_str(s1))
|
|
||||||
print(reverse_str(s2))
|
|
||||||
print(reverse_str(s3))
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
# We define some short strings to test our functions.
|
||||||
|
strings = ('buffy', 'foo', 'bar')
|
||||||
|
# We announce what we are doing.
|
||||||
|
print(', '.join(strings), ' should appear reversed if the function is 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')
|
||||||
|
# We wait until the user wants to quit.
|
||||||
|
input('Press Return to quit.')
|
||||||
|
45
src/further_examples/arrays_and_strings/reverse_words.py
Normal file
45
src/further_examples/arrays_and_strings/reverse_words.py
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# Mari von Steinkirch @ 2013
|
||||||
|
# mari.wahl9@gmail.com
|
||||||
|
|
||||||
|
# Bernardo Sulzbach (mafagafo) @ 2014
|
||||||
|
# 1449441@gmail.com
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_words(string):
|
||||||
|
"""
|
||||||
|
Reverse the order of the words in a sentence.
|
||||||
|
:param string: the string which words wil lbe reversed.
|
||||||
|
:return: the reversed string.
|
||||||
|
"""
|
||||||
|
l1 = string.split(' ')
|
||||||
|
l1.reverse()
|
||||||
|
return ' '.join(l1)
|
||||||
|
|
||||||
|
|
||||||
|
def reverse_words_brute(string):
|
||||||
|
"""
|
||||||
|
Reverse the order of the words in a sentence.
|
||||||
|
:param string: the string which words wil lbe reversed.
|
||||||
|
:return: the reversed string.
|
||||||
|
"""
|
||||||
|
word, sentence = [], []
|
||||||
|
for character in string:
|
||||||
|
if character != ' ':
|
||||||
|
word.append(character)
|
||||||
|
else:
|
||||||
|
# So we do not keep multiple whitespaces. An empty list evaluates to False.
|
||||||
|
if word:
|
||||||
|
sentence.append(''.join(word))
|
||||||
|
word = []
|
||||||
|
# So we do not keep multiple whitespaces. An empty list evaluates to False.
|
||||||
|
if word != '':
|
||||||
|
sentence.append(''.join(word))
|
||||||
|
sentence.reverse()
|
||||||
|
return ' '.join(sentence)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
phrase = 'I love Google, Fedora & Python!'
|
||||||
|
print(reverse_words(phrase))
|
||||||
|
print(reverse_words_brute(phrase))
|
@ -1,44 +0,0 @@
|
|||||||
#!/usr/bin/python3
|
|
||||||
# mari von steinkirch @2013
|
|
||||||
# steinkirch at gmail
|
|
||||||
|
|
||||||
''' reverse words in a setence, keeping the words right
|
|
||||||
>>> str1 = 'I love Google and Python!'
|
|
||||||
>>> reverse_words(str1)
|
|
||||||
'Python! and Google love I'
|
|
||||||
>>> reverse_words('bla')
|
|
||||||
'bla'
|
|
||||||
>>> reverse_words('')
|
|
||||||
''
|
|
||||||
>>> reverse_words_brute(str1)
|
|
||||||
'Python! and Google love I'
|
|
||||||
>>> reverse_words_brute('bla')
|
|
||||||
'bla'
|
|
||||||
>>> reverse_words_brute('')
|
|
||||||
''
|
|
||||||
'''
|
|
||||||
|
|
||||||
def reverse_words(str1):
|
|
||||||
l1 = str1.split(' ')
|
|
||||||
l1.reverse()
|
|
||||||
return ' '.join(l1)
|
|
||||||
|
|
||||||
def reverse_words_brute(str1):
|
|
||||||
aux_lt = []
|
|
||||||
aux_str = ''
|
|
||||||
for i, c in enumerate(str1):
|
|
||||||
if c != ' ':
|
|
||||||
aux_str += c # WE COULD HAVE USED LT HERE, MORE EFFICIENT
|
|
||||||
elif c == ' ':
|
|
||||||
aux_lt.append(aux_str) # WE COULD HAVE USED STR BUT NOT EFFICIENT!
|
|
||||||
aux_str = ''
|
|
||||||
aux_lt.append(aux_str) # REMEMBER THAT THE LAST ONE DOEN'T HAVE SPACE!
|
|
||||||
aux_lt.reverse()
|
|
||||||
return ' '.join(aux_lt)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
import doctest
|
|
||||||
doctest.testmod()
|
|
||||||
|
|
@ -1,9 +1,20 @@
|
|||||||
import sys
|
import sys
|
||||||
print "System version:",sys.version
|
print("System version:", sys.version, '\n')
|
||||||
import numpy as np
|
|
||||||
print "Numpy version:",np.version.version
|
|
||||||
import matplotlib
|
|
||||||
print "Matplotlib version:",matplotlib.__version__
|
|
||||||
import pandas as pd
|
|
||||||
print "Pandas version:",pd.__version__
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
import numpy
|
||||||
|
print("\nNumpy version:", numpy.__version__)
|
||||||
|
except ImportError as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import matplotlib
|
||||||
|
print("\nMatplotlib version:", matplotlib.__version__)
|
||||||
|
except ImportError as e:
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pandas
|
||||||
|
print("\nPandas version:", pandas.__version__)
|
||||||
|
except ImportError as e:
|
||||||
|
print(e)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user