Ruby convert number to 7 segment display

Ruby convert number to 7 segment display
0

#1

I am learning Ruby
I am trying to write method that gets a number value.and will display it with 7 segment display.

example == > 123
` _ _
| _| |
| |
_|


#2

Is this a homework question? Because you haven’t yet shown your attempt at a solution.


#3

yes it homework question in ruby. and i just now learn ruby syntax.
i no have idea how to start.


#4

OK, well you know basic ruby syntax, which is already ahead of me! :slight_smile:

Before you start, break down the problem into chunks as small as possible. Here’s a reasonable starting point:

  1. For each digit in your input, find the corresponding multiline string showing a 7-digit representation.
    These would probably all be stored in some kind of array, list, dictionary, object, or similar (I’m not familiar with Ruby’s data structures).
  2. Then, print the first line of the first digit of your input, followed by the first line of the second digit, etc., until the end (the length of this process will depend on the number of digits in your input… so you’ll probably be using some kind of for loop here).
  3. Print the second line of the first digit, then the second line of the second digit, etc… again, this iterates through the whole input. And keep doing this for all the lines. This is most likely going to be another for loop enclosing the other one, but this time of a fixed size (the number of lines in your seven-segment strings).

That should be enough to get started - report back with what you have and I’m sure there will be a few Ruby experts on hand to help out!


#5

Actually, for fun, I tried making this in Python. As it’s another language, I hope it wouldn’t be considered cheating for your homework.


# Set up the multiline strings for the
# seven-segment numbers to be displayed

numsMultiline = [
'''
 _  
| | 
|_| 
''',
'''
    
  | 
  | 
''',
'''
 _  
 _| 
|_  
''',
'''
 _  
 _| 
 _| 
''',
'''
    
|_| 
  | 
''',
'''
 _  
|_  
 _| 
''',
'''
 _  
|_  
|_| 
''',
'''
 _  
  | 
  | 
''',
'''
 _  
|_| 
|_| 
''',
'''
 _  
|_| 
  | 
'''
]

# Create a list for `nums`

nums = [0] * len(numsMultiline)

# Create a list of these where each string is
# itself broken into a list of lines (easier to
# reference by index)

for i in range(0, len(numsMultiline)):
    nums[i] = numsMultiline[i].splitlines()[1:4] # The
    # line at index [0] is empty (just used for making
    # the format of the multiline strings look nice), so
    # we don't need it - start from index [1].
    #
    # Note that we could also simply set up `nums`
    # directly as a list of lists, without using the
    # multiline strings at all. The only advantage of
    # this method is that it improves readability (for
    # the price of an imperceptibly small performance
    # drop).

# Get user input

myInput = input('Please input your number: ')

# Nested `for` loops to create the output. Note
# that user input collected with `input()` is
# always treated as a string, not any other data
# type, so it needs to be explicitly converted
# into an integer using `int()` when we need to
# use it as an index reference.

for i in range(0, 3): # 3 is the number of lines in
    # each seven-segment digit.
    for ii in range(0, len(myInput)):
        print(nums[int(myInput[ii])][i], end='')
        # `end=''` prevents Python from
        # automatically starting a newline (we
        # want the same line of each character
        # to be printed on the same line).
    print('') # This creates a newline after each
    # line finishes printing.