COL100 Assignment 3 Solved

30.00 $

Category: Tags: , ,
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

Rate this product

1 Maximum Number (In-Lab Component)

This question is the combination of if-else and the variables. Your task is to write a python program which takes 3 numbers as input and find the max among the 3 using if-else statements only. You can not use max function anywhere and data structures like arrays, lists. Finally print the maximum of the three.

Your program should prompt the user to enter the three numbers, namely a, b and c respectively. Example 1:

INPUT:

1 2 3

OUTPUT: 13

EXPLANATION:
Here in this example we are provided with three numbers, a = 1, b = 2 and c = 3. So, maximum of the three numbers is 3

Example 2:

INPUT:

1 2 3

OUTPUT: 11

Example 3:

INPUT:

1 2 3

OUTPUT: 11

Note: a, b, c can be any real number (i.e. need not only be integers). 2 Modified Calculator (5 marks)

Write a python program to perform the following binary operations on two integers a and b with given parameter p.

This time you are given a parameter as well in the input and you have to perform appropriate operation according to the parameter. Parameter p can take values 1, 2, 3, 4 and 5 only, where each number refer to the following binary operation:

1. Addition
2. Subtraction
3. Multiplication 4. Division
5. Modulus

Your program should prompt the user to enter the two numbers, namely a and b (You can assume that b ̸= 0) and a parameter p and perform the operation according to the parameter. For e.g. if p = 1 then you have to print the addition of a and b i.e. a+b, if p = 2 then you have to print the subtraction of a and b i.e. a−b and so on.

Example 1:

INPUT:

1 2 3

1 -1.58 -6

1 1 0

2

2 4 1

1 2 3

OUTPUT: 16

EXPLANATION:
Here in this example we are provided with three numbers, a = 2 and b = 4 and p = 1. So, from the list above 1 stands for addition. So, there addition will be a + b = 6.

Example 2:

INPUT:

1 2 3

OUTPUT: 1 -2

Example 3:

INPUT:

1 2 3

OUTPUT: 18

Example 4:

INPUT:

1 2 3

OUTPUT: 1 0.5

Example 5:

INPUT:

1 2 3

OUTPUT: 12

2 4 2

2 4 3

2 4 4

2 4 5

It is guaranteed that p will not take values other than 1, 2, 3, 4 and 5. Also, a and b are not necessarily integers, and can take any real value.

3 Quadratic Equation Solver (5 marks)

Write a python program to calculate the roots of a real quadratic polynomial. You may use the quadratic formula (Shri Dharacharya Formula) to solve this.

Giventousarealquadraticpolynomialp(x)=ax2+bx+cwherea,b,c∈R,a̸=0. Thepython program takes in the values of a,b and c respectively in its input and prints the two roots in the

Note:

3

√

standard output in x + iy (where i =
one line (printing order of the roots does not matter). Your program should prompt the user to enter the three numbers, namely a, b and c respectively. You are only allowed to use sqrt() function for result calculation apart from binary operator.

Example 1:

INPUT:

1 2 3

OUTPUT: 1

2

INPUT:

1 2 3

OUTPUT: 1

2

INPUT:

1 2 3

OUTPUT: 1

2

-1 0 -1 0

−1) form, where x and y are space separated. Each root is in

1 2 1

EXPLANATION:
Hereinthisexampleweareprovidedwiththreenumbers,a=1,b=2andc=1. So,p(x)=x2+2x+1, hence the two roots are same and both are −1 + 0i. Also note how in both the roots, x and y i.e. the real and the imaginary part of each root is space separated; and there is a different line for each root.

Example 2:

1 0 1

01 0 -1

EXPLANATION:
p(x) = x2 + 1. Therefore, roots are ±i.

Example 3:

1 2 3

-1 1.41
-1 -1.41

Note: a, b, c can take any real value. Space between real and imaginary part should be of 1 unit and both should follow output rule (given at top) individually.

4 Check Palindrome (5 marks)

Write a program to check whether a string is a palindrome or not. The string will be provided as input and you have to use if-else statements to check whether the string is a palindrome or not. A palindrome is a string which reads the same backward as forward. The string madam when spelt backwards is madam therefore is a palindrome however the string check when spelt backwards is kcehc which is not the same and is thus not a palindrome.

4

Your program should prompt the user to input a string. The length of the string will be less than or equal to 4 and length will not be provided, you can use the function len(str) to find the same. You cannot use loops, in-built string functions or the slice statement str[:: −1] to solve this problem. You are only allowed to use the len() function, if-else statements and string indexing. The output will be

“NO” if the string is not a palindrome and “YES” if the string is a palindrome.

Note: The output must be in capital letters only. The input can contain numbers, letters and special characters. Capital and small letters are considered as different i.e. “a” and “A” are not the same.

Example 1:

INPUT: 1 loop

OUTPUT: 1 NO

EXPLANATION:
The string loop when spelt backwards reads as pool which is not the same as loop and is thus not a palindrome.

Example 2:

INPUT: 1 aba

OUTPUT: 1 YES

EXPLANATION:
The string aba when spelt backwards reads as aba which is the same and is a palindrome.

Example 3:

INPUT: 1 a,b

OUTPUT: 1 NO

EXPLANATION:
The string a,b when spelt backwards reads as b,a which is not the same as a,b and is thus not a palindrome.

Example 4:

INPUT: 1 atta

OUTPUT: 1 YES

EXPLANATION:
The string atta when spelt backwards reads as atta which is the same as forwards.

Example 5:

INPUT:

5

1 Aa OUTPUT:

1 NO EXPLANATION:

The string forwards is Aa and backwards is aA, as both are different it is not a palindrome.

  • A3-x9nzn7.zip