将电话号码转换为单词

时间:2013-12-04 05:10:58

标签: ruby arrays hash

所以我发现我可以计算哈希,问题是对于7和9我有四个值。我已经尝试了其他一些没有运气的事情。有人可以帮助理解我还能做些什么来从散列中获取我想要的值。我意识到我可以将数字与密钥相匹配,但我很困惑如何使值变为置换。

letters = {"1" => ["1", "1", "1"],
       "2" => ["a", "b", "c"],
       "3" => ["d", "e", "f"],
       "4" => ["g", "h", "i"],
       "5" => ["j", "k", "l"],
       "6" => ["m", "n", "o"],
       "7" => ["p", "q", "r", "s"],
       "8" => ["t", "u", "v"],
       "9" => ["w", "x", "y", "z"]}


phone_number = gets.chomp.to_s

words = []
word = []

numbers = phone_number.chomp.chars

count0 = 0
while count0 < 3
  count1 = 0
  while count1 < 3
    count2 = 0
    while count2 < 3
      count3 = 0
      while count3 < 3
        count4 = 0
        while count4 < 3
          count5 = 0
          while count5 < 3
            count6 = 0
            while count6 < 3
              word[0] = letters[numbers[0]][count0]
              word[1] = letters[numbers[1]][count1]
              word[2] = letters[numbers[2]][count2]
              word[3] = letters[numbers[3]][count3]
              word[4] = letters[numbers[4]][count4]
              word[5] = letters[numbers[5]][count5]
              word[6] = letters[numbers[6]][count6]
              words << word.join
              count6 += 1
            end
            count5 += 1
          end
          count4 += 1
        end
        count3 += 1
      end
      count2 += 1
    end
    count1 += 1
  end
  count0 += 1
end
puts words

编辑:

我想要一个七位数字并打印出所有可能的字母组合。我是初学者,所以我想了解我现在所知道的事情。我想尝试用if语句来做这件事。

numbers = phone_number.chomp.chars

if letters.key?(numbers[0])
 if letters.key?(numbers[1])
  if letters.key?(numbers[2])
   if letters.key?(numbers[3])
    if letters.key?(numbers[4])
     if letters.key?(numbers[5])
      if letters.key?(numbers[6])

      end
     end
    end
   end
  end
 end 
end

我理解如何从匹配的键中获取值,但是如果有意义的话,我不知道如何在完成其余的操作时保持第一个值。

4 个答案:

答案 0 :(得分:2)

product是您正在寻找的功能,以下适用于任意数量的数字:

digits = '27'
keys = digits.chars.map{|digit|letters[digit]}
p keys.shift.product(*keys).map(&:join) #=> ["ap", "aq", "ar", "as", "bp", "bq", "br", "bs", "cp", "cq", "cr", "cs"]

答案 1 :(得分:1)

这将打印可变大小的电话号码的所有可能单词:

letters = {"1" => ["1"],
       "2" => ["a", "b", "c"],
       "3" => ["d", "e", "f"],
       "4" => ["g", "h", "i"],
       "5" => ["j", "k", "l"],
       "6" => ["m", "n", "o"],
       "7" => ["p", "q", "r", "s"],
       "8" => ["t", "u", "v"],
       "9" => ["w", "x", "y", "z"]}

digits = gets.chomp.split ''

# Total number of combinations
n = digits.inject(1) { |a,b| a * letters[b].size }

words = []
0.upto n-1 do |q|
  word = []
  digits.reverse.each do |digit|
    q, r = q.divmod letters[digit].size
    word.unshift letters[digit][r]
  end
  words << word.join
end

puts words

例如,如果输入为67,则有12种组合:

  

mp mq ms np nq nr ns op oq or os

编辑:我没有看到如你所写的那样使用7 if语句的方法,但也许这更接近你正在寻找的那种答案:

words = []
letters[digits[0]].each do |c0|
  letters[digits[1]].each do |c1|
    letters[digits[2]].each do |c2|
      letters[digits[3]].each do |c3|
        letters[digits[4]].each do |c4|
          letters[digits[5]].each do |c5|
            letters[digits[6]].each do |c6|
              words << [c0,c1,c2,c3,c4,c5,c6].join
            end
          end
        end
      end
    end
  end
end

puts words

一个好的练习是以一种可以用于任何长度的电话号码的方式重写,而不仅仅是7.再次,这仅用于教学目的。在实践中,可以像product一样使用数组的hirolau's answer方法。

答案 2 :(得分:0)

LETTERS = {"1" => ["1", "1", "1"],
   "2" => ["a", "b", "c"],
   "3" => ["d", "e", "f"],
   "4" => ["g", "h", "i"],
   "5" => ["j", "k", "l"],
   "6" => ["m", "n", "o"],
   "7" => ["p", "q", "r", "s"],
   "8" => ["t", "u", "v"],
   "9" => ["w", "x", "y", "z"]}


def convert_to_phone_number(string)
  string.each_char.with_object([]) { |x, arr| LETTERS.each { |k,v| (arr.push k; break) if v.include?(x) }}.join
end

convert_to_phone_number "foobar"
#=> "366227"

答案 3 :(得分:0)

我认为这是缓存内存的问题  你需要改变如下

LETTERS = {"1" => ["1", "1", "1"],
   "2" => ["a", "b", "c"],
   "3" => ["d", "e", "f"],
   "4" => ["g", "h", "i"],
   "5" => ["j", "k", "l"],
   "6" => ["m", "n", "o"],
   "7" => ["p", "q", "r", "s"],
   "8" => ["t", "u", "v"],
   "9" => ["w", "x", "y", "z"]}


def convert_to_phone_number(string)
  string.each_char.with_object([]) { |x, arr| LETTERS.each { |k,v| (arr.push k; break) if v.include?(x) }}.join
end

convert_to_phone_number“foobar”