Python345 1223. validate code with simple regex Basic regex tasks. Write a function that takes in a numeric code of any length. The function should check if the code begins with 1, 2, or 3 and return true if so. Return false otherwise. You can assume the input will always be a number. Solution: def validate_code(code: int) -> bool: return int(str(code)[0]) 2022. 12. 23. 1222. Formatting decimal places #0 Each number should be formatted that it is rounded to two decimal places. You don't need to check whether the input is a valid number because only valid numbers are used in the tests. Example: 5.5589 is rounded 5.56 3.3424 is rounded 3.34 Solution: def two_decimal_places(n: float) -> float: l, r = str(n).split(".") r = str(round(int("1"+r[:5]), -3)) return float(".".join([l, r[1:3]])) def two_de.. 2022. 12. 23. 1221. Name Shuffler Write a function that returns a string in which firstname is swapped with last name. Example(Input --> Output) "john McClane" --> "McClane john" Solution: def name_shuffler(str_): return " ".join(str_.split(" ")[::-1]) 2022. 12. 21. 1220. The 'if' function Create a function called _if which takes 3 arguments: a boolean value bool and 2 functions (which do not take any parameters): func1 and func2 When bool is truth-ish, func1 should be called, otherwise call the func2. Example: def truthy(): print("True") def falsey(): print("False") _if(True, truthy, falsey) # prints 'True' to the console Solution: def _if(bool, func1, func2): return func1() if b.. 2022. 12. 20. 1219. Return the day Complete the function which returns the weekday according to the input number: 1 returns "Sunday" 2 returns "Monday" 3 returns "Tuesday" 4 returns "Wednesday" 5 returns "Thursday" 6 returns "Friday" 7 returns "Saturday" Otherwise returns "Wrong, please enter a number between 1 and 7" Solution: days = { 1: "Sunday", 2: "Monday", 3: "Tuesday", 4: "Wednesday", 5: "Thursday", 6: "Friday", 7: "Saturd.. 2022. 12. 19. 1218. Remove First and Last Character It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters. Solution: def remove_char(s: str, limit: int = 1): return s[limit:-limit] 2022. 12. 18. 이전 1 ··· 8 9 10 11 12 13 14 ··· 58 다음