Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ef to_t9(s)
- l = {
- 'a' => '2',
- 'b' => '22',
- 'c' => '222',
- 'd' => '3',
- 'e' => '33',
- 'f' => '333',
- 'g' => '4',
- 'h' => '44',
- 'i' => '444',
- 'j' => '5',
- 'k' => '55',
- 'l' => '555',
- 'm' => '6',
- 'n' => '66',
- 'o' => '666',
- 'p' => '7',
- 'q' => '77',
- 'r' => '777',
- 's' => '7777',
- 't' => '8',
- 'u' => '88',
- 'v' => '888',
- 'w' => '9',
- 'x' => '99',
- 'y' => '999',
- 'z' => '9999',
- 'A' => '# 2',
- 'B' => '# 22',
- 'C' => '# 222',
- 'D' => '# 3',
- 'E' => '# 33',
- 'F' => '# 333',
- 'G' => '# 4',
- 'H' => '# 44',
- 'I' => '# 444',
- 'J' => '# 5',
- 'K' => '# 55',
- 'L' => '# 555',
- 'M' => '# 6',
- 'N' => '# 66',
- 'O' => '# 666',
- 'P' => '# 7',
- 'Q' => '# 77',
- 'R' => '# 777',
- 'S' => '# 7777',
- 'T' => '# 8',
- 'U' => '# 88',
- 'V' => '# 888',
- 'W' => '# 9',
- 'X' => '# 99',
- 'Y' => '# 999',
- 'Z' => '# 9999',
- '1' => '11111',
- '2' => '2222',
- '3' => '3333',
- '4' => '4444',
- '5' => '5555',
- '6' => '6666',
- '7' => '77777',
- '8' => '8888',
- '9' => '99999',
- '0' => '00',
- '.' => '1',
- ',' => '11',
- '?' => '111',
- '!' => '1111',
- "'" => '*',
- '-' => '**',
- '+' => '***',
- '=' => '****',
- ' ' => '0'
- }
- a = s.chars
- r = []
- # create result array of chars
- a.each_with_index do |e, i|
- if s[i] == s[i+1]
- r << l[e]
- r << '_'
- else
- r << l[e]
- end
- r << ' '
- end
- # but we do not want to type # before each symbol
- # lets just count how many #'s we have'
- r = r.join.split(' ')
- h = 0
- q = []
- r.each_with_index do |e2, i2|
- if e2 == '#'
- h += 1
- q << i2
- end
- end
- # if we have more than 1 '#' in result we should make some cleaning
- # lets create array of indexes where we want add additional '#' and remove others '#'
- ad = []
- if h > 1
- q.each_with_index do |e3, i3|
- if q[i3 - 1].to_i - q[i3 - 2].to_i == 2 && q[i3].to_i - q[i3 - 1].to_i > 2
- ad << { a: q[i3 - 1] + 2 }
- end
- if q[i3].to_i - q[i3 - 1].to_i == 2 && q[i3 + 1] == nil
- ad << { a: q[i3] + 2 }
- end
- if q[i3 + 1].to_i - q[i3].to_i == 2
- r[e3] = '##'
- ad << { d: q[i3 + 1] }
- end
- end
- end
- # if this array size more than 0 let's go reverse on it (so we won't remove any usable symbols)
- if ad.size > 0
- ad.reverse_each do |e4|
- if e4[:d] != nil
- r.delete_at(e4[:d])
- elsif e4[:a] != nil
- r.insert(e4[:a], '#')
- end
- end
- end
- # returning result
- r.join(' ')
- end
Add Comment
Please, Sign In to add comment