ppadru1
ppadru1

Reputation: 63

How to convert numbers into multiple lines of digits

I am trying to solve the code eval challenge of converting a set of integers into a string of big digits. I am having a tough time parsing through the digits and print each row separately.

digits = {

"0" => 
"-**--
*--*-
*--*-
*--*-
-**--
-----",

"1" => 
"--*--
-**--
--*--
--*--
-***-
-----",

"2" =>
"***--
---*-
-**--
*----
****-
-----"
}


x = 12
x = x.to_s.split('')

digits[char].each_line.with_index do |line, index|
        print line.chomp, "\n"
end

I am trying to yield:

--*--***--
-**-----*-
--*---**--
--*--*----
-***-****-
----------

but I am having trouble. How do I iterate through and print only one line at a time? Thanks for your help! I can so far get it to return and print each digit separately on new lines.

Upvotes: 0

Views: 160

Answers (3)

Cary Swoveland
Cary Swoveland

Reputation: 110675

Here's one way:

def prnt(h, x)
  puts h.values_at(*x.to_s.chars)
        .map { |c| c.lines.map(&:chomp) }
        .transpose.map { |a| a.join << "\n" }
end

prnt(digits, 0)
-**--
*--*-
*--*-
*--*-
-**--
-----

prnt(digits, 2)
***--
---*-
-**--
*----
****-
-----

prnt(digits, 10)
--*---**--
-**--*--*-
--*--*--*-
--*--*--*-
-***--**--
----------

prnt(digits, 22)
***--***--
---*----*-
-**---**--
*----*----
****-****-
----------

prnt(digits, 102)
--*---**--***--
-**--*--*----*-
--*--*--*--**--
--*--*--*-*----
-***--**--****-
---------------

prnt(digits, 202)
***---**--***--
---*-*--*----*-
-**--*--*--**--
*----*--*-*----
****--**--****-
---------------

prnt(digits, 000)
-**--
*--*-
*--*-
*--*-
-**--
-----

You might find it easier to just use String#big_digits:

puts '201'.big_digits

 201

Upvotes: 0

steenslag
steenslag

Reputation: 80065

"0" => 
"-**--
*--*-
*--*-
*--*-
-**--
-----",

Is not very practical. The string has 5 line endings, each of which has to be removed every time. An array would be better.

Then create an array of these arrays using map. "12" is thus represented as [["--*--", "-**--", "--*--", "--*--", "-***-", "-----"], ["***--", "---*-", "-**--", "*----", "****-", "-----"]]. Not quite what you want, but have a look what transposedoes with that array, and what join does with arrays of strings.

Upvotes: 0

Satya
Satya

Reputation: 4478

You need to print the first line of the first digit, then the first line of the second, then the next line of the first digit, then the next line of the second.

digits['0'].each_line.with_index do |_, index|
  x.each do |char|
    print digits[char].split("\n")[index]
  end
  print "\n"
end

This gives you an outer loop which iterates over lines of a digit (here, '0'). That just gives you a loop to go over each line.

Then you go over each digit of the input, with x.each. Then you have to print the "current" line, the number of which is given by index from the outer loop. So you get the appropriate digit from digits, split on the newline character, index into the appropriate line, and print it. Not puts, because the next digit goes right next to this one. You print a single newline after printing the indexth line of all the digits in x.

Upvotes: 1

Related Questions