Week 3 - You Try It
Solution Key
This page contains quick solutions to the mystery problems from You Try It #3. A video is available where each is explained in a little more detail.
Solutions
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It adds up all of the numbers from 0 to howMany-1
- If howMany is 5 this would return the sum of 0+1+2+3+4
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(1)
- In 1 or 2 sentences, what does this function do?
- This method adds up the numbers from 0 to 9 regardless of what parameter is put in.
- It always returns 45 and that takes the same amount of time to calculate every time.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- This will return the same number as howMany.
- It arrives at that value by adding 1 to the sum the number of times represented by howMany.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It returns the index of the first occurrence of item in the list named someList.
- If the item does not occur in the list it returns -1.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It returns True or False indicating whether item was found in someList.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It returns the index of the last occurrence of item in the list named someList.
- If the item does not occur in the list it returns -1.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It returns the index of the first occurrence of item in the list named someList.
- If the item does not occur in the list it returns -1.
- Yes, this does the exact same thing as method 4.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n2)
- In 1 or 2 sentences, what does this function do?
- NOTE: A meaningful description to this one is hard because it makes little sense.
- It returns a number which is n+2y where
- n is the length of someList and
- y s the number of "pairs" of matching values
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n2)
- In 1 or 2 sentences, what does this function do?
- It counts the number of unique matching pairs in someList.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It returns True if any of the first five items in the list match with some other item in the remainder of the list
- Note: It will crash if there are not at least 7 items in the list
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(n)
- In 1 or 2 sentences, what does this function do?
- It will count and return how many times item1 and item2 are found in the list.
- What is the Big-Oh value (O(1), O(n) or O(n2)
- O(1)
- In 1 or 2 sentences, what does this function do?
- It will count and return how many times item is found in the first five items of the list.