CSE 1310 - Assignments

Not Graded Practice for Assignment 6


Exercise 1.

Describe the behaviour of functions myfun1 and myfun2: What do these functions do? What is the difference among them? Why is that happening? Note: x is a list of integers. Your answer should be understandable by someone who does not know programming.


def myfun1( x ) :
    for value in x :
        if value%2 == 0 :
            value = 100

def myfun2( x ) :
    length = len( x )
    i = 0
    while i < length :
        if x[i]%2 == 0 :
            x[i] = 100
        i += 1

## Main ##

x = [1, 13, 4, 62, 5]
print ('x = ', x)

myfun1( x )
print ('after myfun1 = ', x)

myfun2( x )
print ('after myfun2 = ', x)
     

Exercise 2.

What does this function do? What does it return if x = 5?

def Func(x):
	total = 0
    for i in range(x):
    	total += i * (i-1)
	return total
    
Your answer should be understandable by someone who does not know programming.

Exercise 3.

Describe the execution of this program: what lines are executed; what namespaces come into existence and when; what variables are in each namespace and what are their values? Your answer must explicitly show the state of each namespace after every execution of a line.

def counter(num, limit):          #line 1
    num += 1                      #line 2
    for x in range(1, num):       #line 3 
        print(x, end = ','),      #line 4
    print('\n')                   #line 5
    if (num ==  limit):           #line 6
        return                    #line 7
    else:                         #line 8
        counter(num, limit)       #line 9
                                  #line 10
num = 1                           #line 11
limit = 5                         #line 12
counter(num, limit)               #line 13

Exercise 4.

Write a function that computes the greatest common factor of a list of integers, i.e., the greatest number that divides evenly to all numbers in the list.

Examples:

#1

Input: [100, 256, 200, 128]

Output: 4

#2

Input: [2, 6, 5, 35]

Output: 1



Exercise 5.

Write a function that computes the least common multiple of a list of integers, i.e., the smallest quantity that is divisible by all integers in the list without a remainder.

Examples:

#1

Input: [100, 256, 200, 128]

Output: 6400

#2

Input: [2, 6, 5, 35]

Output: 210



Exercise 6.

The Fibonacci sequence is 1,1,2,3,5,8, 13, ... You can see that the first and second numbers are both 1. Thereafter, each number is the sum of the previous two numbers. Write a function that receives N as input and prints the first N numbers of Fibonacci sequence.

Example 1:

Input: 15

Output: 1,1,2,3,5,8, 13, 21, 34, 55, 89, 144, 233, 377, 610

Exercise 7.

Write a function that receives an integer input x and returns all primes from 1 to x. Your function should return None if the input number is less than 1.

Example 1:

Input: 50
 
Output: [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]