8) Write Python code for a function called occurances that takes two arguments str1 and str2, assumed to be strings, and returns a dictionary where the keys are the characters in str2. For each character key, the value associated with that key must be either the string ‘‘none’’, ‘‘once’’, or ‘‘more than once’’, depending on how many times that character occurs in str1. In other words, the function roughly keeps track of how many times each character in str1 occurs in str2. You might want to make use of the .count() method for strings.
9)Write Python code for a function called my func that takes two arguments A and B as inputs and returns the smallest positive integer M such that M is strictly bigger than A and M is not a multiple of any positive integer less than or equal to B (except 1). You may assume that A and B are positive integers greater than 1.
8)
Python code:
def occurances(str1,str2):#function occurances which accepts 2 string str1 and str2
dic={}#initializing dictionary
for i in str1:#loopin through each character in str1
if(str2.count(i)==1):#checking if the count of character is equal to 1
dic[i]="once"#assigning value once
elif(str2.count(i)>1):#checking if the count of character is more than 1
dic[i]="more than once"#assigning value more than once
else:#checking if the count of character is less than 1
dic[i]="none"#assigning value none
return(dic)#returning dictionary
print(occurances("lilly","hello world in "))#printing and calling function occurances
Screenshot of code:
Output screenshot:
9)
Python code:
def my_func(A,B):#function my_func with A and B as inputs
for i in range(A+1,B+1):#loop from A+1 to B
flag=0#setting flag to 0 to exit function when the number is obtained
for j in range(2,i//2+1):#loop for identifying multiple of any integer less than or equal to B
if(i%j==0):#checking for multiple
flag=1#setting flag
if(flag==0):#checking flag to identify if the number is found
return(i)#returning the number
print(my_func(5,10))#calling the function and printing it
Screenshot of code:
Output screenshot:
Get Answers For Free
Most questions answered within 1 hours.