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
def number_compare(x,y):
if x%2 == 0 and y%2 ==0:
if x >= y:
return y
else:
return x
if x%2 != 0 or y%2 !=0:
if x >= y:
return x
else:
return y
number_compare(2,4)
number_compare(2,5)
number_compare(3,5)
ANIMAL CRACKERS: Write a function takes a two-word string and returns True if both words begin with same letter
animal_crackers('Levelheaded Llama') --> True
animal_crackers('Crazy Kangaroo') --> False
def letter_first_compare(x,y):
t=list(x.strip())
c=list(y.strip())
print(t[0])
if t[0] ==c[0]:
return True
if t[0] != c[0]:
return False
letter_first_compare('csv','dds')
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
makes_twenty(20,10) --> True
makes_twenty(12,8) --> True
makes_twenty(2,3) --> False
def number_sum_20(x,y):
if int(x)+int(y) == 20:
return True
if int(x) == 20 or int(y) == 20:
return True
else:
return False
OLD MACDONALD: Write a function that capitalizes the first and fourth letters of a name
old_macdonald('macdonald') --> MacDonald
def string_upper_1_4(x):
if len(x) >=4:
t=list(x.strip())
t[0]=t[0].upper()
t[3]=t[3].upper()
x="".join(t)
return x
else:
print("wrong not num")
string_upper_1_4('sxsdfe')
string_upper_1_4('sd')
MASTER YODA: Given a sentence, return a sentence with the words reversed
master_yoda('I am home') --> 'home am I'
master_yoda('We are ready') --> 'ready are We'
def word_reverse(x):
t=list(x.strip())
t=list(reversed(t))
x="".join(t)
return x
word_reverse('sdfsd')
ALMOST THERE: Given an integer n, return True if n is within 10 of either 100 or 200
almost_there(90) --> True
almost_there(104) --> True
almost_there(150) --> False
almost_there(209) --> True
def num_judge_three_conditions(x):
if x <= 10:
return True
elif x == 100 or x == 200:
return True
else:
return False
num_judge_three_conditions(100)
FIND 33:
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 num_check_3(x):
if isinstance(x,list):
if 3 not in x:
return False
if 3 in x:
use_dic=pd.Series(x,index=list(range(len(x))))
use_dic=use_dic[use_dic.values==3]
list_index_tem = use_dic.index
print(len(list_index_tem))
if len(list_index_tem) <= 1:
return False
if len(list_index_tem) > 1:
list_use=list(itertools.permutations(list_index_tem,2))
for x1 in list_use:
if abs((x1[0]-x1[1])) == 1:
return True
elif abs((x1[0] - x1[1])) != 1:
return False
num_check_3([1,2,3,3,1,5645,3])
num_check_3([1,2,3,2,])
num_check_3([3,3,2])
num_check_3([1,2,2,1,3])
num_check_3([1,2,332,1221,1])
PAPER DOLL: Given a string, return a string where for every character in the original there are three characters
paper_doll('Hello') --> 'HHHeeellllllooo'
paper_doll('Mississippi') --> 'MMMiiissssssiiippppppiii'
def word_plus_3(x):
list1=[]
for t in x.strip():
t2=t*3
list1.append(t2)
x="".join(list1)
return x
word_plus_3('sdfsf')
word_plus_3('AAAD')
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'
blackjack(5,6,7) --> 18
blackjack(9,9,9) --> 'BUST'
blackjack(9,9,11) --> 19
def four_decision_num(x,y,z):
if isinstance(x,int):
if isinstance(y,int):
if isinstance(z,int):
if 0<x<12 and 0<y<12 and 0<z<12:
score=x+y+z
if score <21:
return score
elif score>= 21:
if x==11 or y==11 or z==11:
score=score-10
return score
else:
print("Bust")
else:
return False
else:
return False
else:
return False
else:
return False
four_decision_num(12,1,2)
four_decision_num(1.5,2,2)
four_decision_num(1,10,11)
four_decision_num(10,10,10)
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.
summer_69([1, 3, 5]) --> 9
summer_69([4, 5, 6, 7, 8, 9]) --> 9
summer_69([2, 1, 6, 9, 11]) --> 14
def sum_6_9(x):
if isinstance(x,list):
if 6 in x:
if 9 in x:
i=0
while i <= (len(x) - 1):
if x[i] !=6:
i =i+1
if x[i] ==6:
i1=i
i=i+1
while i <= (len(x) - 1):
if x[i] !=9:
i = i+1
if x[i] == 9:
i2=i+1
x=x[:i1]+x[i2:]
sum1=sum(x)
print(sum1)
else:
i=0
while i <=(len(x)-1):
if x[i]!=6:
i = i+1
if x[i] ==6:
i1=i
x = x[:i1]
sum1=sum(x)
print(sum1)
else:
sum1=sum(x)
print(sum1)
else:
return False
sum_6_9([1,2,3])
sum_6_9([1,2,6,3,4,2])
sum_6_9([1,2,6,3,2,9,1])
sum_6_9([1,2,4,6,4,3,9,6])
SPY GAME: Write a function that takes in a list of integers and returns True if it contains 007 in order
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 num_007_judge(x):
if isinstance(x,list):
for t in x:
if t is not int(t):
return False
if isinstance(t,int):
if 0 not in x or 7 not in x:
return False
if 0 in x and 7 in x:
judge_list=[0,0,7]
li_list=[]
for i1,t in enumerate(x):
if t == judge_list[0]:
li_list.append(i1)
for t in (x):
if t== judge_list[2]:
i3 = x.index(t)
li_list=Series(li_list)
li_list=li_list[li_list.values<i3]
if len(li_list) >=2:
return True
else:
return False
num_007_judge([1])
num_007_judge([1,2,3,34])
num_007_judge([1,8,7,0,0])
num_007_judge([1,0,7,23,44.3])
num_007_judge([1,0,0,3,2,7,1])
num_007_judge([7,0,32,1])
num_007_judge([0,0,7])
COUNT PRIMES: Write a function that returns the number of prime numbers that exist up to and including a given number
count_primes(100) --> 25
By convention, 0 and 1 are not prime.
def max_num(x):
i_list=[]
for i in range(1,x+1):
if x%i == 0 and x/i !=1 and i !=1 and i%2 !=0:
i_list.append(i)
print(i_list[-1])
max_num(100)
max_num(99)
max_num(200)