Coding Puzzles
Small programming puzzles and interview-style problems, solved step by step.
-
Sunday afternoon Problem: Lazy Sequences as a Dynamic Programming Primitive in Kotlin
Dynamic programming is usually taught with mutable arrays and index arithmetic — Kotlin’s generateSequence and sequence { } let you express the same recurrences as infinite, lazily-evaluated streams. Background Classic dynamic programming (DP) involves filling a table bottom-up: allocate…
-
Swift & Arithmetic Overflow
When you start implementing your own arithmetic algorithms you will sooner than later encounter overflow errors. Unlike arithmetic operators in C, arithmetic operator in Swift don’t overflow by default. Overflow behaviour is trapped and reported as an error. You have…
-
Solving a simple code puzzle in Swift
Georgina loves making up weird sequences. Here is her latest: “1 2 4 8 10 20 22 44 46 92 94 188 190 380…” Write a program that finds the sum of the first 150 numbers in Georgina’s series. There…
-
Swift “Alphabet Cipher” implementation.
In 1868 Lewis Carroll published “The Alphabet-Cipher”. They say he did this in a kids magazine ?. While Carroll called his cipher “unbreakable” we know it is not. However it is still interesting to implement such a cipher as a…
-
Reversed String
Reversing a string in Swift is done by using the reversed() method on its characters, then creating a new string out of the result. Here’s the code: Reverse a String without using reversed built-in function in Swift can be done using Swift higher order…
-
How to calculate the ROT13 of string in Swift
ROT13 is a simple algorithm that shifts letters in a string forward 13 places. It’s not hard to write a rot13() function, but it is a little harder to wrap it up neatly so you avoid global variables while still making…
-
A “Swifty” Longest Palindrome Substring Algorithm
In computer science, the longest palindromic substring problem is the problem of finding a maximum-length substring of a given string that is also a palindrome. For example, the longest palindromic substring of “bananas” is “anana”. Some people will be tempted…
-
Algorithm to find the area of a Polygon
Today I came across the following problem: “If you know the coordinates of the vertices of a polygon with square edges, find the surrounded area. Or more formally given an array of the X and Y coordinates enumerated in a clockwise…