This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer