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
` _ _
| _| |
| | _|
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
` _ _
| _| |
| | _|
Is this a homework question? Because you haven’t yet shown your attempt at a solution.
yes it homework question in ruby. and i just now learn ruby syntax.
i no have idea how to start.
OK, well you know basic ruby syntax, which is already ahead of me!
Before you start, break down the problem into chunks as small as possible. Here’s a reasonable starting point:
for
loop here).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!
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.