Appearance
question:def fibonacci(n: int) -> int: Returns the nth Fibonacci number. If n is less than 0, returns None. >>> fibonacci(-1) is None >>> fibonacci(-10) is None >>> fibonacci(0) == 0 >>> fibonacci(1) == 1 >>> fibonacci(2) == 1 >>> fibonacci(3) == 2 >>> fibonacci(4) == 3 >>> fibonacci(5) == 5 >>> fibonacci(10) == 55 >>> fibonacci(20) == 6765 >>> fibonacci(30) == 832040 >>> fibonacci(50) == 12586269025
answer:def fibonacci(n): Returns the nth Fibonacci number. If n is less than 0, returns None. if n < 0: return None if n == 0: return 0 if n == 1: return 1 a, b = 0, 1 for _ in range(2, n + 1): a, b = b, a + b return b
question:def watering_schedule(days): Returns the earliest day on which all plants will be in full bloom. Parameters: days (List[int]): Each element represents the number of days required by a plant to bloom. Returns: int: The earliest day on which all plants will bloom. Examples: >>> watering_schedule([3, 1, 2, 4]) == 4 >>> watering_schedule([5, 9, 2, 8]) == 9 >>> watering_schedule([7, 3, 5, 1]) == 7
answer:def watering_schedule(days): Returns the earliest day on which all plants will be in full bloom. Parameters: days (List[int]): Each element represents the number of days required by a plant to bloom. Returns: int: The earliest day on which all plants will bloom. if not days: return 0 return max(days)
question:def is_prime(n: int) -> bool: Write a function is_prime that checks if a given positive integer n is a prime number or not. >>> is_prime(2) == True >>> is_prime(3) == True >>> is_prime(11) == True >>> is_prime(29) == True >>> is_prime(1) == False >>> is_prime(4) == False >>> is_prime(10) == False >>> is_prime(15) == False >>> is_prime(100) == False >>> is_prime(-1) == False >>> is_prime(0) == False # Implement the function here
answer:def is_prime(n): Returns True if n is a prime number, otherwise False. if n <= 1: return False if n == 2: return True if n % 2 == 0: return False for i in range(3, int(n ** 0.5) + 1, 2): if n % i == 0: return False return True
question:def compare_version(version1: str, version2: str) -> int: Compare two version numbers given as strings and determine which one is greater. >>> compare_version("1.0", "1") 0 >>> compare_version("0.1", "1.1") -1 >>> compare_version("1.0.1", "1") 1 >>> compare_version("7.5.2.4", "7.5.3") -1
answer:def compare_version(version1: str, version2: str) -> int: # Split the versions by '.' v1_parts = list(map(int, version1.split('.'))) v2_parts = list(map(int, version2.split('.'))) # Determine the maximum length for comparison max_length = max(len(v1_parts), len(v2_parts)) # Compare each part for i in range(max_length): part1 = v1_parts[i] if i < len(v1_parts) else 0 part2 = v2_parts[i] if i < len(v2_parts) else 0 if part1 > part2: return 1 elif part1 < part2: return -1 # If all parts are equal return 0