JamCoders

💾 Download

Lecture 2, Part 1 Exercises

Question 0: Review

0.0

Whew! Yesterday we dived straight into Python! We hope you are really like Python so far.

Remember, raise your hand if you have any questions. We are here to learn together with you!

0.1

Write, in words in a comment (don't forget the #):

  • Two uses of square brackets []
  • Two uses of round brackets ()
In [ ]:
# Two uses of square brackets here!

# Two uses of round brackets here!

0.2

True or False: Mark each statement as True or False

  1. You can make a list with round brackets ().
  2. The assignment = is the same as the comparison ==.
  3. The assignment a = b means the same thing as the assignment b = a
  4. For integers a,b: a == b is the same as b == a
  5. The result of ==, and, or, not has type boolean (remember, boolean variables store True or False)
  6. The line a = b means we are setting the value stored inside b.
  7. The line a = b means we are setting the value stored inside a.
  8. Using == alone does not change the values stored inside the variables.

Write your answers here!

If you have any questions or are unsure, please raise your hand! We went over this pretty quickly yesterday, so we expect a lot of questions.

Question 1: Printing Things

1.1

Use function print to print "Boaz". On a new line, print his occupation - "Lecturer". Finish by printing the list of his favorite TAs - "Orr", "Jabari", "Natnael", and "Bryan".

In [ ]:
# Your code here.

1.2

Given the code below, print x, y, and z on three different lines.

In [ ]:
x = 17
y = 19
z = 42

# Your code here.

1.3

Given the code below, print u, v, and w on the same line. There are many different ways to do this!

Challenge: Do this two ways if you remember how!

In [ ]:
u = [3, 5, 8]
v = True
w = 3.14

# Your code here.

Question 2: List Indexing

2.1

Given the list list1, print the first element.

Hint: what index is the first element?

In [ ]:
list1 = ["coder", 2022, "jam"]

# Your code here.

2.2

Given the list list2, print the last element.

Hint: what index is the last element?

In [ ]:
list2 = ['apple', 'orange', 'banana', 'pear',
         'dragonfruit', 'lychee', 'starfruit']


# Your code here.

Question 3: String Comparisons

3.1

Try executing the code below. Can you change the output by changing only 1 character (there may be multiple solutions)?

In [ ]:
x = 42
y = 111

# Modify the output of this code by changing only 1 character:
print(x < y)

3.2

What is different in the case below? What will be printed?

x = "42"
y = "111"
print(x < y)
In [ ]:
# Your answer here

3.3

Construct an 4 digit number and a 5 digit number such that the 4 digit number is greater than the 5 digit number when they are of type string.

In [ ]:
# Your code here.

# Make sure to test your code here.

3.4

What would be the result of the following operations?

'A' == 'a' 
'Brunch'< 'banana' 
'coffee' < 'Coffee' 
'Dog' > 'cat'
'Bryan' != 'bryan'
'nadia' > 'nadi'
'CORINA VIRUS' < 'CORONA VIRUS'

3.5

Check your answer in the box below. Can you figure out the rules for string comparisons?

In [ ]:
# Explore here
# print('A' == 'a')

Question 4: Basic Functions

Note: sometimes each cell can also be hidden

4.1

Write a function that takes x as an argument, and returns 5 * x.

In [ ]:
def multFive(x):
  # Your code here.


    # Test your code here.
multFive(2)

4.2

Write a function that takes x as an argument and returns x + 2.

In [ ]:
def addTwo(x):
  # Your code here.


    # Test your code here.
addTwo(2)

4.3

Write a function that returns 5 * x + 10, without using any arithmetic operations.

Hint: use multFive and addTwo. Please run the cells for exercises 4.1 and 4.2 before running this cell.

In [ ]:
# Define your function here.


# Test your code here.

Question 5: Calculator Functions

5.1

Write a function called addXtoY that takes two arguments x and y, and returns their sum.

In [ ]:
# Define addXtoY(x, y) function here.


# Test your code here.

5.2

Write a function called multiplyXandY that takes two arguments x and y, and returns their product.

In [ ]:
# Define multiplyXandY(x, y) function here.


# Test your code here.

5.3

Similar to the functions from 5.1 and 5.2, add a few more functions that simulate a calculator's functions.

In [ ]:
# Define your functions here.


# Test your functions here.

5.4

Use your calculator functions to write a function that represents $ f(x) = 5x + 4 $.

In [ ]:
# Define your function here.


# Test your function here.

Question 6: String Functions

6.1

Write a function called concatXandY that takes two strings x and y, and concatenates them together.

In [ ]:
# Define your function here.


# Test your function here.

6.2

Write a function concatXYZ that takes three strings x, y, and z, and concatenates them together without using any arithmethic (including +) operations.

Hint: you can call the function you wrote in 6.1

In [ ]:
# Define your function here.


# Test your function here.

Question 7: CToF

7.1

Write a function cToF that converts Celsius to Fahrenheit using the equation $F = C \times \frac{9}{5} + 32$.

In [ ]:
# Define your function here.


# Test your function here.

7.2

Write a function fToC that converts Fahrenheit to Celsius using the equation $C = (F - 32) \times \frac{5}{9}$.

In [ ]:
# Define your function here.


# Test your function here.

7.3

What happens when you call cToF(fToC(x))? Try for a few values of x. Can you show that it is true using math?

In [ ]:
# Define your function here.


# Test your function here.

Question 8: Pythag

8.1

Write a function called pythagoreanTriplet that takes in 3 integers which will represent triangle side lengths and returns a bool, to determine if it forms a right-angled triangle. The arguments are guaranteed to be sorted in increasing order.

Hint: A triangle is a right-angled when side lengths a, b, and c satisfy $ a^{2} + b^{2} = c^{2}$.

In [ ]:
# Define your function here.


# Test your function here.
# Add your own test cases to confirm your solution works properly.
print(pythagoreanTriplet(3, 4, 5))
In [ ]:

Question 9: Palindrome

9.1

A palindrome is a string that is the same when read forwards and backwards. For example, "madam", "alabala", and "12321" are palindromes but "daniel", "hello", and "1234" are not. Write a function threePalindrome that takes in a THREE-LETTER string, and returns a boolean that is True when the string is palindrome, and False when it is not.

In [ ]:
# Define your function here.


# Test your function here. Try different three letter palindromes here!

Question 10: Funk! Func!

10.1

Look at the function below. Without calling it, can you predict the outcomes of calling funkyFunc(100), funkyFunc(123456), and funkyFunc(420)?

In [ ]:
# Run this cell first.
def funkyFunc(x):
    z = x * 10
    x = x % 10
    z = z + x
    return z
In [ ]:
# FILL IN YOUR ANSWERS FOR ??

# for x = 100,     funkyFunc(x) = ??
# for x = 123456,  funkyFunc(x) = ??
# for x = 420,      funkyFunc(x) = ??

# Check your answers below with code:

10.2

Can you write a function funkierFunc which takes one argument and for which the following equation is true for any $x>0$:

funkierFunc ( funkyFunc(x) ) = x

In [ ]:
def funkierFunc(x):
  # Your code here.

    # Check if your code is correct here:
funkierFunc(funkyFunc(12))

Question 11: Appendages

11.1

Add 'leg' to the octopus, human, and table using append()

In [ ]:
# 1. Octopus

octopus = ['leg', 'leg', 'leg', 'leg', 'leg', 'leg', 'leg']

# Add one more leg here!
# 

print(len(octopus)) # Should be eight!

# 2. Human

human = ['arm', 'arm', 'leg']

# Add one more leg here!
#

print(len(human)) # Should be four!

# 3. Table

table = ['top', 'leg', 'leg', 'leg']

# Add one more leg here!
#

print(len(table)) # Should be five!

11.2

Add 'end' to the sentence using append()

In [ ]:
sentence = ['put', 'the', 'word', 'end', 'at', 'the']

# Add the word end to sentence here!

print(sentence)

11.3

Add the number 3 to the end of the second list so that each list is the same length!

In [ ]:
list_of_lists = [[1, 2, 3], [1, 2], [1, 2, 3]]

# Add 3 to the end of the second list!

print(list_of_lists)

Question 12: Follow the pattern

Use insert() to fill in what might be missing in the correct place in the list

12.1

my_list_1 = [1, 2, 3, 5, 6] # insert 4
my_list_2 = ['a', 'b', 'd', 'e', 'f'] # insert c
my_list_3 = ['is', 'the', 'country', 'we', 'are', 'in'] # insert jamaica
my_list_4 = ['my', 'name', 'is'] # insert your name
my_list_5 = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6]] # insert [1, 2, 3, 4, 5]

Code below!

In [ ]:
my_list_1 = [1, 2, 3, 5, 6]
# Insert here!

print(my_list_1)
In [ ]:
my_list_2 = ['a', 'b', 'd', 'e', 'f']
# Insert here!

print(my_list_2)
In [ ]:
my_list_3 = ['is', 'the', 'country', 'we', 'are', 'in']
# Insert here!

print(my_list_3)
In [ ]:
my_list_4 = ['my', 'name', 'is']
# Insert here!

print(my_list_4)
In [ ]:
my_list_5 = [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6]]
# Insert here!

print(my_list_5)

Instructions for after you are done!

  • If you are finished, please raise your hand and let us know
  • If everything looks good, please move on to the extra questions in notebook probs2c, and do not start probs2b