Notes and Assignments

  • Prog. Envir.: IDLE, python, others

  • Numerical types, arithmetic

  • Functions

  • Variables

  • Conditionals


posted Feb 17, 2017, 7:03 AM by Samuel Konstantinovich

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!

2017-02-16 HW

posted Feb 16, 2017, 6:46 AM by Samuel Konstantinovich   [ updated Feb 16, 2017, 6:57 AM ]

Log in, then lock screen.

HW: Complete the required codingbat problems. (All of the custom problems should be done)

On paper:

Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string length will be at least 2.

middleTwo("string") → "ri"
middleTwo("code") → "od"
middleTwo("Practice") → "ct"
Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.

withoutX("xHix") → "Hi"
withoutX("xHi") → "Hi"
withoutX("Hxix") → "Hxi"

3. Requies a loop! - This is on paper for now, but you must complete on codingbat as homework.
Given a string, return the string without the vowels.
*is there a part of this code that you would like to break off into a separate function? If so, then assume it was written*

noVowels("fish") → "fsh"
noVowels("spymaster") → "spymstr"
noVowels("Advent") → "dvnt"


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.

2017-02-15 HW

posted Feb 15, 2017, 6:39 AM by Samuel Konstantinovich   [ updated Feb 15, 2017, 7:33 AM ]

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

2017-02-14 HW

posted Feb 14, 2017, 6:49 AM by Samuel Konstantinovich   [ updated Feb 15, 2017, 6:32 AM ]

Goal: Strings

A complete list of string methods are here:


#Get single characters:
s[0] # 'a'
s[1] # '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)


#String methods are the functions attached to the string:
s.find('c') # 2 
s.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.

def findLetter(string,letter):
  #return the index the single letter is located
  #or -1 when the letter is not inside the string 

def countLetter(string,letter):
  #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: 

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:

2017-02-13 HW

posted Feb 13, 2017, 6:42 AM by Samuel Konstantinovich   [ updated Feb 14, 2017, 6:26 AM ]

Today's Homework:
     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:
1) Warmup-1:
    make10,    sleep_in,    monkey_trouble,    diff21,    
    parrot_trouble,    makes10,    near_hundred,    pos_neg
2) Logic-1:
  sorta_sum,      near_ten,      love6
3) Logic-2:
  lone_sum,      lucky_sum,    no_teen_sum
4) Custom Problems: *edit* you can access set2 any way you want. It works both ways.  * 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:
2 1 
3 10 5 16 8 4 2 1
4 2 1
5 16 8 4 2 1
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
print 5,
print 6

5 6

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

2017-02-10 HW

posted Feb 10, 2017, 11:03 AM by Samuel Konstantinovich   [ updated Feb 10, 2017, 11:04 AM ]

Complete the rest of the codingbat set2-loop problems.
(3 of the new problems sumOfPowers, sumRange, SumAtoB)

2017-02-08 HW

posted Feb 8, 2017, 6:55 AM by Samuel Konstantinovich   [ updated Feb 8, 2017, 8:29 AM ]

Goal: While loops
(Loops will not be on Friday's quiz)

Python while loops take the following format:
while boolean_expression:
    inside of loop   
    do code here
    #still inside of loop   
outside 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: ###
### Example 2: ###
### 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


posted Feb 7, 2017, 6:33 AM by Samuel Konstantinovich

Do Now: (5 minutes)

Log in, lock your screen. 

1. What is an 'expression'?

2. What is a keyword?

3. What does it mean for a program to terminate?

4. Can a program not terminate? Explain.

If you are done, and you need to unlock the screen to get yesterday's work, then do so now.

Classwork: (Open or take out the work from yesterday)

5. What is the difference between parameters and arguments? (Formal Parameters vs Actual Parameters)

6. What is 'abstraction' in the context of problem-solving?

 -> Go Over Yesterday's work.


2017-02-06 HW

posted Feb 6, 2017, 6:56 AM by Samuel Konstantinovich   [ updated Feb 6, 2017, 7:13 AM ]

Quiz Friday
All python *not including new material Thursday*

Python Reference sheet:

Homework: (or classwork when done with all classwork)
Codingbat Warmup-1

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.

2017-02-03 HW

posted Feb 3, 2017, 7:11 AM by Samuel Konstantinovich

Goal: Function in python

If you have not shared your Codingbat with me, please do so in your preferences:

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:

if a:
   statement1 #only executes when a is true

statement2 #executes all the time because it is not inside of the if

if a:
   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)

if a:
   if b: 


if a:
   if b: 
   if c: 


if a:
   if b: 
   if c: 


if a:
   if b: 
   if c: 

You may have noticed that if/else statements that are connected start to indent too much.
if a:
   if b: 
      if c:

These statements can be rewriten using elif (else+if) as follows.

if a:
elif b: 
elif c:


Complete: Coding bat problems 

1-10 of 12