Note: revised submission instructions below (also, this is a link to another part of this page, which is not something that we covered in HTML).
Resources for Python exercises:
On the homepage, look at the Python cribsheet
Remember that when Python divides two integers, it does the same thing as Scheme's QUOTIENT function. That's why 12/5 results in 2.
Python uses the operator "%" to calculate the remainder when one number is divided by another. Therefore 13 % 5 results in 3.
So the variable FRED's value is even if FRED % 2 == 0 (assuming FRED contains an integer).
Here are a couple of examples:
|
sum_between(low,high) |
# Add up the numbers between the low first argument and the high second argument
# low and high must be integers, with low <= high
def sum_between(low,high):
count = low
acc = 0
while count <= high:
acc = acc + count
count = count + 1
return acc
|
|
rev_2dig(n) |
# reverse the digits of any number between 0 and 99
# rev_2dig(95) -> 59
# rev_2dig(8) -> 8
def rev_2dig(n):
# one digit?
if n < 10:
answer = n
else:
# two digits:
answer = (n%10)*10 + n/10
return answer
|

There are several Python exercises below. You need to create a web page containing the Python functions that are the answers to those exercises.
Problem 1:
Create a function called div23(n) which will take a
single
non-negative integer and return True if it's evenly
divisible by both 2
and 3, and False otherwise. Special answer: what is div23(4492738)?
Problem 2:
Create the function div23_between(low,high) that will be
given 2
non-negative integers, with low <= high and
will return the
number of integers between low and high (including low and high)
that are
divisible by both 2 and 3. Special answer: what is div23_between(0,399154)?
Problem 3:
Create the function sum_dig(n) that adds up the digits of
n, where n
is a non-negative number less than 1,000. Therefore
sum_dig(314) returns
8. Special answer: what is
sum_dig(667)?
Problem 4:
How many integers between 0 and 999 have the following property:
that the sum
of its digits is evenly divisible by 11. For instance, 65
has that
property and so does 966. How many such numbers are
there? Create
the function how_may_11() to answer that question.
Special answer: what is how_many_11()?
Challenge Problem (optional - but try your hand at it):
Create the function is_prime(n) that will be given an
integer greater
than 1, and will return True if it's a prime number and False
if
not. Test this carefully on the low numbers. Remember:
the first 4
primes are 2, 3, 5 and 7. Special answer: what is is_prime(12343)?