Swift typing test
Swift reads cleanly, with no semicolons and no parentheses around if conditions, but it has punctuation of its own: ... for closed ranges, ? for optionals, guard let to unwrap them, and underscores in parameter lists like func insert(_ root: Node?, _ value: Int).
What you'll type in Swift
...for closed ranges, as in1...15.?for optionals, andguard let ... else { return }to unwrap them._before parameters that don't need a label where the function is called.->before return types.&andinoutfor a value the function can change.[Int]for array types.
Example: FizzBuzz in Swift
Swift's FizzBuzz looks like C without the parentheses: if i % 15 == 0 { goes straight to the brace.
for i in 1...15 {
if i % 15 == 0 {
print("FizzBuzz")
} else if i % 3 == 0 {
print("Fizz")
} else if i % 5 == 0 {
print("Buzz")
} else {
print(i)
}
}
Tips for typing Swift faster
- Three dots in a row are easy to overtype or undertype. Count them:
1...15. guard let node = node else { return }is worth practising as one phrase. Both functions in the binary search tree program start with aguard let.- Watch the space after
_in_ value: the underscore is a label, not part of the name.
All 20 Swift programs
Every program is real, working Swift. Choose one to start typing it straight away.
Beginner
- Hello, World!Print a greeting.
- Count to fivePrint the numbers 1 to 5.
- Celsius to FahrenheitConvert 100 °C to Fahrenheit.
- Sum a listAdd up 3, 1, 4, 1, 5.
- Find the largestFind the biggest number in a list.
- Count vowelsCount the vowels in "keyboard".
Intermediate
- Reverse a stringTurn "hello" into "olleh".
- FizzBuzzFizz for 3s, Buzz for 5s, 1 to 15.
- FactorialA recursive function, called with 5.
- FibonacciPrint the first ten Fibonacci numbers.
- Greatest common divisorEuclid's algorithm for 48 and 18.
Advanced
- Primes to 20Print every prime up to 20.
- PalindromeCheck whether "racecar" reads the same backwards.
- Word countCount the words in a sentence.
- Bubble sortSort 5, 2, 9, 1, 7 by swapping neighbours.
- Binary searchFind 7 in a sorted list by halving.
Expert
- QuicksortSort a list by partitioning around a pivot.
- Binary search treeBuild a tree, then read the values back in order.
- Shortest path (BFS)Breadth-first search through a small graph.
- N-QueensCount the ways to place 6 queens so none attack.
How the test works
The timer starts on your first key and stops once every character is right. Press Enter for a new line and the indentation is filled in for you. A wrong key shows in red and counts as a mistake; press Backspace to go back and fix it. Your best time for each program is saved in your browser. Read more about how speed and accuracy are calculated, or get tips for typing code faster.