Master Data Structures and Algorithms from Big-O notation, arrays, and linked lists to trees, graphs, sorting, and dynamic programming.

Before starting: Basic Python loops, lists and functions.

Practice project: Extend the examples into a small Data Structures and Algorithms study demo. Explain the setup, one normal case and one failure or boundary case.

Start here: concepts and guided practice

  1. Linear search: find the first match

    Linear search checks entries until it finds the target. It works without sorted input. With n entries it may examine all n, so its worst-case running time grows linearly.

    Python 3; save each example as study.py and run python study.py.

    def first_index(values, target):
        for index, value in enumerate(values):
            if value == target:
                return index
        return -1
    
    print(first_index([8, 3, 8], 8))
    print(first_index([], 8))
    Check the output
    0
    -1

    Test an absent value and a target at the last index.

  2. A stack: undo the most recent action

    A stack follows last in, first out. Here a Python list stores action names. append adds one; pop removes the most recent. Check emptiness before popping.

    Python 3; save each example as study.py and run python study.py.

    actions = []
    actions.append("type title")
    actions.append("change color")
    print(actions.pop())
    print(actions)
    Check the output
    change color
    ['type title']

    Guard pop with if actions, then try an empty stack.

Open the Data Structures and Algorithms learning path on DevStudyHub · Play coding games on this blog

logoblog

No comments:

Post a Comment

Please do not enter any spam link in the comment box.