Skip to content

Instantly share code, notes, and snippets.

View rjvitorino's full-sized avatar

Ricardo Vitorino rjvitorino

View GitHub Profile
@rjvitorino
rjvitorino / piano_largest_interval.py
Created March 11, 2025 23:25
Cassidy's interview question of the week: a function that takes a list of piano keys played in sequence and returns the largest interval (in semitones) between any two consecutive keys.
"""
Finds the largest interval in semitones between consecutive piano notes.
Supports both a standard library implementation and an optimized version
using the `mido` library (if installed).
USAGE:
- If `mido` is installed (`pip install mido` or `uv pip install mido`), it will use it automatically.
- If `mido` is not available, it falls back to a pure Python implementation.
"""
@rjvitorino
rjvitorino / calculate_price.py
Created March 3, 2025 10:15
Cassidy's interview question of the week: a function that, given a store's closing date, a visit date, and the original price of a product, calculates the discounted price based on a 10% weekly reduction until the store closes. If the visit date is after the closing date, the price remains unchanged.
"""
A store is going out of business and will reduce the price of all products by 10% every week leading up to the closing date.
Given the closing_date, visit_date, and the original_price of a product, this function returns the price of the product on the visit_date.
The function assumes that original_price is a positive number.
Examples:
>>> calculate_price('2025-04-01', '2025-03-03', 100) # 4 weeks of discounts
65.61
>>> calculate_price('2025-04-01', '2025-03-15', 50) # 2 weeks of discounts
40.5
@rjvitorino
rjvitorino / get_note_names.py
Last active February 24, 2025 11:40
Cassidy's interview question of the week: a function that, given a list of frequencies (in Hz), determines the closest musical note for each frequency based on the A440 pitch standard, indicating if the note is flat or sharp
import math
from typing import List
def get_note_names(frequencies: List[float]) -> List[str]:
"""Convert frequencies to musical note names using A440 pitch standard.
Takes a list of frequencies in Hz and returns their corresponding musical notes,
indicating if they are sharp or flat relative to the standard pitch.
@rjvitorino
rjvitorino / find_shield_break.py
Created February 17, 2025 15:20
Cassidy's interview question of the week: a function that, given an array of attack damages and a shield capacity for a spaceship, returns the index when cumulative damage exceeds the shield
from typing import List
from itertools import accumulate
def find_shield_break(shield_attacks: List[int], shield_capacity: int) -> int:
"""Finds the index where cumulative damage exceeds shield capacity.
Uses itertools.accumulate for efficient cumulative sum calculation.
Short-circuits when shield break is detected.
@rjvitorino
rjvitorino / nfl_numbers.py
Created February 10, 2025 11:16
Cassidy's interview question of the week: a function that returns a list of numbers that an NFL player can choose from for their uniform, given the player's position, and an array of existing numbers on the team
"""
NFL Uniform Number Management
Defines valid NFL uniform number ranges by position according to official regulations.
Provides utilities to validate and find available numbers for players.
Position Groups and Number Ranges:
Offense:
- QB (Quarterback): 1-19
- RB (Running Back): 20-49
@rjvitorino
rjvitorino / evaluate_postfix.py
Created February 3, 2025 15:21
Cassidy's interview question of the week: a function that evaluates a postfix expression (also known as Reverse Polish Notation) and returns the result
from typing import Dict, Callable, Final
from operator import add, sub, mul, floordiv
OPERATORS: Final[Dict[str, Callable[[int, int], int]]] = {
"+": add,
"-": sub,
"*": mul,
"/": floordiv,
}
@rjvitorino
rjvitorino / find_anagrams.py
Created January 27, 2025 17:15
Cassidoo's interview question of the week: given two strings, s and p, return an array of all the start indices of p's anagrams in s.
"""
String Anagrams Finder
This module provides two different implementations for finding anagrams of a pattern
string within a larger text string. It includes both a sliding window approach (efficient)
and a permutation-based approach (for educational purposes).
Example:
>>> find_anagrams_sliding_window("cbaebabacd", "abc")
[0, 6]
@rjvitorino
rjvitorino / longest_subsequence.py
Created January 20, 2025 12:46
Cassidy's interview question of the week: a function that finds the longest subsequence where the difference between consecutive elements is either 1 or -1
from typing import List
def longest_subsequence(numbers: List[int]) -> int:
"""
Find the length of the longest subsequence of consecutive integers in a list.
The difference between consecutive elements is either 1 or -1.
Args:
numbers (List[int]): A list of integers.
@rjvitorino
rjvitorino / natoify.py
Created January 16, 2025 15:42
Cassidy's interview question of the week: a translation function for the NATO phonetic alphabet
"""
NATO phonetic alphabet converter.
This module converts text into NATO phonetic alphabet
representation, handling special cases like decimal points and quotation marks.
"""
from typing import Dict, List, Optional
# Core NATO phonetic alphabet mapping
@rjvitorino
rjvitorino / permute.py
Created January 6, 2025 15:51
Cassidy's interview question of the week: a function that generates all possible permutations of a given string
from typing import List
from itertools import permutations
def permute(text: str) -> List[str]:
"""
Generate all unique permutations of the input string.
Uses Python's built-in itertools.permutations for efficient generation
of all possible arrangements of characters. Duplicate permutations