Problem 1: Write a Python function called max_of_three to find the maximum value between three numbers. Do not use the max() function.
Hint: in my solution, I found it easiest to create a different function called max_of_two and use that function to base my max_of_three off of.
def max_of_two( x, y ):
if x > y:
return x
return y
def max_of_three( x, y, z ):
return max_of_two( x, max_of_two( y, z ) )
print(max_of_three(3, 6, -5))
Problem 2: Write a Python function called multiply to multiply all of the numbers in a list together and return that value. The function should be created in such a way that it will work for both lists, tuples, and sets.
def multiply(numbers):
total = 1
for x in numbers:
total *= x
return total
testList = [8, 2, 3, -1, 7]
print(multiply(testList))
Problem 3: Write a Python function called caseCounter that counts the number of lowercase and uppercase characters in a string. If you're up to it, store the lowercase and uppercase variables in a dictionary within the function. The function should print two sentences that state the number of uppercase and lowercase characters.
def caseCounter(s):
d={"UPPER_CASE":0, "LOWER_CASE":0}
for c in s:
if c.isupper():
d["UPPER_CASE"]+=1
elif c.islower():
d["LOWER_CASE"]+=1
else:
pass
print ("No. of uppercase characters : ", d["UPPER_CASE"])
print ("No. of lowercase Characters : ", d["LOWER_CASE"])
print(caseCounter('Nicholas McCullum'))
Problem 4: Write a Python function called duplicateEliminator that takes in a list, eliminates any duplicate values, and returns a set.
Hint: This is probably easier than you think.
def duplicateEliminator(l):
return set(l)
print(duplicateEliminator([1,1,2,2]))
Problem 5: Write a Python function called squareMachine that takes in a list and returns another list whose elements are the values of the first list squared.
def squareMachine(theList):
i = 0
while i < len(theList):
theList[i] = theList[i]**2
i += 1
return theList
print(squareMachine([1,2,3,4,5]))
Problem 6: Write a Python function called dictDecomposer that takes in a dictionary and prints two lines of text:
An example output from the correct solution is below:
The keys of the dictionary are ['firstName', 'lastName']
The values of the dictionary are ['Nick', 'McCullum']
def dictDecomposer(theDict):
print(f"The keys of the dictionary are {list(theDict.keys())}")
print(f"The values of the dictionary are {list(theDict.values())}")
testDictionary = {
'firstName': 'Nick',
'lastName': 'McCullum'
}
dictDecomposer(testDictionary)
49 videos|38 docs|18 tests
|
|
Explore Courses for Software Development exam
|