Resultado do dojo de python - 04/05/2017 #dojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from sort import BubbleSort | |
class BubbleSortTest(unittest.TestCase): | |
def setUp(self): | |
self.bubble_sort = BubbleSort() | |
def testSortEmptyList(self): | |
lista = [] | |
self.assertEqual(lista, self.bubble_sort.sort(lista)) | |
def testSortListOneElement(self): | |
lista = [1] | |
self.assertEqual(lista, self.bubble_sort.sort(lista)) | |
def testSortListTwoElementSorted(self): | |
lista = [1, 2] | |
self.assertEqual(lista, self.bubble_sort.sort(lista)) | |
def testSortListTwoElementUnsorted(self): | |
lista = [2, 1] | |
self.assertEqual([1, 2], self.bubble_sort.sort(lista)) | |
def testSortListThreeElementUnsorted(self): | |
lista = [2, 1, 5] | |
self.assertEqual([1, 2, 5], self.bubble_sort.sort(lista)) | |
if __name__=='__main__': | |
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Morse: | |
def __init__(self): | |
self.dicionario = { "S" : "...", "O" : "---"} | |
def StringToMorse(self, texto): | |
traduzao = "" | |
texto = texto.strip() | |
texto = texto.upper() | |
for chave in texto: | |
traduzao += " " + self.dicionario[chave] | |
return traduzao.strip() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sort: | |
def run(self, items): | |
for i in range(len(items)): | |
for j in range(len(items)-1-i): | |
if items[j] > items[j+1]: | |
items[j], items[j+1] = items[j+1], items[j] | |
return items |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#http://interactivepython.org/courselib/static/pythonds/SortSearch/TheBubbleSort.html | |
def bubbleSort(alist): | |
for passnum in range(len(alist)-1,0,-1): | |
for i in range(passnum): | |
if alist[i]>alist[i+1]: | |
temp = alist[i] | |
alist[i] = alist[i+1] | |
alist[i+1] = temp | |
alist = [54,26,93,17,77,31,44,55,20] | |
bubbleSort(alist) | |
print(alist) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BubbleSort: | |
def sort(self, lista): | |
for x in (range(1,len(lista))): | |
if lista[x - 1] > lista[x]: | |
aux = lista[x-1] | |
lista[x-1] = lista[x] | |
lista[x] = aux | |
return lista |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from morse import Morse | |
class Test_Morse(unittest.TestCase): | |
def testStringToMorse(self): | |
m = Morse(); | |
self.assertEqual("... --- ...", m.StringToMorse("SOS")) | |
self.assertEqual("... --- ...", m.StringToMorse("sos")) | |
self.assertEqual("... --- ...", m.StringToMorse(" sos ")) | |
if __name__=='__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment