Starting from:

$30

COL100 Assignment 5 -Solved


1. Every problem description is followed by some examples showing how exactly input and output is 
being expected. Please refer to them for more clarity. 
2. If you still have any more doubts, feel free to shoot them at Piazza. 
IMPORTANT NOTE: This assignment is based on functions we want you to make functions 
that take inputs as arguments and return the result. We will check your code by calling the functions 
created by you along with checking the output thus please make sure to make functions. The naming, 
arguments and return value are specified in each question. You can do input/output inside or 
outside the function as long as it returns the correct value as well as printing it. 
1 Calculato
This question uses functions to do the calculator questions done in the previous assignments. We had 
asked you to print the result of some mathematical operations in assignment 2 and 3. In this assignment 
you will be creating different functions for the same operations. We expect the following functions: 
• Addition 
• Subtraction 
• Multiplication 
• Division 
• Modulus 
The name of the function must be the exact same as the operation i.e. for addition we want a function 
named addition(a,b) where a and b are integers. Since each of these is a binary function they willtake in two integer inputs and return the result. The return type is an integer. 
Note: The function names must be in lowercase. 

 

2 Factorial Using Recursion 
Write a program to calculate the factorial of a number using recursion. You created a program that 
calculated the factorial of a number using loops and now you must create a function that recursively 
computes the factorial. Your function must be named factorial(a) where a is the integer argument 
whose factorial must be returned by the function. The return type is an integer. 
Example 1: 
INPUT: 
1 1 
OUTPUT: 
1 1.00 
EXPLANATION: 
The factorial of 1 is equal to 1 
Example 2: 
INPUT: 
1 5 
OUTPUT: 
1 120.00 
EXPLANATION: 
The factorial of 5 is equal to 120 
Example 3: 
INPUT: 
1 10 
OUTPUT: 
1 3628800.00 
EXPLANATION: 
The factorial of 10 is equal to 3628800 
Note: We will be checking your code for recursive functions thus calculating factorial 
using loops will be given a 0. 

 

3 Padovan Sequence Using Recursion 
Padovan sequence is similar to the Fibonacci sequence. In Padovan sequence P(n) = P(n−2)+P(n−3) 
where p(0), p(1), p(2) = 1 unlike Fibonacci sequence in which f(n) = f(n−1)+f(n−2) where f(0) = 0, 
f(1) = 1. We will calculate the n th number of the padovan sequence using recursion. You must create 
a function that recursively computes the n th number of the padovan sequence. Your function must be 
named padovan(a) and will have one integer argument called a and you will return the a th integer of 
2the padovan sequence. The return type and argument is an integer. 
Example 1: 
INPUT: 
1 1 
OUTPUT: 
1 1.00 
EXPLANATION: 
The padovan of 1 is equal to 1 
Example 2: 
INPUT: 
1 5 
OUTPUT: 
1 3.00 
EXPLANATION: 
The padovan of 5 is equal to p(5)=p(3)+p(2) where p(3)=p(1)+p(0)=2 and p(2)=1 
Example 3: 
INPUT: 
1 10 
OUTPUT: 
1 12.00 
EXPLANATION: 
The padovan of 10 is p(10)=p(8)+p(7) where p(8)=7 and p(7)=5 
Note: We will be checking your code for recursive functions thus calculating padovan 
using loops will be given a 0. 

 

4 Number of words in a sentence

 to count the number of words in each sentence and 
print the number of words in the sentence which has minimum words from the given sentences. You 
have to create a function called numwords() that takes an array of strings(sentences) and returns the 
value of the number of words in the string(sentence) with minimum words. The first line of input will 
be the number of sentences given. Two words may be separated by any number of commas, spaces 
or period and a word contains any number (one or more than one) of alphabets i.e. a-z and A-Z. See 
examples for more clarification. 
Note: 
The quotes are not part of the input and are just there to identify strings. For exam
ple they are needed in Example 2 to show spaces, we will not give quotes in actual input. Sentences 
may or may not be grammatically correct and the words may or may not have a meaning and will 
contain one or more than one letters. 
Example 1: 
INPUT: 
31 4 
2 "there is a buffalo" 
3 "buffalo is male gender" 
4 "cow is female animal" 
5 "buffalo and cow are of same family" 
OUTPUT: 
1 4.00 
EXPLANATION: "there is a buffalo", "buffalo is male gender","cow is female animal" are the sentences 
with minimum four words, "buffalo and cow are of same family" have 7 words. 
Example 2: 
INPUT: 
1 3 
2 "This is computer science programming assignment" 
3 "" 
4 " " 
OUTPUT: 
1 0.00 
EXPLANATION: There are no words in "" and " ", and 6 words in "This is computer science programming 
assignment" 
Example 3: 
INPUT: 
1 4 
2 "New Assignment of COL" 
3 "The assignment is harder," 
4 "than the previous ones" 
5 "Id disagree" 
OUTPUT: 
1 2.00 
EXPLANATION: "Id disagree" has two words - Id and disagree and is thus the sentence of the minimum 
words. 
Example 4: 
INPUT: 
1 2 
2 "A,a,a 
A" 
3 "b 
b" 
OUTPUT: 
1 2.00 
EXPLANATION: The first sentence has 4 words and the second sentence has 2. 
Note: You have to return the number of words in the smallest sentence and not the smallest sentence. 

45 Figure out the pattern
Write a recursive program to print the pattern shown for any positive number N. Your task is to figure 
out how the pattern can be printed using recursion. You cannot use loops to print this pattern, any 
student found using loops will be given a 0. The name of the function created must be pattern(a) 
where a is the integer input for the function. The function returns the pattern as a string which has to 
be printed to the terminal. As in previous questions you may print it outside the function or inside. 
NOTE: You DO NOT have to print to 2 decimal places here. 
Example 1: 
INPUT: 
1 1 
OUTPUT: 
1 111 
EXPLANATION: The base case for the pattern is 111 for the number 1. 
Example 2: 
INPUT: 
1 2 
OUTPUT: 
1 211121112 
EXPLANATION: Notice the repetition in the pattern - the pattern can be looked as 2, 111, 2, 111, 2. 
Example 3: 
INPUT: 
1 3 
OUTPUT: 
1 321112111232111211123 
EXPLANATION: If you notice the pattern the pattern is 3, 211121112, 3, 211121112, 3 
Example 4: 
INPUT: 
1 4 
OUTPUT: 
1 432111211123211121112343211121112321112111234 
EXPLANATION: If you notice the pattern the pattern is 4 then 321112111232111211123 then 321112111232111211123 
then 4. 
Hint: What does the pattern "321112111232111211123" indicate in the 4 th example? 
5

More products