数字のサイズは?


13

7セグメントの数字は、文字を使用してASCIIで表すことができます_|。サイズの1数字は次のとおりです。

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

各セグメントを比例的に長くすることにより、より大きなサイズが形成されます。ここにカップルサイズの3桁があります。

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

ゴール

この課題では、1桁を入力としてそのサイズを特定できるプログラム/関数を作成します。キャッチ:入力が有効な数字でない場合、プログラムはを出力する必要があります0

これはcode-golfであり、バイト数が最も少なくなります。

STDINまたは引数として数字を受け取り、値を出力/返すことができるプログラムまたは関数を作成できます。

数字は、複数行の文字列として提供され、完全な長方形にするために必要な最小限の末尾の空白が埋め込まれます。末尾の改行は、入力のオプション部分です。不要な先行スペースはありません。

数字以外が渡されても、_|文字で構成され、長方形に埋め込まれ、不要な先行スペースはありません。空白行はありません。空の入力を処理する必要はありません。

出力は、オプションの末尾の改行を含む、単一の非負整数でなければなりません。入力が任意のサイズの適切な数字でない場合、output 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

@ETHproductions私は知っています。
PhiNotPi

@ETHproductionsにその要件がなかった場合、codegolf.stackexchange.com
Level River Stの

回答:


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}

無効な入力が非常に多いため、これを行う唯一の方法は、すべての正しい数字を生成し、入力が一致するかどうかを確認することでした。

各桁を下から上に、2つの半分と上の行で作成します。(左のセグメントは、オフ、またはの場合であってもよいことを考慮12個の可能性があるものの、3そして7完全に存在しない)のみ7は、符号化の実際に存在し、注意深い選択は、Aに符号化される(一番上の行を除く)すべての情報を可能にします単一の文字。

数字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[
"__.
..|
..|
..|
..|
"]
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.