JamCoders

💾 Download

Lecture 1, Part 2 Exercises

Question 1: Conceptual Recap from Part 1

1.1

Loosely define the following terms:

  • Code
  • Python
  • Programming Language
  • Jupyter Notebook
  • Algorithm
In [ ]:
# Anything reasonable works

1.2

In lecture, we saw that most algorithms have:

  • variables
  • operations (ex. +, %, *)
  • control (ex. loops, if statements).

What is the purpose of each of these things? What do they do?

In [ ]:

Question 2: len()

2.1

What is the length of "Jamcoders!"

In [ ]:

2.2

How about the length of "Jamcoders! "? (Note that there is a space at the end)

In [ ]:

2.3

What is the length of "I am twenty nine letters long"?

In [ ]:

2.4

Does None have a length?

In [ ]:

2.5

What is the length of ['one', 'two', 'three']?

In [ ]:

2.6

Print the length of 0, then in the next line, the length of 15

In [ ]:

2.7

Why might you think the above (in 2.6) does not work?

In [ ]:
# Answer in a comment here!

2.8

Print the length of each of the following:

  • 'hi'
  • ['h', 'i']
  • [['h', 'i']]
  • ''
  • []
  • [1, 2, 3]
  • '[1, 2, 3]'
  • ['']
  • [['h', 'i'], '', []]

Try to guess before you find out!

In [ ]:
# Write code here
# len('hi')
# ...

Question 3: Errors

3.1

Generate a TypeError below

In [ ]:

3.2

Generate a NameError below

(for example if you have an error with capitalization of a variable name)

In [ ]:

3.3

Generate a ZeroDivisionError below

In [ ]:

3.4

Generate an IndexError below

(for example if your list has 5 elements, but you try to get the 10th element)

In [ ]:

Question 4: Brackets

Different types of brackets can be confusing, and in different contexts brackets can mean different things.

Give an example of:

  • using square brackets: [] to create a list
  • using square brackets: [] to index into a list
  • using round brackets: () to specify order of operations
  • using round brackets: () to call a function
In [ ]:
# [] to create a list
# Code here!

# [] to index into a list
# Code here!

# () to specify the order of operations
# Code here!

# () to call a function
# Code here!

4.1 - optional

This question is optional

What is the type of [1, 'hi']?

In the next line, what is the type of (1, 'hi')?

In [ ]:

Question 5: Matching a Truth Table

For this question, generate the boolean expression that fulfulls the given truth table.

For example if you are given this table:

A Output
True False
False True

One solution could be not A

In [ ]:
# Example:

# If A is True then the Output of not A is False
A = True
print(not A)

# If A is False, then the Output of not A is True
A = False
print(not A)

# So, not A is a valid solution!

5.1

A Output
True True
False True
In [ ]:

5.2

A B Output
True True True
True False False
False True False
False False False
In [ ]:

5.3

A B Output
True True False
True False False
False True False
False False True
In [ ]:

5.4

A B Output
True True True
True False False
False True True
False False True
In [ ]:

5.5

A B Output
True True False
True False True
False True True
False False False

For a challenge, try to make your expressions as short as possible!

In [ ]:

5.6 - optional

this question is completely optional! You can try it if you have extra time

Can you make the truth table for or using only the and and not operators?

This is the truth table for or:

A B Output
True True True
True False True
False True True
False False False
In [ ]:

5.7 - optional

this question is completely optional! You can try it if you have extra time

Based on 5.6, suppose you have a truth table that you have represented using and/or/not. Can you also represent this truth table only using and/not without using or?

Why or why not?

In [ ]:
# write your answer as a comment here if you want to try this problem!

Question 6: Fix my mistake!

Elijah makes a lot of mistakes when coding. Can you help fix his mistakes?

6.1

T = True
print(T == true)
>>> NameError

Write the correct code in the cell below!

In [ ]:
T = True
print(T == true) # Fix Me!

6.2

foods_and_numbers = ['ackee', 2, ['callaloo', 'Jerk chicken'], 7.5]

# I want to print out the first element of the list
print('The first element of the list is: ', foods_and_numbers[1])
>>> 2

Write the correct code in the cell below!

In [ ]:
foods_and_numbers = ['ackee', 2, ['callaloo', 'Jerk chicken'], 7.5]

# I want to print out the first element of the list
print('The first element of the list is: ', foods_and_numbers[1]) # Fix me

6.3

foods_and_numbers = ['ackee', 2, ['callaloo', 'Jerk chicken'], 7.5]

# I want to print out Jerk chicken
print('I like to eat: ', foods_and_numbers[3][1])
>>> TypeError

Write the correct code in the cell below!

In [ ]:
foods_and_numbers = ['ackee', 2, ['callaloo', 'Jerk chicken'], 7.5]

# I want to print out 'I like to eat: Jerk chicken'
print('I like to eat: ', foods_and_numbers[3][1]) # Fix me

6.4

random_numbers = [1, 3, 7, 2, 9, 8, 16, 15, 6, 2, 5, 7]

# I want to get the last element
last_index = len(random_numbers)

# So I take the last element using the last index
print(random_numbers[last_index])
>>> IndexError

Write the correct code in the cell below!

