Due: March 1st, 2017, before midnight
Late penalties: Thu -5%, Fri -10%, Sat, Sun 20%.
lab5.py
, and run
it. Then do the exercises below.
Scroll to the bottom of the file. When you run the
module in IDLE, or type python lab5.py
on the
command line, the code that reads
if __name__ == '__main__': main()will call the function
main()
.
The function main()
is where your program
will begin running. Notice that it has a several calls to
various functions. As written, it is testing the
functions that you will be modifying.
All of your programs should be written this way: they
should include a function main()
, which is
where your program will begin running.
The function bits_to_int
is passed a list of
bits, and returns the number which they represent. For
example, the binary number 110012
(base 2) is the number 2510
(base 10).
Thus the following code:
print bits_to_int([1, 0, 0, 1, 1])shoud print
25
. Notice that the list of bits
is given in reverse order from what you would expect, but
don't worry: it's more convenient that way.
Your Task: implement the
function digits_to_int
, which is similar to
this function, but is passed a second parameter, the
base. Simply replace all the 2
s
with base
.
The function int_to_bits
does the opposite
job: it is passed an int
, and returns a list of
bits. Again, the list will be backwards from the way
you'd normally expect, but don't worry.
Your Task: implement the
function int_to_digits
, which is similar to
this function, but is passed a second parameter, the
base. Simply replace all the 2
s
with base
.
Suppose you are given a number in binary, written as a
string. For example, '11001'
. We want to
obtain its individual bits, and obtain the number as
an int
. The
function
bit_string_to_int(bit_string)does this, and you must implement it.
Your Task: The task can be done as follows:
list()
function can turn a string
into a list of strings. For example,
list('banana')returns
['b', 'a', 'n', 'a', 'n', 'a']
.
Call list(bit_string)
, which splits the
string into single strings, each of which
is '0'
or '1'
. Store the
result in a variable called bit_strings
.
'0'
or '1'
strings into numbers. Create
an empty list, called bits
. Then, write
a for
loop, which visits each of these
strings, applies int()
to it, and appends
the result onto bits
.
bits.reverse()
,
because they're in the right order, and our existing
functions expect them in the wrong order!
bits_to_int(bits)
, and return its
result.
Aliasing and list copy:
In the final exercise, you will implement a function
pair_averages
which is passed a list of numbers, and returns another
list of numbers, which contains the averages of each pair
of numbers in the first list.
For example, if the list passed in is
[10, 20, 30, 40]then the function should return
[10, 15, 25, 35]The average of 10 and 20 is 15. The average of 20 and 30 is 25. The average of 30 and 40 is 35. We can't average 10 with the number before it, because 10 is the first number. So we just leave it alone.
I've written an incorrect version of the function. Notice that it returns
[10, 15, 22, 31]What went wrong? First it replaced the second value with (10+20)/2=15, then replaced the third value with (15+30)/2=22, then replaced the last value with (22+40)/2=31
The problem is that we are modifying the data, as we go through it. Try this instead:
averages = numbers for i in range(1, len(numbers)): averages[i] = (numbers[i - 1] + numbers[i]) / 2 return averagesThis produces the same result! What went wrong?
The problem is that averages = numbers
does
not produce a new copy of numbers
. It makes
a new variable, which refers to the same data.
Try this instead:
averages = copy.copy(numbers)This does make a separate copy of the list. The program now works. Well, you'll have to import the
copy
module first!
But you can do it yet another way:
averages
for
loop, and append a
value to averages
, each time
This almost works, because you'll end up missing the first value. Fix that.
mycourses.unh.edu
,
find CS414, and find lab 5. Click the "Submit" button and
upload lab5.py
.