Part I due by 11:59 p.m. on Thursday, October 24, 2023.
Part II due by 11:59 p.m. on Sunday, October 27, 2023.
In your work on this assignment, make sure to abide by the collaboration policies of the course.
If you have questions while working on this assignment, please
come to office hours, post them on Piazza, or email
cs111-staff@cs.bu.edu
.
Make sure to submit your work on Gradescope, following the procedures found at the end of Part I and Part II.
5 points; individual-only
Grace Murray Hopper was one of the pioneers of computer science. Among other things, Hopper played an important role in the transition from low-level programming (using binary machine-language instructions that the CPU could execute directly) to programming in higher-level languages like Python.
This week’s reading involves a biographical sketch of Hopper that provides an overview of her impressive career.
After reading this piece, create the necessary file for your response by taking the following steps:
Access the template that we have created by clicking on this link and signing into your Google account as needed.
When asked, click on the Make a copy button, which will save a copy of the template file to your Google Drive.
Select File->Rename, and change the name of the file to
ps5pr0
.
In your copy of ps5pr0
, write a response that addresses the
following question:
According to the piece, Grace Murray Hopper was instrumental in “opening up new worlds to computing.” How did she do this? What do you find most impressive about her work?
Once you have completed your response, choose
File→Download→Plain Text, and save the file on your
machine. The resulting file (ps5pr0.txt
) is the one that you will
submit. See the submission guidelines at the end of Part I.
In the rest of this assignment you will design several digital circuits using a circuit-design tool called Logicly. If you haven’t already installed Logicly on your own computer, you should do so now:
(Important: Although it’s possible to use Logicly in your browser, you need to download it in order to be able to save your work.)
That page also includes guidelines for how to use Logicly – including a link to a video that provides a good overview of the program.
Important: There appears to be a bug in Logicly that causes it to occasionally display red wires. If this happens, save your file, close down Logicly, and then reopen it. This will typically fix the problem.
If you can’t get Logicly to work on your own computer, you can use it on the virtual desktop. Instructions for using the virtual desktop can be found here.
Download the following zip file: ps5_circuits.zip
Unzip this archive, and you should find a folder named ps5_circuits
, and
within it a starter file for each of the circuit problems.
Warning
In each of the starter files, we have included switches for the inputs and one or more light bulbs for the outputs. You must use the provided components in your circuit, and you should not change their properties (e.g., their names) in any way.
If you fail to use the components that we have provided or if you change them in any way, we will not be able to automate the grading of your submission, and you will lose points as a result.
5 points; individual-only
Open the ps5pr1.logicly
starter file in Logicly (see above for how
to obtain the starter files).
The XOR function takes two inputs (x
and y
), and it outputs a 1
if and only if exactly one of its inputs is 1. It has the following
truth table:
inputs | x y | output ------------------- 0 0 | 0 0 1 | 1 1 0 | 1 1 1 | 0
Build a circuit for the XOR function using the minterm expansion
principle that we discussed in lecture. The resulting circuit should use
only AND
, OR
, and NOT
gates.
Notes:
We’ve provided you with switches for the inputs x
and y
, as
well as with a NOT
gate for the negation of each input. For
example, the leftmost switch is for x
itself, and the NOT
gate
immediately to its right is for NOT x
. We have also provided you
with a light bulb for the output. You must use these
components in your circuit, and you should not change their
properties.
You are welcome to move the provided components as needed, but
please keep their relative positions the same. For example, the
inputs should remain at the top of your circuit, and x
should be
to the left of y
.
Use the Selection Tool to drag the necessary logic gates into your circuit and to add the necessary wires. See the overview video for more detail.
You may also find it helpful to use the Pan Tool (the icon at the top of the screen that looks like a hand – or possibly a different icon that looks like crossed green arrows) and the magnification slider available in the lower right-hand corner to adjust how much of your circuit is visible. After using the Pan Tool, don’t forget to switch back to the Selection Tool.
Test your circuit by clicking the input switches to turn them on and off. Check to see if you obtain the correct output for all possible inputs. Remember that XOR should output a 1 if and only if exactly one of the two inputs is a 1.
Once this circuit is working, save your work by using the File->Save menu option.
25 points; pair-optional
This is one of only two problems that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.
Open the ps5pr2.logicly
starter file in Logicly.
If you are working with a partner, there is a text block at the top
of this subcircuit for your partner’s name and email (if any). To add the
necessary information, select the text block with the Selection Tool
(the arrow) and edit the Text in the Label Properties pop-up
window.
Build a Full Adder (FA) as described in lecture, using the minterm
expansion principle to create a circuit that uses only AND
, OR
,
and NOT
gates.
Remember that a Full Adder adds a single column of digits from the bitwise sum of two binary numbers. It takes three inputs:
x
and y
, the two bits being added from this column of the additionc_in
, the carry bit into this column of the additionand it should produce two separate outputs:
s
, the bit from the sum that would go at the bottom of this column
of the additionc_out
, the carry bit out of this column of the addition.See below for how to export your Full Adder so that it can be used in later problems.
Notes:
After opening the file, you may need to use the Pan Tool (the hand icon available at the top of the screen) and the magnification slider available in the lower right-hand corner to see all of the provided components. After using the Pan Tool, don’t forget to switch back to the Selection Tool.
We’ve provided you with the three inputs and the two outputs, as well
as with NOT
gates for the inputs. You must use the provided inputs
and outputs, and you should not change their properties.
You can adjust the positions of the inputs and output as needed, but please keep their relative positions the same.
You should start with the truth table for a Full Adder. You can
write it out yourself, or you can use the one that we gave you
in the lecture notes. Once you have the truth table, you should
apply the minterm expansion principle twice: once to obtain a
circuit that will produce the correct outputs for s
, and once to
obtain a circuit that will produce the correct outputs for c_out
.
Don’t try to simplify your circuit, and don’t use gates other
than AND
, OR
, and NOT
. The purpose of this problem is to
practice using the minterm expansion principle. (Note: Logicly
provides a sample Full Adder circuit, but it uses an approach other
than minterm expansion, so it’s not useful for this problem.)
Here again, you should test your circuit by clicking the input switches to turn them on and off. Once this circuit is working, save your work by using the File->Save menu option.
Exporting your Full Adder: Because you will use your Full Adder in later problems, you should take the following steps to export it:
Use the Edit->Select All menu option to select the entire circuit.
Click the building block icon (which looks like a Lego piece) at the top of the screen. (Alternatively, you can select the Edit->Create Integrated Circuit menu option.)
In the window that pops up, give your circuit a name (e.g., Full
Adder
) by entering it in the Full Name text box. You can also
add a Symbol Label, but doing so is not required.
Click the Create IC button to create the integrated circuit.
Use the File->Export Integrated Circuit Library menu option
to export your work. Give the file an appropriate name (e.g.,
full_adder
) and save it in a place where you can easily find it.
15 points; individual-only
Open the ps5pr3.logicly
starter file in Logicly.
Build a 4-Bit Ripple-Carry Adder. We examined a 2-Bit Ripple-Carry Adder in lecture; this problem requires you to extend that circuit to a 4-bit version.
Recall that a Ripple-Carry Adder combines multiple Full Adders to produce a circuit that adds two binary numbers. Each Full Adder handles one column of the addition. The circuit as a whole takes eight inputs:
x3
, x2
, x1
, and x0
y3
, y2
, y1
, and y0
and it should produce five outputs (s4
, s3
, s2
, s1
, and s0
)
that correspond to the five bits of the sum. The relationships between the
inputs and the outputs are shown below:
x3 x2 x1 x0 + y3 y2 y1 y0 -------------- s4 s3 s2 s1 s0
Note that the sum has five bits to accommodate the possibility of a carry.
This circuit should not be built using the minterm expansion principle. Rather, it should be built by combining four instances of your Full Adder circuit.
See below for information about how to import your Full Adder, as well as for the steps needed to export Ripple-Carry Adder so that it can be used in later problems.
Notes:
Here again, you should use the Pan Tool and the magnification slider as needed to see all of the provided components.
You must use the provided inputs and outputs.
Please keep their relative positions the same, and do not change
their properties. (Note that we haven’t provided NOT
gates for the
inputs because you won’t need to negate them.)
Using Your Full Adder:
Custom
that contains a building block for your Full Adder.You will need a c_in
of 0 on the far right of the Ripple-Carry
Adder. Use the built-in component labeled Low Constant
for this
purpose. Simply drag it into your circuit and connect it to the
appropriate input of the rightmost FA.
Here again, you should test your circuit by clicking the input switches to turn them on and off. Once this circuit is working, save your work by using the File->Save menu option.
Exporting your Work: Because you will use your 4-Bit Ripple Carry Adder in later problems, you should take the following steps to export it:
Use the Edit->Select All menu option to select the entire circuit.
Click the building block icon (which looks like a Lego piece) at the top of the screen. (Alternatively, you can select the Edit->Create Integrated Circuit menu option.)
In the window that pops up, give your circuit a name (e.g., 4-Bit
Ripple-Carry Adder
) by entering it in the Full Name text
box. You can also add a Symbol Label, but doing so is not
required.
Click the Create IC button to create the integrated circuit.
Use the File->Export Integrated Circuit Library menu option to
export your work. Note that you will end up exporting both your
Full Adder (since you imported it for this problem) and your
Ripple-Carry Adder. Give the file an appropriate name (e.g.,
both_adders
) and save it in a place where you can easily find it.
Important
Make sure that you do not have any imported components that you are not actually using in your circuit. Such components will cause an error in Gradescope.
To check for this, open your circuit in Logicly and scroll down to the Custom section of the left-hand component bar. For each component in the Custom section, right click on it and choose “Delete Integrated Circuit.” If the component is actually being used in your circuit, Logicly will display a message indicating that the component cannot be deleted. If the component is not being used, Logicly will allow you to delete it.
10 points; individual-only
Open the ps5pr4.logicly
starter file in Logicly.
Build a 4x1 multiplier in the space provided. This circuit takes five inputs:
x3
, x2
, x1
, and x0
y
.It will have four outputs (p3
, p2
, p1
, and p0
) that correspond to
the four bits of the product. The relationships between the
inputs and the outputs are shown below:
x3 x2 x1 x0 * y -------------- p3 p2 p1 p0
Because you are multiplying the first factor by either 0 or 1, the product can be computed very simply, using exactly four gates! Considering some concrete cases should help you to determine which gates you should use, and how they should be arranged. You should not need to employ the minterm expansion principle.
Notes:
Here again, you should use the Pan Tool and the magnification slider as needed to see all of the provided components.
You must use the provided inputs and outputs, and you must not change their properties. Please keep their relative positions the same.
We’ve positioned the outputs at the bottom of the circuit, because doing so will make your circuit easier to use in the context of the next problem. Because of this, you may want to adjust the directions in which your gates are facing. After adding a gate to the circuit, you can rotate it by selecting it and then using one of the rotate icons at the top of the screen.
As always, you should test your circuit by clicking the input switches to turn them on and off. Once this circuit is working, save your work by using the File->Save menu option.
Exporting your 4x1 Multiplier: Because you will use your 4x1 Multiplier in the next problem, you should take the following steps to export it:
Use the Edit->Select All menu option to select the entire circuit.
Click the building block icon (which looks like a Lego piece) at the top of the screen. (Alternatively, you can select the Edit->Create Integrated Circuit menu option.)
In the window that pops up, give your circuit a name (e.g.,
4x1 Multiplier
) by entering it in the Full Name text
box. You can also add a Symbol Label, but doing so is not
required.
Click the Create IC button to create the integrated circuit.
Use the File->Export Integrated Circuit Library menu option
to export your work. Give the file an appropriate name (e.g.,
4x1_multiplier
) and save it in a place where you can easily
find it.
15 points; individual-only
Open the ps5pr5.logicly
starter file in Logicly.
Build a 4x2 multiplier, which takes six inputs:
x3
, x2
, x1
, and x0
y1
and y0
.and produces as outputs the six bits of the product: p5
, p4
, p3
,
p2
, p1
, and p0
.
Your circuit should perform the computation shown below:
x3 x2 x1 x0 * y1 y0 ----------------- p5 p4 p3 p2 p1 p0
You circuit should employ the “elementary-school” binary multiplication
algorithm that we discussed in lecture. More specifically, you need to
compute two partial products: the product of the top number
(x3 x2 x1 x0
) with the bit y0
, and the product of the top number
with the bit y1
. You then need to add these partial products together
in the appropriate way.
This circuit should not be built using the minterm expansion principle. Rather, it should be built by combining one or more instances of your 4x1 Multiplier and your 4-Bit Ripple-Carry Adder. See below for information about adding those components to your circuit.
Notes:
Here again, you should use the Pan Tool and the magnification slider as needed to see all of the provided components.
You must use the provided inputs and outputs, and you must not change their properties. Please keep their relative positions the same.
You can insert one or more copies of your 4x1 Multiplier and 4-Bit Ripple-Carry Adder using the same procedure that you used to add instances of your Full Adder in Problem 3. In this case, you will need to import two files: the one containing both adders (i.e., your work from Problem 3), and the one containing your 4x1 Multiplier (i.e., your work from Problem 4).
We recommend that you rotate each 4x1 Multiplier and 4-Bit Ripple-Carry Adder clockwise, following the procedure used to rotate your Full Adder in Problem 3. Doing so will cause each component’s inputs to be at the top of its box, and its outputs to be at the bottom of its box.
You will need at least one constant in your circuit. See the notes that accompany Problem 3 for a reminder of how to add a constant.
Don’t forget to test your circuit and save your work! During testing, you may find it helpful to use a calculator that can multiply in binary. Here are three options:
In Windows 10 or later, open the Calculator by searching for it from the Start menu. Then use the Programmer menu option, and choose the BIN option near the left-hand side of the window.
In older versions of Windows, open the Calculator by going to Start Menu->Accessories->Calculator. Then use the View->Programmer menu option, and choose the Bin option on the left-hand side of the window.
In Mac OS X, open the Calculator from /Applications, Spotlight, or Launchpad. Then select the View->Programmer menu option.
Write a function in Python that makes use of the base-conversion functions that you wrote for Problem Set 4!
Important
See our warning at the end of Problem 3 about unused imported components. Make sure to delete any unused components before you submit your work.
Login to Gradescope by clicking the link in the left-hand navigation bar, and click on the box for CS 111.
Important: You should submit a plain-text file for Problem 0, not a PDF. See Step 1 below as needed.
You should only submit the following files:
ps5pr0.txt
ps5pr1.logicly
ps5pr2.logicly
ps5pr3.logicly
ps5pr4.logicly
ps5pr5.logicly
You do NOT need to submit the files that Logicly produces when you export your components.
Here are the steps:
If you still need to create a plain-text file for Problem 0,
open your ps5pr0
file on Google Drive, choose
File->Download->Plain Text, and save the file on your machine.
Click on the name of the assignment in the list of assignments. You should see a pop-up window with a box labeled DRAG & DROP. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.)
Add your files to the box labeled DRAG & DROP. You can either drag and drop the files from their folder into the box, or you can click on the box itself and browse for the files.
Click the Upload button.
You should see a box saying that your submission was successful.
Click the (x)
button to close that box.
The Autograder will perform some tests on your files. Once it is done, check the results to ensure that the tests were passed. If one or more of the tests did not pass, the name of that test will be in red, and there should be a message describing the failure. Based on those messages, make any necessary changes. Feel free to ask a staff member for help.
Note: You will not see a complete Autograder score when you submit. That is because additional tests for at least some of the problems will be run later, after the final deadline for the submission has passed. For such problems, it is important to realize that passing all of the initial tests does not necessarily mean that you will ultimately get full credit on the problem. You should always run your own tests to convince yourself that the logic of your solutions is correct.
If needed, use the Resubmit button at the bottom of the page to resubmit your work. Important: Every time that you make a submission, you should submit all of the files for that Gradescope assignment, even if some of them have not changed since your last submission.
Because your Logicly files have a format that Gradescope can’t read, you won’t be able to view those files on the Code page. Instead, you should click on the Download button for each Logicly file to download it. Once you have done so, open the downloaded file in Logicly to ensure that the uploaded file is the correct one.
Important
It is your responsibility to ensure that the correct version of every file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check your submissions carefully using the steps outlined above.
If you are unable to access Gradescope and there is enough
time to do so, wait an hour or two and then try again. If you
are unable to submit and it is close to the deadline, email
your homework before the deadline to
cs111-staff@cs.bu.edu
Problems 6, 7 and 8 will be completed in a single PDF file. To create it, you should do the following:
Access the template that we have created by clicking on this link and signing into your Google account as needed.
When asked, click on the Make a copy button, which will save a copy of the template file to your Google Drive.
Select File->Rename, and change the name of the file to
ps5pr678
.
Add your work for Problems 6, 7 and 8 to this file.
Once you have completed these problems, choose
File->Download->PDF document, and save the PDF file on your machine.
The resulting PDF file (ps5pr678.pdf
) is the one that you will
submit. See the submission guidelines at the end of Part II.
10 points; individual-only
Consider the following Python program:
def foo(a, b): a += 4 b *= 2 c = mystery(a) print('foo', a, b, c) c += mystery(b) print('foo', a, b, c) return c def mystery(c): a = c + 3 if c > 8: a //= 2 if a > 6: c -= 2 elif c < 20: a += 1 if a == 5: c += 1 else: c -= 3 return a a = 2 b = 3 c = 8 print(a, b, c) a = foo(b, c) print(a, b, c)
In the Problem 6 section of ps5pr678
, we have provided tables for
you to complete. We have started some of the tables for you. You
should:
You may not need all of the rows provided in the tables.
range
function6 points; individual-only
As we saw earlier in the semester, range()
is a built-in
function that can be used to generate a sequence of integers.
This function is often used in conjunction with for
loops in
Python, so it’s important that you fully understand how it works.
Put your answers to the following questions in the Problem 7
section of ps5pr678
.
What results do you obtain when you evaluate each of the following
expressions in the console? (Note: In order to see the
sequence that the range
function generates, you need to use the
list
function, as shown below.)
a. list(range(5))
b. list(range(2, 7))
c. list(range(8, 11))
d. list(range(1, 10, 2))
e. list(range(9, 0, -3))
f. list(range(0, 9, -3))
The for
loops below use an explicit list of numbers (e.g.,
[0, 1, 2, 3, 4]
). Let’s say that we want to replace these
explicit lists with expressions involving the range()
function. What range()
-function expression would be needed in
each case? (Note: You do not need to include the list
function in this case, because we only need the appropriate
range
expression to generate the necessary sequence of integers.)
a. first for
statement:
for x in [0, 1, 2, 3]: print(x)
b. second for
statement:
for x in [5, 6, 7]: print(x)
c. third for
statement:
for x in [15, 11, 7, 3]: print(x)
10 points; individual-only
Consider the following Python function, which uses an
index-based for
loop:
def mystery(values): count = 0 for i in range(len(values)): if values[i] < values[i-1]: count += 1 return count
Trace the execution of the following call to this function:
mystery([4, 7, 3, 5, 8, 1])
To do so, use the template that we have provided in
section 8-1 of ps5pr678
. In particular, you should:
Complete the table to illustrate how the expression at the top of each column changes during the execution of the loop.
The table begins with a row for the initial value of the
variable count
. Use the remaining rows to show what happens
for each value of the loop variable i
. You may not need all
of the rows.
Specify the return value of this function call in the space provided.
Consider the following Python program, which uses a while
loop:
a = 8 b = 3 print(a, b) while a > 2: a -= b b -= 1 print(a - b)
Complete the table that we have provided in section 8-2 of
ps5pr678
to show how the variables change over time and the
values that are printed. You may not need all of the rows.
30 points; pair-optional
This is one of only two problems in this assignment that you may complete with a partner. See the rules for working with a partner on pair-optional problems for details about how this type of collaboration must be structured.
Getting started
If you haven’t already done so, create a folder named ps5
for
your work on this part of the assignment. You can find
instructions for doing so here.
In Spyder, open a new editor window using File -> New File,
and save it to your ps5
folder using the name ps5pr9.py
.
Important guidelines
Here are the guidelines that you should follow for this and all remaining Python problems over the course of the semester:
Include comments at the top of the file
that are similar to the ones that we gave you at the top of
ps1pr3.py
.
Your functions must have the exact names specified below,
or we won’t be able to test them. Note in particular that the
case of the letters matters (all of them should be lowercase),
and that some of the names include an underscore character (_
).
As usual, each of your functions should include a docstring that describes what your function does and what its inputs are. In addition, you should include any other comments that might be necessary to explain your code. More generally, you should aim to make your code easy to read. For example, you should choose descriptive names for your variables. In addition, we encourage you to leave a blank line between logical parts of your function.
If a function is supposed to return a value, make sure that it actually returns the specified value, rather than printing it.
You should not use any Python features that we have not discussed in lecture or read about in the textbook.
Your functions do not need to handle bad inputs – inputs with a type or value that doesn’t correspond to the description of the inputs provided in the problem – unless the problem explicitly states otherwise.
Leave one or two blank lines between functions to make things more readable.
If you want to add test calls to this or any other Python file,
please do so in appropriate test function like the one that we
gave you at the bottom of the
ps1pr3.py
starter file for Problem
Set 1. After running your Python file, you can call the
test
function (by entering test()
) to see the results of
all of the test calls.
Do not add any lines of code that belong to the global scope of the program (i.e., lines that are outside of any function).
Write a function sum_squares(vals)
that takes as input a list of
numbers vals
and uses a loop to compute and return
the sum of the squares of the individual numbers in the list. For
example:
>>> sum_squares([2, 5, 3]) result: 38 >>> sum_squares([1, 2, 3, 4]) result: 30 >>> sum_squares([6, 8]) result: 100 >>> sum_squares([]) result: 0
You solved a similar problem in Problem Set 2 using recursion, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Here is a template that you can use to get started:
def sum_squares(vals): """ your docstring goes here """ total = 0 for ____ in ______: ...
Hint: This function should perform a cumulative computation that gradually builds up the sum of the squares of the numbers.
Write a function double(s, target)
that takes as input an arbitrary
string s
and and a single-character string target
, and that uses
a loop to determine and return a new string in which
all occurrences of target
in s
are doubled/repeated. For example:
>>> double('program', 'r') result: 'prrogrram' >>> double('program', 'o') result: 'proogram' >>> double('banana', 'a') result: 'baanaanaa'
You solved this problem using recursion in Problem Set 2, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Here’s a template that you can use to get started:
def double(s, target): """ your docstring goes here """ result = '' for ____ in ______: ...
Hint: This function should perform a cumulative computation that
gradually builds up a string. We discussed a similar function (a
loop-based remove_vowels
) in Monday’s lecture.
Write a function process(vals, x)
that takes as input a list of
0 or more integers vals
and a single integer x
, and that uses
a loop to create and return a new list in which each
element of the original list that is larger than x
is replaced
with a 0
. For example:
>>> process([5, 8, 3, 12], 6) result: [5, 0, 3, 0] >>> process([5, 8, 3, 12], 4) result: [0, 0, 3, 0] >>> process([5, 8, 3, 12], 15) result: [5, 8, 3, 12] >>> process([], 2) result: []
You solved a similar problem in Problem Set 2 using recursion, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Hint: This function should perform a cumulative computation that gradually builds up a list. We’ll write a similar function in this week’s lab.
Write a function diff(vals1, vals2)
that takes as inputs two
arbitrary lists of non-negative integers vals1
and vals2
and
uses a loop to construct and return a new list in
which each element is the the absolute value of the difference of
the corresponding elements from the original lists. For example:
>>> diff([3, 4, 2, 5], [7, 2, 9, 5]) result: [4, 2, 7, 0] :::python >>> diff([5, 6, 2], [1, 9, 15]) result: [4, 3, 13] >>> diff([], []) result: []
If one of the lists is longer than the other, its “extra” elements – the ones with no counterparts in the shorter list – should appear immediately after the differences (if any) in the returned list. For example:
>>> diff([0, 3, 6, 9], [1, 1]) result: [1, 2, 6, 9] >>> diff([3, 1, 9], [2, 4, 5, 7]) result: [1, 3, 4, 7]) >>> diff([1, 2], []) result: [1, 2] >>> diff([], [3, 4, 5]) result: [3, 4, 5]
You solved a similar problem using recursion in Problem Set 2, but for this problem set you must use a loop. You may not use recursion or a list comprehension.
Hints:
You will need to use an index-based loop so that you can
access the corresponding elements from vals1
and vals2
at
the same time. Here is a template that you can use to get
started:
def diff(vals1, vals2): """ your docstring goes here """ result = [] len_shorter = min(len(vals1), len(vals2)) for i in range(len_shorter): ...
Note that we determine the length of the shorter list before beginning the loop, because the loop should only consider the index values that are present in both lists.
After the loop, you will need to handle any “extra” numbers
from the longer list (for cases in which the lists don’t
have the same length). One way to do that is to use
conditional execution (e.g., an if-else
statement),
although other approaches may also be possible.
14 points; individual-only
In Spyder, open a new editor window for your code, and save it to
your ps5
folder using the name ps5pr10.py
.
This problem is designed to give you practice in selecting the correct type of loop for a given problem.
Important
One of the functions must use a for
loop, and
one must use a while
loop. You will need to
decide which loop construct is the appropriate one for each
function. Think about whether each function is better suited to a
definite loop or an indefinite loop.
You should continue to follow the guidelines that we gave you in the previous problem.
Here again, any test calls should go in an appropriate test function. Do not add any lines of code that belong to the global scope of the program (i.e., lines that are outside of any function).
Write a function get_even_val()
that takes no inputs and that uses a
loop to obtain an even integer from the user. More specifically,
the function should perform the following tasks:
Get an initial integer value from the user using the input
function and the prompt
Enter an even integer:
If the user does not enter an even integer, keep asking for additional inputs until the user enters an even value. These additional inputs (if any) should use the following prompt:
The input must be even. Try again:
Once the user enters an even value, return the value.
You may assume that the user always enters an integer.
For example:
>>> get_even_val() Enter an even integer: 3 The input must be even. Try again: -5 The input must be even. Try again: 10 result: 10
Notes/hints:
The final line shown above displays the return value of the function. The function should not actually print this line; it should just return the appropriate value.
The function should use either a for
loop or a while
loop. You should decide which loop construct is the better
one for this function, based on whether its task lends itself
better to a definite loop or an indefinite loop.
Don’t forget that input
always returns a string. As a result,
you will need to use the int
function to convert the input
to an integer as discussed in lecture.
Write a function add_powers(m, n)
that takes a positive integer
m
and an arbitrary integer n
, and that uses a loop to add
together the first m
powers of n
(from n**0
up to and including n**(m-1)
power) and that returns the
resulting sum.
In addition, your function should also print a sequence of messages that describe the powers that are included in the sum. For example:
>>> add_powers(4, 2) 2 ** 0 = 1 2 ** 1 = 2 2 ** 2 = 4 2 ** 3 = 8 result: 15
Notes/hints:
The final line shown above displays the return value of the function. The function should not actually print this line; it should just return the appropriate value.
The function should use either a for
loop or a while
loop. You should decide which loop construct is the better
one for this function, based on whether its task lends itself
better to a definite loop or an indefinite loop.
You will need an accumulator variable for the sum of the powers, which is the value that you will ultimately return. Don’t forget to initialize that variable before the start of the loop, and to update it within the loop.
Here are some other examples:
>>> add_powers(6, 3) 3 ** 0 = 1 3 ** 1 = 3 3 ** 2 = 9 3 ** 3 = 27 3 ** 4 = 81 3 ** 5 = 243 result: 364 >>> add_powers(3, -6) -6 ** 0 = 1 -6 ** 1 = -6 -6 ** 2 = 36 result: 31
Important
If you made the correct choice of loop for both functions in this
problem, one of them should have used a for
loop and one should
have used a while
loop.
Login to Gradescope by clicking the link in the left-hand navigation bar, and click on the box for CS 111.
You will make submissions to two separate assignments on Gradescope. The steps needed for the two submissions are different, so please make sure to follow carefully the procedures outlined below.
PS 5: Problems 6-8
Submit your ps5pr678.pdf
file using these steps:
If you still need to create a PDF file, open your file on Google Drive, choose File->Download->PDF document, and save the PDF file on your machine.
Click on the name of the assignment in the list of assignments on Gradescope. You should see a pop-up window labeled Submit Assignment. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.)
Click the Select PDF button and find the PDF file that you created. Then click the Upload PDF button.
You should see a question outline along with thumbnails of the pages from your uploaded PDF. For each question in the outline:
As you do so, click on the magnifying glass icon for each page and doublecheck that the pages that you see contain the work that you want us to grade.
Once you have assigned pages to all of the problems in the question outline, click the Submit button in the lower-right corner of the window. You should see a box saying that your submission was successful.
If needed, use the Resubmit button at the bottom of the page to resubmit your work.
PS 5: Problems 9 and 10
Submit your ps5pr9.py
and ps5pr10.py
files using these steps:
Click on the name of the assignment in the list of assignments. You should see a pop-up window with a box labeled DRAG & DROP. (If you don’t see it, click the Submit or Resubmit button at the bottom of the page.)
Add your files to the box labeled DRAG & DROP. You can either drag and drop the files from their folder into the box, or you can click on the box itself and browse for the files.
Click the Upload button.
You should see a box saying that your submission was successful.
Click the (x)
button to close that box.
The Autograder will perform some tests on your files. Once it is done, check the results to ensure that the tests were passed. If one or more of the tests did not pass, the name of that test will be in red, and there should be a message describing the failure. Based on those messages, make any necessary changes. Feel free to ask a staff member for help.
Note: You will not see a complete Autograder score when you submit. That is because additional tests for at least some of the problems will be run later, after the final deadline for the submission has passed. For such problems, it is important to realize that passing all of the initial tests does not necessarily mean that you will ultimately get full credit on the problem. You should always run your own tests to convince yourself that the logic of your solutions is correct.
If needed, use the Resubmit button at the bottom of the page to resubmit your work. Important: Every time that you make a submission, you should submit all of the files for that Gradescope assignment, even if some of them have not changed since your last submission.
Near the top of the page, click on the box labeled Code. Then click on the name of each file to view its contents. Check to make sure that the files contain the code that you want us to grade.
Important
It is your responsibility to ensure that the correct version of every file is on Gradescope before the final deadline. We will not accept any file after the submission window for a given assignment has closed, so please check your submissions carefully using the steps outlined above.
If you are unable to access Gradescope and there is enough
time to do so, wait an hour or two and then try again. If you
are unable to submit and it is close to the deadline, email
your homework before the deadline to
cs111-staff@cs.bu.edu
Last updated on October 21, 2024.