Advertisement
UniQuet0p1

Untitled

Oct 9th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. """T08."""
  2.  
  3.  
  4. def alarm_clock(day: int, vacation: bool) -> str:
  5. """
  6. Return what time the alarm clock should be set.
  7.  
  8. Given a day of the week encoded as 0=Mon, 1=Tue, ... 5=Sat, 6=Sun
  9. and a boolean indicating if we are on vacation,
  10. return a string of the form "08:00" indicating when the alarm clock should ring.
  11.  
  12. Weekdays, the alarm should be "08:00" and on the weekend it should be "10:00".
  13. Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off".
  14.  
  15. alarm_clock(1, False) # -> '08:00'
  16. alarm_clock(3, True) # -> '10:00'
  17. alarm_clock(6, False) # -> '10:00'
  18.  
  19. :param day: Day of week.
  20. :param vacation: Whether it is vacation.
  21. :return: String when to set alarm clock.
  22. """
  23. if day <= 4 and vacation is False:
  24. return "08:00"
  25. if day >= 5 and vacation is False:
  26. return "10:00"
  27. if day <= 4 and vacation is True:
  28. return "10:00"
  29. if day >= 5 and vacation is True:
  30. return "off"
  31.  
  32.  
  33. def keep_if_first_two_symbols_same(stringys: list) -> list:
  34. """
  35. Return list of strings where two first symbols of the element are the same.
  36.  
  37. keep_if_first_two_symbols_same(["a", "bb", "bbbccc"]) # -> ['bb', 'bbbccc']
  38. keep_if_first_two_symbols_same(["mmem", "memm"]) # -> ['mmem']
  39. keep_if_first_two_symbols_same(["a", "a"]). # -> []
  40.  
  41. :param stringys: List of strings.
  42. :return: Filtered list.
  43. """
  44. return [x for x in stringys if len(x) > 1 and x[0] == x[1]]
  45.  
  46.  
  47. def middle_chars(s: str) -> str:
  48. """Return two chars in the middle of string.
  49.  
  50. The length of the string is an even number.
  51.  
  52. middle_chars("abcd") # -> "bc"
  53. middle_chars("bc") # -> "bc"
  54. middle_chars("aabbcc") # -> "bb"
  55. middle_chars("") # -> ""
  56. """
  57. return s[(len(s) - 1) // 2:(len(s) + 2) // 2]
  58.  
  59.  
  60. def remove_nth_symbol(s, n) -> str:
  61. """
  62. Return a new string where n-th symbol is removed.
  63.  
  64. If the n is outside of the string, return original string.
  65. If n is 1, the first symbol is removed etc.
  66.  
  67. remove_nth_symbol("tere", 1) # -> "ere"
  68. remove_nth_symbol("tere", 3) # -> "tee"
  69. remove_nth_symbol("tere", 5) # -> "tere"
  70.  
  71. :param s: Input string.
  72. :param n: Which element to remove.
  73. :return: String where n-th symbol is removed.
  74. """
  75. if n <= 0:
  76. return s
  77. else:
  78. return s[:n - 1] + s[n:]
  79.  
  80.  
  81. def in1to10(n: int, outside_mode: bool) -> bool:
  82. """d."""
  83. if outside_mode is False and 10 >= n >= 1:
  84. return True
  85. if outside_mode is False and 10 <= n >= 1:
  86. return False
  87. if outside_mode is True and n <= 1 or n >= 10:
  88. return True
  89. else:
  90. return False
  91.  
  92.  
  93. def love6(a: int, b: int) -> bool:
  94. """
  95. Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6.
  96.  
  97. love6(6, 1) # -> True
  98. love6(3, 3) # -> True
  99. love6(2, 3) # -> False
  100. """
  101. if a == 6 or b == 6:
  102. return True
  103. elif a + b == 6:
  104. return True
  105. elif a - b == 6 or b - a == 6:
  106. return True
  107. else:
  108. return False
  109.  
  110.  
  111. def addition_and_subtraction(stringy: str) -> int:
  112. """
  113. Return the value of equation.
  114.  
  115. The equation only consists of plus (+), minus (-) and digits (0-9).
  116. There are no 2 consecutive operands. In the end, there is no operand.
  117. In the beginning, both "+" and "-" are acceptable.
  118.  
  119. "1+2" # -> 3
  120. "-1+21" # -> 20
  121. "+1-1" # -> 0
  122.  
  123. :param stringy: Equation.
  124. :return: The result of equation.
  125. """
  126.  
  127.  
  128.  
  129. def equalizer(unequal: list) -> list:
  130. """
  131. Equalize numbers between bigger or smaller numbers.
  132.  
  133. [1, 4, 1] # -> [1, 3, 1]
  134. [1, 2] # -> [1, 2]
  135. [1, 4, 1, 0, 2] # -> [1, 3, 1, 1, 2]
  136.  
  137. :param unequal: Unequalized list.
  138. :return: Equalized list.
  139. """
  140. result = []
  141.  
  142.  
  143. if __name__ == '__main__':
  144. print(alarm_clock(0, False)) # 8
  145. print(alarm_clock(5, False)) # 10
  146. print(alarm_clock(2, True)) # 10
  147. print(alarm_clock(5, True)) # off
  148. print()
  149. print(keep_if_first_two_symbols_same(["aaa", "aa", "aaaa"])) # ["aaa", "aa", "aaaa"]
  150. print()
  151. print(middle_chars("abcd"))
  152. print(middle_chars("!!"))
  153. print(middle_chars("abcdefgkjurkjdkrjaku"))
  154. print()
  155. print(remove_nth_symbol("tere", 1)) # ere
  156. print(remove_nth_symbol("tere", 0)) # tere
  157. print(remove_nth_symbol("tere", -1)) # tere
  158. print()
  159. print(in1to10(0, True)) # True
  160. print(in1to10(-13, True)) # True
  161. print(in1to10(-99, False)) # False
  162. print(in1to10(0, False)) # False
  163. print(addition_and_subtraction("1+2"))
  164. print(addition_and_subtraction("1+2+3-5"))
  165. print(addition_and_subtraction("-1+8"))
  166. print(addition_and_subtraction(""))
  167. print()
  168. print(keep_if_first_two_symbols_same(["abd", "aba", "abs", "ac", "ac"])) # []
  169. print(keep_if_first_two_symbols_same(["aaa", "aa", "aaaa"])) # ["aaa", "aa", "aaaa"]
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement