From 73fb7b881d13ad6037dafab175110cc944cfebc4 Mon Sep 17 00:00:00 2001 From: shatha6 Date: Mon, 3 Oct 2022 21:57:20 +0200 Subject: [PATCH] all 4Q done --- Shatha_W2_Q1.py | 29 ++++++++++++++++++++ Shatha_W2_Q2.py | 19 ++++++++++++++ Shatha_W2_Q3_v2 | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ Shatha_W2_Q4.py | 34 ++++++++++++++++++++++++ 4 files changed, 152 insertions(+) create mode 100644 Shatha_W2_Q1.py create mode 100644 Shatha_W2_Q2.py create mode 100644 Shatha_W2_Q3_v2 create mode 100644 Shatha_W2_Q4.py diff --git a/Shatha_W2_Q1.py b/Shatha_W2_Q1.py new file mode 100644 index 0000000..1652b98 --- /dev/null +++ b/Shatha_W2_Q1.py @@ -0,0 +1,29 @@ +"""Write a program that takes two inputs; one of them is a list and the other is a number, and returns the list obtained by shifting the elements in the list n places to the right (left) when n is positive (negative). Use wrap-around: if an element is shifted beyond the end of the list, then continue to shift starting at the beginning of the list. + +``` +Example +Inputs>>> [1, 2, 3, 4, 5], 2 +Output>>> [4, 5, 1, 2, 3] +Inputs>>> [1, 2, 3, 4, 5], -2 +Output>>> [3, 4, 5, 1, 2] +```""" +lst = [] +lst2 = [] +num = int(input('Enter number of elements')) +print('Enter the elements') +for i in range(0,num): + ele = int(input()) + lst.append(ele) # adding the element + +shift = int(input('Enter the number for shifting elements')) + +print(lst) + +for i in range(0,num): + j = i+shift + if j < num: + lst2.insert(j,lst[i]) + else: + lst2.insert(j-num,lst[i]) + +print(lst2) \ No newline at end of file diff --git a/Shatha_W2_Q2.py b/Shatha_W2_Q2.py new file mode 100644 index 0000000..2bf15a1 --- /dev/null +++ b/Shatha_W2_Q2.py @@ -0,0 +1,19 @@ +""" +Write a code snippet that inputs a sentence from the user, then uses a dictionary to summarize the number of occurrences of each letter. Ignore case, ignore blanks and assume the user does not enter any punctuation. Display a two-column table of the letters and their counts with the letters in sorted order. + +``` +Example +Input >>> "This is a sample text with several words This is more sample text with some different words" +Output >>> +[('a', 4), ('d', 3), ('e', 10), ('f', 2), ('h', 4), ('i', 7), ('l', 3), ('m', 4), ('n', 1), ('o', 4), ('p', 2), ('r', 5), ('s', 10), ('t', 9), ('v', 1), ('w', 4), ('x', 2)] +```""" +occ = {} +str = input('Enter a sentence to count the occurrences of each letter ') +str1 = str.replace(" ", "") +str2 = sorted(str1) +#print (str2) +for i in str2: + #print(str2.count(i)) + occ[i] = str2.count(i) +#x = list(occ) +print(occ) \ No newline at end of file diff --git a/Shatha_W2_Q3_v2 b/Shatha_W2_Q3_v2 new file mode 100644 index 0000000..ffcd621 --- /dev/null +++ b/Shatha_W2_Q3_v2 @@ -0,0 +1,70 @@ +""" +Write a program that takes in two words as input and returns a list containing three elements, in the following order: +- Shared letters between two words. + +- Letters unique to word 1. + +- Letters unique to word 2. + +Each element of the output should have unique letters, and should be alphabetically sorted. Use set operations. The strings will always be in lowercase and will not contain any punctuation. + +``` +Example +Input1>>> "sharp" +Input2>>> "soap" +Output>>> ['aps', 'hr', 'o'] +``` +""" +import functools +#str = "" +str_sort = "" +global final_lst +final_lst = [] +global lst +lst = [] +st1 = {} +st2 = {} +st3 = {} +st4 = {} +st5 = {} +st6 = {} + +def lst_insertion (st6): + import functools + lst = [] + str ="" + lst = list(st6) + str = functools.reduce(lambda x,y : x+y, lst) #converts list to string + # str_sort = sorted(str) + + str_sort = ''.join(sorted(str)) + + final_lst.append(str_sort) + #################################################### + + +str1 = input("Enter first word ") +str2 = input("Enter second word ") +st1 = set(str1) +st2 = set(str2) + +st6 = st1&st2 +lst = list(st6) +lst_insertion(lst) + +st3 =(st1^st2) + +st5 = st3 - st2 +lst = list(st5) +lst_insertion(lst) + + +st4 = st3 - st1 +st5 = st3 - st2 +lst = list(st4) +lst_insertion (lst) + + +print(final_lst) + +####################################### diff --git a/Shatha_W2_Q4.py b/Shatha_W2_Q4.py new file mode 100644 index 0000000..ee7ba53 --- /dev/null +++ b/Shatha_W2_Q4.py @@ -0,0 +1,34 @@ + +#A palindromical prime number is a prime number that reads the same when reversed. +#Write a function which returns the nearest palindromical prime number less than the multiplication of all the arguments. + + +#Example +#Input1>>> some_function(2, 3, 4) +#Output1>>> the nearest palindromical prime number less than 24 +#Input2>>> some_function(31, 77, 99) +#Output2>>> the nearest palindromical prime number less than 236,313 + + +def plnd_prm (n1,n2,n3): + x=0 + mult = n1*n2*n3 + for i in range(mult,1,-1): + x=0 + for j in range (i-1,1,-1): + if i%j == 0: + x= 1 + break + if x == 0: + num_str = str(i) + new = num_str[::-1] + if num_str == new: + print(i) + break + + +num1 = int(input("Enter first number ")) +num2 = int(input("Enter second number ")) +num3 = int(input("Enter third number ")) + +plnd_prm(num1,num2,num3) \ No newline at end of file