Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. text = "source text banana banana banana ..."
  2.  
  3. // Transcoding map to convert keybords characters sequence to natural numbers: { 'q' => 0, 'w' => 1, ... }
  4. characterCodesMap = "qwertyuiopasdfghjklzxcvbnm".mapWithIndex((character, index) => { return [character, index] }).toHash
  5.  
  6. // Character code ranges for keyboard rows
  7. row1 = 0..9
  8. row2 = 10..18
  9. row3 = 19..25 // Actually this one will never be used
  10.  
  11. // Loop through each word
  12. text.eachWord((word) => {
  13. inRange1 = true
  14. inRange2 = true
  15. inRange3 = true
  16.  
  17. word.each((character) => {
  18. code = characterCodesMap[character]
  19.  
  20. // Range inclusion check function suppose to work like this:
  21. // includes = (value) => { return (range1.first >= code) && (range1.last <= code) }
  22.  
  23. inRange1 = inRange1 && code.inRange(row1)
  24. inRange2 = inRange2 && !inRange1 && code.inRange(row2)
  25.  
  26. // Last range check is simplified
  27. inRange3 = inRange3 && !(inRange1 || inRange2)
  28.  
  29. // Continue to the next work after range test fails first time
  30. if (!(inRange || inRange || inRange)) break;
  31. })
  32.  
  33. if (inRange1) print("'#{word}' belongs to row 1")
  34. if (inRange2) print("'#{word}' belongs to row 2")
  35. if (inRange3) print("'#{word}' belongs to row 3")
  36. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement