CSE 1310 - Assignments

Not Graded Practice for Assignment 5


Exercise 1.

The function below was designed to compute the square of a number x, however it is not working. Your task is to find and correct the error.

#####  function  #####
def MySquareFunction(x) :
    x = x*x
#####  main  #####

x = 5
MySquareFunction(x)
print(x)


Exercise 2.

The function below is an attempt to find the minimal and maximal element of a list of integers.

def max(mylist) :
    mymax = 100
    mymin = 0
    for n in mylist :
        if n > mymax :
            mymax = n
        elif mymin < n:
	        mymin = n
    return mymax, mymin    

Find out in which cases the above function does not work and correct them.


Exercise 3.

Write a function that receives as an input a 2D list (i.e., a list of lists of numbers), and returns the maximal element as well as it position on the 2D list.
Input: L = [[1.1, 2, 55, 78], [0.1, 3.3, -5.5, 0.8], [11, 24, 5.5, 0.8]]

Output: 
max element: 78
postion = [0, 3]

Exercise 4.

Mr. Bugs associate, Mr. Throws, has decided to create a new encryption for their communication channel by reversing the vowels in a sentence. Your task is to write a function to decipher the received sentences. Your function will receive a string text as input, find all vowels, and finally reverse their appearing order in text.

Example:

Input: 
'My ageina as vury smirt'

Output: 
'My iguana is very smart'

Exercise 5.

Write a function that given two lists, returns the union between lists, i.e., the elements that appear in either lists and with no repetition.
Example 1:

Input:
list1 = ['12', 'hello', 'world', 'world', '1.02', 'home', '!!!!']
list2 = ['21', 'hallo', 'world', '1.2', 'home', '~~~']

Output:
['12', 'hello', 'world', '1.02', 'home', '!!!!', '21', 'hallo', '1.2', '~~~']


Exercise 6.

Write a function that given two lists, returns the intersection between lists, i.e., the elements that both lists have in common.

Example 1:

Input:
list1 = ['12', 'hello', 'world', '1.02', 'home', '!!!!']
list2 = ['21', 'hallo', 'world', '1.2', 'home', '!!!']

Output:
['world', 'home']

Exercise 7.

Write a function to find the roots of quadratic polynomials. Your function should receive three inputs corresponding to the quadratic, linear and constant coefficients of a polynomial, and it should return the roots (real or imaginary) of the polynomial.

Background: for a polynomial of the form a*(x^2) + b*x + c, a root is any number r such that a*(r^2) + r*x + c = 0. To compute the two roots r1 and r2 of the polynomial, you use these steps:

NOTE: if b**2 - 4*a*c is negative, then D is an imaginary number. If you find it difficult to deal with imaginary numbers, an acceptable partial solution is to handle correctly the case where D is a real number, and print a message that "the polynomial has no real roots" if D is imaginary.

Example 1:

Input: [1, 2, 4] (corresponding to polynomial x^2 + 2x + 4)
 
Output: roots = [-2, 2]

Example 2:

Input: [1, 0, 1] (corresponding to polynomial x^2 + 1)
 
Output: roots = [-i, i]