Question

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.

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:

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 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 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 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 `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, 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 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 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...

Function Example: Write a Python function that receives two integer
arguments and writes out their sum and their product. Assume no
global variables.
def writer(n1, n2):
sum = n1 + n2
product = n1*n2
print("For the numbers", n1, "and", n2)
print("the sum is", sum)
print("and the product is", product)
...
1) Create a PYHW2 document that will contain your algorithms in
flowchart and pseudocode form along with your screen shots of the
running program.
2) Create the algorithm in both...

Write a function
called TaylorSin.m that takes as input an array x, and positive
integer N, and returns the Nth Taylor polynomial approximation of
sin(x), centered at a = 0. The first line of your code should
read
function s =
TaylorSin(x,N)
HINT: in computing k!,
use kfact = k*(k-1)*kfact since you are counting by 2

ADVERTISEMENT

Get Answers For Free

Most questions answered within 1 hours.

ADVERTISEMENT

asked 6 minutes ago

asked 30 minutes ago

asked 42 minutes ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago

asked 1 hour ago