Question

USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take...

USING PYTHON!

The second function you will write should be called ‘varOrd’. Your function should take two (2) arguments, an integer and a string. The function should return one (1) integer calculated as follows. If the input integer is 1, the function should return the sum of the return value of the ord() function on each character of the string (e.g., varOrd(1, ‘cat’) should return the result of ord(‘c’) + ord(‘a’) + ord(‘t’)). If the input integer is 2, the function should first sum the return value of the ord() function on each character of the string and second return that value modulus 3 (e.g., varOrd(2, ‘cat’) should return the result of (ord(‘c’) + ord(‘a’) + ord(‘t’)) mod 3). If the value of the input integer is 3, the function should return the factorial of the length of the string (e.g., varOrd(3, ‘cat’) should return the result of 3!). If the value of the input integer is anything else, the function should return -1 (e.g., varOrd(6, ‘cat’) should return -1)..

Testing: Create your own test cases.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def varOrd(count,text):
total = 0
if count == 1:
for t in text:
total = total + ord(t)
return total
elif count == 2:
for t in text:
total = total + ord(t)
return total % 3
elif count ==3:
length = len(text)
fact = 1;
for i in range(1,length+1):
fact = fact * i
return fact
else:
return -1

output = varOrd(1,'Car')
print(output)
output = varOrd(2,'Car')
print(output)
output = varOrd(3,'Car')
print(output)
output = varOrd(4,'Car')
print(output)
output = varOrd(3,'Vinoth')
print(output)
output = varOrd(2,'Football')
print(output)
  

Output:

278
2
6
-1
720
0

Add a comment
Know the answer?
Add Answer to:
USING PYTHON! The second function you will write should be called ‘varOrd’. Your function should take...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON The first function you will write should be called ‘countDown’. Your function should take zero...

    PYTHON The first function you will write should be called ‘countDown’. Your function should take zero (0) arguments. The function should return one (1) list containing integers from 10 to 1 and finally, the string “Liftoff!”. (i.e., [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ‘Liftoff!’]). You function must be recursive (i.e., it should contain a call to itself within the function body). You will get NO credit if you use any loops (for, while, etc). 3. Function...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

  • #Write a function called check_formula. The check_formula #function should take as input one parameter, a string....

    #Write a function called check_formula. The check_formula #function should take as input one parameter, a string. It #should return True if the string holds a correctly #formatted arithmetic integer formula according to the rules #below, or False if it does not. # #For this problem, here are the rules that define a #correctly-formatted arithmetic string: # # - The only characters in the string should be digits or the five arithmetic operators: +, -, *, /, and =. Any other...

  • Python The Python "<" and ">" comparison operators can be used to compare which string variable...

    Python The Python "<" and ">" comparison operators can be used to compare which string variable has a greater value based on comparing the ASCII codes of the characters in each string, one by one. To take some examples: "tets" > "test" returns True because the third letter of the first string, "t", has a greater value than the third letter of the second string, "s". "testa" > "test" returns True because—while the first four letters in both words are...

  • Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a...

    Please solve the program in C++(Recursive) Write a function called factorialIterative(). This function should take a single BIG integer (bigint) as its parameter n, and it will compute the factorial n! of the value and return it as its result (as a bigint). Write this functions using a loop (do not use recursion). 2. Write the factorial function again, but using recursion. Call this function factorialRecursive(). The function takes the same parameters and returns the same result as described in...

  • Python code define a function car that takes 'swift' as a parameter function should count how...

    Python code define a function car that takes 'swift' as a parameter function should count how many letters in the string function should return a dictionary where key is lower case alpha caracter and value is the number of times that character appears in sentence. ex- input- that big car is so expensive output- 't':1, 'h':1, 'a':2, 'b':1, 'i':3,......

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Python 3 code Write a function named coinflip that accepts an input called flips. The function...

    Python 3 code Write a function named coinflip that accepts an input called flips. The function should sim-ulate flipping a coin flips times. The function should return True if the coin was the same result for every flip. You can assume the function receives a positive integer as input. Write a second function named simulation that runs trials of the coin flip experiment. simulation should accept two parameters: the number of trials, and the number of coin flips to do...

  • Using Python: write a function that takes two arguments: (1) a target item to find, and...

    Using Python: write a function that takes two arguments: (1) a target item to find, and (2) a list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT