Assignment1 Solutions

Assignment1: Python & Javascript

Hello, everybody! Assignment1 is meant to be mostly a review of coding in Python and some new coding in Javascript. The problems map closely to the labs that we have done so far in class.

The rules for this assignment may be a little different than for future assignments, mainly because future assignments will be submitted via Moodle once I have full access to Moodle. For Assignment1, here is what you need to do:

Please email your completed solutions to me at dgeorge@iwu.edu. I'll expect three separate files: one .py file and two .js files (if you are working in an editor, like Visual Studio Code, this just means you save your file with that extension). When you submit, zip your files together:

Name your zipped folder using this convention: firstname_lastname_Assignment1.zip. The due date for Assignment1 is Tuesday, September 19, at 9:25am CST (i.e., start-of-class on that day). I will grade your submission with the most-recent timestamp (so that if you need to re-submit this assignment, that is OK up until the deadline).

If you have any questions or need any help with this assignment, Anna is available during her office hours and I too will be available to answer questions and talk through your ideas. Please remember the Illinois Wesleyan honor code and the class policies regarding plagiarism outlined on day 1. Again, for this assignment: it is OK to talk through your ideas with your classmates, but please note who you talked with (this can be as simple as a line at the top of your submission, "For problem 1, I collaborated with Taylor Smith and used the idea to iterate through an array." However, it is not OK to plug in your question to ChatGPT and have it generate a solution for you. If you have any questions about this, you have an open invitation to ask either myself or Anna anytime!

A big part of computer science and engineering is being curious, thinking about the details and the big picture, and also...just having fun. These problems are NOT meant to overwhelm and if you feel stressed, you have an open invite to talk to me, or come to office hours (not to sound like a broken record; I just want to emphasize that the course staff is rooting for you, and you have resources). You can do it!! 😊

1. Hashtags (25 points)

Implement code in Python to solve this problem and write 3 tests for your solution function. Feel free to use any editor to test out your solution (PyCharm, Visual Studio Code, vim...anything you want).

What is better than a hashtag? Two hashtags like '##yay.'

Find the first double-hash in a string and return the tag following the double-hash. The end of the tag will be be marked by a period '.' or by the end of the string. The tag may contain any type of char. If there is no double-hash, return None. Use string.find(). Examples:

'xx ##spring. xx' -> 'spring'
'xx ##summer' -> 'summer'
'x#. ##l@@k!.' -> 'l@@k!'
'xx ##.' -> ''
'xx #nope. xx' -> None
'abc' -> None

Additionally, write a docstring comment which describes what your function does, what the arguments are, and what the output should be.

"""
    (replace this with your function comment)
"""
def double_hash(s):
    hash = s.find('##')
    if hash == -1:
        return None
    dot = s.find('.', hash + 2)  # + 2 optional
    if dot != -1:
        return s[hash + 2:dot]
    # no dot, so use end of string
    return s[hash + 2:]

import unittest

class BasicTest(unittest.TestCase):
    # re-name this test, implement, and add two more
    def basic_test(self):
        # any three acceptable tests will do

if __name__ == '__main__':
    unittest.main()

2. Brackets (15 points)

Write Javascript code to solve this problem. You can use JsFiddle (similar to what we did in class) to test your code. You do not need to write any tests for this problem.

Find the first '[[' and the first ']]' after it in s. If both are present, return the chars between the two, and null otherwise. Use s.indexOf().

'xx[[abc]]xx' -> 'abc'
'xxx[[123]] [[]]' -> '123'
'xx]] [[ok]]' -> 'ok'
'xx]] [[nope' -> null
'xx]] [[nope]' -> null
'abc' -> null
/**
    (replace this with your function comment which describes 
    what your function does, what the arguments are, and what 
    the output should be.)
*/
function doubleBracket(s) {
    const left = s.indexOf("[[");
    if (left == -1){
        return null;
    }

    const right = s.indexOf("]]", left + 2);  //# + 2 not mandatory
    if (right == -1){
        return null;
    }
    
    return s.substring(left + 2, right);
}

3. Object-States (20 points)

Write Javascript code to solve this problem. You can use JsFiddle (similar to what we did in class) to test your code. You do not need to write any tests for this problem.

Suppose we have a "states" object where the keys are state codes like 'ca'. The value for each state is a nested object where the key is an int zipcode, and its value is the vote count for that zipcode, like this:

  
states = {
    'ca': {94301: 3456, 94303: 255, ... },
    'tx': {77494: 400, 75034: 9863, ... },
    ...
}

We'll say a location is a state-zipcode string, like this: 'ca-94303'. The "locs" is an array of location strings:

locs = ['ca-94303', 'tx-77494', ...]

Write a findVotes() function that takes in the states object and the locs array, and looks up the vote count for each location 'ca-94303'. Return an array containing one array of the form ['ca-94303', 255] for each location, giving the vote count for that location. So the result array looks like

[['ca-94303', 255], ['tx-77494', 400], ...]

If the data for a location is not present in the states object, then do not include an array for that location in the result. The arrays may be in any order in the result array.

/**
    (replace this with your function comment which describes 
    what your function does, what the arguments are, and what 
    the output should be.)
*/
function findVotes(states, locs){
    const result = [];
    for (var i = 0; i < locs.length; i++){
        const loc = locs[i];
        const state = loc.substring(0, 2);
        const zip = loc.substring(3);
        if (state in states){
            zips = states[state];
            if (zip in zips){
                const count = zips[zip];
                const pair = [loc, count];
                result.push(pair);
            }
        }
    }
    return result;
}