list1.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. from typing import List # Почитай https://habr.com/ru/company/lamoda/blog/432656/
  21. def match_ends(words: List[str]) -> int:
  22. i = 0
  23. for string in words:
  24. if len(string) > 1 and string[0] == string[-1]:
  25. i += 1
  26. return i
  27. # B. front_x
  28. # Given a list of strings, return a list with the strings
  29. # in sorted order, except group all the strings that begin with 'x' first.
  30. # e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields
  31. # ['xanadu', 'xyz', 'aardvark', 'apple', 'mix']
  32. # Hint: this can be done by making 2 lists and sorting each of them
  33. # before combining them.
  34. def front_x(words: List[str]) -> List[str]:
  35. # Сработало только на последнем тесте
  36. # Не понял почему
  37. # чтобы понять что работала не так, хотелось бы увидеть весь код, так как тут точно не хватает
  38. # объявление xList и что ты будешь возвращать, и как совместишь с words
  39. # for string in words:
  40. # if string[0] == 'x':
  41. # xList.append(string)
  42. # words.remove(string)
  43. #Так что пошёл через генераторы
  44. xList = [i for i in words if i.startswith('x')]
  45. xList.sort()
  46. words.sort()
  47. xList.extend([i for i in words if i not in xList])
  48. return xList
  49. # C. sort_last
  50. # Given a list of non-empty tuples, return a list sorted in increasing
  51. # order by the last element in each tuple.
  52. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
  53. # [(2, 2), (1, 3), (3, 4, 5), (1, 7)]
  54. # Hint: use a custom key= function to extract the last element form each tuple.
  55. def sort_last(tuples: List[tuple]) -> List[tuple]:
  56. tuples.sort(key=lambda i: i[-1])
  57. return tuples
  58. # Simple provided test() function used in main() to print
  59. # what each function returns vs. what it's supposed to return.
  60. def test(got, expected):
  61. if got == expected:
  62. prefix = ' OK '
  63. else:
  64. prefix = ' X '
  65. print('%s got: %s expected: %s' % (prefix, repr(got), repr(expected)))
  66. # Calls the above functions with interesting inputs.
  67. def main():
  68. print('match_ends')
  69. test(match_ends(['aba', 'xyz', 'aa', 'x', 'bbb']), 3)
  70. test(match_ends(['', 'x', 'xy', 'xyx', 'xx']), 2)
  71. test(match_ends(['aaa', 'be', 'abc', 'hello']), 1)
  72. print()
  73. print('front_x')
  74. test(front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa']),
  75. ['xaa', 'xzz', 'axx', 'bbb', 'ccc'])
  76. test(front_x(['ccc', 'bbb', 'aaa', 'xcc', 'xaa']),
  77. ['xaa', 'xcc', 'aaa', 'bbb', 'ccc'])
  78. test(front_x(['mix', 'xyz', 'apple', 'xanadu', 'aardvark']),
  79. ['xanadu', 'xyz', 'aardvark', 'apple', 'mix'])
  80. print()
  81. print('sort_last')
  82. test(sort_last([(1, 3), (3, 2), (2, 1)]),
  83. [(2, 1), (3, 2), (1, 3)])
  84. test(sort_last([(2, 3), (1, 2), (3, 1)]),
  85. [(3, 1), (1, 2), (2, 3)])
  86. test(sort_last([(1, 7), (1, 3), (3, 4, 5), (2, 2)]),
  87. [(2, 2), (1, 3), (3, 4, 5), (1, 7)])
  88. if __name__ == '__main__':
  89. main()