In [ ]:
random_numbers = [1, 3, 7, 2, 9, 8, 16, 15, 6, 2, 5, 7]

# Fix me!

# I want to get the last element
# last_index = len(random_numbers)

# So I take the last element using the last index
# print(random_numbers[last_index])

6.5

random_numbers = [1, 3, 7, 2, 9, 8, 16, 15, 6, 2, 5, 7]

# I want to add my favorite number 14 to the end of the list
random_numbers += 14
>>> TypeError

Write the correct code in the cell below!

In [ ]:
random_numbers = [1, 3, 7, 2, 9, 8, 16, 15, 6, 2, 5, 7]

# I want to add my favorite number 14 to the end of the list
# random_numbers += 14 # Fix me!

6.5

random_numbers = [1, 3, 7]

# I want [1, 3, 7, 1, 3, 7, 1, 3, 7]
random_numbers = [random_Numbers] *= 3
prin(random_numbers)

Write the correct code in the cell below!

In [ ]:
random_numbers = [1, 3, 7]

# Fix me!

# I want [1, 3, 7, 1, 3, 7, 1, 3, 7]
# random_numbers = [random_Numbers] *= 3
# prin(random_numbers)
[[1, 3, 7], [1, 3, 7], [1, 3, 7]]

Question 7: What time is it?

7.1

Suppose time is a variable that is given and stores some number of seconds. How many days, hours, minutes, and seconds is this? Create these four variables with the appropriate values.

For example, 3662 seconds should mean days == 0, hours == 1, minutes == 1, seconds == 2.

T = 3662

# Your code here...
days = ...
hours = ...
minutes = ...
seconds = ...

print('Days: ', days)
...
In [ ]:
T = 1000000

# Your code here! 
#

# The should print out something like: 11 days 13 hours 46 minutes 40 seconds

Question 8: Obstacle Course

In this question, you control a variable called player. Your goal is to never print out False, and never run into an Error. However, there are some lines that you are not allowed to edit.

In other words, the rules to the game are:

  • Do not alter any print statement
  • Otherwise, add any code you want, anywhere
  • Make the code Error free
  • Pass all levels!
In [ ]:
# Set player to any value you would like!
player = None

print('Level 1 passed?', 
      type(player) == str) # Do not edit this print statement!

print('Level 2 passed?', 
      type(player) == int and player % 2 == 1) # Do not edit this print statement!

print('Level 3 passed?', 
      len(player) == 3) # Do not edit this print statement!

print('Level 4 passed?', 
      player == 'secret passcode') # Do not edit this print statement!

print('Level 5 passed?', 
      player % 3 == player % 5 == player % 7 and (player > 8)) # Do not edit this print statement!

print('Level 6 passed?', 
      player[3] == 'Boaz' and player[0] == player[1]) # Do not edit this print statement!

print('Level 7 passed?', 
      type(player) == str and len(player) == 5) # Do not edit this print statement!

print('Level 8 passed?', 
      type(player[0]) == list and player[0] + player[1] == player[2] and len(player[2]) > 0) # Do not edit this print statement!

print('Level 9 passed?', len(player) == 2022 and type(player) == str) # Do not edit this print statement!

print('Level 10 passed?', player[2021] == 'jamcoders') # Do not edit this print statement!

print('Bonus level passed?', type(player) == list and len(player) == 2022 and len(player[0]) == 2022) # Do not edit this print statement!

Question 9: Create Your Own Obstacle Course -optional

This question is completely optional!

Write your own obstacle course, just like we did in question 13! Then, get a friend who has also finished the other exercises, or a TA, to play your obstacle course!

Please make sure the obstacle course is possible to complete :)

In [ ]:
# Write your obstacle course here!

# Let the player set the values of the player
player = None

print(True) # Write your obstacles here! The player cannot edit these lines!

Question 10: Open Ended -optional

This is a completely optional, open ended question that you can fill out if you'd like to!

Feel free to:

  • comment about today's lecture
  • ask any questions about what we've talked about
  • ...or ask anything about computer science and algorithms in general
  • ... write a reflection, or anything else!

and/or you can explore more about Python:

  • What if you try using different types in boolean operations. For example, is a number True? Is 0 True? Is a string True? How about a float? What happens when you try to use boolean operations like and/or on these types?
  • How is the equal sign (=) in Python the same or different from the equal sign (=) you might see in a math class? Another thing you could explore is the similarities and differences between is and ==?
  • For many math operations, the order does not matter. (For example, a + b = b + a). What are some operations where order does matter? Does order matter in all cases?
  • ...Or anything that you might be curious about! Have fun :)
In [ ]:
# Optionally explore here!

# What if you try using different types in boolean operations. 
# For example, is a number True? 
# Is 0 True? 
# Is a string True? 
# How about a float? 
# What happens when you try to use boolean operations like and/or on these types?
In [ ]:
# Optionally explore here!

# How is the equal sign (=) in Python the same or different from 
# the equal sign (=) you might see in a math class? 
# Another thing you could explore is the similarities and differences between is and ==?
In [ ]:
# Optionally explore here!

# For many math operations, the order does not matter. 
# (For example, a + b = b + a). 
# What are some operations where order does matter? 
# Does order matter in all cases?