Skip to content

Core Python — Intermediate (50+ Coding Exercises)

  1. Implement merge_dicts(a, b) where keys in b override a.
  2. Implement group_by_length(words) returning a dict {length: [words...]}.
  3. Implement flatten(nested) that flattens arbitrarily nested lists.
  4. Implement dedupe_by_key(items, key_fn) preserving first occurrence.
  5. Implement chunked(iterable, size) that yields chunks as lists.
  6. Implement sliding_window(seq, k) returning all length-(k) windows.
  7. Implement pairwise(seq) returning consecutive pairs.
  8. Implement top_k(nums, k) without sorting the full list.
  9. Implement median(nums) for odd/even lengths.
  10. Implement mode(nums) returning all most-frequent values.
  11. Implement parse_int(s, default=None) that returns default on failure.
  12. Implement safe_div(a, b) raising a custom exception for division by zero.
  13. Implement read_json(path) and write_json(path, obj) with UTF-8.
  14. Implement load_env(path) that parses KEY=VALUE lines into a dict.
  15. Implement a context manager timer() that prints elapsed time.
  16. Implement a context manager suppress(*exc_types) like contextlib.suppress.
  17. Implement a decorator retry(times, delay) for transient exceptions.
  18. Implement a decorator memoize(maxsize=None) for function caching.
  19. Implement a decorator validate_types using function annotations at runtime.
  20. Implement a generator fibonacci() that yields indefinitely.
  21. Implement a generator read_lines(path) that yields lines stripped of \n.
  22. Implement a custom iterator class RangeLike(start, stop, step).
  23. Implement batched_file_reader(path, batch_size) yielding lists of lines.
  24. Implement word_frequencies(text) returning a Counter-like dict.
  25. Implement most_common_words(path, n) reading from a file.
  26. Implement extract_emails(text) using regex.
  27. Implement parse_date(s) supporting multiple formats (e.g., YYYY-MM-DD, DD/MM/YYYY).
  28. Implement time_ago(dt, now) returning strings like \"5 minutes ago\".
  29. Implement stable_multi_sort(records, keys) where keys is list of (field, asc_bool).
  30. Implement unique_permutation_count(s) for a string with duplicates.
  31. Implement is_balanced_brackets(s) for ()[]{}.
  32. Implement evaluate_rpn(tokens) for Reverse Polish Notation.
  33. Implement infix_to_postfix(expr) for + - * / with parentheses.
  34. Implement evaluate_infix(expr) using your postfix converter.
  35. Implement serialize_dataclass(obj) and deserialize_dataclass(cls, data).
  36. Implement a dataclass Person(name, age) with validation in __post_init__.
  37. Implement a Point class supporting +, -, and scalar *.
  38. Implement a Vector class with len, iteration, and indexing.
  39. Implement log_setup() creating console and file log handlers.
  40. Implement tail(path, n) that returns last n lines efficiently.
  41. Implement head(path, n) returning first n lines.
  42. Implement csv_average(path, col_index) for numeric columns.
  43. Implement csv_to_dicts(path) returning list of dicts based on header row.
  44. Implement diff_files(a_path, b_path) returning added/removed lines.
  45. Implement find_duplicates(paths) returning files with identical content hashes.
  46. Implement walk_tree(path) returning all files under a directory using pathlib.
  47. Implement simple_cli() with subcommands using argparse (e.g., add, sub).
  48. Implement an in-memory key-value store with TTL and get/set/delete/cleanup.
  49. Implement rate_limiter(max_per_min) that blocks/denies after limit.
  50. Implement run_tasks_concurrently(funcs, max_workers) using concurrent.futures.
  51. Implement bounded_queue_producer_consumer() using queue.Queue.
  52. Implement merge_sorted_lists(a, b) in O(n).