Skip to content

Instantly share code, notes, and snippets.

View rjvitorino's full-sized avatar

Ricardo Vitorino rjvitorino

View GitHub Profile
@rjvitorino
rjvitorino / squares.py
Created August 5, 2024 09:35
Cassidoo's interview question of the week: a function that should take one argument n, a positive integer, and return the sum of all squared positive integers between 1 and n, inclusive.
from typing import Union
def squares(n: int) -> Union[int, None]:
"""
Calculate the sum of all squared positive integers from 1 to n, inclusive.
Args:
n (int): A positive integer.
@rjvitorino
rjvitorino / find_unused.py
Last active October 8, 2024 14:45
Cassidoo's interview question of the week: a function that given an array of logs and variable assignments, return a list of all unused variables.
from collections import defaultdict
from typing import List, Set
def find_unused(lines: List[str]) -> List[str]:
"""
Identify variables that are assigned but never used.
Examples:
```find_unused(["a = 1", "b = a", "c = 2", "log(b)"]) # ["c"]```
@rjvitorino
rjvitorino / calculate_execution_times.py
Last active October 8, 2024 14:44
Cassidoo's interview question of the week: a function that given an array of logs, where each log consists of a function name, a timestamp, and an event (either start or end), returns the total execution time for each function
from collections import defaultdict
from typing import Dict, List
class LogEntry:
"""
Represents a log entry for a function call.
Attributes:
name (str): The name of the function.
@rjvitorino
rjvitorino / equal_letters_digits.py
Last active August 26, 2024 14:47
Cassidy's interview question of the week: a function that finds the longest substring in the input string `s` where the number of distinct letters equals the number of distinct digits
from typing import Set
def equal_letters_digits(s: str) -> str:
"""
Finds the longest substring in the input string `s` where the number of distinct letters equals the number of distinct digits.
If there are multiple substrings with the same length, return the one that appears first.
:param s: The input string containing letters and digits.
:return: The longest valid substring according to the conditions, or an empty string if no such substring exists.
@rjvitorino
rjvitorino / max_pairs.py
Last active September 3, 2024 08:49
Cassidy's interview question of the week: a function to calculate the maximum number of matching shoe pairs in an array of strings
from collections import defaultdict
from typing import List
def max_pairs(shoes: List[str]) -> int:
"""
Calculate the maximum number of matching pairs of shoes that can be formed.
:param shoes: A list of strings where each string represents a shoe with
its type ('L' for left or 'R' for right) and its size.
@rjvitorino
rjvitorino / min_rows.py
Created September 9, 2024 14:08
Cassidy's interview question of the week: a function to calculate the minimum number of rows required to seat everyone such that no group is split
from typing import List
def min_rows(groups: List[int], row_size: int) -> int:
"""
Calculate the minimum number of rows required to seat all groups
without splitting them across rows.
:param groups: List of integers where each integer represents a group size.
:param row_size: Integer representing the maximum number of people that can sit in a row.
@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.
@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 / 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 / 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.