Skip to content

Instantly share code, notes, and snippets.

View rjvitorino's full-sized avatar

Ricardo Vitorino rjvitorino

View GitHub Profile
@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
@rjvitorino
rjvitorino / generate_circle.py
Created October 20, 2024 21:15
Cassidoo's interview question of the week: a function that generates a valid SVG string for a circle given its radius, center position, and color.
from xml.sax.saxutils import escape
def generate_circle(radius: int, center: tuple[int, int], color: str) -> str:
"""
Generates a valid SVG string for a circle.
Args:
radius (int): The radius of the circle (must be positive).
center (tuple[int, int]): The (x, y) center position of the circle.
@rjvitorino
rjvitorino / missing_ingredients.py
Last active October 8, 2024 14:47
Cassidy's interview question of the week: a function that, given a list of ingredients needed for a recipe (represented as strings), and a list of ingredients available in the pantry, returns the minimum number of additional ingredients one needs to buy to make the recipe
from collections import Counter
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import List
@dataclass
class Ingredient:
"""
Class to represent an ingredient and its expiration date.
@rjvitorino
rjvitorino / split.py
Last active October 8, 2024 14:47
Cassidy's interview question of the week: implementation of String split() function in my preferred programming language (Python)
from typing import List, Optional
class StringSplitter:
@staticmethod
def split(string: str, delimiter: Optional[str] = None) -> List[str]:
"""
Splits the input string by the specified delimiter.
Args:
@rjvitorino
rjvitorino / min_laundry_loads.py
Created September 23, 2024 09:48
Cassidy's interview question of the week: a function to sort laundry items into the minimum number of loads, where items of the same colour can be washed together, and some different fabric types cannot be mixed together
from collections import defaultdict
from typing import List, Tuple
# Define the type alias for better readability (colour and fabric tuple)
ClothingItem = Tuple[str, str]
def min_laundry_loads(items: List[ClothingItem]) -> int:
"""
This function takes a list of clothing items, each represented by a tuple
@rjvitorino
rjvitorino / ways_to_score.py
Created September 16, 2024 11:03
Cassidy's interview question of the week: a function to determine the number of unique ways an American Football team can achieve exactly n points
def ways_to_score(total_points: int) -> int:
"""
Calculate the number of unique ways in American Football 🏈 to score exactly total_points
using combinations of touchdowns (6 points), field goals (3 points), and safeties (2 points).
:param total_points: An integer representing the total points to score.
:return: The number of unique ways to score exactly total_points.
"""
# ways[i] will hold the number of ways to score exactly i points.