1.1
Loosely define the following terms:
# Anything reasonable works
1.2
In lecture, we saw that most algorithms have:
+
, %
, *
)What is the purpose of each of these things? What do they do?
2.1
What is the length of "Jamcoders!"
2.2
How about the length of "Jamcoders! "
? (Note that there is a space at the end)
2.3
What is the length of "I am twenty nine letters long"
?
2.4
Does None
have a length?
2.5
What is the length of ['one', 'two', 'three']
?
2.6
Print the length of 0
, then in the next line, the length of 15
2.7
Why might you think the above (in 2.6) does not work?
# 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!
# Write code here
# len('hi')
# ...
3.1
Generate a TypeError below
3.2
Generate a NameError below
(for example if you have an error with capitalization of a variable name)
3.3
Generate a ZeroDivisionError below
3.4
Generate an IndexError below
(for example if your list has 5 elements, but you try to get the 10th element)
Different types of brackets can be confusing, and in different contexts brackets can mean different things.
Give an example of:
[]
to create a list[]
to index into a list()
to specify order of operations()
to call a function# [] 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')
?
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
# 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 |
5.2
A | B | Output |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
5.3
A | B | Output |
---|---|---|
True | True | False |
True | False | False |
False | True | False |
False | False | True |
5.4
A | B | Output |
---|---|---|
True | True | True |
True | False | False |
False | True | True |
False | False | True |
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!
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 |
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?
# write your answer as a comment here if you want to try this problem!
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!
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!
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!
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!
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!
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!
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)
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)
...
T = 1000000
# Your code here!
#
# The should print out something like: 11 days 13 hours 46 minutes 40 seconds
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:
print
statement# 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!
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 :)
# 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!
This is a completely optional, open ended question that you can fill out if you'd like to!
Feel free to:
and/or you can explore more about Python:
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 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 ==
?a + b
= b + a
). What are some operations where order does matter? Does order matter in all cases?# 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?
# 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 ==?
# 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?