Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming now.

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class length: 24 weeks. Start anytime.

Creator: duallain

Status: Established

Join this class!

Lesson 12: Assignment 1: PS:7 Review Problems

Problems handout

Homework Submissions

14 total

ChapLeo (Self-grade: Outstanding)
Submitted 2 months ago | Permalink | Time spent: 3 hours

I did this problem-set, converted it to pdf format and uploaded it to Zippyshare.com. If you are stuck on this problem-set, I would recommend viewing my problem-set since I have explained it thoroughly (using diagrams and tables). The link is below:

Problem-set 7

DvorakAJS (Self-grade: Pretty good)
Submitted 2 months ago | Permalink | Time spent: 20 minutes

1)On first sight I'd say linear, every call of the function calls a simpler function until i-k = 1.

2)Also linear for same reason, in this case it's even easier to see.

3)Linear., runs s times through loop.

4)I think this is a quadratic function because of the loop in a loop, I see other poeple having a lot of different answers though.

5) s1 = [1] s2 = [2]

assertation ok: both lists tmp=[1] s1=[2]] --> return s2=[1]] --> return

print [1],[2]

6)print [2],[1]

7)for i in range(1): tmp=1 s[0]=s[-1] --> 1 and 3 swap s[-1]=1

prints [3,2,1]

nickrt (Self-grade: Outstanding)
Submitted 10 months ago | Permalink | Time spent: 30 minutes

1) This is linear based on the size of i because it is a recursive algorithm that reduces i by 1 every time until it is equal to 1 or 0 so it performs the recursive algorithm for the length of i.

2) This is linear based on the size of i because the first 2 commands only happen once, and there is only one while loop, and they are not nested. Inside the while loop 2 basic commands happen each time and the while loop runs until i = 1 and it subtracts 1 from i every time.

3) O(len(s)(len(s)+1)/2) because the worst case scenario is that c has no repeating symbols, so res grows by 1 for every iteration of the loop, and the loop runs for the length of s. Then the second part comes because it takes a linear time to search all of res and res grows by 1 every time until it reaches len(s) so it is the sum of 1 to len(s) e.g. if s was 5, 1+2+3+4+5. And that is n(n+1)/2. so it is quadratic.

4) The worst case scenario is that there is no repeating symbols in s1 or s2 and that s1 and s2 contain none of the same symbols. This way it is O(2O(makeSet) + s1s2). So this is also quadratic, but a larger quadratic than problem 3.

5) bind s1 to [1]

bind s2 to [2]

Check if s1 and s2 are type lists, if not stop, else continue

set temp to a copy of s1 [1]

set s1(local) to a copy of s2 [2]

set s2(local) to temp(alias) [1]

pint s1 and s2 ([2],[1])

NOTE: It prints 1 and 2 because s1 and s2 are only local variables in the function and the function doesn't return anything

6) bind s1 to [1]

bind s2 to [2]

check if s1 and s2 are lists, if not stop, else continue

set s1 to s2 and s2 to s1 (s1 = [2], s2 = [1])

pint s1 and s2 ([2], [1])

7) set s to [1,2,3]

check if s is a list, if not stop, else continue (continues)

tmp = s[0] = 1

s[0] = s[-1] (s = [3,2,3])

s[-1] = temp = 1 (s = [3,2,1])

tmp = s[1] = 2

s[1] = [s(-2)] (s = [3,2,1])

s[-2] = tmp = 2

print s (s = [3,2,1])

virpriscus (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 15 minutes

1) The computational complexity of fact0 is linear - O(n), where n is the value of i+1. -- fact0 recursively works its way through the problem, reducing the value of i by one each time. It continues to do so until i = 0.

2) The computational complexity of fact1 is linear - O(n), where n is the value of i-1. -- fact1 iteratively works its way through the problem, reducing the value of 1 by one each time. It ceases calculation when i = 1.

3) The computational complexity of makeSet is linear - O(n), where n is the value of len(s). -- makeSet goes through each character in the string and checks if its in the string res. If not, it adds it and conintues to the next character in the string s.

4) The computational complexity of intersect is quadratic - O(n)(m), where n is the value of len(s1) and m is the value of len(s2). It creates the two sets s1 and s2, and then iterates through the set s1 - O(n) - checking its value against the set s2. Since we are interested in worst-case scenarios - the search will provide no matches, and iterate through the entirety of s2 with each search - O(m). As such, we are performing a search equal to O(m) O(n) times - O(n)(m).

5)

assign s1 = [1] s2 = [2]

swap0 tmp = [1] s1 = [2] s2 = [1]

print [2] [1]

6)

assign s1 = [1] s2 = [2]

swap1 [2], [1]

print [2], [1]

7)

assign s=[1,2,3]

rev(s) tmp=1 s[1] = 3 s[-0] = 1 s = [3,2,1]

print [3,2,1]

sebrenner (Self-grade: Pretty good)
Submitted 1 year ago | Permalink | Time spent: 2 hours

MIT OpenCourseWare 6.00 Problem Set 7

This problem set is designed to help you solidify your understanding of some material that we have covered in lecture, but not emphasized on the programming problems. You should do it, but do NOT hand it in.

1) What is the computational complexity of fact0? Explain your answer.

def fact0(i): assert type(i) == int and i >= 0 if i == 0 or i == 1: return 1 return i * fact0(i-1)

This is recursive function. Each time the function calls itself the time to complete the function increases by 1x. Its computational complexity is linear time: O(n).

2) What is the computational complexity of fact1? Explain your answer.

def fact1(i): assert type(i) == int and i >= 0 res = 1 while i > 1: res = res * i i -= 1 return res

This is not recursive and its complexity is linear.

3) What is the computational complexity of makeSet? Explain your answer.

def makeSet(s): assert type(s) == str res = '' for c in s: if not c in res: res = res + c return res

This is not recursive and its complexity is linear. The longer the string the longer it takes to parse it.

4) What is the computational complexity of intersect? Explain your answer.

def intersect(s1, s2):

assert type(s1) == str and type(s2) == str
s1 = makeSet(s1)
s2 = makeSet(s2)
res = ''
for e in s1:
    if e in s2:
        res = res + e
return res

This one depends on makeSet(). If make set is linear then, the complexity of this function depends on the the length of makeSet(s1). It the set returned by makeSet(s1) is long then the function will take longer to run, but it will only take a little bit longer to run because the for loop will only run one more time for each additional character in the makeSet(s1) set.

5) Present a hand simulation of the code below. Describe the value to which each identifier is bound after each step of the computation. Note that “s1” and “s2” exist in more than one scope.

def swap0(s1, s2): assert type(s1) == list and type(s2) == list tmp = s1[:] s1 = s2[:] s2 = tmp return s1 = [1] s2 = [2] swap0(s1, s2) print s1, s2

before swap0: s1, s1 = [1],[2] at end of swap0: s1, s1 = [2],[1] out of swap0: s1, s1 = [1],[2]

The global s2 and s2 are not altered by the local s1 and s2 swap. swap0() only swaps the local s1 and s2.

6) Present a hand simulation of the following code:

def swap1(s1, s2): assert type(s1) == list and type(s2) == list return s2, s1 s1 = [1] s2 = [2] s1, s2 = swap1(s1, s2) print s1, s2

before swap1: s1, s1 = [1],[2] at end of swap0: s1, s1 = [1],[2] out of swap0: s1, s1 = [2],[1]

In this snippit swap1() 'swaps' s1 and s2 by merely returning them in reverse order. The real magic is in the assignment operator assigning two variable at the same time.

7) Present a hand simulation of the following code:

def rev(s): assert type(s) == list for i in range(len(s)/2): tmp = s[i] s[i] = s[-(i+1)] s[-(i+1)] = tmp s = [1,2,3] rev(s) print s

before rev s --> [1,2,3,] for i tmp --> 1 s[0] --> 3 s[-1] --> 1

@@@@@@@@@ Wait a minute. Compare result in 7 with result in 5. Why are these inconsistent. Is it because there is an assignment operator in 5 that reassigns the variable, while in 7 there the assignment operator is only changing an element of the list?

tuckertuck (Self-grade: Outstanding)
Submitted 1 year ago | Permalink | Time spent: 1 hour
#
# Problem 1: What is the computational complexity of fact0()?
#

def fact0(i):
    assert type(i) == int and i >= 0
    if i == 0 or i == 1:
        return 1
    return i * fact0(i-1)

#
# The complexity of fact0 is O(n) or Linear where n = i.
# The function recurs once for every i and then returns i - 1 until it hits
# the base case.
#

#
# Problem 2: What is the computational complexity of fact1()?
#

def fact1(i):
    assert type(i) == int and i >= 0
    res = 1
    while i > 1:
        res = res * i
        i -= 1
    return res

#
# The complexity of fact1 is O(n) or Linear where n = i - 1.
# The function iterates from i to i > 1.
#


#
# Problem 3: What is the computational complexity of MakeSet()?
#

def makeSet(s):
    assert type(s) == str
    res = ''
    for c in s:
        if not c in res:
            res = res + c
    return res

#
# The complexity of MakeSet is O(n) or Linear where n = len(s).
# The function iterates through the string s and decides whether or not to add
# the character, c to res, before finally returning the value of res, which is
# the unique characters in the string.
#

#
# Problem 3: What is the computational complexity of intersect()?
#

def intersect(s1, s2):
    assert type(s1) == str and type(s2) == str
    s1 = makeSet(s1)
    s2 = makeSet(s2)
    res = ''
    for e in s1:
        print e
        if e in s2:
            print res
            res = res + e
    return res

#
# The complexity of intersect is O(n) or Linear where n = len(s1).
# The function iterates through the string s1 and checks whether the character,
# e is also in s2. If it is then it saves the character to res and finally
# returns all characters that are the same in both words.
#

#
# Problem 5: Present a hand simulation of the following code:
#

def swap0(s1, s2):
    assert type(s1) == list and type(s2) == list
    tmp = s1[:]
    s1 = s2[:]
    s2 = tmp
    return
s1 = [1]
s2 = [2]
swap0(s1, s2)
print s1, s2

#
# __main__
# s1 --> [1]
# s2 --> [2]
# swap0(s1, s2)
#
# __swap0__
# tmp --> [1]
# s1 --> [2]
# s2 --> [1]
# s1 = [2], s2 = [1]
#
# __main__
# s1 = [1], s2 = [2]
# print [1], [2]
#

#
# Problem 6: Present a hand simulation of the following code:
#

def swap1(s1, s2):
    assert type(s1) == list and type(s2) == list
    return s2, s1
s1 = [1]
s2 = [2]
s1, s2 = swap1(s1, s2)
print s1, s2

#
# __main__
# s1 --> [1]
# s2 --> [2]
# swap1(s1, s2)
#
# __swap1__
# return s2, s1
#
# __main__
# s1, s2 --> s2, s1
# s1 = [2], s2 = [1]
# print [2], [1]
#

#
# Problem 7: Present a hand simulation of the following code:
#

def rev(s):
    assert type(s) == list
    for i in range(len(s)/2):
        tmp = s[i]
        s[i] = s[-(i+1)]
        s[-(i+1)] = tmp
s = [1,2,3]
rev(s)
print s

#
# __main__
# s --> [1,2,3]
# rev(s)
#
# __rev__
# for i in range(0,1)
# for i is 0
# tmp --> s[0] --> 1
# s[0] --> s[-1] --> 3
# s[-1] --> 3
# for i is 1
# tmp --> s[1] --> 2
# s[1] --> s[-2] --> 2
# s[-2] --> 2
#
# __main__
# print [3,2,1]
#
conwayblue (Self-grade: Could be better)
Submitted 2 years ago | Permalink | Time spent: 10 minutes

Problem Set 7 conwayblue

  1. Linear. O(i)
  2. Linear. O(i)
  3. Linear. O(s)
  4. Linear. O(s)
  5. Prints [1] [2]. s1 and s2 are only swapped locally within the function and are not returned as swapped values.
  6. Prints [2] [1]. s1 and s2 get swapped globally.
  7. Prints the reversed list [3,2,1].
