#!/usr/bin/env python # coding: utf-8 # # Exercises for day 1b (Extra) # ## Exercise # # Write code that contains each letter of the alphabet **at most once** and prints: # - `chechebsa` (Example below) # - `banananight` # - `abebe abebe abebe !` # In[ ]: "che" * 2 + "bsa" # In[ ]: # In[ ]: # ## Exercise # # Look at this code doing a computation with multiple steps: # # ``` # x = 4 # y = x * 2 # z = y / (x - 2) # z # ``` # # what is the value of `z`? Verify your solution by running the code above. # # Note: We put `z` on the last line so that we can see the value of `z` at the end. When you execute a cell, you always see the value of the last expression in that cell. # In[ ]: # ### Exercise # # What happens if we swap the first two lines, like this: # # ``` # y = x * 2 # x = 4 # z = y / (x - 2) # z # ``` # # Why? # In[ ]: # ### Exercise # # Given this code: # ``` # d = 1 # c = d # b = c # a = b # ``` # what is the value of a? Verify your solution by running the code above. # # Note: You may not see any output. Why is that? (Look at the previous two exercises.) Add another line so that you can see the value of `a`. # In[ ]: # ### Exercise # # Given this code: # ``` # x = 12 # y = 5 # z = y # y = 12 # x = 1 # z = x + y + z # z # ``` # what is the value of z? Verify your solution by running the code above. # In[ ]: # ### Exercise # # Given this code: # # ``` # exp = 10/2*4 # exp = exp**2/4 # exp # ``` # # what is the value of `exp`? Verify your solution by running the code above. # # Note: Here you see that variables can have names with multiple letters. # In[ ]: # ## Exercise # # Consider a circle of diameter 5 and a rectangle with side lengths 2 and 4. Which one has the larger area? Use a comparison operator to find out. # # Note: You can approximate $\pi$ as `3.14`. # In[ ]: # ### Exercise # # Now consider a circle of diameter `d` and a rectangle with side lengths `d-1` and `d+1`. Which one has the larger area? Does it depend on `d`? # # It is tedious to rewrite the comparison from the previous exercises repeatedly to replace the value of `d` everywhere. Instead, do the following: # # Define an integer `d=5`, and write your comparison in terms of `d`. Now change the value of `d` and run your code again to explore the behavior of these areas. # In[ ]: # ## Exercise # Let's say you roll a toy car off of a perfectly horizontal table at a speed of $v = 2m/s$. The table is $h = 1 m$ tall measured from the ground. How far from the table would the car go before reaching the ground? Find this distance $d$! You can assume gravity is $9.81 m/s^2 $ # # ![](toycar.png) # Step 1: Compute $t$, the time taken until the car hits the ground. # In[ ]: # Step 2: Using the value of $t$, now compute the distance $d$. # In[ ]: # ### Exercise # # If you want to compute $d$ for different values of $h$ and $v$, you have to rewrite the computations from above. You can use variables to avoid that, as follows: # # ``` # g = 9.81 # h = 1 # v = 2 # ``` # # Write the expression for $t$, the time taken until the car hits the ground, as an expession using $g$, $h$ and $v$. # In[ ]: # We can save this computed value of $t$ in a variables as well. Take the code below, and insert your answer from above for t. # # ``` # g = 9.81 # h = 1 # v = 2 # t = [insert your answer from above] # ``` # # Add a line at the end with the computation for $d$ (the distance the car travels before hitting the ground) in terms of $t$ and the other variables. # In[ ]: # Change the values of $v$ and $h$ and run the your code from above again. Now you can solve this question for different values of $v$ and $h$! # ## Exercise # Abebe is a billionaire. As a billionaire he doesn't have much time to spend on reading things. He wants to look at his bank account and know how much money he has right away. Unfortunately, the bank's mobile app doesn't use commas to separate digits so we have to help him. Currently the bank's mobile applicaiton says he has `M=1123456789`amount of money. Can you write a few lines of code to make the amount of money to look like `1,123,456,789`? Make your code work for any billionaire who is not a trillionaire (in other words, assume that `1000000000