CS 103 University of Alabama at Birmingham Computing Worksheet
CS103 Spring 2020 Homework 1 Page 1 of 4
HW2 CS103 Spring 2021 Unan
due: 02.28.2021 Sunday 11:59pm
HW2 syntax tested
the scalar types int, float, and bool • the arithmetic operators (+ – * / // ** %)
• assignment to a variable • comparators (<, ≤, >, ≥, ==, !=) • Boolean operators (and, or, not)
• branching (if) • assertions • the type function (testing the type of a variable)
• type casting • loops
HW2 style constraints
• each line should contain no more than 80 characters • do not hardcode the test cases into your
solutions (this is counterproductive anyway, since we will be testing your code using different test cases)
Grading
Grading: a problem is correct if all our tests pass, otherwise it will be considered as False.
Each Questions Worth 20 points
*** Do not forget to include “independent completion form” ***
HW2 syntax constraints
Certain Python syntax will be banned in each homework. The following syntax is banned for HW2:
printing, built in functions related to sequences (such as count in strings/lists/tuples). Associated
keywords are also banned.
Instructions
To work on HW2:
• build a HW2 directory under your 103sp21 directory
• Create hw2.py in this directory
• starting with the first problem, solve the rest of the HW2 problems by writing code in the file hw2.py
using the Sublime text editor, then testing in a terminal by running python hw2.py
• when testing your code, always run it from a terminal and from the same directory that contains your
hw2.py
• once you are confident that your code works, submit hw2.py on Canvas (please only submit once)
• If you forget how to do any of these steps, refer to the primer.
CS103 Spring 2020 Homework 1 Page 2 of 4
Mandatory Functions
The following functions should be implemented, and the return statements should be modified
with the correct credentials. Do not forget to call the functions
def myName():
return “James Bond”
def myBlazerID():
return “jbon12”
# Call these functions
print(“My Name is =”, myName(), “ and my BlazerId is =”,myBlazerID())
HW2 problems
Please use exactly the same names for the functions and the input parameters. Otherwise, the
auto grader will not work on your submissions. Also, make sure your script file name is hw2.py.
Failing to do so will be penalized.
isOdd (n1)
Write the function isOdd() that takes a single parameter n1 as input and returns the Boolean
value True if n is an odd integer and False otherwise. Note that the parameter may be any
type, but the return value is False whenever it is not an int.
For example, isOdd(5) is True and isOdd(5.1) is False.
The parity of an integer is an important property and a good introduction to modulo arithmetic.
Modulo arithmetic evolves into finite fields, which are used in cryptography. Finite fields are a
discrete form of periodic functions, like the cosine function, which are important throughout
science, such as in signal processing and complex analysis.
tupleCounter (t)
Write the function “tupleCounter” that takes a tuple t and returns an integer. The
function will check each element of the tuple and returns the total number of odd elements.
Assume the tuple contains only integers.
CS103 Spring 2020 Homework 1 Page 3 of 4
Sample Input: Expected Output:
t = (2,4,6,7,9,121,1) 4
t = (1,3) 2
t = (10,30,44) 0
cubeOfOdd (n2)
Write the function “cubeOfOdd” that takes an int n2 as input. The function will print i3
value for all non-negative odd integers i<n2.
Sample Input: Expected
Output:
Hints
n2 = 5 1
27
1
3=1
3
3=27
n2 =3 1 1
3=1
n2 =8 1
27
125
343
1
3=1
3
3=27
5
3=125
7
3=343
paintTheRoom(length, width, height)
Write the function paintTheRoom(length, width, height)that takes three floats
for length, width, and height of the rectangular room, all in feet. Using this
information and the nominal coverage for a given paint, compute the amount of paint needed
to cover the four walls and ceiling, assuming no doors or windows. The function returns the
whole number of gallons of paint needed. Assume one gallon of paint will cover 400 square
feet. You need to round up the required gallon number.
Sample Code Run
Assume a room of length 10 feet, width 12 feet and height 8 feet.
The area of the walls would be:
10 ft x 8 ft = 80 sq. ft. (2 walls this size)
12 ft x 8 ft = 96 sq. ft. (2 walls this size)
The area of the ceiling would be:
10 ft x 12 ft = 120 sq. ft.
The total area would then be:
2 x 80 sq ft. + 2 x 96 sq. ft. + 120 sq. ft = 472 sq. ft.
472 sq. ft / 400 sq. ft per gallon = 1.18 gallons thus 2
CS103 Spring 2020 Homework 1 Page 4 of 4
stringMerge(s1)
Write a function stringMerge to get a string s1 and returns a new string made of the first 2 and the
last 2 characters of the given string. If the string length is less than 2, return an empty string instead.
Sample Input 1
s1 = ‘uabcs2021’
Expected Output 1
ua21
Sample Input 2
s1 = ‘ua’
Expected Output 2
uaua
Sample Input 3
s1 = ‘u’
Expected Output 2
“” (Empty String)
Bonus Question (+20 points)
interestingMerge(l2,l3)
Write a function interestingMerge(l2,l3) that accepts two lists and return a list of tuples. The
function finds the sorted listed of all pairs of numbers. Each pair should satisfy the following conditions;
The first element of the pair comes from the first list and the second element of the pair comes from the
second list. Additionally, first element of the pair divides the second element evenly.
Sample Input
l2 = [1,2,3,4,5,6]
l3 = [12,15,24,66,80]
Expected Output
[(1, 12), (1, 15), (1, 24), (1, 66), (1, 80), (2, 12), (2, 24), (2, 66),
(2, 80), (3, 12), (3, 15), (3, 24), (3, 66), (4, 12), (4, 24), (4, 80),
(5, 15), (5, 80), (6, 12), (6, 24), (6, 66)]
Do you need a similar assignment written for you from scratch? We have qualified writers to help you.
You can rest assured of an A+ quality paper that is plagiarism free. Order now for a FREE first Assignment!
Use Discount Code "FREE" for a 100% Discount!
NB: We do not resell papers. Upon ordering, we write an original paper exclusively for you.