Guest User

Untitled

a guest
Jul 6th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.82 KB | None | 0 0
  1. ef to_t9(s)
  2.   l = {
  3.       'a' => '2',
  4.       'b' => '22',
  5.       'c' => '222',
  6.       'd' => '3',
  7.       'e' => '33',
  8.       'f' => '333',
  9.       'g' => '4',
  10.       'h' => '44',
  11.       'i' => '444',
  12.       'j' => '5',
  13.       'k' => '55',
  14.       'l' => '555',
  15.       'm' => '6',
  16.       'n' => '66',
  17.       'o' => '666',
  18.       'p' => '7',
  19.       'q' => '77',
  20.       'r' => '777',
  21.       's' => '7777',
  22.       't' => '8',
  23.       'u' => '88',
  24.       'v' => '888',
  25.       'w' => '9',
  26.       'x' => '99',
  27.       'y' => '999',
  28.       'z' => '9999',
  29.       'A' => '# 2',
  30.       'B' => '# 22',
  31.       'C' => '# 222',
  32.       'D' => '# 3',
  33.       'E' => '# 33',
  34.       'F' => '# 333',
  35.       'G' => '# 4',
  36.       'H' => '# 44',
  37.       'I' => '# 444',
  38.       'J' => '# 5',
  39.       'K' => '# 55',
  40.       'L' => '# 555',
  41.       'M' => '# 6',
  42.       'N' => '# 66',
  43.       'O' => '# 666',
  44.       'P' => '# 7',
  45.       'Q' => '# 77',
  46.       'R' => '# 777',
  47.       'S' => '# 7777',
  48.       'T' => '# 8',
  49.       'U' => '# 88',
  50.       'V' => '# 888',
  51.       'W' => '# 9',
  52.       'X' => '# 99',
  53.       'Y' => '# 999',
  54.       'Z' => '# 9999',
  55.       '1' => '11111',
  56.       '2' => '2222',
  57.       '3' => '3333',
  58.       '4' => '4444',
  59.       '5' => '5555',
  60.       '6' => '6666',
  61.       '7' => '77777',
  62.       '8' => '8888',
  63.       '9' => '99999',
  64.       '0' => '00',
  65.       '.' => '1',
  66.       ',' => '11',
  67.       '?' => '111',
  68.       '!' => '1111',
  69.       "'" => '*',
  70.       '-' => '**',
  71.       '+' => '***',
  72.       '=' => '****',
  73.       ' ' => '0'
  74.   }
  75.   a = s.chars
  76.   r = []
  77.  
  78.   # create result array of chars
  79.   a.each_with_index do |e, i|
  80.     if s[i] == s[i+1]
  81.       r << l[e]
  82.       r << '_'
  83.     else
  84.       r << l[e]
  85.     end
  86.     r << ' '
  87.   end
  88.  
  89.   # but we do not want to type # before each symbol
  90.   # lets just count how many #'s we have'
  91.   r = r.join.split(' ')
  92.   h = 0
  93.   q = []
  94.   r.each_with_index do |e2, i2|
  95.     if e2 == '#'
  96.       h += 1
  97.       q << i2
  98.     end
  99.   end
  100.  
  101.  
  102.   # if we have more than 1 '#' in result we should make some cleaning
  103.   # lets create array of indexes where we want add additional '#' and remove others '#'
  104.   ad = []
  105.   if h > 1
  106.     q.each_with_index do |e3, i3|
  107.       if q[i3 - 1].to_i - q[i3 - 2].to_i == 2 && q[i3].to_i - q[i3 - 1].to_i > 2
  108.         ad << { a: q[i3 - 1] + 2 }
  109.       end
  110.       if q[i3].to_i - q[i3 - 1].to_i == 2 && q[i3 + 1] == nil
  111.         ad << { a: q[i3] + 2 }
  112.       end
  113.       if q[i3 + 1].to_i - q[i3].to_i == 2
  114.         r[e3] = '##'
  115.         ad << { d: q[i3 + 1] }
  116.       end
  117.     end
  118.   end
  119.  
  120.   # if this array size more than 0 let's go reverse on it (so we won't remove any usable symbols)
  121.   if ad.size > 0
  122.     ad.reverse_each do |e4|
  123.       if e4[:d] != nil
  124.         r.delete_at(e4[:d])
  125.       elsif e4[:a] != nil
  126.         r.insert(e4[:a], '#')
  127.       end
  128.     end
  129.   end
  130.  
  131.  
  132.   # returning result
  133.   r.join(' ')
  134. end
Add Comment
Please, Sign In to add comment