Here are some more challenging practice problems that I did, using what I learnt earlier:
Additional Practices
- Create & use 2 functions to
a. find all the prime numbers < a given number, put them in a list& then
b. return the square of all those prime numbers, in a list'''First get a number from user''' number_input=int(input("Number: ")) '''Then check if a number is prime''' def prime_check(inputted_no): flag=0 for number in range(2,inputted_no): if inputted_no%number==0: flag=1 break return flag '''Then check if those prime numbers are < the number input by user''' '''Then square them if yes''' def squaredprimelist(input_number): the_list=[] for number in range(2,input_number): flag=prime_check(number) if flag==0: the_list.append(number**2) print(the_list) squaredprimelist(number_input)
- Iterate thru the numbers before the input number, and return a list of all prime numbers.
E.g. input = 10 ; output = [2, 3, 5, 7]'''First get a number from user''' number_input=int(input("Number: ")) '''Then check if a number is prime''' def prime_check(inputted_no): flag=0 for number in range(2,inputted_no): if inputted_no%number==0: flag=1 break return flag '''Then check if those prime numbers are < the number input by user''' '''Then append them to list if yes''' def primelist(input_number): the_list=[] for number in range(2,input_number): flag=prime_check(number) if flag==0: the_list.append(number) print(the_list) primelist(number_input)
- Print all the number factors of all numbers less than the input number
E.g. input = 5 ; output = [ [1], [1, 2], [1, 3], [1, 2, 4] ]'''First get a number from user''' number_input=int(input("Number: ")) '''Find factors of numbers''' '''don't forget to return the value at end of custom function''' def factors_list(inserted_number): list_of_factors=[] for number in range(1,inserted_number+1): if inserted_number%number==0: list_of_factors.append(number) return list_of_factors '''if the numbers < input number, then append their list of factors to list''' def longer_list(inputtedno): the_factors_list=[] for number in range(1,inputtedno): shorter_list=factors_list(number) the_factors_list.append(shorter_list) print(the_factors_list) longer_list(number_input)
- Find length of list without using len( ) function
a_list=[1,4,6,8] def length_list(the_list): count=0 for item in the_list: count+=1 return count listlength=length_list(a_list) print(listlength)
- Compare keys of a dictionary to item in another list, if exist in both, print the key’s value
for item in another list, if (item ==) key in dictionary, print the key’s value'''create a dictionary of countries & capitals''' d={} d['India']='New Delhi' d['Japan']='Tokyo' for key in d.keys(): print(key) '''compare to another list of countries, if country is in dictionary, print its capital.''' list_of_countries=['India', 'Singapore', 'Japan'] for country in list_of_countries: if country in d: print(f"Capital is: {d[country]}") else: d[country]='' print(d)
Thoughts
For more complex problems like these, I find it really helps when I write down the thought process of how to solve the problem in comments/ doc strings first, so that I can have an orderly step-by-step guide to know what code I need to write to generate the output. I definitely encountered some problems and had to debug along the way. However, it is also really satisfying when the code finally works well!