Notes and Assignments
Goal: Dealing with the digits of an integer.
There are 5 more functions added to the custom problems on codingbat. And another 3 to be added soon. ALL Custom problems should be completed by the Tuesday after break. You will have a quiz on the Wed after break.
How do we split up a number into digits?
Can you look at the last digit?
How about the second to last digit?
Would this pattern help you get any digit? Explain!
Log in, then lock screen.
HW: Complete the required codingbat problems. (All of the custom problems should be done)
Regarding None: When a function does not have a return statement, then it returns a special value: None. When you see a None you may have forgotten to return, OR you printed instead of returning. Another possibility is you put your return statements inside of conditional statements, and they never triggered.
Some more string methods:
We can replace parts of a string, and make a new string, but the original doesn't change
s = "abcd"
print s.replace('bc','XYZ') # prints 'aXYZd'
print s # prints 'abcd'
We can see if a string ends with a different string.
s.endswith('ca') # False
s.endswith('cd') # True
We can check if strings are letters, numbers, or both!
"asdf".isalpha() # evaluates to True
"asdf2".isalpha() # evaluates to False
'123'.isdigit() # True
'123.45'.isdigit() # False
"asdf2".isalnum() # evaluates to True
We can remove spaces, or characters we don't want from the left/right sides of a string!
" ab ".strip() # 'ab'
"abcddda".strip("ad") # 'bc'
CLASSWORK + Complete as homework:
Complete Codingbat String-2 and the new custom problems:
-The last problem of String-2 is challenging!
-You may use all built-in methods to solve String-2
-The custom problems restrict you from using a method, pay attention
A complete list of string methods are here:
#Get single characters:
s # 'a'
s # 'b'
#slices of the string s[i,j] gives you the charactes starting at index i
#up to but not including index j. This can be multiple characters!
s[1:3] # 'bc'
s[2:7] # 'cdbcA'
s[x:x+n] gives you n letters, starting at x.
#There is a function to calculate the length.
len(s) # 7
CodingBat String-1 (complete these 4 for homework if you do not finish)
MORE FEATURES OF STRINGS
#String methods are the functions attached to the string:
s.find('c') # 2s.find('c',3) # 5
s.find('bc') # 1
s.find("fish!") # -1
s.count('bc') # 2
s.count('BC') # 0
STOP! This is where we get to write something that is built in so you don't think that the methods are magical constructs created by elves in a software development fairytale.
#return the index the single letter is located
#or -1 when the letter is not inside the string
#return how many times the single letter is located
#in the string, 0 means it isn't there.
(complete these 2 for homework if you do not finish)
Both of these can be found here: http://firstname.lastname@example.org/all
s.replace('bc','PQ-') # 'aPQ-dPQ-A'
s.isalpha() # True
'123'.isdigit() # True
'123.45'.isdigit() # False
s.endswith('cA') # True
“ ab “.strip() # 'ab'
"abcddda".strip("ad") # 'bc'
HW: I consolidated all of the codingbat assignments onto this page:
Complete Codingbat set3 (hailstone)
OLD HOMEWORK REMINDER: (I will not remind you in the future to keep up with codingbat)
To avoid penalties and understand the work we are doing next, you MUST complete ALL codingbat problems assigned.
This includes the following currently:
make10, sleep_in, monkey_trouble, diff21,
parrot_trouble, makes10, near_hundred, pos_neg
sorta_sum, near_ten, love6
lone_sum, lucky_sum, no_teen_sum
4) Custom Problems: *edit* you can access set2 any way you want. It works both ways.
http://email@example.com/set3 * this is new as of today *
Goal: Generating a sequence with a loop.
The hailstone sequence starts at any natural number, and always terminates with the value 1.
To generate the sequence you follow a simple set of rules:
if n = 1 : done
if n is even: the next number is n / 2
if n is odd: the next number is 3 * n + 1
So hailstone sequences starting with the first 6 values:
3 10 5 16 8 4 2 1
4 2 1
5 16 8 4 2 1
6 3 10 5 16 8 4 2 1
Write a function printHail(n) that will print the hailstone sequence (on one line for readability)
You can print with a comma at the end of the line to prevent the new line from printing
Normally we don't print out things, instead we return them. We know the starting/ending number of each sequence, so that isn't a useful thing to return. Something we cannot predict is the length of the sequence!
Write a function that calculates the length of a hailstone sequence that starts with n:
(look at the example sequences above to see why!)
n -> hailLen(n)
1 -> 1
2 -> 2
3 -> 8
4 -> 3
5 -> 6
6 -> 9
Complete the rest of the codingbat set2-loop problems.
(3 of the new problems sumOfPowers, sumRange, SumAtoB)
Goal: While loops
(Loops will not be on Friday's quiz)
Python while loops take the following format:
inside of loop
do code here
#still inside of loop
Evaluate the boolean expression:
When the boolean is true: Do the entire body, then check the boolean again.
When the boolean is false: Skip the entire body, and continue with the rest of the code.
**See Examples below.
Classwork: With a partner, open Idle, and write code to accomplish the following goals. Come up with tests to verify you are correct! BEFORE class ends, email the code to both you and your partner. You will switch who is typing halfway through.
1. Write a loop that prints the sequence 2,4,6, ... 28,30. (multiples of two from 2 to 30 inclusive)
2. Write a loop that prints the multiples of 3 from 12 to 60 (inclusive).
3. Write a loop that prints all of the perfect squares from 1 to 10,000 inclusive (the squares are between 1 and 10000, you are not squaring 10000!!!!)
...Give the keyboard to your partner!
4. Write a loop that calculates the sum of all of the numbers between 0 and 3000 that are multiples 5. Print the sum.
5.Write a loop that calculates the sum of all of the numbers between 0 and 3000 that are multiples of 5 and also multiples of 7. Print the sum.
I placed custom codingbat problems here: (under set2 )
You have had regular codingbat problems to do, but also custom ones! (set1) In order to check custom problems, remember to go back to this link for custom problems.
print "### Example 1: ###"
counter = 10 while counter > 0: print counter counter = counter - 1
print "### Example 2: ###"
i = 1 while i < 500: print i i = i * 2
print "### Example 3: ###"
temp = 200.0 while temp > 150: print "cooling off: ",temp temp = temp * .95 print "cool enough at ", temp
### Example 1: ### 10 9 8 7 6 5 4 3 2 1 ### Example 2: ### 1 2 4 8 16 32 64 128 256 ### Example 3: ### cooling off: 200.0 cooling off: 190.0 cooling off: 180.5 cooling off: 171.475 cooling off: 162.90125 cooling off: 154.7561875 cool enough at 147.018378125
All python *not including new material Thursday*
Python Reference sheet:
Homework: (or classwork when done with all classwork)
Goal: Top-Down Design of a program
1. Go over multiple ways to solve a few of the coding bat problems.
2. Designing a function, and breaking it up into subproblems.
Qa. What is an algorithm?
Many of the functions and programs you write are simple enough to complete without designing them. This is because you can easily understand the whole algorithm. A formula, or a simple set of if statements don't seem to require a design.
As you write more complex code, you cannot understand the whole algorithm or process all of the consequences at once. This is why it is a good idea to DESIGN your program rather than just hack it together by writing code until it works, even though it may seem like a waste of time for now.
Your design will be in plain English, which makes it faster and easier to write than actual code. It is also easier to find errors in your logic, and faster to fix than after you have coded the algorithm.
3. CConsider the following function: DO NOT WRITE CODE until instructed.
Look at the ones digit, and tens digit of n.
When they are the same, return the value of that digit.
When they are different, return the sum of the digits.
It doesn't matter if the number is a single digit, or more than 2 digits.
magicValue(816) -> 7 #add the ones and tens digits, extra digits do not count.
magicValue(122) -> 2 #doubles only count once
magicValue(2) -> 2 #when there is no tens digit, there is really just a 0 there.
Top down design: A plain English plan for your program.
***Write out your design, and answer the questions. DO NOT WRITE CODE until you do this.***
1. TASK: Write your steps out in English, tell the steps to your rubber ducky.
Qb. There are some calculations that must be made, and it would be much easier to read code if you named the calculations by creating a function. What helper function(s) would be nice to have before you write your function?
2. Show your steps to two neighbors, and see if you both agree on each other's algorithm. Do you agree on your helper functions?
3. Write test cases for your program. Not my test cases.
4. Write the function, and use your test cases.
Goal: Function in python
If you have not shared your Codingbat with me, please do so in your preferences: firstname.lastname@example.org
Variable and function names in python:
1. Start with a letter or underscore
2. The rest of the characters can be letters, numbers, and underscores.
Here you can find a short warmup problem set:
-The link is here, but you should read the rest of the warmup notes below it.
-Testing code is important, codingbat does it for you. Try testing your code in IDLE, by using the test cases that codingbat provides. Keep your IDLE window open, and your test cases displayed. You can always test more problems later! (You will be more likely to do this if you have IDLE running and ready)
-These codingbat problems are easy enough to write in the codingbat window. This does not give you all the features of IDLE, so if you don't get the right answer, you can get better error messages and parenthesis matching in IDLE.
If statements in python work the same way they did in scheme and netlogo:
statement1 #only executes when a is true
statement2 #executes all the time because it is not inside of the if
statement1 #execute when a is true
statement2 #execute when a is false
statement3 #executes all the time because it is not inside of the if
Since an if statement can be put inside another if statement we have to be careful. Different ways of connecting them can produce different effects. (discussed in class)
You may have noticed that if/else statements that are connected start to indent too much.
These statements can be rewriten using elif (else+if) as follows.
Complete: Coding bat problems
1-10 of 12