Small Coding Problems

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/5

encourage image

There's no tags or description

Looks like no tags are added yet.

6 Terms

1
New cards

Linked List: Dedup

  • prev = None: Keeps track of the node immediately before the current node.

  • curr = self.head: Starts traversing the linked list from the head.

  • repeat = set(): Creates an empty set to store values that have already been encountered.

  • while curr:: Traverses the linked list until the end is reached.

  • if curr.data in repeat:: Checks whether the current node's value has already appeared.

    • If yes, prev.next = curr.next skips over the current node, removing the duplicate.

  • else:: If the value has not appeared before:

    • repeat.add(curr.data) adds the value to the set.

    • prev = curr updates the previous pointer to the current node.

  • curr = curr.next: Advances to the next node, whether the current node was a duplicate or not.

  • Result: The linked list contains only the first occurrence of each value, preserving the original order.


<ul><li><p><code>prev = None</code>: Keeps track of the node immediately before the current node.</p></li><li><p><code>curr = self.head</code>: Starts traversing the linked list from the head.</p></li><li><p><code>repeat = set()</code>: Creates an empty set to store values that have already been encountered.</p></li><li><p><code>while curr:</code>: Traverses the linked list until the end is reached.</p></li><li><p><code>if curr.data in repeat:</code>: Checks whether the current node's value has already appeared.</p><ul><li><p>If yes, <code>prev.next = curr.next</code> skips over the current node, removing the duplicate.</p></li></ul></li><li><p><code>else:</code>: If the value has not appeared before:</p><ul><li><p><code>repeat.add(curr.data)</code> adds the value to the set.</p></li><li><p><code>prev = curr</code> updates the previous pointer to the current node.</p></li></ul></li><li><p><code>curr = curr.next</code>: Advances to the next node, whether the current node was a duplicate or not.</p></li><li><p>Result: The linked list contains only the first occurrence of each value, preserving the original order.</p></li></ul><p></p>
2
New cards

Stacks / Recursion: Language

  • stack = Stack(): Creates an empty stack to store strings that need to be processed.

  • stack.push(""): Pushes the empty string onto the stack as the starting point.

  • results = []: Creates an empty list to store all generated strings.

  • while not stack.is_empty():: Continues processing until the stack is empty.

  • current = stack.pop(): Removes and returns the top string from the stack.

  • results.append(current): Adds the current string to the results list.

  • if len(current) < k:: Checks whether the current string is shorter than the maximum allowed length.

    • If yes, the function can generate longer strings from it.

  • for character in s:: Iterates through every character in the original string s.

  • new_string = current + character: Creates a new string by adding the character to the end of the current string.

  • stack.push(new_string): Pushes the newly generated string onto the stack so it can be processed later.

  • return results: Returns the list of all generated strings.


<ul><li><p><code>stack = Stack()</code>: Creates an empty stack to store strings that need to be processed.</p></li><li><p><code>stack.push("")</code>: Pushes the empty string onto the stack as the starting point.</p></li><li><p><code>results = []</code>: Creates an empty list to store all generated strings.</p></li><li><p><code>while not stack.is_empty():</code>: Continues processing until the stack is empty.</p></li><li><p><code>current = stack.pop()</code>: Removes and returns the top string from the stack.</p></li><li><p><code>results.append(current)</code>: Adds the current string to the results list.</p></li><li><p><code>if len(current) &lt; k:</code>: Checks whether the current string is shorter than the maximum allowed length.</p><ul><li><p>If yes, the function can generate longer strings from it.</p></li></ul></li><li><p><code>for character in s:</code>: Iterates through every character in the original string <code>s</code>.</p></li><li><p><code>new_string = current + character</code>: Creates a new string by adding the character to the end of the current string.</p></li><li><p><code>stack.push(new_string)</code>: Pushes the newly generated string onto the stack so it can be processed later.</p></li><li><p><code>return results</code>: Returns the list of all generated strings.</p></li></ul><p></p>
3
New cards

BST: Width

  • if self.root is None:: Checks whether the tree is empty. If it is, returns 0 because there are no nodes.

  • queue = [self.root]: Creates a queue containing the root node. The queue is used to traverse the tree level by level.

  • max_width = 0: Initializes a variable to keep track of the largest number of nodes found at any level.

  • while queue:: Continues traversing the tree until the queue is empty.

  • level_size = len(queue): Counts how many nodes are currently in the queue. This represents the number of nodes at the current level.

  • if level_size > max_width:: Checks whether the current level has more nodes than the previous maximum.

    • If yes, updates max_width = level_size.

  • for i in range(level_size):: Processes exactly the nodes belonging to the current level.

  • curr = queue.pop(0): Removes the first node from the queue (FIFO: First In, First Out).

  • if curr.left is not None:: Checks whether the current node has a left child.

    • If yes, adds the left child to the end of the queue.

  • if curr.right is not None:: Checks whether the current node has a right child.

    • If yes, adds the right child to the end of the queue.

  • return max_width: Returns the largest number of nodes found at any level.


<ul><li><p><code>if self.root is None:</code>: Checks whether the tree is empty. If it is, returns <code>0</code> because there are no nodes.</p></li><li><p><code>queue = [self.root]</code>: Creates a queue containing the root node. The queue is used to traverse the tree level by level.</p></li><li><p><code>max_width = 0</code>: Initializes a variable to keep track of the largest number of nodes found at any level.</p></li><li><p><code>while queue:</code>: Continues traversing the tree until the queue is empty.</p></li><li><p><code>level_size = len(queue)</code>: Counts how many nodes are currently in the queue. This represents the number of nodes at the current level.</p></li><li><p><code>if level_size &gt; max_width:</code>: Checks whether the current level has more nodes than the previous maximum.</p><ul><li><p>If yes, updates <code>max_width = level_size</code>.</p></li></ul></li><li><p><code>for i in range(level_size):</code>: Processes exactly the nodes belonging to the current level.</p></li><li><p><code>curr = queue.pop(0)</code>: Removes the first node from the queue (FIFO: First In, First Out).</p></li><li><p><code>if curr.left is not None:</code>: Checks whether the current node has a left child.</p><ul><li><p>If yes, adds the left child to the end of the queue.</p></li></ul></li><li><p><code>if curr.right is not None:</code>: Checks whether the current node has a right child.</p><ul><li><p>If yes, adds the right child to the end of the queue.</p></li></ul></li><li><p><code>return max_width</code>: Returns the largest number of nodes found at any level.</p></li></ul><p></p>
4
New cards

n-queens

HW 1: N-Queens

  • Checks whether queen placements are valid (no same row or diagonal).

  • Uses a dictionary to store queen positions.

  • Uses a stack to track which rows to try.

  • If a placement works, moves to the next column.

  • If no placement works, backtracks by removing the previous queen and trying another row.

  • Returns the completed solution or None if no solution exists.


5
New cards

shakespeare

  • Uses linked lists to store words grouped by word length.

  • Each node stores a word, its frequency, and a pointer to the next node.

  • find_frequency() increments a word's frequency or adds it if it is new.

  • sort() uses insertion sort to order words by highest frequency, then alphabetically.

  • Reads each query's word length and rank, then returns the word at that position or "-" if none exists.


6
New cards

triangles

  • The Triangle class stores the three side lengths.

  • getTriangleClass() checks that there are exactly three sides, all sides are positive, and the triangle inequality is satisfied.

  • Uses a set to count distinct side lengths:

    • 1 distinct length = equilateral

    • 2 distinct lengths = isosceles

    • 3 distinct lengths = scalene

  • Returns "invalid" if the sides cannot form a valid triangle.

  • The main program reads side lengths from an input file, classifies each triangle, and writes the results to an output file.

  • If a line has too few sides, the IndexError handler writes "-".