Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions 1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ### 1.
# 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]
# ``

def shift(lis, num):
"""
we are going to slice the list , depending on the number n :
if n =2 , we are going to slice the last two element ,
put them in a 'slice list', and lastly add the last 3 elements of the original list to them

"""
slice_list=[]
if num==0 or num==len(lis) :
return lis
elif num==-1* len(lis) :
return sorted(lis, reverse=True)
elif num> len (lis):
return lis
elif num > 0 :
slice_list=lis [-1*num:]

return slice_list + lis[:len(lis)-num]
elif num < 0 :
slice_list=lis [:num-1]

return lis[len(lis)+num-1 :] + slice_list

print (shift( [1, 2, 3, 4, 5] , 2) )


25 changes: 25 additions & 0 deletions 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

# ### 2.
# 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)]
# ```
def count_dect(string):
"""this function takes a string an return a dictionary for every letter and its frequancy"""
string=string.lower()

dict={}
for letter in 'abcdefghijklmnopqrstuvwxyz':
if string.count(letter) !=0 :
dict[letter]= string.count(letter)
return dict

out=count_dect("This is a sample text with several words This is more sample text with some different words")
print (out)
30 changes: 30 additions & 0 deletions 3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

# ### 3.
# 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']
# ```
def two_words(w1, w2):
w1=set(w1)
w2=set(w2)
lis=[]

lis.append ("".join (sorted (w1& w2 )) )
lis.append ("".join (sorted (w1- w2 )) )
lis.append ("".join (sorted (w2-w1 )) )

return lis
print (two_words("sharp","soap") )
49 changes: 49 additions & 0 deletions 4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

# ### 4.
# 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 nearst_pali_prime_num(*num):

def is_prime ( num):
if num%2==0 and num!=2 :
return False
for i in range (3,num,2):
if num%i==0 :
return False
return True


def is_palindromical(num):
string=str(num)
str_list=list(string)
str_list_rev= str_list.copy()
str_list_rev.reverse()
if str_list ==str_list_rev:
return True
return False

multiply = 1
for i in num:
multiply*=i

#now going backwards from our 'multiply' , checking every number if it is prim ? if yes > checking if it's palindromical
#if yes, returning it
print (multiply)
if multiply%2==0 : #if it is even : reducing it to the nearst odd, going 2 steps backwards (for better efficiency)
multiply-=1
for i in range ( multiply, 0, -2): #we made sure we are going to start with an odd num> thus we can jump 2 in every step
if is_prime(i) :
if is_palindromical(i) :
return i


print( nearst_pali_prime_num(11))