Advertisement
UniQuet0p1

Untitled

Oct 17th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 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. if stringy == "":
  127. return 0
  128. return eval(stringy)
  129.  
  130.  
  131. def equalizer(unequal: list) -> list:
  132. """
  133. Equalize numbers between bigger or smaller numbers.
  134.  
  135. [1, 4, 1] # -> [1, 3, 1]
  136. [1, 2] # -> [1, 2]
  137. [1, 4, 1, 0, 2] # -> [1, 3, 1, 1, 2]
  138.  
  139. :param unequal: Unequalized list.
  140. :return: Equalized list.
  141. """
  142. for i in range(1, len(unequal) - 1):
  143. number = unequal[i]
  144. if unequal[i - 1] < number > unequal[i + 1]:
  145. unequal[i] -= 1
  146. elif unequal[i - 1] > number < unequal[i + 1]:
  147. unequal[i] += 1
  148. return unequal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement