#!/usr/bin/env python
# coding: utf-8
# # Exercises for day 2b
#
# You will be working on a series of exercises for the material covered in lecture 2b.
#
# We covered:
# - What lists are
# - Adding lists
# - Multiplying lists
# - Accessing lists/strings via indexing
# - Slices
# - Lengths
# - Dictionaries
#
# We will go through a series of exercises to reinfornce the concepts covered today
# ## Exercise 1.0 (Basic)
#
# Use `len` function to find the lengths of a string `s = "hello"` and a list `lst = [2 ,3, 45, "t5", "ew", 4]`
# In[1]:
s = "hello"
lst = [2 ,3, 45, "t5", "ew", 4]
# In[ ]:
# In[ ]:
# ## Exercise 1.1 (Basic)
# Which of the following options are the correct way to swap two elements `m` and `n`'s values.
#
# For example, `m = 3` and `n = 2` initially, and after certain operations, `m = 2` and `n = 3`
#
# A.
# $\newline m = 3
# \newline n = 2
# \newline m = n
# \newline n = m$
#
# B.
# $\newline m = 3
# \newline n = 2
# \newline m = n
# \newline temp = m
# \newline n = temp$
#
# C.
# $\newline m = 3
# \newline n = 2
# \newline temp = n
# \newline n = m
# \newline m = temp$
#
# D.
# $\newline m = 3
# \newline n = 2
# \newline temp = m
# \newline n = temp
# \newline m = n$
#
# Type your answer here:
#
# ## Exercise 2 (Basic)
#
# Create one list containing the following:
# The number `5731`, the string `"bye"`, and the boolean `True`
# In[ ]:
# ## Exercise 3 (Easy)
#
# First define two lists:
# 1. `[87, 109, 38]`
# 2. `["string1", "string2", "string3"]`
#
# Assign them to two variables `list1`, `list2`
#
# Add the two lists together to create a new list `list3`. Use `print(list3)` to print out `list3`. What happens when you switch the order of addition?
# In[ ]:
# ## Exercise 4 (Easy)
#
# Suppose `list1` is [1, 2, 3]. Use list multiplication to create a new list `list2`, which is [1, 2, 3, 1, 2, 3]. Use `print(list2)` to print out `list2`
# In[2]:
list1 = [1, 2, 3]
# In[ ]:
# ## Exercise 5 (Basic)
#
# Suppose that `lst = [2, 5, 2, 5, 9, 7]`, what is `lst[2]`?
# In[3]:
lst = [2, 5, 2, 5, 9, 7]
# Type your answer here:
# ## Exercise 6 (Medium)
# Suppose that you are given a sorted list `lst = [0 ,2, 3, 5, 7, 21, 837, 3284, 23842934]`, what is the median of this list? Use `len(lst)` to find the length of this list.
# In[4]:
lst = [0 ,2, 3, 5, 7, 21, 837, 3284, 23842934]
# In[ ]:
# ## Exercise 7 (Basic)
#
# Given a string `st = "addis coder"`, find the second to the last char.
# In[5]:
st = "addis coder"
# In[ ]:
# ### 7.1
#
# Change the second to the last char of the above `st` to `'i'`, and see if it is possible.
# In[ ]:
# ## Exercise 8 (Easy)
#
# Given a string `st = "ddkang"`, remove the first two letters of `st` to create a new string `str1`. Create a second string `str2` from `str1` so that `str2` is `"daniel kang"`
# In[6]:
st = "ddkang"
# In[ ]:
# ## Exercise 9 (Medium)
#
# Given a list `lst = [2 ,3 ,4 ,5, 6]` of odd length, print out the middle three numbers in a new list.
# In[7]:
lst = [2 ,3 ,4 ,5, 6]
# In[ ]:
# ## Exercise 10 (Basic)
#
# Suppose that you have a dictionary `dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }`. Find `"Jelani"`'s city.
# In[8]:
dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }
# In[ ]:
# ### 10.1
#
# Suppose that you have a dictionary `dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }`. Change `"Jelani"`'s city to `"Berkeley"`.
# In[9]:
dic = {"Daniel": "San Fransisco", "Nati": "Addis Ababa", "Jelani": "Cambridge" }
# In[ ]:
# ### 10.2
#
# Print the length of `dic`.
# In[ ]:
# ### 10.3
# Print the keys of `dic` as a list
# In[ ]:
# ## Exercise 11 (Basic)
#
# Suppose you have a list of 3 numbers `lst = [1, 3, -4]`, and you want to negate the elements of the list to get `[-1, -3, 4]`. Create a new list containing the negated elements of `lst` (in such a way that your code works even if the values of the entries are different)!
# In[10]:
lst = [1, 3, -4]
# In[ ]:
# ## Exercise 12 (Medium)
#
# Suppose you have a nested list `lst = [["addis", 1, 3, ["coder"]], [237894, "ajhfoiah"]]`.
# - What's the type of `lst[0]`?
# - What's the value of `lst[0][0]`?
# - Print out the element `"coder"` from this list.
# Type your answer here:
# Type your answer here:
# In[11]:
lst = [["addis", 1, 3, ["coder"]], [237894, "ajhfoiah"]]
# In[ ]:
# ## Exercise 13 (Easy)
#
# You can put lists inside of lists to create a matrix, like:
#
#
# ```
# matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# ```
#
# to represent
#
# $$\begin{bmatrix}
# 1&2&3\\
# 4&5&6\\
# 7&8&9
# \end{bmatrix}$$
#
# How would you access the entry in the first row, first column (whose value is 1)?
# Write your answer here:
# How would you access the entry whose value is 8?
# Write your answer here:
# In[ ]:
# ## Exercise 14 (Medium)
# You are given two **lists of strings** called ```names``` and ```cities```. The **first person** in ```names``` lives in the **last city** in ```cities```, the second person lives in the second to last city, the third lives in the third to last, and so on. Write code to create a third list called ```inOrder``` such that the even indexes (0, 2, 4, etc.) are the names of the people (in order) and that the odd indexes (1, 3, 5, etc.) are the cities. For a person in index $i$, their corresponding city should be in index $i+1$.
#
# For example:
#
# ```python
# names = ["Daniel", "Nati", "Jelani"]
# cities = ["Cambridge","Addis Ababa", "San Fransisco"]
# ```
# In this case,
#
# - Daniel lives in San Francisco
# - Nati lives in Addis Ababa
# - Jelani lives Cambridge
#
#
# You should create the final list:
# ```python
# inOrder = ["Daniel","San Fransisco", "Nati", "Addis Ababa", "Jelani", "Cambridge"]
# ```
# In[16]:
names = ["Timnit", "Binam", "Basi"]
cities = ["Harar", "Addis Ababa", "Gondar"]
# In[ ]:
# ## While-loops!
# Note: If your code does not stop running and has an `[*]` on the left, it is in an infinite loop. To get out of this loop, in the top menu, select Kernel -> Interrupt, or press the black square button in the top menu bar.
# What happens with the code inside the loop if you write a `while True:` loop? Explain below!
# **You answer here.**
# Try running the code below and see what happens (hint: you might need to use the stop button at the top of the page).
# In[ ]:
i = 0
while True:
i += 1
print(i)
# What happens with the code inside the loop if you write a `while False:` loop?
# **You answer here.**
# In[ ]:
# ## Exercise
# Use a while loop to print the numbers between `0` to `10` (inclusive on both ends).
# In[ ]:
# Use a while loop to print the **even** numbers between `0` to `10` (inclusive on both ends).
# In[ ]:
# ### Exercise
#
# When computing 1+2+3+4+..., how many numbers do you need to sum up so that the sum exceeds 1000?
#
# Hint: Use a while loop.
# In[ ]:
# ### Exercise
# In each of the following code snippets, there is a mistake. Find it and fix it!
# Suggestion: First run the code and look at the error message. Fix it and run it again.
# In[ ]:
# I want to sum up the first 10 square numbers,
# but the loop just runs infinitely.
i = 1
summ = 0
while i <= 10:
summ += i**2
print(summ)
# In[ ]:
# I want to add (and print) the first few numbers until their sum exceeds 100.
# but nothing is being printed
total = 0
i = 1
while total >= 100:
total += i
i += 1
print(i)
# In[ ]:
# I want to print the first number in my list that's bigger than 10.
# But the answer I get, 5, is certainly not bigger than 10.
myList = [1,3,9,5,-1,13,5,11,2]
number = 0
while myList[number] <= 10:
number += 1
print(number)
# ### Exercise
# Let `n = 345323`. Write a code that checks if `n` is prime. Print `"n is prime"` if it is, or `"n is not prime"` otherwise.
# In[12]:
n = 345323
# In[ ]:
# ### Exercise
#
# The Collatz sequence of a number $n$ is defined as follows:
# - It starts with the number $a_0 = n$ itself
# - If the $i$th number $a_i$ is even, then the next number $a_{i+1}$ is $\frac{a_i}{2}$
# - If the $i$th number $a_i$ is odd, then the next number $a_{i+1}$ is $3\cdot a_i + 1$
# - The sequence ends once you reach the number 1.
#
# Example: for the number 12, the sequence is: 12, 6, 3, 10, 5, 16, 8, 4, 2, 1.
#
# The Collatz conjecture says that whichever number you start with, you'll always reach 1 at some point.
#
# Define an integer variable `n` and then use a `while` loop to print out its Collatz sequence!
# In[ ]:
# ### Exercise
# Suppose that you have `lst = [1, 7, 4, 5, 10, 2, 3, 6, 9, 12]`. Use a while loop to find the first number that is at least 10.
# In[14]:
lst = [1, 7, 4, 5, 10, 2, 3, 6, 9, 12]
# In[ ]:
# ### Exercise
# Suppose that you have `lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]`, reverse the list using a while loop.
# In[14]:
lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# In[ ]:
# ### Exercise
# The Fibonnaci sequence is defined as follows: The first two elements in the sequence `f(0)` and `f(1)` are equal to `1`. For every `n>1`, the `n`-th element in the sequence is $$f(n)=f(n-1)+f(n-2).$$
# Use a while loop to write a code that prints out all the Fibonnaci numbers less than 10000.
# In[ ]: