Programming Assignment

168 views 9:54 am 0 Comments March 20, 2023

1
CS 112 – Spring 2023 – Programming Assignment 3
Loops and Lists with Functions
Due Date: Monday March 13, Noon (12 pm)
This assignment is an Individual Effort assignment.
The Honor Code Applies.
The purpose of this assignment is to practice using loops and basic lists effectively.
See the
“assignment basics” file for more detailed information about getting assistance, running the test
file, grading, commenting, and many other extremely important things. Each assignment is governed by
the rules in that document.
Needed files:
Download the attached file to use the tests we will use when grading your assignment
Tester3.py
PA3Template.py
Background
Loop statements allow us to run the same block of code repeatedly. You may use either/any looping
commands that you choose (
while or for). Lists are sequences that can be inspected value-by-value and
modified at will. In this assignment we will use loops to perform calculations over
one-dimensional lists.
Consider that you may need nested or multiple loops for particular tasks below.
Restrictions
Any function that violates any of the following will lose significant number of points on the assignment.
You may use (and will likely need):
The range() function
The len() function
The .append() method on lists
You
may not use anything that hasn’t been covered in class yet. You may not:
import anything
use slicing
use sets and dictionaries
use built-in functions (with the exception of range() and len() )
use any string method
use list methods (exept for.append().)
use the del operator
use the in operator (except within the syntax of the for statement. In other words, use in
with a for loop only.
2
Insert comments in the lines you deem necessary
Testing
A template file (PA3Template.py) is provided which includes the basic outline of the functions you
are required to complete. The tester will be calling your functions with different arguments and
checking the return values to decide the correctness of your code.
Your functions should not
ask for user input and should not print anything.
When you run your code with the tester or
submit your code to
Gradescope, you can see the results of the tests.
Note: if you want to perform your own tests, you do not need to modify the tester. Simply call the
functions you’ve written providing arguments you want to check.
Submitting your code:
Submit your .py file (correctly named) to Gradescope under Programming Assignment 3
Link to Gradescope is in our Blackboard page and labeled “Gradescope”
Do not forget to name your file netID_2xx_PA3py, where netID is NOT your G-number but
your GMU email ID and 2xx is your lab section number.
Do not forget to copy and paste the honor code statement from the Assignment Basics
document to the beginning of your submission .py file.
Do not forget to comment your code
No hard coding!
Grading Rubric
:
5pts – Correct Submission (file is named correctly)
5pts – Well-Documented (code is commented)

40pts –Correct Program Results
————————-
Total: 50pts

Note: if your code does not run (immediately crashes due to errors), it will receive at most 12
points. No exceptions. As stated on our syllabus, turning in running code is essential.

3
TASKS
The signature of each function is provided below, do not make any changes to them otherwise the
tester will not work properly. The following are the functions you must implement:
1) (7 pts) Write a function called
sum_evens() that adds up all the even numbers from 1 up to
the number given as a parameter, inclusively.
Assumptions:
The argument in the function calls are positive integers.
Signature:
def sum_evens(n):
#your code here
# …
return sum1
Examples:
sum_evens(2) 2
sum_evens(10)
30
sum_evens(19)
90
2) (5 pts) Write a function called is_deficit() that determines if an integer is deficit. An
integer is deficit if the sum of its divisors (not including the number itself) is smaller than the
number itself.
Assumptions:
The argument in the function calls are positive integers.
Signature:
def is_deficit(n):
#your code here
# …
return result
Examples:
is_deficit(8) True
#8 has factors 1, 4 & 2; 1+4+2<8, so 8 is
deficit
is_deficit(12) False
#12 has factors 1, 2,3,4,6; 1+2+3+4+6>12, so 12 is
not deficit
3) (5 pts) Write a function called list_deficits() that consumes two integers which are the
top and bottom values of a range of integers. The function returns a list of deficit numbers in
the inclusive range of the two integers given as parameters.

4
HINT: You can re-use your code from Task 2, but you (should) use/call your is_deficit()
function from #2.
Assumptions:
Of the two integers given for the range, the first is less than the second. (You can assume that
bottom is less than top.)
Signature:
def list_deficits(bottom, top):
#your code here
# …
return def_list
Examples:
list_deficits(1,15) [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15]
list_deficits(28, 37)
[28, 29, 31, 32, 33, 34, 35, 37]
4) (7 pts) Write a function called hail_steps() that determines how many iterations are
needed to reach a result of 1 following the following formula:
If n is even, divide it by two.
If n is odd, multiply it by 3 then add 1.
If n is 1, then you’re “done”
Assumptions:
The argument in the function call is an integer.
Signature:
def hail_steps(n):
#your code here
# …
return num_steps
Examples:
hail_steps(4) 2
hail_steps(12)
9
hail_steps(55)
112
5) (8 pts) Write a function called keep_em() that takes a list of names and a list of Booleans of
the same length. This function strips out the names for which there is a corresponding value
False in the Boolean list.
Assumptions:
The length of the list of names is not longer than the list of Booleans.
Signature:
def keep_em(names, keepers):
#your code here
# …

5
return new_list
Examples:
keep_em([‘Ana’,’Buster’,’Carla’,’Demos’,”Estaban;,’Farhad’,’Gerri’,
‘Herrah’, ‘Irma’],[True, True, False, False, False, True, True,
True, False])
[‘Ana’, ‘Buster’, ‘Farhad’, ‘Gerri’, ‘Herrah’,]
keep_em([‘Rita’, ‘Rory’, ‘Rusty’],[False, False, False])
[]
6) (8 pts) Write a function called more_mults() that consumes top and bottom values for two
lists, and a factor and returns a string The function examines two ranges of numbers and
declares a ‘winner’ between the two lists. Given an integer,
n, the winning list is the one with
more multiples of
n. When the two lists have the same number of multiples of the given
number, then the shorter list wins. There is only a tie when the lists are the same length and
have the same number of multiples of
n, identified as the fifth parameter in the function call.
Assumptions:
The list-bottom numbers (positive integer) are less than or equal to their corresponding listtop number (positive integer). Also, the factor in the function call is also a positive integer.
Signature:
def more_mults (ls1_bottom, ls1_top, ls1_bottom, ls2_top, n):
#your code here
# …
return <‘
List 1‘ or ‘List 2‘ or ‘Tie‘>
Examples:
more_mults(166, 312, 55, 220, 3) List 2
more_mults(310, 400, 410, 500, 10)
Tie
more_mults (310, 400, 410, 506, 10)
List 1
NOTE: The last argument in these function calls (the 3 or the 10) is the factor being tested on each list.