Skip to content

Instantly share code, notes, and snippets.

@dreikanter
Last active March 8, 2017 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dreikanter/81a910d8b4cc8a7291c9a5557ecbf0d9 to your computer and use it in GitHub Desktop.
Save dreikanter/81a910d8b4cc8a7291c9a5557ecbf0d9 to your computer and use it in GitHub Desktop.
Find words on a single row of a querty keyboard
text = "source text banana banana banana ..."
// Transcoding map to convert keybords characters sequence to natural numbers: { 'q' => 0, 'w' => 1, ... }
characterCodesMap = "qwertyuiopasdfghjklzxcvbnm".mapWithIndex((character, index) => { return [character, index] }).toHash
// Character code ranges for keyboard rows
row1 = 0..9
row2 = 10..18
row3 = 19..25 // Actually this one will never be used
// Loop through each word
text.eachWord((word) => {
inRange1 = true
inRange2 = true
inRange3 = true
word.each((character) => {
code = characterCodesMap[character]
// Range inclusion check function suppose to work like this:
// includes = (value) => { return (range1.first >= code) && (range1.last <= code) }
inRange1 = inRange1 && code.inRange(row1)
inRange2 = inRange2 && !inRange1 && code.inRange(row2)
// Last range check is simplified
inRange3 = inRange3 && !(inRange1 || inRange2)
// Continue to the next work after range test fails first time
if (!(inRange || inRange || inRange)) break;
})
if (inRange1) print("'#{word}' belongs to row 1")
if (inRange2) print("'#{word}' belongs to row 2")
if (inRange3) print("'#{word}' belongs to row 3")
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment