Skip to content

Instantly share code, notes, and snippets.

View rjvitorino's full-sized avatar

Ricardo Vitorino rjvitorino

View GitHub Profile
@rjvitorino
rjvitorino / only_evens.py
Last active July 13, 2024 17:52
Cassidoo's interview question of the week: a function that takes an array of integers and returns a new array containing only the even numbers, and sorted.
from typing import List
def only_evens(numbers: List[int]) -> List[int]:
"""
Returns a sorted list of even numbers from the input list.
Args:
numbers (list of int): List of integers.
Returns:
list of int: Sorted list of even integers.
@rjvitorino
rjvitorino / four_sum.py
Last active July 13, 2024 17:53
Cassidoo's interview question of the week: a function that takes an array of integers and a target sum, and returns all unique quadruplets [a, b, c, d] in the array such that a + b + c + d = target
from itertools import combinations
from typing import List
def four_sum(nums: List[int], target: int) -> List[List[int]]:
"""
Finds all unique quadruplets in the list that sum up to the target value.
Args:
nums (List[int]): The list of integers.
@rjvitorino
rjvitorino / sort_vowels.py
Last active July 13, 2024 17:54
Cassidoo's interview question of the week: a function that takes a list of names and returns the names sorted by the number of vowels in each name in descending order. If two names have the same number of vowels, sort them alphabetically.
from typing import List
def count_vowels(name: str) -> int:
"""Helper function to count the number of vowels in a name."""
vowels = "aeiouAEIOU"
return sum(1 for char in name if char in vowels)
def clean_names(names: List[str]) -> None:
@rjvitorino
rjvitorino / unique_sum.py
Last active July 13, 2024 18:01
Cassidy's interview question of the week (20240401 - April fools!): Given an array of numbers, add all of the values together but only if the number does not repeat a digit.
def has_unique_digits(number: int) -> bool:
"""
Check if the given number has all unique digits.
Args:
number (int): The number to check.
Returns:
bool: True if all digits are unique, False otherwise.
"""
@rjvitorino
rjvitorino / daily_temperatures.py
Last active October 8, 2024 14:45
Cassidoo's interview question of the week: a function that takes an array of daily temperatures and returns an array where each element is the number of days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
from typing import List, Tuple
def daily_temperatures(temperatures: List[int]) -> List[int]:
"""
Calculates, for each day, the number of days expected until a warmer temperature.
If there is no future day for which a temperature is warmer, a 0 is returned.
:param temperatures: List of daily temperatures.
:return: List where each element is the number of days to wait for a warmer temperature.
@rjvitorino
rjvitorino / fruit_stand.py
Last active October 8, 2024 14:45
Cassidoo's interview question of the week: a FruitStand class that allows you to add different types of fruits with their quantities and prices, update them, and calculate the total value of all the fruits in the stand.
from typing import Dict, List
class FruitStand:
"""
The FruitStand class allows you to add different types of fruits
with their quantities and prices, update them, and calculate the
total value of all the fruits in the stand. It supports the following
functions to change the contents in the stand as well as get the stand's
total value and the list of fruits without stock:
@rjvitorino
rjvitorino / flower_planter.py
Last active July 13, 2024 18:03
Cassidoo's interview question of the week: a function that takes an array of integers representing the number of flowers planted in a line, and an integer k representing the number of additional flowers you want to plant. Return whether it's possible to plant all k flowers without planting any two flowers adjacent to each other.
from typing import List
class FlowerPlanter:
def __init__(self, garden: List[int]) -> None:
"""Initialise the garden with padded zeros at both ends."""
self.garden = [0] + garden + [0]
def has_enough_spaces(self, k: int) -> bool:
"""Check if there are enough spaces to plant k flowers without adjacent flowers."""
@rjvitorino
rjvitorino / longest_increasing_subsequence.py
Last active July 15, 2024 10:51
Cassidoo's interview question of the week: a function that given an integer array nums, return the length of the longest increasing subsequence.
from typing import List
def increasing_subsequence(nums: List[int], allow_non_consecutive: bool = True) -> int:
"""
Given an integer array nums, return the length of the longest increasing subsequence.
Args:
nums (List[int]): The input list of integers.
allow_non_consecutive (bool): If True, allows non-consecutive subsequences;
@rjvitorino
rjvitorino / word_break.py
Last active October 8, 2024 14:48
Cassidoo's interview question of the week: a function that given a string s and a list of words word_dict, determine if s can be segmented into the space-separated sequence of all the dictionary words.
import logging
from typing import List, Set, Tuple
# Configure logging, change level to DEBUG to trace all checks
logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(message)s")
def word_break(s: str, word_dict: List[str]) -> bool:
"""
Determine if a given string can be segmented into the sequence of words from the given word list.
@rjvitorino
rjvitorino / unit_conversion.py
Created July 29, 2024 10:44
Cassidoo's interview question of the week: a function that converts between metric and imperial units, breaking up the units into millimeters, centimeters, and meters for metric, and into inches and feet for imperial, up to 2 decimal places.
from typing import Union
try:
# Attempt to import the 'pint' library. If it is not available, set PINT_AVAILABLE to False.
# To install 'pint', use the following command: pip install pint
import pint
PINT_AVAILABLE = True
except ImportError:
PINT_AVAILABLE = False