CS 2000 - Fall 2025.


[Home]

Welcome to CS 2000! 10/27/2025

Video of the Day

Socratica Python videos:

Logical problem of the day

Who played for the New York Rangers, the Brooklyn Dodgers, and the New York Knicks?

https://pollev.com/slade You may also download the app to your phone. Use the "slade" poll id.

Canvas Quiz of the Day (need daily password)

Most days, there will be a simple canvas quiz related to the lecture. You need a password to activate the quiz, which I will provide in class. These quizzes will count toward your class participation grade. The quiz is available only during class. Note: each quiz is now worth 5 points.

Click for today's quiz.

Lecture 16: Data Structures + PVM.

Administrivia

  • I have office hours Wednesdays from 4-6 pm, on zoom, id 459 434 2854.

  • I will be available for lunch on Mondays at 1 pm in Morse.

  • ULA office hours are found at Ed Discussions on Canvas.

  • CS Peer Advisers. Fall 2025 Peer Advisors Office Hours Schedule.

  • Homework assignments: [Assignments]. hw5 is now available. Extension for hw4 to Wednesday, October 29th.

    hw4: staff solution for myhash had errors:

        def __eq__(self, other):
            if type(other) != type(self):
                return False
            if self.table == other.table:
                return True
            else:
                return False
      
    The eq method compares lists, in which order matters. However, for the hash table buckets, the order does not matter.
    >>> from hw4a import *
    >>> h = myhash(5)
    >>> h.put('a',1)
    >>> h.put('f',2)
    >>> h2 = myhash(5)
    >>> h2.put('f',2)
    >>> h2.put('a',1)
    >>> str(h)
    "myhash([[('a', 1), ('f', 2)], [], [], [], []])"
    >>> str(h2)
    "myhash([[('f', 2), ('a', 1)], [], [], [], []])"
    >>> h == h2
    False
    >>> 
      
    You might want to use flatten as a set comprehension, so that order does not matter.
    def flatten(matrix):
        return {item for row in matrix for item in row}
      

    I have fixed the problem in hw4a.pyc. Note that the tables do not need to be the same size.

    >>> from hw4a import *
    >>> h = myhash()
    >>> h.put('a',1)
    >>> h.put('b',2)
    >>> h2 = myhash(5)
    >>> h2.put('b',2)
    >>> h2.put('a',1)
    >>> str(h)
    "myhash([[], [], [], [], [], [], [], [], [], [], [('a', 1)], [('b', 2)], [], [], [], [], [], [], [], []])"
    >>> str(h2)
    "myhash([[('a', 1)], [('b', 2)], [], [], []])"
    >>> h == h2
    True
    

    There is a different problem with the copy method.

        def copy(self):
            h = myhash(self.size)
            h.table = self.table[:]
            h.count = self.count
            return h
      
    This code makes a shallow copy of the list. We need to make a deep copy of the embedded lists. Otherwise, there are problems. You can use Python Tutor to demonstrate.
    from hw4a import *
    >>> h = myhash(5)
    >>> h.put('a', 1)
    >>> h2 = h.copy()
    >>> str(h)
    "myhash([[('a', 1)], [], [], [], []])"
    >>> str(h2)
    "myhash([[('a', 1)], [], [], [], []])"
    >>> h.put('b',2)
    >>> str(h)
    "myhash([[('a', 1)], [('b', 2)], [], [], []])"
    >>> str(h2)
    "myhash([[('a', 1)], [('b', 2)], [], [], []])"
      
    The current staff solution, hw4a.pyc, fixes these issues.

    Announcements

  • Yale Information Society Project See this week's events.

  • Office of Career Services STEMConnect Pop Up advising Fall schedule. Drop in to one and have your resume reviewed, hear advice about finding internships, and learn more about how to get connected to alumni for networking opportunities.

    Midterm Exam II: Thursday November 13th, 7pm, DL 220

    The second midterm will similar to the first. You will have 2 hours. Here is a sample midterm exam. (solutions) There will also be a question on data structures.

    There will be a UNIX question, as in the first midterm. Here is a sample UNIX transcript (solutions) UNIX will cover through principle 4.

    old review slides

    Data Structures

    DataStructures.html (jupyter) Data Structures - heaps.

    There was a question about overflow in the hash function, djb2:

    The djb2 hash function inherently relies on integer overflow for its operation, particularly when implemented in languages like C or C++ where unsigned integer types exhibit defined wrap-around behavior on overflow.

    How djb2 handles overflow:

    Initialization: The hash starts with an arbitrary large prime number, 5381.

    Iteration: For each character c in the input string, the current hash value hash is updated using the formula:
    hash = ((hash << 5) + hash) + c.
    This is equivalent to hash = hash * 33 + c.

    Overflow as a feature: As the hash value grows with each character, it will eventually exceed the maximum value representable by the chosen unsigned integer type (e.g., unsigned int, unsigned long). When this occurs, the value "wraps around" or "overflows" according to modular arithmetic rules inherent to unsigned integer types in C/C++. This wrap-around behavior is a core part of the djb2 algorithm, contributing to its non-linear nature and helping to distribute hash values.

    Implications of overflow:

    Non-linearity: The overflow ensures that the hash function is not simply a linear accumulation, making it harder to predict output values and reducing the likelihood of simple collisions.

    Fixed-size output: Regardless of the input string length, the final hash value will always fit within the chosen unsigned integer type, providing a fixed-size hash.

    Consistency: The predictable wrap-around behavior of unsigned integer overflow ensures that the same input string will always produce the same hash value, which is crucial for a hash function.

    I recorded a lecture on Friday which covers iterators and decorators.

  • Iterators.html it14()
  • Decorators.html The decorators lecture uses time.time(). Here is a fragment from the lecture transcript:
    >>> time.time()
    1761309320.6466186
    >>> time.time()
    1761309374.0827036
    
    Key aspects of time.time():

    Unix Epoch: This is a fixed point in time used as a reference for measuring time in many systems. It is defined as January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC).

    Seconds since Epoch: The returned value is a float, providing a high-precision measurement of elapsed seconds since this epoch.

  • Exceptions.html
  • Python match statements See hw3.py next_value vs. hw3match.py next_value.

    Python Virtual Machine

    PVM.html jupyter notebook - Python Virtual Machine and hw5.

  • bytecode.py
  • pvm.py

    Getting to know UNIX

    UNIX Introduction Principle 3.
    [Home]