rfh (Self-grade: Pretty good)
Submitted 2 years ago | Permalink | Time spent: 1 minute
# Problem 1
#
# What is the computational complexity of fact0?
#
# Linear, a constant amount of operations are called
# n times, where n is the size of the input (in this
# case an integer)
#
def fact0(i):
    assert type(i) == int and i >= 0
    if i == 0 or i == 1:
        return 1

    return i * fact0(i-1)

# Problem 2
#
# What is the computational complexity of fact1? Explain your answer.
#
# Linear, like the recursive version, a constant amount of operations
# are executed once per iteration in the while loop. The number of
# iterations is equal to the size of the input
#
def fact1(i):
    assert type(i) == int and i >= 0

    res = 1
    while i > 1:
        res = res * i
        i -= 1

    return res

# Problem 3
#
# What is the computational complexity of makeSet? Explain your answer.
#
# Linear, a constant amount of operations are executed once per
# iteration. The number of iterations is equal to the number of elements
# in s.
#
def makeSet(s):
    assert type(s) == str

    res = ''
    for c in s:
        if not c in res:
            res = res + c

    return res

# Problem 4
#
# What is the computational complexity of intersect? Explain your answer.
#
# Linear, the number of operations is O(makeSet) + O(makeSet) + the number
# of elements in s1
#
def intersect(s1, s2):
    assert type(s1) == str and type(s2) == str
    s1 = makeSet(s1)
    s2 = makeSet(s2)
    res = ''
    for e in s1:
        if e in s2:
            res = res + e
    return res

# Problem 5
#
# Present a hand simulation of the code below. Describe the value to
# which each identifier is bound after each step of the computation.
# Note that “s1” and “s2” exist in more than one scope.
#
# s1    -> [1]
# s2    -> [2]
#
# (in function)
# tmp   -> [1]
# s1    -> [2]
# s2    -> [1]
# 
# print s1, s2 will print [2] and [1]
#
def swap0(s1, s2):
    assert type(s1) == list and type(s2) == list
    tmp = s1[:]
    s1 = s2[:]
    s2 = tmp
    return

s1 = [1]
s2 = [2]
swap0(s1, s2)
print s1, s2

# Problem 6
#
# Present a hand simulation of the following code:
#
# s1    -> [1]
# s2    -> [2]
#
# s1    -> s2
# s2    -> s1
#
# print s1, s2 will print [2] and [1]
#
def swap1(s1, s2):
    assert type(s1) == list and type(s2) == list
    return s2, s1

s1 = [1]
s2 = [2]
s1, s2 = swap1(s1, s2)
print s1, s2

# Problem 7
#
# Present a hand simulation of the following code:
#
# s     -> [1,2,3]
#
# (in function)
# i     -> 0
# tmp   -> 1
# s[0]  -> 3
# s[-1] -> 1
#
# print s will print [3,2,1]
#
def rev(s):
    assert type(s) == list
    for i in range(len(s)/2):
        tmp = s[i]
        s[i] = s[-(i+1)]
        s[-(i+1)] = tmp

s = [1,2,3]
rev(s)
print s
chrcoe (Self-grade: Outstanding)
Submitted 2 years ago | Permalink
#Problem Set 7
#Name: chrcoe
#Collaborators: none
#Time: 10 min
#

#1)
def fact0(i):
    '''recursive function with complexity O(n).
    It is linear because the fucntion is called one time until i = 1 or i = 0'''
    assert type(i) == int and i >= 0
    if i == 0 or i == 1:
        return 1
    return i * fact0(i-1)

#2)
def fact1(i):
    '''Complexity is linear because the function's while loop
    is called however many times i is. O(n)'''
    assert type(i) == int and i >= 0
    res = 1
    while i > 1:
        res = res * i
        i -= 1
    return res

#3)
def makeSet(s):
    '''Complexity is linear because the function's while loop
    is called however many times s is. O(n)
    Only doing one thing each time through the loop as well.'''
    assert type(s) == str
    res = ''
    for c in s:
        if not c in res:
            res = res + c
    return res

#4)
def intersect(s1, s2):
    '''the loop is only doing one thing and it depends on s1 and uses s2.
    should be O(n) - linear'''
    assert type(s1) == str and type(s2) == str
    s1 = makeSet(s1)
    s2 = makeSet(s2)
    res = ''
    for e in s1:
        if e in s2:
            res = res + e
    return res

