diff --git a/question1.py b/question1.py new file mode 100644 index 0000000..876df4a --- /dev/null +++ b/question1.py @@ -0,0 +1,15 @@ +num = int(input("Enter the number of the elements of the list: ")) +k = int(input('Enter how many shifts you want? ' + '\nA positive number for right shifting and a negative number for left shifting: ')) + +my_list = (list(range(num))) +print(my_list) + +if k > 0: #right shift + for i in range(k): + my_list.insert(0,my_list.pop()) +elif k < 0: #left shift + for i in range(abs(k)): + my_list.append(my_list.pop(0)) + +print(my_list) \ No newline at end of file diff --git a/question2.py b/question2.py new file mode 100644 index 0000000..b0391a9 --- /dev/null +++ b/question2.py @@ -0,0 +1,14 @@ +alphabet = "abcdefghijklmnopqrstuvwxyz" + +sentence = input("Enter a sentence please: ").lower().strip() + +result = {} +for letter in sentence: + if letter in alphabet: + if letter in result.keys(): + result[letter] += 1 + else: + result[letter] = 1 + + +print([item for item in sorted(result.items())]) \ No newline at end of file diff --git a/question3.py b/question3.py new file mode 100644 index 0000000..0546f2d --- /dev/null +++ b/question3.py @@ -0,0 +1,13 @@ +import re + +first = input("Enter the first word: ").lower().strip() +first = set(re.sub(r'[^\w\s]', '', first).replace(" ", ""))#remove the punctuations and blanks from the first word +second = input("Enter the second word: ").lower().strip() +second = set(re.sub(r'[^\w\s]', '', second).replace(" ", "")) #remove the punctuations and blanks from the second word + +inters = first & second +f_diff = first - second +s_diff = second - first + +print([''.join(inters), ''.join(f_diff), ''.join(s_diff)]) + diff --git a/question4.py b/question4.py new file mode 100644 index 0000000..7ccbc2d --- /dev/null +++ b/question4.py @@ -0,0 +1,19 @@ +def palindrome_prime(a,b,c): + first = 1 + max_value = a * b * c + + for i in range(first,int(max_value)): + if (str(i) == str(i)[::-1]): #checks if it is palindromical + if(i>2): + for j in range(2,i): + is_prime = True + if (i%j==0): + is_prime = False + break + if is_prime: + max_prime = i + + print(f"the nearest palindromical prime number less than {max_value} is ",max_prime) + + +