3 ms·
This is hard to read because they specifically asked for a short line count, but it's not really very complicated. an if/else, a map, and a recursive call. Coul
by qqq 18y ago
This is hard to read because they specifically asked for a short line count, but it's not really very complicated. an if/else, a map, and a recursive call. Could maybe do the string manipulation more cleverly, but the obvious (assigning a new string to an index) is destructive in ruby.
h = {'a' => ['A', '@', '4'], 'b' => ['B'], 'c' => ['C']}
def foo str, h, pos=0
(pos>str.size-1) ? [str] : ([str[pos..pos]] + h[str[pos..pos]]).map {|new_char| foo(str[0...pos] + new_char + str[(pos+1)...(str.size)], h, pos+1) }.flatten
end
p foo('abc', h)