Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. # 1. Counting substrings.
  2. # https://www.tutorialspoint.com/python3/string_count.htm
  3.  
  4.  
  5. def count_substrings(haystack, needle):
  6. return haystack.count(needle)
  7.  
  8.  
  9. print(count_substrings("This is a test string", "is"))
  10. print(count_substrings("babababa", "baba"))
  11. print(count_substrings("Python is an awesome language to program in!", "o"))
  12. print(count_substrings("We have nothing in common!", "really?"))
  13. print(count_substrings("This is this and that is this", "this"))
  14.  
  15.  
  16. # 2. Sum numbers in matrix
  17. # http://www.u.arizona.edu/~erdmann/mse350/topics/list_comprehensions.html
  18.  
  19.  
  20. def sum_matrix(m):
  21. return sum(map(sum, m))
  22.  
  23.  
  24. m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  25. print(sum_matrix(m))
  26.  
  27. m = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  28. print(sum_matrix(m))
  29.  
  30. m = [[1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]
  31. print(sum_matrix(m))
  32.  
  33.  
  34. # 3. NaN Expand
  35.  
  36. def nan_expand(times):
  37. return "{}{}".format("Not a " * times, "NaN" if times != 0 else "")
  38.  
  39.  
  40. print(nan_expand(0))
  41. print(nan_expand(1))
  42. print(nan_expand(2))
  43. print(nan_expand(3))
  44.  
  45.  
  46. # 4. Integer prime factorization
  47.  
  48.  
  49. def prime_factors(n):
  50. prfac = []
  51. d = 2
  52. times = 0
  53. while d * d <= n:
  54. if n % d == 0:
  55. while (n % d) == 0:
  56. times += 1
  57. n //= d
  58. prfac.append((d, times))
  59. d += 1
  60. times = 0
  61. if n > 1:
  62. prfac.append((n, 1))
  63. return prfac
  64.  
  65.  
  66. print(prime_factors(1000))
  67.  
  68.  
  69. # 5. https://docs.python.org/3/library/itertools.html
  70.  
  71. def max_concecutive(items):
  72. from itertools import groupby
  73.  
  74. return max(sum(1 for i in g) for k, g in groupby(items))
  75.  
  76.  
  77. print(max_concecutive([1, 2, 3, 3, 3, 3, 4, 3, 3]))
  78. # 6. https://docs.python.org/3/library/itertools.html
  79.  
  80.  
  81. def group(elem):
  82. from itertools import groupby
  83.  
  84. return [list(grp) for k, grp in groupby(elem)]
  85.  
  86.  
  87.  
  88. print(group([1, 1, 1, 2, 3, 1, 1]))
  89.  
  90.  
  91. # 7.
  92.  
  93. def first_max(curr, rest, tank_size):
  94. for x in rest:
  95. if tank_size < x - curr:
  96. return rest.index(x) - 1
  97.  
  98.  
  99. def gas_stations(distance, tank_size, stations):
  100. stations.insert(0, 0)
  101. currentst = 0
  102. st = []
  103. current = 0
  104.  
  105. while current >= stations[-1]:
  106. x = first_max(current, stations[currentst:], tank_size)
  107. print((stations[x + current]))
  108. return st
  109.  
  110.  
  111. print(gas_stations(320, 90, [50, 80, 140, 180, 220, 290]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement