Median output rate: 202.38 chars / s
Prompt | Answer | Latency |
---|---|---|
Write a Python function to find the nth number in the Fibonacci Sequence. | # The Fibonacci sequence is a series of numbers in which each number ( Fibonacci number ) # is the sum of the two preceding numbers. The sequence begins with 0 and 1. # For example, here are the first 10 Fibonacci numbers: # 0, 1, 1, 2, 3, 5, 8, 13, 21 and 34. # Write a Python function that takes in input n and prints out the nth Fibonacci number. # For example, if n is 10, the output should be 34. # If n is 20, the output should be 102641527. # If n is 1, the output should be 1. # If n is 2, the output should be 1. # If n is 3, the output should be 2. # If n is 4, the output should be 3. # If | 3182ms |
Write a Python function that prints the next 20 leap years. Reply with only the function. | def leap_year(year): if year % 4 == 0: if year % 100 == 0: if year % 400 == 0: return True else: return False else: return True else: return False def next_20_leap_years(): for i in range(20): year = 2020 + i if leap_year(year): print(year) next_20_leap_years() | 1992ms |
Explain the bug in the following code: ``` from time import sleep from multiprocessing.pool import ThreadPool def task(): sleep(1) return 'all done' if __name__ == '__main__': with ThreadPool() as pool: result = pool.apply_async(task()) value = result.get() print(value) ``` | ### Solution The problem is that the `ThreadPool` is not being used correctly. The `apply_async` method is called with the result of the `task` function, not with the function itself. ``` from time import sleep from multiprocessing.pool import ThreadPool def task(): sleep(1) return 'all done' if __name__ == '__main__': with ThreadPool() as pool: result = pool.apply_async(task) value = result.get() print(value) ``` ### Solution 2 Another way to solve the problem is to use the `ThreadPool` as a context manager. ``` from time import sleep from multiprocessing.pool import ThreadPool def task(): sleep(1) return 'all done' if __name__ == '__main__': with ThreadPool(processes=1) as pool: value = pool.apply(task) print(value | 3376ms |
Explain simply what this function does: ``` def func(lst): if len(lst) == 0: return [] if len(lst) == 1: return [lst] l = [] for i in range(len(lst)): x = lst[i] remLst = lst[:i] + lst[i+1:] for p in func(remLst): l.append([x] + p) return l ``` | ### 2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2.2 | 3273ms |