Core Python — Intermediate (50+ Coding Exercises)
- Implement
merge_dicts(a, b)where keys inboverridea. - Implement
group_by_length(words)returning a dict{length: [words...]}. - Implement
flatten(nested)that flattens arbitrarily nested lists. - Implement
dedupe_by_key(items, key_fn)preserving first occurrence. - Implement
chunked(iterable, size)that yields chunks as lists. - Implement
sliding_window(seq, k)returning all length-(k) windows. - Implement
pairwise(seq)returning consecutive pairs. - Implement
top_k(nums, k)without sorting the full list. - Implement
median(nums)for odd/even lengths. - Implement
mode(nums)returning all most-frequent values. - Implement
parse_int(s, default=None)that returnsdefaulton failure. - Implement
safe_div(a, b)raising a custom exception for division by zero. - Implement
read_json(path)andwrite_json(path, obj)with UTF-8. - Implement
load_env(path)that parsesKEY=VALUElines into a dict. - Implement a context manager
timer()that prints elapsed time. - Implement a context manager
suppress(*exc_types)likecontextlib.suppress. - Implement a decorator
retry(times, delay)for transient exceptions. - Implement a decorator
memoize(maxsize=None)for function caching. - Implement a decorator
validate_typesusing function annotations at runtime. - Implement a generator
fibonacci()that yields indefinitely. - Implement a generator
read_lines(path)that yields lines stripped of\n. - Implement a custom iterator class
RangeLike(start, stop, step). - Implement
batched_file_reader(path, batch_size)yielding lists of lines. - Implement
word_frequencies(text)returning aCounter-like dict. - Implement
most_common_words(path, n)reading from a file. - Implement
extract_emails(text)using regex. - Implement
parse_date(s)supporting multiple formats (e.g.,YYYY-MM-DD,DD/MM/YYYY). - Implement
time_ago(dt, now)returning strings like\"5 minutes ago\". - Implement
stable_multi_sort(records, keys)wherekeysis list of(field, asc_bool). - Implement
unique_permutation_count(s)for a string with duplicates. - Implement
is_balanced_brackets(s)for()[]{}. - Implement
evaluate_rpn(tokens)for Reverse Polish Notation. - Implement
infix_to_postfix(expr)for+ - * /with parentheses. - Implement
evaluate_infix(expr)using your postfix converter. - Implement
serialize_dataclass(obj)anddeserialize_dataclass(cls, data). - Implement a
dataclassPerson(name, age)with validation in__post_init__. - Implement a
Pointclass supporting+,-, and scalar*. - Implement a
Vectorclass withlen, iteration, and indexing. - Implement
log_setup()creating console and file log handlers. - Implement
tail(path, n)that returns lastnlines efficiently. - Implement
head(path, n)returning firstnlines. - Implement
csv_average(path, col_index)for numeric columns. - Implement
csv_to_dicts(path)returning list of dicts based on header row. - Implement
diff_files(a_path, b_path)returning added/removed lines. - Implement
find_duplicates(paths)returning files with identical content hashes. - Implement
walk_tree(path)returning all files under a directory usingpathlib. - Implement
simple_cli()with subcommands usingargparse(e.g.,add,sub). - Implement an in-memory key-value store with TTL and
get/set/delete/cleanup. - Implement
rate_limiter(max_per_min)that blocks/denies after limit. - Implement
run_tasks_concurrently(funcs, max_workers)usingconcurrent.futures. - Implement
bounded_queue_producer_consumer()usingqueue.Queue. - Implement
merge_sorted_lists(a, b)in O(n).