Skip to content

Instantly share code, notes, and snippets.

View rjvitorino's full-sized avatar

Ricardo Vitorino rjvitorino

View GitHub Profile
@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!.
@rjvitorino
rjvitorino / white_elephant.py
Created December 16, 2024 13:07
Cassidy's interview question of the week: a white elephant gift exchange class that simulates the game. It generates a sequence of random but valid gift-opening and gift-stealing moves for n participants, tracks steal counts and frozen gifts, and ends the game when everyone has a gift.
from typing import Dict, Optional
import random
class WhiteElephantGame:
"""
A simulation of the White Elephant Gift Exchange game. https://www.whiteelephantrules.com/
Overview:
The game involves players taking turns to either open a new gift from a pool of unopened gifts
@rjvitorino
rjvitorino / wrap_gifts.py
Created December 10, 2024 11:16
Cassidy's interview question of the week: a function that finds the maximum number of gifts that can be wrapped using a single strip of wrapping paper of a given width
from typing import Sequence
from array import array
from itertools import accumulate
def wrap_gifts(gift_lengths: Sequence[int], paper_width: int) -> int:
"""
Find the maximum number of gifts that can be wrapped using a single strip of wrapping paper.
Args:
@rjvitorino
rjvitorino / roll_call.py
Created December 2, 2024 11:05
Cassidy's interview question of the week: a function that reverses the names in a list and puts them in alphabetical order!
from typing import List
def roll_call(names: List[str]) -> List[str]:
"""
Reverses each name in the input list, then sorts the names alphabetically.
Args:
names (List[str]): A list of names to reverse and sort.
@rjvitorino
rjvitorino / max_the_stock.py
Created November 19, 2024 10:52
Cassidy's interview question of the week: a function that determines the maximum profit one can achieve by buying and selling stock once, given an array of integers representing the stock prices of a company in chronological order
from typing import List
def max_the_stock(prices: List[int]) -> int:
"""
Determine the maximum profit achievable by buying and selling a stock once.
Args:
prices (List[int]): A list of integers representing stock prices in chronological order.
@rjvitorino
rjvitorino / see_buildings_left.py
Created November 11, 2024 11:07
Cassidoo's interview question of the week: a function that given a list of integers representing the heights of buildings, returns the maximum number of buildings that can be seen when looking from the left.
from typing import List
def see_buildings_left(buildings: List[int]) -> int:
"""
Calculate the number of buildings visible from the left.
A building is visible if it is taller than all the buildings to its left.
Args:
@rjvitorino
rjvitorino / group_anagram.py
Created November 4, 2024 13:10
Cassidoo's interview question of the week: a function that given an array of strings, groups the anagrams together.
from collections import defaultdict
from typing import List
def group_anagrams(words: List[str]) -> List[List[str]]:
"""Groups an array of strings into lists of anagrams.
Args:
words (List[str]): List of words to group by anagrams.
@rjvitorino
rjvitorino / yahtzee_round.py
Last active November 4, 2024 12:54
Cassidoo's interview question of the week: a function that implements a round of the game Yahtzee, where 5 dice are randomly rolled, and the function returns what options the user has to score more than 0 points.
import random
from collections import Counter
from typing import List, Dict
def roll_dice(num_dice: int = 5) -> List[int]:
"""
Rolls a specified number of dice and returns their values as a list.
Args:
@rjvitorino
rjvitorino / get_rss.py
Created October 25, 2024 22:33
Cassidoo's interview question of the week: a function that takes in an RSS feed URL, and returns the title of and link to the the original feed source
import xml.etree.ElementTree as ET
from urllib.request import urlopen, Request
from urllib.error import URLError
from typing import Optional
# Attempt to import feedparser; set flag based on availability
try:
import feedparser
USE_FEEDPARSER = True