Advertisement
fmasanori

Lista 07

Oct 14th, 2011
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. #!/usr/bin/python -tt
  2. # Copyright 2010 Google Inc.
  3. # Licensed under the Apache License, Version 2.0
  4. # http://www.apache.org/licenses/LICENSE-2.0
  5.  
  6. # Google's Python Class
  7. # http://code.google.com/edu/languages/google-python-class/
  8.  
  9. # A. fim_igual
  10. # Dada uma lista de strings, retorna o número de strings
  11. # com tamanho >= 2 onde o primeiro e o último caracteres são iguais
  12. def fim_igual(words):
  13.   return
  14.  
  15. # B. x_antes
  16. # Dada uma lista de strings retorna uma lista onde todos os elementos
  17. # que começam com x ficam sorteados antes
  18. # Exemplo ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] retorna
  19. # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
  20. # Dica: monte duas listas separadas e junte-as no final
  21. def x_antes(words):
  22.   return
  23.  
  24. def last(a):
  25.   return
  26.  
  27. # C. sort_last
  28. # Dada uma lista de tuplas não vazias retorna uma tupla ordenada
  29. # por ordem crescente do último elemento
  30. # Exemplo [(1, 7), (1, 3), (3, 4, 5), (2, 2)] retorna
  31. # [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
  32. # Dica: use key=função que você definiu e que retorna o último elemento
  33. def sort_last(tuples):
  34.   return
  35.  
  36. def test(obtido, esperado):
  37.   if obtido == esperado:
  38.     prefixo = ' Parabéns!'
  39.   else:
  40.     prefixo = ' Ainda não'
  41.   print ('%s obtido: %s esperado: %s' % (prefixo, repr(obtido), repr(esperado)))
  42.  
  43. def main():
  44.   print ('fim_igual')
  45.   test(fim_igual(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  46.   test(fim_igual(['', 'x', 'xy', 'xyx', 'xx']), 2)
  47.   test(fim_igual(['aaa', 'be', 'abc', 'hello']), 1)
  48.  
  49.   print ()
  50.   print ('x_antes')
  51.   test(x_antes(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
  52.        ['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
  53.   test(x_antes(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
  54.        ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
  55.   test(x_antes(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
  56.        ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
  57.  
  58.        
  59.   print ()
  60.   print ('sort_last')
  61.   test(sort_last([(1, 3), (3, 2), (2, 1)]),
  62.        [(2, 1), (3, 2), (1, 3)])
  63.   test(sort_last([(2, 3), (1, 2), (3, 1)]),
  64.        [(3, 1), (1, 2), (2, 3)])
  65.   test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
  66.        [(2, 2), (1, 3), (3, 4, 5), (1, 7)])
  67.  
  68.  
  69. if __name__ == '__main__':
  70.   main()
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement