More changes

This commit is contained in:
Bernardo Sulzbach 2014-07-08 15:15:02 -03:00
parent 2835a0d27e
commit a870dd74a6
5 changed files with 61 additions and 50 deletions

View File

@ -1,10 +1,19 @@
#!/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):
''' implement bubble sort, a O(n^2) not efficient algorithm'''
"""
Implementation of bubble sort.
O() and thus highly ineffective.
:param seq: the sequence to be sorted.
:return: the sorted sequence.
"""
size = len(seq) -1
for num in range(size, 0, -1):
for i in range(num):
@ -13,7 +22,6 @@ def bubble_sort(seq):
seq[i] = seq[i+1]
seq[i+1] = temp
return seq
def test_bubble_sort(module_name='this module'):

View File

@ -1,4 +1,6 @@
# Mari von Steinkirch @ 2013
#!/usr/bin/python3
# Mari von Steinkirch @ 2013
# mari.wahl9@gmail.com
# Bernardo Sulzbach (mafagafo) @ 2014

View 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))

View File

@ -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()

View File

@ -3,7 +3,7 @@ print("System version:", sys.version, '\n')
try:
import numpy
print("\nNumpy version:", numpy.version.version)
print("\nNumpy version:", numpy.__version__)
except ImportError as e:
print(e)