Programming Search⌘K

Classic string exercises

The small text puzzles that the task lists hold and that teach string handling: palindromes, anagrams, letter and word counts, edit distance and simple ciphers.

Tasks

  • Apply a Caesar or ROT13 cipher

    Move each letter a fixed number of places along the alphabet, from z back to a, and keep the case and all other characters. ROT13 is a shift of 13 and is its own inverse. The cipher gives no security.

    10 languagesintro
  • Check whether a string is a palindrome

    Check whether a text reads the same backwards as forwards, with case, spaces and punctuation ignored: compare the cleaned text with its reverse, or move two indexes toward the middle.

    10 languagesintro
  • Count the letters of a text

    Count each letter from a to z in a text, with case ignored: an array of 26 counters indexed by the letter, or a map such as Counter. Letters outside ASCII need a map and a case fold.

    10 languagesintro
  • Detect anagrams

    Check whether two texts use the same letters the same number of times, with case, spaces and punctuation ignored: count the letters, or sort them and compare. Group anagrams by their sorted letters.

    10 languagesintro
  • Compute the edit distance of two strings

    Compute the Levenshtein distance of two strings, the minimum number of one-character insertions, deletions and substitutions that change one into the other, with a dynamic program that keeps two rows.

    10 languagescore
  • Count word frequencies

    Split a text into words, change each word to lower case, count the words in a map, and return the n most frequent words, with ties in code point order.

    10 languagescore