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
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...
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...
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
In Python: This will require you to write several functions, and then use them in a...
In Python: This will require you to write several functions, and then use them in a program. Logical Calculator The logical calculator does math, but with logical operators. In logic, we represent a bit with 0 as false and a bit with 1 as true. The logical operators are NOT, AND and OR. Bitwise logical calculations operate on each bit of the input. The NOT operator works on just one three-bit argument. NOT 011 = 100 The AND operator works...
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
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1,...
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1, and returns the number of numeric characters (‘0’ .. ‘9’) + in the string. Helpful clues:   The string class has a function called length() that can be accessed by str1.length().   To access an individual character in the string; use string indexing (str1[i]) where I is an integer. Indexing of a string array starts at 0.
Write a function called char_counter that counts the number of a certain character in a text...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
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...
Write a Python function first_chars that takes one parameter, which is a nested list of strings....
Write a Python function first_chars that takes one parameter, which is a nested list of strings. It returns a string containing the first character of all of the non-empty strings (at whatever depth they occur in the nested list) concatenated together. Here are a couple of examples of how the function should behave when you're done: >>> first_chars(['Boo', 'is', 'happy', 'today']) 'Biht' >>>first_chars(['boo', 'was', ['sleeping', 'deeply'], 'that', 'night', ['as', ['usual']]]) 'bwsdtnau'
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT