1

JavaScript: Can we convert 7-segment numbers made of characters(_ & |) into parsed number using javascript/angularjs

Input :

 
      _  _     _  _  _  _  _  
    | _| _||_||_ |_   ||_||_| 
    ||_  _|  | _||_|  ||_| _|

Output :

123456789
user7549774
  • 51
  • 1
  • 4
  • 1
    the anwer is yes, but what have you tried? – Nina Scholz Feb 11 '17 at 10:57
  • Well I am new to angularJS so I am not getting how to do this. – user7549774 Feb 11 '17 at 11:01
  • 1
    this question has nothing to do with one particular framework, it's about programming in general, and how to solve a problem. JS and angular only describe the context/tools you have. So, how would you approach the problem in general? – Thomas Feb 11 '17 at 11:05
  • Using my past experiences, I would analyse the problem, think for an alternative, try to debug using coding – user7549774 Feb 11 '17 at 11:33
  • you have at least two possibilites to solve the problem, one by checking the pattern as string, and the other to identify the segments and use a binary operation for checking the number. – Nina Scholz Feb 11 '17 at 11:45
  • @NinaScholz : Do you have any working example to support this? – user7549774 Feb 11 '17 at 13:02
  • @user7549774, Did you actually try anything on your own? We're not a free code service. – Thomas Feb 11 '17 at 23:47
  • Your last answer is just a bunch of empty phrases. `Using my past experiences` great, that's how we learn and grow, `I would analyse the problem` go on, please analyse it. `think for an alternative` an alternative to the problem? either you have a problem, or you don't. `try to debug` that's actually the point you should have asked this question. When you're right at some particular problem and fail at debugging it on your own. `using coding` yes pleeeeeease, show us some/your code. Then we have something to talk about. Nina told you two possible ways how **you** could approach the problem. – Thomas Feb 12 '17 at 00:16
  • @Thomas: Thanks for your expert opinion Thomas, but if you cant help then please keep your thoughts upto you only, it might hurt others. As you rightly said that you dont provide free service so I would suggest you to keep yourself out of this, it will save both of our times in a big way. – user7549774 Feb 12 '17 at 17:57

1 Answers1

11

You could use the encoding of a seven-segment display and the following

  • split by linefeed '\n'
  • build an array with single ASCII digits
  • join a single ASCII digit to a string
  • map the ASCII value
  • join the result to a string

The ASCII value is taken with an object for a number, and the single segments are weighted with a value for the segment.

Value of segments is 2n.

  _0_    
|5   1|
  _6_  
|4   2|
  _3_

Dots as segments

 0
561
432

This generates the string

'909561432'
  ^ ^^^^^^  denoted segments with the number above
 ^ ^        no segments

For example, take

 012
0
1  |
2  |

the ASCII string of 1, and the above segment numbers, then you get for the segment 1 the value 21 and for 2 the value 22. The result is 2 + 4 = 6.

After a lookup in the bits object,

{
    63: 0,
    6: 1, // <----
    91: 2, 
    /* ... */
}

you get the digit 1.

function get7segment(ascii) {
    return ascii.
        split('\n').
        reduce(function (r, a, i) {
            a.match(/.../g).forEach(function (b, j) {
                r[j] = r[j] || [];
                r[j][i] = b;
            });
            return r;
        }, []).
        map(function (a) {
            return a.join('');
        }).
        map(function (a) {
            var bits = { 63: 0, 6: 1, 91: 2, 79: 3, 102: 4, 109: 5, 125: 6, 7: 7, 127: 8, 111: 9, 0: ' ' },
                v = '909561432'.split('').reduce(function (r, v, i) {
                    return r + ((a[i] !== ' ') << v);
                }, 0);
            return v in bits ? bits[v] : '*'; // * is an illegal character
        }).
        join('');
}

function print(ascii) {
    var pre = document.createElement('pre');
    pre.innerHTML = ascii + '\n\n' + get7segment(ascii);
    document.body.appendChild(pre);
}

print(' _     _  _     _  _  _  _  _ \n| |  | _| _||_||_ |_   ||_||_| \n|_|  ||_  _|  | _||_|  ||_| _|');
print('    _  _  _    ...\n  | _| _|  |   ...\n  | _| _|  |   ...');
print('    _  _  _  _  _ \n|_||_|| ||_||_   |\n  | _||_||_||_|  |');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • thats great, how did you do that? – user7549774 Feb 11 '17 at 16:46
  • Can you please explain how did you binded 7 segment pattern to var data1, data2 ? Cant we have same variable for binding both. Considering a scenarion we have two numbers returned to same string. – user7549774 Feb 11 '17 at 16:55
  • what do you mean by *binding both*? – Nina Scholz Feb 11 '17 at 17:20
  • Got it ! but will this same function works for variable holding both numbers 123456789 and 1331 together with former placed above and latter beneath(in & segment characters).Also can we check for illegal tokens like '?' or ',' ? – user7549774 Feb 11 '17 at 17:36
  • there is no check for illegal characters, bur you could ch in advance for illegal characters. you might get some undefined value for missing keys in the object. – Nina Scholz Feb 11 '17 at 17:40
  • _ _ _ _ _ _ _ | _| _||_||_ |_ ||_||_| ||_ _| | _||_| ||_| _| _ _ _ _ |_||_|| ||_|| | | _||_||_||_| – user7549774 Feb 11 '17 at 17:43
  • please add the string to your question. – Nina Scholz Feb 11 '17 at 17:49
  • Download image for your reference: [link](http://imgur.com/vKIQAgg) – user7549774 Feb 12 '17 at 17:52
  • @user7549774, what should i do with the image? – Nina Scholz Feb 12 '17 at 17:53
  • This is in refer to my question as one of a scenario where I can have numbers split as shown in image. Write now you are using two variables for both numbers, how come if these numbers are clubbed just like screenshot – user7549774 Feb 12 '17 at 18:47
  • then you need an algorithm to separate the two numbers. – Nina Scholz Feb 12 '17 at 19:03
  • Hi @Nina Can you please tell me one thinkgs, In this line { v = '909561432'.split('').reduce(function (r, v, i) } why you have taken 909561432 this number and what is the use ? – Shubham Verma Jul 25 '17 at 10:51
  • @ShubhamVerma, it's the single value of the segment, please see above, in the section: Value of segments is 2^n. the value of `9` ist just denote the these segments are not valid. – Nina Scholz Jul 25 '17 at 11:30
  • Thanks@Nina , I know how you create { 63: 0, 6: 1, 91: 2, } But didn't get 909561432 this number , Can you be more specific please. How you are calculating this 909561432 ? – Shubham Verma Jul 26 '17 at 04:22
  • @ShubhamVerma, maybe the edit helps a bit. – Nina Scholz Jul 26 '17 at 07:55