1 filea month ago
2

13WPs

gửi cho Tạ Đạt

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
import math import string #WP1 def hello(name): return 'Hello '+ name+'!' urname=input("What is your name?") print(hello(urname)) #Wp2 import math def calculate_hypotenuse(a,b): return math.sqrt(a*a+b*b) calculate_hypotenuse(3,4) #Wp3 def are_all_condition_true(conditions): for i in conditions: if(i==False): return False return True iconditions={True,True,True} print(are_all_condition_true(iconditions)) #WP4 def is_a_condition_true(conditions): for i in conditions: if(i): return True return False iconditions={True, False, False} print(is_a_condition_true(iconditions)) #WP5 def filter_integers_greater_than(list,n): return [i for i in list if i>n] filter_integers_greater_than([0, 3, 5, -2, 9, 8],4) #WP6 def find_cheapest_hotels(hotel_daily_rates, maximum_daily_rate): return [i for i in hotel_daily_rates if i[1]<=maximum_daily_rate] hotel_daily_rates = [('Majestic Saigon Hotel', 93), ('Hotel Grand Saigon', 120), ('Sofitel Saigon Plaza', 123), ('Hotel Continental', 62), ('Caravelle Hotel', 180), ('Sheraton Saigon Hotel', 216), ('Park Hyatt Saigon', 209)] find_cheapest_hotels(hotel_daily_rates, 85) #WP7 def calculate_euclidean_distance_between_2_points(p1,p2): return math.sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2) calculate_euclidean_distance_between_2_points((0, 0), (3, 4)) #WP8 from WP7 import calculate_euclidean_distance_between_2_points def calculate_euclidean_distance_between_points(points): if(len(points)>=2): return sum([calculate_euclidean_distance_between_2_points(points[i],points[i+1]) for i in range(0,len(points)-1)]) else: raise ValueError('The list MUST contain at least 2 points') calculate_euclidean_distance_between_points([(0, 0), (3, 4), (-1, -1)]) calculate_euclidean_distance_between_points([(1, 1)]) calculate_euclidean_distance_between_points([]) #WP9 def haveDuplicateSpace(s): for i in range(0,len(s)): if(s[i]==' 'and s[i+1]==' '): return True return False def capitalize_words(s): if(type(s)!=str): raise TypeError('Not a string') else: while(haveDuplicateSpace(s)): s=s.replace(" "," ") s=s.capitalize() for i in range(0,len(s)): if(s[i]==' '): tmp="" for j in range(0,i+1): tmp+=s[j] tmp+=s[i+1].upper() for j in range(j+2,len(s)): tmp+=s[j] s=tmp return s print(capitalize_words('hello, word!')) capitalize_words('JACK CARVER') capitalize_words(69) #WP10 def uppercase_lowercase_words(istring): if(istring==None): return None if(type(istring)!=str): raise TypeError('Not a string') else: ilist=istring.split() return_string='' flag=True for i in ilist: if(flag==True): return_string=return_string + i.upper()+' ' else: return_string=return_string + i.lower()+' ' flag=not(flag) return_string=return_string[:-1] #bo di cai dau cach cuoi cung return return_string uppercase_lowercase_words(69) print(uppercase_lowercase_words(None)) uppercase_lowercase_words('1 one two 2 3 three four 4 five six') #WP11 def factorial(n): if(type(n)!=int): raise TypeError('Not an integer') elif(n<0): raise ValueError('Not a positive integer') else: if(n==0): return 1 else: return factorial(n-1)*n [(n, factorial(n)) for n in range(6)] #WP12 def char_to_int(c): if(type(c)!=str): raise TypeError('Not a string') if(len(c)>1 or c.isdigit()==False): raise ValueError('Not a single digit') else: return ord(c)-48 print(char_to_int('9')) #WP13 def full_digit(s): for i in s: if(i.isdigit()==False): return False return True def string_to_int(istring): if(type(istring)!=str): raise TypeError('Not a string') elif(not full_digit(istring)): raise ValueError('Not a positive integer string expression') else: return [char_to_int(i) for i in istring] string_to_int('17049171') #WP14