mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
More changes
This commit is contained in:
parent
2835a0d27e
commit
a870dd74a6
@ -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):
|
||||||
@ -15,7 +24,6 @@ def bubble_sort(seq):
|
|||||||
return seq
|
return seq
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_bubble_sort(module_name='this module'):
|
def test_bubble_sort(module_name='this module'):
|
||||||
seq = [4, 5, 2, 1, 6, 2, 7, 10, 13, 8]
|
seq = [4, 5, 2, 1, 6, 2, 7, 10, 13, 8]
|
||||||
assert(bubble_sort(seq) == sorted(seq))
|
assert(bubble_sort(seq) == sorted(seq))
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
# Mari von Steinkirch @ 2013
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
# Mari von Steinkirch @ 2013
|
||||||
# mari.wahl9@gmail.com
|
# mari.wahl9@gmail.com
|
||||||
|
|
||||||
# Bernardo Sulzbach (mafagafo) @ 2014
|
# Bernardo Sulzbach (mafagafo) @ 2014
|
||||||
|
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()
|
|
||||||
|
|
@ -3,7 +3,7 @@ print("System version:", sys.version, '\n')
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import numpy
|
import numpy
|
||||||
print("\nNumpy version:", numpy.version.version)
|
print("\nNumpy version:", numpy.__version__)
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user