All Practices
99 practices available
Filter by tags
All Practices
99 practices available
Filter by tags
Create variables of different types (string, integer, float, boolean) and print their values and types.
Write a program that prints numbers 1 to 10 using both for and while loops.
Create a function that calculates the area of a rectangle given length and width.
Practice using different types of print statements and see how they appear in the console output below.
Use list comprehension to create a list of squares of even numbers from 1 to 20.
Write a program that reads a text file, counts the number of words, and writes the count to a new file.
Create a function that safely divides two numbers and handles division by zero.
Create a generator function that yields Fibonacci numbers up to a given limit.
Create a decorator that measures and prints the execution time of a function.
Create a Vector class with dunder methods for addition, string representation, and length.
Write a function classify_number(n)
that returns 'positive even', 'positive odd', 'negative even', 'negative odd', or 'zero'.
Implement two functions: count_vowels(s)
returning the number of vowels (a, e, i, o, u) in a string (case-insensitive), and reverse_words(s)
that reverses the order of words in a sentence.
Write list_stats(nums)
that returns a tuple of (min, max, average) for a non-empty list of numbers.
Create char_frequency(s)
that returns a dictionary mapping each alphabetic character to its frequency, ignoring case and spaces.
Implement unique_words(text)
that returns a set of unique, lower-cased words from a sentence. Words are separated by whitespace and stripped of punctuation (.,!?:;).
Write reverse_string(s)
that returns the reverse of a string and first_three(nums)
that returns the first three elements of a list (or the whole list if it has fewer than three).
Implement fizzbuzz(n)
that returns a list of values from 1..n, replacing multiples of 3 with 'Fizz', 5 with 'Buzz', and both with 'FizzBuzz'.
Use a dictionary comprehension to create a mapping of even numbers from 1..n to their squares.
Write evens_squared(nums)
that returns a list of squares of only the even numbers using filter
and map
(or comprehensions).
Implement sort_people(people)
where people
is a list of dicts with keys name
and age
. Return a new list sorted by age descending, then name ascending.
Write extract_emails(text)
that returns a list of email addresses found in the input string.
Implement days_between(d1, d2)
where dates are 'YYYY-MM-DD'. Return the absolute number of days between them.
Write to_json(obj)
that returns a JSON string with sorted keys and from_json(s)
that parses it back to a Python object.
Create a BankAccount
class with methods deposit
, withdraw
(raise ValueError
if insufficient funds), and a read-only balance
property.
Implement a Timer
context manager that measures elapsed seconds in an attribute elapsed
.
Write grouper(iterable, n, fillvalue=None)
that collects data into fixed-length chunks using only stdlib tools.
Create run_tasks(nums)
that doubles numbers concurrently using asyncio with gather
.
Use concurrent.futures.ThreadPoolExecutor
to implement sum_squares_threaded(nums)
that returns the sum of squares.
Implement fib(n)
using @lru_cache
and verify fib(10) == 55
.
Create a User
dataclass with fields name: str
and age: int
, enabling ordering by age then name.
Implement http_status_category(code)
using match/case
that returns one of: 'informational', 'success', 'redirection', 'client-error', 'server-error', or 'unknown'.
Implement sum_list(nums)
without using built-in sum()
.
Write fact(n)
that returns n! using a loop (n>=0).
Implement is_prime(n)
returning True if n is prime (n>=2).
Implement gcd(a,b)
using Euclid's algorithm.
Implement lcm(a,b)
using your gcd
.
Write is_palindrome(s)
ignoring case and spaces.
Implement are_anagrams(a,b)
(case-insensitive, ignore spaces).
Write second_largest(nums)
for list with ≥2 unique values.
Implement sum_digits(n)
that sums digits of non-negative integer.
Write to_binary(n)
returning binary string without '0b' prefix.
Implement all_unique(s)
that returns True if no characters repeat.
Implement word_count(s)
returning number of whitespace-separated words.
Write title_case(s)
capitalizing first letter of each word without using str.title()
.
Implement manual_max(nums)
without using max()
.
Implement dedupe_order(seq)
removing duplicates while keeping first occurrence order.
Create zip_to_dict(keys,values)
mapping keys to values (truncate to shortest).
Write index_of(seq, target)
returning first index or -1 using enumerate
.
Implement merge_sorted(a,b)
merging two ascending lists.
Write transpose(m)
for a rectangular matrix (list of lists).
Implement flatten_once(ll)
that flattens one level of nested lists.
Implement caesar(s, shift)
shifting only letters (wrap, keep case).
Write chunks(seq,n)
yielding sublists of size n (last may be shorter).
Implement running_sum(nums)
returning cumulative sums.
Write count_sub(s, sub)
that counts non-overlapping occurrences.
Implement word_freq(s)
returning a Counter-like dict for words (lower-case).
Create Point(x,y) and distance(a,b)
Euclidean distance.
Use deque to rotate a list by k steps to the right.
Implement top_k(nums, k)
returning k largest numbers.
Implement insert_index(a,x)
returning index to keep list sorted (bisect_left).
Write permute(s)
that returns all permutations of a short string.
Implement rle(s)
returning list of (char, count) using groupby
.
Implement product(nums)
using functools.reduce
.
Implement recursive bsearch(a,x)
returning index or -1 (a sorted).
Implement matmul(A,B)
for square matrices (lists of lists).
Write list_by_ext(paths, ext)
filtering names ending with ext.
Implement is_ipv4(s)
that validates IPv4 addresses (0-255 per octet).
Given CSV text with header 'value', implement sum_csv_values(text)
.
Implement mean_median(nums)
using statistics
module.
Implement seeded_shuffle(seq, seed)
returning same permutation for same seed.
Create Thermo
with celsius property (cannot be < -273.15). Provide fahrenheit getter.
Create TodoList
dataclass with items:list default []. Add add(item)
method.
Use accumulate
to compute cumulative sums.
Implement reformat_date(s)
from YYYY-MM-DD to DD/MM/YYYY.
Write set_ops(a,b)
returning (union, intersection, difference a-b).
Implement merge_dicts(a,b)
using |
operator.
Implement has_keys(d, req)
returning True if all required keys are present.
Create an Enum Status
with PENDING, OK, FAIL and function is_ok(s)
.
Create @repeat(n)
that calls a function n times and returns last result.
Implement @memoize
caching by args tuple.
Create class Evens(n)
iterating 0,2,4..<=n.
Implement LRU(capacity)
with get/put
using OrderedDict
.
Implement Trie with insert
, search
, starts_with
.
Implement toposort(edges)
where edges is list of (u,v) for DAG.
Parse 'YYYY-MM-DD' using named groups and return tuple(int,int,int).
Write sha256_hex(s)
returning lowercase hex digest.
Parse simple INI text and return value for section/key.
Use Decimal
to sum [19.99, 0.01] exactly and quantize to 2 dp.
Add two rational numbers using Fraction
and return string 'a/b'.
Create square
using partial(pow, x, 2)
style and compute for list.
Return all 2-combinations of a list.
Sort list of dicts by key 'score' descending using itemgetter
.
Convert list of strings to ints, skipping invalid entries.
Create frozen Point dataclass usable as set key.
Run coroutines with max concurrency of 2 using Semaphore
.
Implement maybe_fast(coro, t)
that returns 'timeout' if coro exceeds t sec.
Use @cached_property
to cache expensive computation.
Validate fields in __post_init__
(age>=0).
Compute Cartesian product of two lists.