#5)
def swap0(s1, s2):
    '''hand simulation of swap0.
    takes two #'s and swaps their values'''
    assert type(s1) == list and type(s2) == list
    tmp = s1[:]
    #tmp -> [1]
    s1 = s2[:]
    #s1 -> [2]
    s2 = tmp
    #s2 = [1]
    return #s1 = 2 and s2 = 1 ... effectively swapping the #'s

s1 = [1]
s2 = [2]
swap0(s1, s2)
print s1, s2
#>>> [2] [1]

#6)
def swap1(s1, s2):
    '''hand simulation of swap0.
    takes two #'s and swaps their values'''
    assert type(s1) == list and type(s2) == list
    return s2, s1 #returns s1 as value of s2 and s2 as value of s1

s1 = [1]
s2 = [2]
s1, s2 = swap1(s1, s2)
print s1, s2
#>>> [2] [1]

#7)
def rev(s):
    '''reverses the order of items in list s'''
    assert type(s) == list
    for i in range(len(s)/2):
        tmp = s[i]  #tmp(s[0])->1 
                    #tmp(s[1])->2
                    #tmp(s[2])->3
        s[i] = s[-(i+1)]    #s[0]->s[-1]->3
                            #s[1]->s[-2]->2
                            #s[2]->s[-3]->3
        s[-(i+1)] = tmp #s[-1]->3
                        #s[-2]->2
                        #s[-3]->1
s = [1,2,3]
rev(s)
print s
#>>> [3,2,1]
mrphud (Self-grade: Pretty good)
Submitted 2 years ago | Permalink
#Problem Set 7

# 1) What is the computational complexity of fact0? Explain your answer.
# def fact0(i):
#   assert type(i) == int and i >= 0
#   if i == 0 or i == 1:
#       return 1
#   return i * fact0(i-1)

# Constant + f(n-1) = 2*Constant + f(n-2) = n*Constant = linear

# The computational complexity of this recursive function is O(n), where
# n is the input of the function. The function must be called n times in
# order to get to the base case.


# 2) What is the computational complexity of fact1? Explain your answer.
# def fact1(i):
#   assert type(i) == int and i >= 0
#   res = 1
#   while i > 1:
#       res = res * i
#       i -= 1
#   return res

# a + n*b = linear

# The computational complexity of this function is still O(n), where n is
# the input i of the function. This function just uses a while loop to count
# down until base case.

# 3) What is the computational complexity of makeSet? Explain your answer.
# def makeSet(s):
#   assert type(s) == str
#   res = ''
#   for c in s:
#       if not c in res:
#           res = res + c
#   return res

# a + b*n + c = a' + b*n = linear

# The computational complexity of this function should be O(n), where n
# is the length of s. This function passes through the string s and adds
# letters missing from res to res.

# 4) What is the computational complexity of intersect? Explain your answer.
# def intersect(s1, s2):
#   assert type(s1) == str and type(s2) == str
#   s1 = makeSet(s1)
#   s2 = makeSet(s2)
#   res = ''
#   for e in s1:
#       if e in s2:
#           res = res + e
#   return res

# a + (a' + b*n) + (a'' + b*m) + c*n = d + n*e = linear

# The computational complexity of intersect should be ~O(n), where n
# is the length of s1 and m the length of s2. This function calls two previous
# functions of O(n) and then searches through n to combine the like terms.

# 5) Present a hand simulation of the code below. Describe the value to which each
# identifier is bound after each step of the computation. Note that “s1” and “s2” exist
# in more than one scope.

def swap0(s1, s2):
    assert type(s1) == list and type(s2) == list # Checks to see that s1 & s2 are lists
    tmp = s1[:] # tmp points to a copy of s1
    s1 = s2[:]  # s1 now points to a copy of s2
    s2 = tmp    # s2 points to the same list as tmp
    return      # returns None. The swap is only done locally
s1 = [1]
s2 = [2]
swap0(s1, s2)   # This does nothing
print s1, s2    # prints s1 = [1] and s2 = [2]

# 6) Present a hand simulation of the following code:

def swap1(s1, s2):
    assert type(s1) == list and type(s2) == list
    return s2, s1   # Returns the inputs, but switched assignment
s1 = [1]
s2 = [2]
s1, s2 = swap1(s1, s2) # The swap is now done globally
print s1, s2           # This will print [2] [1]

# 7) Present a hand simulation of the following code:

def rev(s):
    assert type(s) == list
    for i in range(len(s)/2): # Scans the list in range 0 to len(s)/2. 1 in this case.
        tmp = s[i]            # points local variable (tmp) to s[i]
        s[i] = s[-(i+1)]      # points global s[i] to s[-(i+1)] at end of list
        s[-(i+1)] = tmp       # points global s[-(i+1)] to original s[i]
s = [1,2,3]                   
rev(s)          # reverses the list s. The function is switching pointer assignments.
print s         # prints [3, 2, 1]

hendrix (Self-grade: Pretty good)
Submitted 2 years ago | Permalink
Problem set 7
Mark Calderwood

1) What is the computational complexity of fact0? 
def fact0(i):

it is O(i) (linear with respect to size of i) because the function is called
once each time it recurses through from i to 0.

2) What is the computational complexity of fact1?

Also O(i) (linear) because the function contains code in a loop
that is executed a number of times = to i

3) What is the computation complexity of makeSet?

O(s) where s is the number of character in s, it is looping through s 1
character at a time and performing some operations in each loop

4) What is the computation complexity of intersect?

Exponential because we have a double loop (not sure how to write this
in asymptotic notation so for every char in s1 we do something for
ever char in s2 therefor we have s1 x s2 number of operations

5) Present a hand simulation of the code below.

1 - We bind the list [1] to s1
2 - We bind the list [2] to s2
3 - We call the function swap0 with s1 and s2 as arguments
4 - swap0 checks that the arguments are lists
5 - Inside swap0 we create a local variable tmp which we assign to a copy of s1
6 - We make a local variable called s1 and assign it a copy of s2
7 - We make a local s2 and assign it to tmp
8 - We print the global s1 & s2 which have not been changed

6) Present a hand simulation of the code below.

1 - We bind the list [1] to s1
2 - We bind the list [2] to s2
3 - We call the function swap1 with s1 and s2 as arguments
4 - swap1 checks the arguments are lists
5 - swap1 returns the two arguments in the opposite order
6 - These are bound to s1 and s2 effectively switching them
7 - We print s1 and s2 and see that they have been changed

7) Present a hand simulation of the code below.

1 - We bind the list[1,2,3] to s
2 - We call revs with s as the argument
3 - revs checks that s is a list
4 - We work out the middle point of s
5 - We initialise a loop to go from the begining of s to the middle
6 - Going through the loop we bind a local variable tmp to the actual
object s's first character (s[0])
7 - We make the first character of the actual object s a copy of the last character ( s[0] = s[-(0+1)] aka s[0] = s[-1] )
8 - We make the last character tmp
9 - The loop return to the begining but i is now = 1
10 - We store the 2nd charcter of s (s[1]) in tmp
11 - We replace the 2nd character with the 2nd last character (s[1] = s[-(1+1)], in this case this is also the second character
12 - We replace the second last character with tmp
13 - the loop is finished because 3/1 = 1 so we print s which is now [3,2,1}
chip (Self-grade: Outstanding)
Submitted 2 years ago | Permalink
  1. Complexity of fact0 is O(n), because recursion have fixed nested level that depends on a "n" and works until n != 1.
  2. Complexity of fact1 is O(n), because there only one for loop
  3. If except loop in conditional statement than the complexity equals O(n), because there also one constant loop
  4. O(n), because there are same one constant loop, and makeSet functions execute only in the first time
#5
def swap0([1], [2]): 
        assert type(s1) == list and type(s2) == list #True
        tmp = s1[:]   #[1]
        s1 = s2[:]     #[2]
        s2 = tmp      #[1]
        return
s1 = [1]
s2 = [2]
swap0(s1, s2)        #s1 = 1, s2 = 2
print(s1, s2)          #[1] [2]

#6
def swap1(s1, s2):
    assert type(s1) == list and type(s2) == list #True
    return s2, s1 
s1 = [1] 
s2 = [2] 
s1, s2 = swap1(s1, s2) #s1 = 2, s2 = 1
print(s1, s2) #[2] [1]

#7
def rev([1,2,3]): 
        assert type(s) == list 
            for i in range(0,1):
                tmp = s[i]           #1
                s[i] = s[-(i+1)]    # [3,2,3]
                s[-(i+1)] = tmp   #[3,2,1]
rev(s)
print(s) #[3,2,1]


Joe (Self-grade: Outstanding)
Submitted 2 years ago | Permalink

1) T(n)=O(n): the function will recurse until i=1

