数字是多少?


13

7段数字可以使用_|字符以ASCII表示。以下是尺寸1位数:

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

通过使每个段成比例地变长来形成更大的尺寸。这是一对3位数字的数字。

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

目标

在这个挑战中,您将编写一个程序/函数,该程序/函数可以以一位数字作为输入并标识其大小。要注意的是:如果输入的数字无效,则您的程序应输出0

这是代码高尔夫球,最少字节获胜。

您可以编写程序或函数,以STDIN或参数的形式接收数字,然后打印/返回该值。

数字将以多行字符串的形式提供,并以使其成为理想矩形所​​需的最小尾随空白填充。尾随换行符是输入的可选部分。不会有多余的领导空间。

传递非数字时,它将仍然由_|字符组成,并填充为矩形,并且没有多余的前导空格。没有空白行。您将不必处理空输入。

输出应为单个非负整数,并带有可选的尾随换行符。如果输入不是任意大小的正确数字,则输出0。否则,输出大小。

这是给定大小的每个手指的宽度和高度的便捷指南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实例

在:

__ 
  |
__|
  |
__|

出:

2

在:

|
|
|

出:

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

在:

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

出:

4

在:

 ___ 
|    
|___ 
|   |
|___|

出:

0 //1 char too wide

在:

 _ 
|_|
| |

出:

0 //it's not a digit

在:

 __ 
|   
|__ 
   |
 __|

出:

2

在:

 _  _ 
 _| _|
|_  _|

出:

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

在:

 _ 
|_|
|_|

出:

1

在:

|
|

出:

1

在:

__|_
 |  
 _ |
  _ 
|__ 

出:

0

从3年前开始,这大约是将数字转换为7段显示模式过程。


@steveverrill实际上没有大小0数字之类的东西,是吗?除非您想出一种绘制它们的方法。
PhiNotPi

8
如果不是因为它必须是一个有效数字的规则,这将非常容易...
ETHproductions 2016年

我知道@ETHproductions。
PhiNotPi

@ETHproductions如果没有该要求,它将与codegolf.stackexchange.com/q/19548/15599
Level River St

Answers:


1

红宝石250

->x{d=y=0
x.size.downto(0){|n|y=n
a=["|
"*2*n]
"XNRDqpm@A".bytes{|z|p=[?|,' ','']
h=s=""
(n*2).times{|i|
i%n<1&&(d=z>>i/n*3&7)&&h=[?_,' '][d/3%2]*n
s=p[d%3]+h+p[d/6]+"
"+s
h=' '*n}
z!=68&&s=' '*(1-d%3/2)+?_*n+" 
"+s
a<<s};puts a
a.index(x)&&break}
y}

鉴于有太多可能的无效输入,唯一的方法是生成所有正确的数字并检查输入是否匹配。

我将每个数字从下到上建立起来,分为两半加上顶行。虽然有12点的可能性(考虑到左段可以是开,关,或在的情况下37完全不存在)仅7实际上存在和编码的仔细选择使得所有的信息(除了顶行)要被编码成单个字符。

该数字1并不完全适合该模式,并且需要单独处理,用于初始化数组。

取消测试程序

.由于诊断原因,此版本使用空格代替。

#Encoding used for half-digits (radix 3,2,2 most significant digit at right)

#000    |_|  0

#100    ._|  1  . = space

#200    X_|  2  X = no space (for digits 3 and 7)  

#010    |.|  3

#110    ..|  4

#210    X.|  5

#001    |_.  6


f=->x{d=y=0                                        #d and y required to be intialized for scoping reasons
  x.size.downto(0){|n|y=n                          #Assume max possible size of character = length of input and iterate down through all possible sizes n   
    a=["|\n"*2*n]                                  #Make an array containing the digit 1 (different shape to others)
    "XNRDqpm@A".bytes{|z|                          #Each character encodes the pattern for a digit. Iterate through them
      p=['|','.','']                               #Possible components for left and right of digit
      h=s=""                                       #h initialized for scoping reasons. s will contain the digit string 
      (n*2).times{|i|                              #For each row
        i%n<1&&                                    #If i%n==1 we are at the bottom of a half digit
        (d=z>>i/n*3&7)&&                           #so extract info from z and store in d
        h=[?_,'.'][d/3%2]*n                        #h is the horizontal part of the half digit, either _ or spaces 
        s=p[d%3]+h+p[d/6]+"\n"+s                   #Build one row of digit, working upwards: left,middle,right
        h='.'*n                                    #If row i%n!=0 (not bottom row of half digit)the middle section must contain spaces
      }                                            #We now have both halves of the digit, only the top segment missing 
      z!=68&&s='.'*(1-d%3/2)+?_*n+".\n"+s          #If z!=68 (digit 4) add a top to the digit, with appropriate leading and trailing spaces        
      a<<s                                         #Add the completed digit of size n to a
    }
    #puts a                                        #Diagnostic: uncomment to print all the strings checked
    a.index(x)&&break                              #If string x is in a, break

  }
y                                                  #and return last value of n 
}


# digit 7, size 2. Trailing newline required. Outputs 2
puts f[
"__.
..|
..|
..|
..|
"]
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.