Question

8) Write Python code for a function called occurances that takes two arguments str1 and str2,...

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.

Homework Answers

Answer #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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called count_word that takes a phrase and a word as arguments and returns...
Write a function called count_word that takes a phrase and a word as arguments and returns a count of the number of occurrences of that particular word in the phrase. The function should return an integer. Python Script in ATOM or REPL
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145' PYTHON
Hello! I am confused on the code for this question. Write a complete Python function called...
Hello! I am confused on the code for this question. Write a complete Python function called Frog, with two integer parameters M and N, where N has the default value of 5. The function computes and returns one of two results: if N is odd it returns the equation 2 * M + N, but if it is even it returns the equation 3 * M - N instead.
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
Please write the python code for a function called called count_all_words that takes a phrase as...
Please write the python code for a function called called count_all_words that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. (See example below.) Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
Write a template function maxn() that takes as its arguments an array of items of type...
Write a template function maxn() that takes as its arguments an array of items of type T and an integer representing the number of elements in the array and that returns the largest item in the array. The number of elements should take the default value of 10. The program should include a specialization that takes an array of strings as an argument and returns the longest string. (If there is a tie, the function should return the first one...
python pls Create function math_life() this function takes one or more argument. If this function called...
python pls Create function math_life() this function takes one or more argument. If this function called with no argument, raise typeError. The math_life() function returns to a function defined inside. this will take a one argument. For the second argument, it will calculate second function passed to math_life(). You should assume that the math_life() passed x functions, when it called the x times it will calculate the xth function passed to math_life() arguments when it called x+1 time it again...