#!/usr/bin/env python # coding: utf-8 # ## Supplementary exercises for Week 1 Day 3 notebook B # ### Run the following cell first. # In[201]: 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!") # ## Exercise 1 (Basic) # Write a `while` loop that prints the integers between `0` and `10` (inclusive on both ends). # In[ ]: # Change the loop above to be a `for` loop. # In[ ]: # ## Exercise 2 (Basic) # 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` # # In[202]: lst = [12, 34, 15, 11, 9, 34, 433, 23, 24, 121, 343, 32, 111, 4454, 323] # In[ ]: # ### 2.1 # Change the loop above to be a `for` loop. # In[ ]: # ### 2.2 # Print only the odd elements in `lst` using a `for` loop. # In[ ]: # ### 2.3 # Print every other element in `lst` using a `for` loop. # In[ ]: # ## Exercise 3(Easy) # 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"`. # In[203]: keys = ["Jelani", "Daniel"] vals = ["Week 4", "Week 1"] # In[ ]: # ## Exercise 4 (Medium) # Use a `for` loop to print the following: # $\newline * # \newline * \ \ \ * # \newline * \ \ \ * \ \ \ * # \newline *\ \ \ *\ \ \ *\ \ \ *$ # # In[204]: for i in range(1, 5): print("* " * i) # ### 4.2 (Hard) # Use a nested `for` loop to print the following: # $\newline 1 # \newline 2 \ \ \ 3 # \newline 4 \ \ \ 5 \ \ \ 6 # \newline 7\ \ \ 8\ \ \ 9\ \ \ 10$ # In[205]: start = 1 for i in range(1, 5): st = "" for j in range(i): st += str(start) + " " start += 1 print(st) # ### 4.3 (Hard) # Use a nested `for` loop to print the following: # $\newline 7\ \ \ 8\ \ \ 9\ \ \ 10 # \newline 4 \ \ \ 5 \ \ \ 6 # \newline 2 \ \ \ 3 # \newline 1$ # In[206]: start = 10 for i in range(4, 0, -1): st = "" for j in range(i): st = str(start) + " " + st start -= 1 print(st) # ## Exercise 5 (Hard) # 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$ # ### 5.1 # Use a nested `for` loop to solve it # In[ ]: # ### 5.2 # Use just one `for` loop to solve it # In[ ]: # ## Exercise 6 (Easy) # 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]`. # In[ ]: # ## Exercise 7 (Basic) # 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. # In[ ]: # Run the following code cell to check your solution. # In[207]: check(isEven(2), True) check(isEven(-2), True) check(isEven(3), False) # ## Exercise 8 (Medium) # 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. # In[ ]: # Use the following code cell to check your solution. # In[208]: check(isPrime(1), False) check(isPrime(2), True) check(isPrime(3), True) check(isPrime(97), True) check(isPrime(85), False) check(isPrime(87), False) # ## Exercise 9 (Medium) # 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. # In[ ]: # Use the following code cell to check your solution. # In[209]: 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]) # ### Exercise 10 (Hard) # 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`. # In[ ]: # Run the following code cell to check your solution. # In[210]: 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!") # ## Exercise 11 (Medium) # 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$. # In[ ]: