My solution didn’t think about the hidden test cases seriously, please refer to the standard solution.
Exercise 1
The doctest
module in Python is a tool for testing code by comparing the output of code snippets written in a program’s docstrings to expected results. It allows developers to write simple test cases as part of their documentation and automatically validate that code behaves as documented.
This exercise requires interpreting a pattern where each argument in vertical_bars()
represents the number of asterisks (*)
to print in a specific “column” position across multiple lines. Here’s a breakdown of the observed pattern and how to implement it:
- The biggest number in the input list determines the number of lines to print.
- For each row, iterate over the input list and print the number of asterisk.
My Solution:
1 | # Note that NONE OF THE LINES THAT ARE OUTPUT HAS TRAILING SPACES. |
Standard Solution:
1 | if x: |
Exercise 2
This exercise ask us to find the gaps between successive members of a list and output them in a specific format. The gaps are calculated as the difference between two successive elements where the second element is strictly greater than the first. The output should be sorted by gap value and then by the start of the gap.
So in this exercise, we can use:
- A dictionary to store the gaps and corresponding pairs of numbers.
- Two pointers to iterate through the list and calculate the gaps.
My Solution:
1 | # You can assume that the argument L to positive_gaps() |
Optimize Solution:
we can optimize further by skipping over sequences of consecutive identical numbers, as they do not contribute to any positive gaps. Consecutive identical values (like 2, 2, 2) have zero gaps between them, so we can skip ahead to the next distinct value each time we encounter a repeat.
1 | # Dictionary to store gaps and corresponding pairs |
Standard Solution:
1 | gaps = defaultdict(set) |
Exercise 3
his problem requires solving equations of the form $x+y=z$, where each part (x, y, and z) consists of a mix of digits and underscores (_)
. The underscores represent a missing, single-digit number (0–9), and all underscores must be replaced by the same digit across the entire equation.
Standard Solution:
1 | def solve(equation): |
Explanation:
Explanation of Each Step
Check for Underscores:
contains_ = '_'
in equation: Determines if there are underscores to replace, which helps decide whether to print solutions for cases without underscores.Iterate Through Possible Digits (0–9):
for digit in '0123456789':
: This loop iterates over each possible digit that can replace_
.Replace Underscores:
eq = equation.replace('_', digit)
: Replaces all underscores with the current digit inequation
, creating a new equation string without underscores.Parse Equation Parts:
left, right = eq.split('='):
Splits the equation into the left side (x + y) and the right side (z).left_1, left_2 = left.split('+')
: Further splits the left side intox
andy
.Each of these parts (
left_1
,left_2
, andright
) is converted to integers to perform arithmetic validation.
Validate the Equation:
if
left_1 + left_2 == right
: Checks ifx + y
equalsz
. If so, the equation is valid with this digit replacement.The solution is printed with print(f’{left_1} + {left_2} = {right}’) if it’s either the first solution or if the equation contains underscores.
No Solution Case:
If no valid solution is found after testing all digits, print(‘No solution!’) is called.
Exercise 4
My Solution:
1 | __rectangle = [['' for _ in range(width)] for _ in range(height)] |
Standard Solution:
1 | start = ord(starting_from) - ord('A') |
Explanation:
Character Offset Calculation:
It starts by converting the starting_from character to its corresponding numerical position (using ASCII values).
Then, it calculates an offset for each character, determining which letter should be printed based on the current row (i) and column (j).
Zigzag Filling:
The columns are filled zigzag-style:
- Even columns are filled top-down.
- Odd columns are filled bottom-up.
This zigzag pattern is achieved through a condition:
offset + height - i - 1 if j % 2 else offset + i
. This ensures the characters in each column switch directions depending on whether the column index (j
) is even or odd.
Efficient Calculation:
- Instead of using nested loops to iterate through and assign each character step-by-step, the solution leverages mathematical modular arithmetic (
offset % 26
) to ensure that character sequences wrap around from ‘Z’ back to ‘A’. - This method allows for the direct printing of characters, making it memory efficient since it doesn’t need to store the whole matrix beforehand.
- Instead of using nested loops to iterate through and assign each character step-by-step, the solution leverages mathematical modular arithmetic (
Exercise 5:
This problem revolves around finding paths in a 10x10 grid (of dimensions dim = 10
). The paths are allowed to connect cells in specific directions, and they must start from one value at the top of the grid and end at another value at the bottom. Here’s a breakdown of the question and solution.
- Grid Generation
- Path Finding
- Displaying Results
1 | # You can assume that paths() is called with an integer as first |
Exercise 6:
Problem Description:
The function word_pairs()
takes a string of uppercase letters, called available_letters
, and outputs all possible pairs of distinct words from the dictionary.txt
file that can be formed using all of the available_letters
. Here are some key requirements:
Word Pair Requirements:
- Each word pair should use all of the letters in available_letters without any leftover.
- Each letter must be used exactly as many times as it appears in available_letters.
- A word cannot be used more than once in a pair.
- The second word in each pair should be lexicographically after the first word.
Output Order:
- Pairs should be printed with the first word in lexicographic order.
- For each first word, the corresponding second word should also be in lexicographic order.
Standard Solution:
1 | # You can assume that word_pairs() is called with a string of |