2) T(n)=O(n): each loop is constant and there is about n loop

3) T(n)=O(n): each loop is constant and there is about n loop

4) T(n)=O(n): at first there is 2n, followed by a for loop with constant each loop, so it's still O(n)

#5)
def swap0([1], [2]): 
	assert type(s1) == list and type(s2) == list 
	tmp = s1[:] 
	# tmp = [1]
	s1 = s2[:] 
	# tmp = [1], s1 = [2]
	s2 = tmp 
	# tmp = [1], s1 = [2], s2 = [1]
	return
	# [2],[1]

#6)
def swap1([1], [2] ): 
	assert type([1]) == list and type([2] ) == list 
	return [2] , [1]
s1, s2 = swap1([1], [2]) 
print s1, s2
[2],[1]

#7)
def rev([1,2,3]): 
	assert type(s) == list 
	    for i in range(0,1):
		tmp = s[i] 	# tmp=1
		s[i] = s[-(i+1)] # s=[3,2,3]
		s[-(i+1)] = tmp # s=[3,2,1]
rev(s) print s # [3,2,1]
theabraham (Self-grade: Pretty good)
Submitted 2 years ago | Permalink

With the first four problems, it helped me to watch lecture 8 again. I think I have them all right, but I may have deduced the answer incorrectly. My biggest concern is how to handle statements like if not in c that are in for loops (Does the search count as a loop in itself? If so, wouldn't it make it quadratic?) Any comments would be appreciated.

Problem 1 Has a complexity of O(n). It's a recursive function that has about 3n+f(i-n) steps in it.

Problem 2 Has a complexity of O(n). There are about 3+3n steps, making it have a linear growth.

Problem 3 Has a complexity of O(n). There are about 3+3n steps (unless the if statement counts as a loop within a loop, then it'd be quadratic)

Problem 4 Has a complexity of O(n). There are 3+2(3+3n)+3n, or 9+9n steps (because the makeSet function, which has a for loop within it is being used).

#
# Problem 5
#
def swap0(s1, s2): #Given s1 = [1] and s2 = [2]
    assert type(s1) == list and type(s2) == list #True
    tmp = s1[:] #tmp = [1], a new copy of s1
    s1 = s2[:] #s1 = [2]
    s2 = tmp #s2 = [1]
    return 

s1 = [1]
s2 = [2]
swap0(s1, s2) #Doesn't change anything because s1 and s2 are only changed
	      #locally in the function, and were not returned properly
print s1, s2  #Prints [1] [2]

#
# Problem 6
#
def swap1(s1, s2): #Given s1 = [1] and s2 = [2] 
    assert type(s1) == list and type(s2) == list #True
    return s2, s1 #Return them, with s2 being returned first
s1 = [1] 
s2 = [2] 
s1, s2 = swap1(s1, s2) #Translates to s2, s1 setting the values to swap
print s1, s2 #Prints [2] [1]

#
# Problem 7
#
def rev(s): #Given s = [1,2,3]
    assert type(s) == list #True
    for i in range(len(s)/2): #Loop through once (3/2 = 1)
        tmp = s[i] #tmp = 1
        s[i] = s[-(i+1)] #Change the first value, 1, to that of the last's, 3
        s[-(i+1)] = tmp #Change the last's value, 3, to the temporarily stored value, 1
s = [1,2,3] 
rev(s) #Reverses s
print s #Prints [3,2,1]