list1.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3 -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. # Google's Python Class
  6. # http://code.google.com/edu/languages/google-python-class/
  7. # Basic list exercises
  8. # Fill in the code for the functions below. main() is already set up
  9. # to call the functions with a few different inputs,
  10. # printing 'OK' when each function is correct.
  11. # The starter code for each function includes a 'return'
  12. # which is just a placeholder for your code.
  13. # It's ok if you do not complete all the functions, and there
  14. # are some additional functions to try in list2.py.
  15. # A. match_ends
  16. # Given a list of strings, return the count of the number of
  17. # strings where the string length is 2 or more and the first
  18. # and last chars of the string are the same.
  19. # Note: python does not have a ++ operator, but += works.
  20. def match_ends(words: list[str]) -> int:
  21. i = 0
  22. for string in words:
  23. if len(string) > 1 and string[0] == string[len(string)-1]:
  24. i += 1
  25. return i
  26. # B. front_x
  27. # Given a list of strings, return a list with the strings
  28. # in sorted order, except group all the strings that begin with 'x' first.
  29. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
  30. # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
  31. # Hint: this can be done by making 2 lists and sorting each of them
  32. # before combining them.
  33. def front_x(words: list[str]) -> list[str]:
  34. # Сработало только на последнем тесте
  35. # Не понял почему
  36. #for string in words:
  37. # if string[0] == 'x':
  38. # xList.append(string)
  39. # words.remove(string)
  40. #Так что пошёл через генераторы
  41. xList = [i for i in words if i[0]=='x']
  42. xList.sort()
  43. words.sort()
  44. xList.extend([i for i in words if i not in xList])
  45. return xList
  46. # C. sort_last
  47. # Given a list of non-empty tuples, return a list sorted in increasing
  48. # order by the last element in each tuple.
  49. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
  50. # [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
  51. # Hint: use a custom key= function to extract the last element form each tuple.
  52. def sort_last(tuples: list[tuple]) -> list[tuple]:
  53. tuples.sort(key=lambda i: i[len(i)-1])
  54. return tuples
  55. # Simple provided test() function used in main() to print
  56. # what each function returns vs. what it's supposed to return.
  57. def test(got, expected):
  58. if got == expected:
  59. prefix = ' OK '
  60. else:
  61. prefix = ' X '
  62. print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
  63. # Calls the above functions with interesting inputs.
  64. def main():
  65. print('match_ends')
  66. test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  67. test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
  68. test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
  69. print()
  70. print('front_x')
  71. test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
  72. ['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
  73. test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
  74. ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
  75. test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
  76. ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
  77. print()
  78. print('sort_last')
  79. test(sort_last([(1, 3), (3, 2), (2, 1)]),
  80. [(2, 1), (3, 2), (1, 3)])
  81. test(sort_last([(2, 3), (1, 2), (3, 1)]),
  82. [(3, 1), (1, 2), (2, 3)])
  83. test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
  84. [(2, 2), (1, 3), (3, 4, 5), (1, 7)])
  85. if __name__ == '__main__':
  86. main()