COGS 18 - Exam 1 (Practice Take-Home)#

This is the practice take-home portion of the first exam for Spring 2024. It covers the technical implementation of topics through the Collections Lecture.

This practice is worth 0 pts and 0% of your grade. But, the actual exam will be worth 15 pts (12.5 pts in-class; 2.5 points take-home)

PLEASE DO NOT CHANGE THE NAME OF THIS FILE.

PLEASE DO NOT COPY & PASTE OR DELETE CELLS INLCUDED IN THE ASSIGNMENT. (Note that you can add additional cells, if you want to test things out.

The Practice exam for the in-class portion of exam can be found here: https://docs.google.com/document/d/1gj1Vw_gf9LLMsN9_4FK3iZM6usdpoG7_hYSShnVYK-0/edit?usp=sharing

The practice exam answer key can be found here: https://docs.google.com/document/d/1-IFfQ9Y36leBxoDmU0Ga16MqOxYn9TsgkNbizeFFBEk/edit?usp=sharing

Instructions#

Timing#

  • There take-home portion will be released 3:30 the day of the exam and due the same day at 11:59PM

The Rules#

  • You are to complete this exam on your own.

  • This is open-notes & open-Internet.

  • You may not talk to any humans about this exam.

  • Clarification questions will not be allowed and there will be no posting on Piazza about the exam at all.

    • If you are confused about wording, add a note to your exam explaining your confusion and how you interpreted the question.

    • If you have a technical issue completing the exam, message (Piazza or email) Prof Ellis ASAP.

Note: This portion of the midterm will be autograded, similar to an assingment. assert statements are there to guide your thinking but do not guarantee you’ve gotten the answer correct.

Q0 - Honor Code (0.05 points)#

In the cell below, include a variable honor_code that stores the boolean True if you agree to the following statement:

I agree that this exam was completed individually with the knowlege in my brain, information the notes from this course, and/or with searching for help on the Internet without searching for answers to the text from these questions directly. I did not ask anyone about specific questions on this exam. I did not post these questions (in part or in whole) on the Internet. I did not copy answers to these questions from anywhere or anyone else. I understand all code I’ve written on this exam and could explain my answers to someone else if asked.

### BEGIN SOLUTION
honor_code = True
### END SOLUTION
assert honor_code

Background#

On the in-person portion of the midterm you were told the following: You sat down to write code to make a very basic memory game. The idea here is that there will be three functions: 1) a function that displays a single character; 2) a function that displays a set of characters; 3) a function that determines if the single character is in the set of characters. Now, let’s write the code for those functions!

Q1 - Function execution (0.6 pts)#

On the midterm you were provided the function provided beow and had the opportunity to describe how the function worked.

Here, now that you have understanding, execute the function such that the provided assert statements for this question pass silently.

def display_char(index):
    
    objects = ['x', 'ã', 'ƫ', '%', '§', '¶',
              'Ł', 'Œ', 'Ɓ', 'Ɍ', 'Ɏ', 'ḉ']
    
    char = objects[index]
    
    return char
### BEGIN SOLUTION
out1 = display_char(5)
out2 = display_char(2)
out3 = display_char(-1)

print(out1, out2, out3)
### END SOLUTION
¶ ƫ ḉ
# you can use this cell to test/execute/check your thinking (optional)
assert out1 == '¶'
assert out2 == 'ƫ'
assert out3 == 'ḉ'

Q2 - Debugging a function (0.4 pts)#

The following function was provided on the midterm:

def display_objects(start, stop, skip):
    
    objects = ['x', 'ã', 'ƫ', '%', '§', '¶',
              'Ł', 'Œ', 'Ɓ', 'Ɍ', 'Ɏ', 'ḉ']
    
    objects[start:stop:skip]
    
    return objects

…with the following additional information:

You sit down to write a function (display_objects) that will return a slice of a specified list (objects). The idea is that, when executed, the function would use the values specified for the three parameters (start, stop, and skip)

Debug this function so that it accomplishes what you had intended.

### BEGIN SOLUTION
def display_objects(start, stop, skip):
    
    objects = ['x', 'ã', 'ƫ', '%', '§', '¶',
              'Ł', 'Œ', 'Ɓ', 'Ɍ', 'Ɏ', 'ḉ']
    
    output = objects[start:stop:skip]
    
    return output # could also return the output directly
### END SOLUTION
# you can use this cell to test/execute/check your thinking (optional)
assert callable(display_objects)

# hidden tests that check the function works as specified
### BEGIN HIDDEN TESTS
assert display_objects(0,1,1) == ['x']
assert display_objects(1,6,2) == ['ã', '%', '¶']
### END HIDDEN TESTS

Q3 - Defining a function (1.45 pts)#

Define a function determine_match.

This function should take in two parameters, char (the output from display_char) and list_of_objects (the output from display_objects).

If the output from display_char is in an element in the output from display_objects, this function should return True; otherwise, it should return False.

For example:

  • determine_match(display_char(5), display_objects(1,6,2)) should return True

  • determine_match(display_char(0), display_objects(1,6,2)) should return False

### BEGIN SOLUTION
def determine_match(char, list_of_objects):

    if char in list_of_objects:
        output = True
    else:
        output = False
    
    return output
### END SOLUTION
# you can use this cell to test/execute/check your thinking (optional)
assert callable(determine_match)

# hidden test that checks the output is of the expected type
### BEGIN HIDDEN TESTS
# note: this would still give credit, even if you had an error earlier
assert type(determine_match('a', ['a','b']))
### END HIDDEN TESTS
# hidden tests that check the parameter names
### BEGIN HIDDEN TESTS
# note: this would still give credit, even if you had an error earlier
assert determine_match(char='a', list_of_objects=['a','b'])
assert not determine_match(char='c', list_of_objects=['a','b'])
### END HIDDEN TESTS
# hidden tests that check the provided examples
### BEGIN HIDDEN TESTS
# note: this requires all three functions to work  to get credit
assert determine_match(display_char(5), display_objects(1,6,2))
assert not determine_match(display_char(0), display_objects(1,6,2))
### END HIDDEN TESTS
# hidden tests that check overall functionality
### BEGIN HIDDEN TESTS
# note: this requires all three functions to work  to get credit
assert not determine_match(char=display_char(5), list_of_objects=display_objects(1,6,3))
assert determine_match(display_char(0), display_objects(0, 12, 4))
### END HIDDEN TESTS