Problems are arranged in increasing difficulty:
lesser_of_two_evens(2,4) --> 2
lesser_of_two_evens(2,5) --> 5
def lesser_of_two_evens(a,b):
if a%2 == 0 and b%2 == 0:
return min(a,b)
else:
return max(a,b)
# Check
lesser_of_two_evens(2,4)
2
# Check
lesser_of_two_evens(2,5)
5
animal_crackers('Levelheaded Llama') --> True
animal_crackers('Crazy Kangaroo') --> False
def animal_crackers(text):
wordlist = text.split()
return wordlist[0][0] == wordlist[1][0]
# Check
animal_crackers('Levelheaded Llama')
True
# Check
animal_crackers('Crazy Kangaroo')
False
makes_twenty(20,10) --> True
makes_twenty(12,8) --> True
makes_twenty(2,3) --> False
def makes_twenty(n1,n2):
return (n1+n2)==20 or n1==20 or n2==20
# Check
makes_twenty(20,10)
True
# Check
makes_twenty(12,8)
True
#Check
makes_twenty(2,3)
False
old_macdonald('macdonald') --> MacDonald
Note: 'macdonald'.capitalize()
returns 'Macdonald'
def old_macdonald(name):
if len(name) > 3:
return name[:3].capitalize() + name[3:].capitalize()
else:
return 'Name is too short!'
# Check
old_macdonald('macdonald')
'MacDonald'
master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'
def master_yoda(text):
return ' '.join(text.split()[::-1])
# Check
master_yoda('I am home')
'home am I'
# Check
master_yoda('We are ready')
'ready are We'
almost_there(90) --> True
almost_there(104) --> True
almost_there(150) --> False
almost_there(209) --> True
NOTE: abs(num)
returns the absolute value of a number
def almost_there(n):
return ((abs(100 - n) <= 10) or (abs(200 - n) <= 10))
# Check
almost_there(90)
True
# Check
almost_there(104)
True
# Check
almost_there(150)
False
# Check
almost_there(209)
True
Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.
has_33([1, 3, 3]) → True
has_33([1, 3, 1, 3]) → False
has_33([3, 1, 3]) → False
def has_33(nums):
for i in range(0, len(nums)-1):
# nicer looking alternative in commented code
#if nums[i] == 3 and nums[i+1] == 3:
if nums[i:i+2] == [3,3]:
return True
return False
# Check
has_33([1, 3, 3])
True
# Check
has_33([1, 3, 1, 3])
False
# Check
has_33([3, 1, 3])
False
paper_doll('Hello') --> 'HHHeeellllllooo'
paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'
def paper_doll(text):
result = ''
for char in text:
result += char * 3
return result
# Check
paper_doll('Hello')
'HHHeeellllllooo'
# Check
paper_doll('Mississippi')
'MMMiiissssssiiissssssiiippppppiii'
blackjack(5,6,7) --> 18
blackjack(9,9,9) --> 'BUST'
blackjack(9,9,11) --> 19
def blackjack(a,b,c):
if sum((a,b,c)) <= 21:
return sum((a,b,c))
elif sum((a,b,c)) <=31 and 11 in (a,b,c):
return sum((a,b,c)) - 10
else:
return 'BUST'
# Check
blackjack(5,6,7)
18
# Check
blackjack(9,9,9)
'BUST'
# Check
blackjack(9,9,11)
19
summer_69([1, 3, 5]) --> 9
summer_69([4, 5, 6, 7, 8, 9]) --> 9
summer_69([2, 1, 6, 9, 11]) --> 14
def summer_69(arr):
total = 0
add = True
for num in arr:
while add:
if num != 6:
total += num
break
else:
add = False
while not add:
if num != 9:
break
else:
add = True
break
return total
# Check
summer_69([1, 3, 5])
9
# Check
summer_69([4, 5, 6, 7, 8, 9])
9
# Check
summer_69([2, 1, 6, 9, 11])
14
spy_game([1,2,4,0,0,7,5]) --> True
spy_game([1,0,2,4,0,5,7]) --> True
spy_game([1,7,2,0,4,5,0]) --> False
def spy_game(nums):
code = [0,0,7,'x']
for num in nums:
if num == code[0]:
code.pop(0) # code.remove(num) also works
return len(code) == 1
# Check
spy_game([1,2,4,0,0,7,5])
True
# Check
spy_game([1,0,2,4,0,5,7])
True
# Check
spy_game([1,7,2,0,4,5,0])
False
count_primes(100) --> 25
By convention, 0 and 1 are not prime.
def count_primes(num):
primes = [2]
x = 3
if num < 2: # for the case of num = 0 or 1
return 0
while x <= num:
for y in range(3,x,2): # test all odd factors up to x-1
if x%y == 0:
x += 2
break
else:
primes.append(x)
x += 2
print(primes)
return len(primes)
# Check
count_primes(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
25
BONUS: Here's a faster version that makes use of the prime numbers we're collecting as we go!
def count_primes2(num):
primes = [2]
x = 3
if num < 2:
return 0
while x <= num:
for y in primes: # use the primes list!
if x%y == 0:
x += 2
break
else:
primes.append(x)
x += 2
print(primes)
return len(primes)
count_primes2(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
25
print_big('a')
out: *
* *
*****
* *
* *
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".
def print_big(letter):
patterns = {1:' * ',2:' * * ',3:'* *',4:'*****',5:'**** ',6:' * ',7:' * ',8:'* * ',9:'* '}
alphabet = {'A':[1,2,4,3,3],'B':[5,3,5,3,5],'C':[4,9,9,9,4],'D':[5,3,3,3,5],'E':[4,9,4,9,4]}
for pattern in alphabet[letter.upper()]:
print(patterns[pattern])
print_big('a')
* * * ***** * * * *