Path: blob/master/03-Methods and Functions/03-Function Practice Exercises.ipynb
666 views
Function Practice Exercises
Problems are arranged in increasing difficulty:
Warmup - these can be solved using basic comparisons and methods
Level 1 - these may involve if/then conditional statements and simple methods
Level 2 - these may require iterating over sequences, usually with some kind of loop
Challenging - these will take some creativity to solve
WARMUP SECTION:
LESSER OF TWO EVENS: Write a function that returns the lesser of two given numbers if both numbers are even, but returns the greater if one or both numbers are odd
ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter
MAKES TWENTY: Given two integers, return True if the sum of the integers is 20 or if one of the integers is 20. If not, return False
LEVEL 1 PROBLEMS
OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name
Note: 'macdonald'.capitalize()
returns 'Macdonald'
MASTER YODA: Given a sentence, return a sentence with the words reversed
Note: The .join() method may be useful here. The .join() method allows you to join together strings in a list with some connector string. For example, some uses of the .join() method:
This means if you had a list of words you wanted to turn back into a sentence, you could just join them with a single space string:
ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200
NOTE: abs(num)
returns the absolute value of a number
LEVEL 2 PROBLEMS
FIND 33:
Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.
PAPER DOLL: Given a string, return a string where for every character in the original there are three characters
BLACKJACK: Given three integers between 1 and 11, if their sum is less than or equal to 21, return their sum. If their sum exceeds 21 and there's an eleven, reduce the total sum by 10. Finally, if the sum (even after adjustment) exceeds 21, return 'BUST'
SUMMER OF '69: Return the sum of the numbers in the array, except ignore sections of numbers starting with a 6 and extending to the next 9 (every 6 will be followed by at least one 9). Return 0 for no numbers.
CHALLENGING PROBLEMS
SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order
COUNT PRIMES: Write a function that returns the number of prime numbers that exist up to and including a given number
By convention, 0 and 1 are not prime.
Just for fun:
PRINT BIG: Write a function that takes in a single letter, and returns a 5x5 representation of that letter
HINT: Consider making a dictionary of possible patterns, and mapping the alphabet to specific 5-line combinations of patterns.
For purposes of this exercise, it's ok if your dictionary stops at "E".