Skip to main content
replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

This is approximately the inverse of Transform number into 7-segment display pattern, from 3 years back.

This is approximately the inverse of Transform number into 7-segment display pattern, from 3 years back.

Bumped by Community user
Bumped by Community user
Bumped by Community user
Tweeted twitter.com/StackCodeGolf/status/685829036073992192
Source Link
PhiNotPi
  • 29.3k
  • 10
  • 86
  • 161

What size is the digit?

7-segment digits can be represented in ASCII using _| characters. Here are the size 1 digits:

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

Larger sizes are formed by making each segment proportionately longer. Here are a couple size 3 digits.

 ___ ___ ___ ___ ___ ___ ___ | | | | | | | | | | | | | | | | | | | | |___| |___ | |___ ___| | | |___| | | | | | | | | | | | | | | | | | | | | |___| |___| | ___| ___| |___| ___| 

Goal

In this challenge, you are to write a program/function that can take a single digit as input and identify its size. The catch: if the input is not a valid digit, then your program should output 0.

This is code-golf, fewest bytes wins.

You can write either a program or a function, which can receive the digit either as STDIN or an argument, and print/return the value.

Digits will be provided as a multi-line string, padded with the minimal amount of trailing whitespace needed to make it a perfect rectangle. The trailing newline is an optional part of input. There will be no unneeded leading spaces.

When a non-digit is passed, it will still be comprised of _| characters, padded to a rectangle, and have no unneeded leading spaces. There will be no blank lines. You won't have to deal with empty input.

Output should be a single non-negative integer, with optional trailing newline. If the input is not a proper digit of any size, output 0. Else, output the size.

Here is a handy guide for the widths and heights of each digit for a given size N.

Digit Height Width (not counting newlines) 1 2N 1 2 2N+1 N+2 3 2N+1 N+1 4 2N N+2 5 2N+1 N+2 6 2N+1 N+2 7 2N+1 N+1 8 2N+1 N+2 9 2N+1 N+2 0 2N+1 N+2 

I/O Examples

In:

__ | __| | __| 

Out:

2 

In:

| | | 

Out:

0 //because it is of an invalid height. Either 1 char too short or tall. 

In:

| | | | | | |____| | | | | 

Out:

4 

In:

 ___ | |___ | | |___| 

Out:

0 //1 char too wide 

In:

 _ |_| | | 

Out:

0 //it's not a digit 

In:

 __ | |__ | __| 

Out:

2 

In:

 _ _ _| _| |_ _| 

Out:

0 //both would be valid individually, but input should be a *single* digit 

In:

 _ |_| |_| 

Out:

1 

In:

| | 

Out:

1 

In:

__|_ | _ | _ |__ 

Out:

0 

This is approximately the inverse of Transform number into 7-segment display pattern, from 3 years back.