Python coding challenges

Small problems to practise what you've learned. Write your solution in the browser, run the examples, then submit it to check it against every test. No sign-up needed.

0of 104
Beginner0/28
Intermediate0/39
Advanced0/26
Expert0/11
Status Challenge Topics Level XP
1. Reverse the words Return the words of a sentence in reverse order. strings, lists Beginner 25
2. Add two numbers Read two whole numbers and print their sum. input, math Beginner 25
3. FizzBuzz List the numbers from 1 to n as strings, with Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for both. loops, conditions Beginner 25
4. Celsius to Fahrenheit Convert a temperature from Celsius to Fahrenheit. math Beginner 25
5. Even or odd Read a whole number and print whether it is even or odd. input, conditions Beginner 25
6. Count the vowels Count the vowels in a piece of text, in upper or lower case. strings, loops Beginner 25
7. Largest of three Return the largest of three numbers without using max(). conditions Beginner 25
8. Add up a list Add up the numbers in a list without using sum(). lists, loops Beginner 25
9. Say hello Read a name and print a greeting with it. input, strings Beginner 25
10. Letter grade Turn a test score from 0 to 100 into a letter grade from A to F. conditions Beginner 25
11. Palindrome check Check whether a phrase reads the same forwards and backwards, counting only its letters and digits and ignoring case. strings Beginner 25
12. Countdown Count down from a number to 1, then print Liftoff! loops, input Beginner 25
13. Times table Print the 1 to 10 times table for a number. loops, input Beginner 25
14. Word frequency Count how often each word appears in a sentence. dictionaries, strings Intermediate 50
15. Remove duplicates Remove repeated items from a list, keeping the first of each in order. lists, sets Intermediate 50
16. Second largest Find the second largest different number in a list, or None if there isn't one. lists Intermediate 50
17. Flatten a list Turn a list of lists into one list. lists, comprehensions Intermediate 50
18. Anagrams Check whether two phrases are anagrams: the same letters, the same number of times, ignoring case, spaces and punctuation. strings, sorting Intermediate 50
19. Caesar cipher Encode a message by shifting each letter along the alphabet, as a Caesar cipher does. strings, loops Intermediate 50
20. Merge two sorted lists Merge two sorted lists into one sorted list without sorting again. lists, two pointers Intermediate 50
21. Squares of the evens Square the even numbers of a list in one comprehension. comprehensions Intermediate 50
22. Safe division Divide two numbers, returning None instead of crashing when dividing by zero. exceptions Intermediate 50
23. Initials Turn a full name into its initials, like A.L. for Ada Lovelace. strings Intermediate 50
24. Run-length encoding Compress text by writing each run of a repeated character as the character and its count, like a3b1c2. strings, loops Intermediate 50
25. Group words by first letter Group words into a dictionary by their first letter, each group in alphabetical order, ignoring case. dictionaries, sorting Intermediate 50
26. Balanced brackets Check that every bracket in a string is closed by its partner, in the right order. stacks, strings Advanced 100
27. The nth Fibonacci number Find the nth Fibonacci number, fast enough for n up to 90. recursion, dynamic programming Advanced 100
28. Binary search Find the first place a number appears in a sorted list, repeats allowed, by halving the search at each step. searching, loops Advanced 100
29. Transpose a matrix Swap the rows and columns of a grid of numbers. lists, comprehensions Advanced 100
30. Roman numerals Turn a Roman numeral such as XIV into a number. dictionaries, strings Advanced 100
31. Count the primes Count the prime numbers up to n, fast enough for n up to 200,000. math, loops Advanced 100
32. Longest increasing subsequence Find the length of the longest strictly increasing subsequence: numbers picked from a list in order, each bigger than the last. dynamic programming, lists Expert 150
33. All the permutations List every ordering of the letters of a word, without repeats, in alphabetical order. backtracking, recursion, strings Expert 150
34. Edit distance Count the fewest single-letter edits that turn one word into another. dynamic programming, strings Expert 150
35. Leap year Decide whether a year is a leap year, using the full rule for the years that end a century. conditions, math Beginner 25
36. Triangle type Say whether three lengths make an equilateral, isosceles or scalene triangle, or no triangle at all. conditions Beginner 25
37. Format a duration Turn a number of seconds into hours, minutes and seconds, written like a clock: 1:01:05. math, strings Beginner 25
38. The missing number Find the one number missing from a list that should hold every number from 0 to n. math, lists Beginner 25
39. Mask all but the last four Hide every character but the last four, as receipts do with account numbers. strings Beginner 25
40. Pangram Check whether a sentence uses every letter of the alphabet at least once. sets, strings Beginner 25
41. First unique character Find the position of the first character that appears only once in a word. dictionaries, strings Beginner 25
42. Count the rises Count how many times a list of readings goes up from one reading to the next. lists, loops Beginner 25
43. Collatz steps Count the steps a number takes to reach 1 when you halve the even numbers and treble and add one to the odd. loops, math Beginner 25
44. Where does the robot end up? Follow a robot's up, down, left and right moves on a grid and return where it stops, as an (x, y) pair. simulation, strings Beginner 25
45. Merge settings Merge any number of settings dictionaries into one, each later one overriding the ones before it. dictionaries Beginner 25
46. Add up the valid numbers Add up the texts in a list that are numbers, skipping the ones that aren't, with try and except. exceptions, strings Beginner 25
47. Days between two dates Count the days between two dates written as YYYY-MM-DD, leap years and all. math, parsing Beginner 25
48. Guess the number Judge a guessing game: read the secret number, then guesses until one is right, saying too high or too low. input, simulation Beginner 25
49. Print a receipt Read items and prices and print them as a tidy receipt, names on the left, prices lined up on the right. input, strings Beginner 25
50. Two sum Find the two numbers in a list that add up to a target, in one pass, with a dictionary. dictionaries, lists Intermediate 50
51. Word pattern Check whether a sentence's words follow a pattern of letters, one letter for each word and one word for each letter. dictionaries, strings Intermediate 50
52. Product of the others For each item of a list, multiply together all the other items, without dividing. lists Intermediate 50
53. Is it a subsequence? Check whether the letters of one word appear in another in the same order, gaps allowed. two pointers, strings Intermediate 50
54. Reverse only the letters Reverse the letters of a string while every other character stays exactly where it was. two pointers, strings Intermediate 50
55. Best k in a row Find the biggest total of k numbers in a row in a list, sliding a window instead of re-adding. sliding window, lists Intermediate 50
56. Evaluate postfix notation Work out an arithmetic expression written in postfix notation, where each operator follows its two numbers. stacks, parsing Intermediate 50
57. Simplify a file path Clean up a Unix file path: drop repeated slashes and single dots, and let double dots go up a folder. stacks, parsing Intermediate 50
58. Last one standing People stand in a circle and every k-th one leaves: find who is left at the end, with a deque. queues, simulation Intermediate 50
59. Sort by age, then name Sort people from oldest to youngest, and alphabetically by name when their ages are the same. sorting Intermediate 50
60. Integer square root Find the whole-number square root of a number by binary search on the answer, without sqrt or powers. searching, math Intermediate 50
61. Tower of Hanoi List the moves that carry a tower of disks from one peg to another, never putting a disk on a smaller one. recursion Intermediate 50
62. Sum a nested list Add up every number in a list whose items can be lists of their own, nested to any depth. recursion, lists Intermediate 50
63. Largest sum of a run Find the largest total of a run of consecutive numbers in a list, in one pass with Kadane's method. dynamic programming, lists Intermediate 50
64. Best time to buy and sell Find the most you could make by buying a share on one day and selling it on a later day. greedy, lists Intermediate 50
65. Word wrap Break text into lines no wider than a given width, filling each line with as many words as fit. strings, greedy Intermediate 50
66. Merge overlapping intervals Merge a list of intervals so that none of them overlap, joining the ones that overlap or touch. intervals, sorting Intermediate 50
67. Spiral order Read the items of a grid in a clockwise spiral, from the top-left corner inward. grids, simulation Intermediate 50
68. Game of Life Work out the next generation of Conway's Game of Life on a grid of live and dead cells. grids, simulation Intermediate 50
69. Compare version numbers Decide which of two version numbers like 1.10 and 1.9 is newer, part by part. parsing, strings Intermediate 50
70. Simplify a fraction Write a fraction in its lowest terms, with any minus sign on top, using the greatest common divisor. math Intermediate 50
71. Prime factors Break a number into the primes that multiply to make it, trying divisors only up to its square root. math, loops Intermediate 50
72. Happy number Decide whether repeatedly adding the squares of a number's digits ever reaches 1, catching loops with a set. sets, math Intermediate 50
73. Interleave lists Take items in turn from several lists of different lengths, like dealing cards, with iter() and next(). lists, loops Intermediate 50
74. Luhn checksum Check a number with the Luhn checksum that card and phone numbers use to catch typing mistakes. math, strings Intermediate 50
75. Valid Sudoku Check that a partly filled Sudoku grid breaks no rule: no digit twice in a row, a column or a 3x3 box. sets, grids Intermediate 50
76. Total a CSV column Add up one column of a CSV file, quoted fields with commas in them included, with the csv module. parsing, strings Intermediate 50
77. Runs that add up to k Count the runs of consecutive numbers that add up to exactly k, negatives included, with prefix sums. dictionaries, lists Advanced 100
78. Longest consecutive run Find the length of the longest run of consecutive whole numbers hidden in an unsorted list, without sorting. sets Advanced 100
79. Container with the most water Choose two walls from a row of walls to hold the most water between them, in one pass with two pointers. two pointers, greedy Advanced 100
80. Three that add to zero List every different set of three numbers from a list that add up to zero, each set once. two pointers, sorting Advanced 100
81. Longest stretch without a repeat Find the length of the longest stretch of a string in which no character appears twice. sliding window, strings Advanced 100
82. Days until a warmer day For each day, count the days until a warmer one comes, using a stack of the days still waiting. stacks Advanced 100
83. Decode a repeated string Expand a string where 3[ab] means ab three times, with repeats nested inside each other. stacks, parsing Advanced 100
84. Largest number from parts Arrange whole numbers side by side to make the largest possible number, sorting with a custom comparison. sorting, strings Advanced 100
85. Search a rotated sorted list Binary search a sorted list that has been turned round at an unknown point, still in O(log n). searching Advanced 100
86. Slowest eating speed Find the slowest eating speed that finishes every pile of snacks in time, by binary search on the answer. searching Advanced 100
87. All subsets List every subset of a set of numbers, deciding for each number whether it is in or out. backtracking, recursion Advanced 100
88. Balanced brackets, all of them List every way to write n pairs of brackets so that they all match up, building only valid strings. backtracking, recursion Advanced 100
89. No two neighbours Pick fruit from a row of trees, never from two trees side by side, to gather as much as possible. dynamic programming Advanced 100
90. Fewest coins Find the fewest coins that make an amount exactly, for coins where taking the biggest first goes wrong. dynamic programming Advanced 100
91. Paths around the walls Count the routes from one corner of a grid to the other, moving only right or down and going round walls. dynamic programming, grids Advanced 100
92. Can you reach the end? Decide whether jumps of up to the given lengths can carry you from the start of a list to its end. greedy, lists Advanced 100
93. Meeting rooms needed Work out the fewest meeting rooms that fit a day's meetings, from the most meetings going on at once. intervals, sorting Advanced 100
94. Count the islands Count the islands on a map of land and water, exploring each island once with a depth-first search. graphs, grids Advanced 100
95. Shortest way through a maze Find the fewest steps from the start of a maze to its exit with a breadth-first search and a deque. graphs, grids, queues Advanced 100
96. Fast power Work out a number to a huge power, modulo another, by squaring: about 60 steps for an exponent of 10^18. math, recursion Advanced 100
97. Smallest window with every letter Find the shortest stretch of a text that contains every given letter, as many times as it is given. sliding window, dictionaries Expert 150
98. Maximum of every window Find the largest number in every window of k items as it slides along a list, in O(n) with a deque. sliding window, queues Expert 150
99. Largest rectangle in a histogram Find the biggest rectangle that fits under a bar chart, in one pass with a stack of rising bars. stacks Expert 150
100. Trapping rain water Work out how much rain water a row of walls of different heights holds between them, in O(1) space. two pointers Expert 150
101. A calculator with brackets Work out an arithmetic expression with + - * /, brackets and minus signs, written as text, without eval. parsing, recursion Expert 150
102. N queens Count the ways to place n chess queens on an n by n board so that no two can attack each other. backtracking Expert 150
103. Course order Put courses in an order that takes every course after the ones it requires, or say that none exists. graphs, sorting Expert 150
104. Knapsack Choose items, each at most once, to carry the most value without going over a weight limit. dynamic programming Expert 150