-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathexercise2.py
More file actions
49 lines (42 loc) · 1.35 KB
/
exercise2.py
File metadata and controls
49 lines (42 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Use AI to document some Python code.
# Put your cursor on a function.
# It is possible that the AI will suggest a docstring,
# which you can accept via tab-complete.
# You can also edit the docstring as needed.
# If the AI is not suggesting something,
# use Ctrl + I to invoke the AI to suggest a docstring,
# or start typing """ which will clue it in to suggest a docstring.
# At the bottom of the file,
# use Ctrl + I to write example usages of the functions,
# or start typing if __name__ == "__main__":
# which will clue it in to suggest example usages.
# For extra clout/credit, try suggesting that the docstrings
# are written so that the package doctest can test them for correctness.
# You can then run `python make-unit-tests.py` to run the tests.
from math import comb, isqrt
from typing import List
def fibonacci(n):
if n < 0:
raise ValueError("n must be non-negative")
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
def is_prime(k):
if k < 2:
return False
if k % 2 == 0:
return k == 2
r = isqrt(k)
for d in range(3, r + 1, 2):
if k % d == 0:
return False
return True
def first_ten_primes():
primes = []
candidate = 2
while len(primes) < 10:
if is_prime(candidate):
primes.append(candidate)
candidate += 1
return primes