def check(actual, expected):
if expected != actual:
print(
f"Function should return the value {expected}, it is returning the value {actual}")
else:
print(f"Congratulations, the test case passed!")
Write a while
loop that prints the integers between 0
and 10
(inclusive on both ends).
Change the loop above to be a for
loop.
Let lst = [12, 34, 15, 11, 9, 34, 433, 23, 24, 121, 343, 32, 111, 4454, 323]
be a list of integers. Use a while
loop to print all the elements in lst
lst = [12, 34, 15, 11, 9, 34, 433, 23, 24, 121, 343, 32, 111, 4454, 323]
Change the loop above to be a for
loop.
Print only the odd elements in lst
using a for
loop.
Print every other element in lst
using a for
loop.
Given an empty dictionary dic
, and two equal-length lists keys
and vals
. Put every corresponding key and value from keys
and vals
(with the same index) into the dictionary dic
. For example, if the list are keys = ["Jelani", "Daniel"]
and vals = ["Week 4", "Week 1"]
, then your dictionary dic
should contain two keys, and dic["Jelani"] = "Week 4"
, dic["Daniel"] = "Week 1"
.
keys = ["Jelani", "Daniel"]
vals = ["Week 4", "Week 1"]
Use a for
loop to print the following:
$\newline *
\newline * \ \ \ *
\newline * \ \ \ * \ \ \ *
\newline *\ \ \ *\ \ \ *\ \ \ *$
for i in range(1, 5):
print("* " * i)
* * * * * * * * * *
Use a nested for
loop to print the following:
$\newline 1
\newline 2 \ \ \ 3
\newline 4 \ \ \ 5 \ \ \ 6
\newline 7\ \ \ 8\ \ \ 9\ \ \ 10$
start = 1
for i in range(1, 5):
st = ""
for j in range(i):
st += str(start) + " "
start += 1
print(st)
1 2 3 4 5 6 7 8 9 10
Use a nested for
loop to print the following:
$\newline 7\ \ \ 8\ \ \ 9\ \ \ 10
\newline 4 \ \ \ 5 \ \ \ 6
\newline 2 \ \ \ 3
\newline 1$
start = 10
for i in range(4, 0, -1):
st = ""
for j in range(i):
st = str(start) + " " + st
start -= 1
print(st)
7 8 9 10 4 5 6 2 3 1
Print out the following. $ \newline 11\ \ \ 12\ \ \ 13 \ \ \ 14\ \ \ 15 \newline 20\ \ \ 19\ \ \ 18\ \ \ 17 \ \ \ 16 \newline 21\ \ \ 22\ \ \ 23\ \ \ 24\ \ \ 25 \newline 30\ \ \ 29\ \ \ 28\ \ \ 27\ \ \ 26 \newline 31\ \ \ 32\ \ \ 33\ \ \ 34\ \ \ 35$
Use a nested for
loop to solve it
Use just one for
loop to solve it
For a given number n
, use a for
loop to print a list contains all the factors of n
. For example, for n = 20
, your output should be [1, 2, 4, 5, 10, 20]
.
Write a function isEven
with input x
, so that isEven
checks if x
is even. The function returns True
if x
is even, and returns False
otherwise.
Write your solution in the following cell.
Run the following code cell to check your solution.
check(isEven(2), True)
check(isEven(-2), True)
check(isEven(3), False)
Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed!
Write a function isPrime
with input x
. The function returns True
if x
is a prime, returns False
otherwise. Please use a for
loop instead of a while
loop.
Hint: You can use isEven
function you definied above.
Use the following code cell to check your solution.
check(isPrime(1), False)
check(isPrime(2), True)
check(isPrime(3), True)
check(isPrime(97), True)
check(isPrime(85), False)
check(isPrime(87), False)
Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed!
Write a function factors
that, given an input n
, returns a list contains all the prime factors of n
. For example, for n = 20
, your function should return [2, 5]
. You can use the function isPrime
from the previous question.
Use the following code cell to check your solution.
check(sorted(factors(20)),[2, 5])
check(sorted(factors(8)),[2])
check(sorted(factors(1)),[])
check(sorted(factors(100)),[2, 5])
check(sorted(factors(105)),[3, 5, 7])
Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed! Congratulations, the test case passed!
Define a function twinPrimes
with no input to print all the pairs of twin primes between 2 and 200 in the form of a nested list.
For example, a pair of twin primes is both of the numbers are prime numbers and they have the difference of 2. Ex. [5, 7], [11, 13], [29, 31]
The result should be in the form of the following [[..., ...], [..., ...], ....]
Use a for
loop and the function isPrime
.
Run the following code cell to check your solution.
lst = twinPrimes()
correct_samples = [[5, 7], [137, 139], [107, 109], [11, 13], [197, 199]]
c_cnt = 0
wrong_samples = [[2, 3], [9, 11], [23, 25], [17, 18], [31, 33], [87, 89], [227, 229]]
for c_sample in correct_samples:
if c_sample not in lst:
print("Unfortunately, your twinPrimes function does not work properly, please edit your solution.")
break
else: c_cnt += 1
for w_sample in wrong_samples:
if w_sample in lst:
print("Unfortunately, your twinPrimes function does not work properly, please edit your solution.")
break
else: c_cnt += 1
if c_cnt == len(correct_samples) + len(wrong_samples): print("Congratulations, the test case passed!")
Congratulations, the test case passed!
Write a function pi
that approximates $\pi$. Use the following approximation of $\pi$:
$$\pi = 4(\frac{1}{1} - \frac{1}{3} + \frac{1}{5} - \frac{1}{7} + ... )$$
Your function should get an input n
and compute the sum of the first n
terms in the above approximation.
Play around the input number see when it converges to $\pi$.