EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Python for EmSAT Achieve  >  Functions: Practice Problems

Functions: Practice Problems | Python for EmSAT Achieve PDF Download

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:

  • One line stating the keys of the dictionary
  • One line stating the values of the dictionary

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)

The document Functions: Practice Problems | Python for EmSAT Achieve is a part of the EmSAT Achieve Course Python for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
Are you preparing for Exam? Then you should check out the best video lectures, notes, free mock test series, crash course and much more provided by EduRev. You also get your detailed analysis and report cards along with 24x7 doubt solving for you to excel in exam. So join EduRev now and revolutionise the way you learn!
Sign up for Free Download App for Free
49 videos|38 docs|18 tests

Up next

49 videos|38 docs|18 tests
Download as PDF

Up next

Related Searches

video lectures

,

Functions: Practice Problems | Python for EmSAT Achieve

,

Exam

,

practice quizzes

,

Sample Paper

,

Free

,

ppt

,

pdf

,

Extra Questions

,

Semester Notes

,

Important questions

,

Functions: Practice Problems | Python for EmSAT Achieve

,

past year papers

,

Viva Questions

,

study material

,

mock tests for examination

,

Objective type Questions

,

Summary

,

shortcuts and tricks

,

Previous Year Questions with Solutions

,

MCQs

,

Functions: Practice Problems | Python for EmSAT Achieve

;