Skip to content

Instantly share code, notes, and snippets.

View rjvitorino's full-sized avatar

Ricardo Vitorino rjvitorino

View GitHub Profile
@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
@rjvitorino
rjvitorino / holidays.py
Last active January 6, 2025 15:50
Cassidy's interview question of the week: given a year, a script that determines weekdays and dates for US holidays (New Year's, Easter, Memorial Day, Independence Day, Thanksgiving, Christmas)
"""
Holiday date calculation module.
This module provides functionality to calculate dates and weekdays for various holidays
in the Gregorian calendar, including both fixed-date holidays (like Christmas and New
Years Day) and floating holidays (like Easter and Thanksgiving).
"""
from datetime import date, timedelta
from enum import Enum
@rjvitorino
rjvitorino / factorial_trailing_zeros.py
Created December 24, 2024 16:53
Cassidy's interview question of the week: a function to determine how many trailing zeros are in n!.
def count_trailing_zeros_in_factorial(n: int) -> int:
"""
Calculate the number of trailing zeros in n! to determine how many perfectly round cookies are made.
Args:
n (int): The input number for which to calculate n! and count trailing zeros.
Returns:
int: The count of trailing zeros in n!.