Submit all homework before class on the LMS.
Practice problems available here. Do as many as you can!
If you want to get better at programming - http://imgur.com/gallery/qr9gq
What we have covered in this class:
for
and while
, nested loops)Last things to review
Introducing
How can we calculate the xth prime number?
It is important to be able to use functions to break complex problems into smaller parts.
Write a function to find the x-th prime number. You will need a helper method as well to determine if a number is prime (return a boolean).
def find_prime(x):
???
def is_prime(x):
???
I’m sick :( Make up class at the end of the semester.
Festival - great job on all your performances and activities.
The .items() function is very helpful
my_dict = {'a': 1, 'g':2}
for (number, letter) in my_dict.items():
print (number,letter)
# a 1
# b 2
Let’s use this to break a code.
We will decode this message without the secret key.
Mjqqt rd sfrj nx Rfwp fsi N qnpj yjfhmnsl ajwd rzhm fy ymnx zsnajwxnyd. N fr xt mfuud yt xjj mtb lwjfy dtz mfaj itsj ymnx xjrjxyjw
We need to count the frequency of each letter. The most frequent letter is mapped to an ‘e’, the most frequent letter in the English language. Remember to use triple quotes for long strings.
ciphertext = """Mjqqt rd sfrj nx Rfwp fsi N qnpj yjfhmnsl ajwd rzhm fy ymnx zsnajwxnyd. N fr xt mfuud yt xjj mtb lwjfy dtz mfaj itsj ymnx xjrjxyjw"""
lettercount={}
for letter in ciphertext:
if letter not in lettercount:
lettercount[letter] = 1
else:
lettercount[letter] += 1
for k,v in lettercount.items():
print (k, v)
Analyze the frequency of words in a Korean Buddhist text. Find a Korean text online (The Lotus Sutra is a good example), and report which words appear most frequently. What conclusion can you draw about these words. You will need to use the function split().
["A","string","with,"words"] == "A string with words".split()
Use the code above for letter counting to get started.
<hr>
## 05/11
You can use the try/except blocks if you like, but they are not generally necessary.
```python
def foo(lst,i):
if (i >= len(lst)):
return lst[(len(lst)-1)]
return lst[i]
def foo2(lst,i):
try:
lst[i]
except:
return lst[(len(lst)-1)]
Finish all of the 8 assigned codecademy lessons by Monday. You lose 20 points for each lesson that is not complete.
Review selection sort.
Like lists, but more fun! Now we can reference items by name, rather than index. You can read an in-depth lesson, but codecademy has everything you need.
Finish codecademy lesson on dictionaries, sections 10-14.
A new way to sort a list with selection sort
[1,2,3,4,2] -> [1,2,2,3,4]
[-5,7,0,3] -> [-5,0,3,7]
def sort_a_list(x):
# your code
return x
Like lists, but more fun! Now we can reference items by name, rather than index. You can read an in-depth lesson, but codecademy has everything you need.
Finish codecademy lesson on dictionaries, sections 10-14.
Errors are important! be sure to read them!
We learned how to sort a list (Bubble sort)! Here is a nice video
Finish writing the sorting algorithm.
[1,2,3,4,2] -> [1,2,2,3,4]
[-5,7,0,3] -> [-5,0,3,7]
def sort_a_list(x):
for i in range (?,?):
for j in range (?,?):
if ( x[?] > x[?]):
#chage the items
???
return x
More on lists.
[1,2,3,4,2] -> 4
[-5,7,0,3] -> 7
[1,2,3,4,2] -> 12
[-5,7,0,3] -> 5
Finish codecademy lesson on lists, sections 1-9.
Review the solutions for the exam.
Outdoor activity practicing sorting of lists.
Lists and Dictionaries
Do sections 1-3 of Codecademy lesson Python Lists and Dictionaries
No class, go vote.
Midterm Exam
Midterm Exam on 4/11 - next week!
We will review functions by working on Rock, Paper, Scissors. If you have any questions about the exam, ask them today.
Some example questions. You might be asked what a program will print:
def triple(x):
print("Tripling", x)
return x * 3
def subtract(y, z):
print("Subtracting", y, z)
return y - z
if subtract(20, triple(10)) > 0 and subtract(-25, triple(5)) > 0:
print("Woot!")
else:
print("Yikes!", subtract(triple(3), 0))
(min(2, 13, 3, 7)) * max(2, -34, 4)) - abs(-3)
You might be asked to write a program:
Or you might be asked to identify the error in bad code:
"april"+16
word = "computer"
word[8]
5.lower()
(13+9)/0
Review for the test - you can use the worksheet. If you don’t know the answer to a question, ask the person who wrote it!
You can also redo lessons on codecademy. There are also many lessons on the internet.
We will program the game Rock, Paper, Scissors today. Start by filling in the basic template here - then we will add new features.
import random
#a loop
#get the human input
computerChoice = random.randInt(1,3)
#if the computerChopie is 1 then 'R'
#if the computerChopie is 2 then 'P'
#if the computerChopie is 3 then 'S'
#print the computer's choice
#if(???):
# print ("its a tie")
#elif (???):
# print ("computer wins")
#elif (???):
# print ("human wins")
Now let’s add the ability to keep score - how many games has the computer won, and how many games did the human win? Now we will use functions to make the code look a little nicer.
Write the RPS game code.
Finish the NIM game together - you can download the final code. Then, review what we have learned so far.
True
, False
, and
, or
+ * / - % **
, str(3)
, ==
, >
""
, len("test")
, "test".lower()
if elif else
for
, range
, while
, break
input()
and print()
def
, return
Review/finish the functions lesson on codecademy.
We reviewed the worksheet that you made as a class.
(due 3/30)
We introduced functions.
Enjoy your weekend.
Presentations. Well done! Topics presented include variables, Booleans, Strings, and
, toAlpha
(due 3/23) Practice problem 1 - Write a “what does this program print” exercise for your classmates. Your program should use some of the ideas from the presentations today. When the program runs it should print something and your classmates will guess what it prints. The best 3 submissions will be used on the mid-term exam.
(due 3/21) Prepare a group presentation for Monday on a programming topic. The presentation should be in Korean. Explain your topic in detail. Show how to code with your topic. Give many (5+) examples of different ways to use your topic. For example:
True AND False
(3>4) AND (7<9)
(x==y OR y>x) AND (len("Hello")==4)
Make sure you have completed the following lessons on Codecademy:
Complete lesson 3 on Codecademy.
english python Some of our work will be done on this site, in English. korean python If the English is challenging, this site has similar materials in Korean that may be used as a supplement, but not a replacement
Complete lesson 1 & 2
Download the Syllabus.
We will use codecademy.com, please create an account. After you create an account, please take this survey, where you will be asked to input your username from codecademy.com.
In your free time, install python on your local computer. This is not required right now, but will be needed later in the semester.