USING PYTHON! The second function you will write should be called ‘varOrd’. Your
ID: 3714346 • Letter: U
Question
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.
Explanation / Answer
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.