Code Golf - Banner Generation

2019-01-31 06:47发布

When thanking someone, you don't want to just send them an e-mail saying "Thanks!", you want to have something FLASHY:

Input: THANKS!!
Output:
TTT H H AAA N N K K SSS !!! !!! 
 T  H H A A NNN K K S   !!! !!! 
 T  HHH AAA NNN KK  SSS !!! !!! 
 T  H H A A N N K K   S            
 T  H H A A N N K K SSS !!! !!! 

Write a program to generate a banner. You only have to generate upper-case A-Z along with spaces and exclamation points (what is a banner without an exclamation point?). All characters are made up of a 3x5 grid of the same character (so the S is a 3x5 grid made of S). All output should be on one row (so no newlines). Here are all the letters you need:

Input: ABCDEFGHIJKL
Output:
AAA BBB CCC DD  EEE FFF GGG H H III JJJ K K L
A A B B C   D D E   F   G   H H  I    J K K L
AAA BBB C   D D EE  FF  G G HHH  I    J KK  L
A A B B C   D D E   F   G G H H  I  J J K K L
A A BBB CCC DD  EEE F   GGG H H III JJJ K K LLL

Input: MNOPQRSTUVWX
Output:
M M N N OOO PPP QQQ RR  SSS TTT U U V V W W X X
MMM NNN O O P P Q Q R R S    T  U U V V W W  X
M M NNN O O PPP Q Q RR  SSS  T  U U V V WWW  X
M M N N O O P   QQQ R R   S  T  U U V V WWW  X
M M N N OOO P   QQQ R R SSS  T  UUU  V  WWW X X

Input: YZ!
Output:
Y Y ZZZ !!!
Y Y   Z !!!
YYY  Z  !!!
  Y Z
YYY ZZZ !!!

The winner is the shortest source code, as counted by the number of bytes it takes to store the file in utf-8 encoding. Source code should read input from stdin, output to stdout. You can assume input will only contain [A-Z! ]. If you insult the user on incorrect input, you get a 10 character discount =P.

I was going to require these exact 28 characters, but to make it more interesting, you can choose how you want them to look - whatever makes your code shorter! To prove that your letters do look like normal letters, show the output of the last three runs.


Shortest codes so far, in characters (utf8 encoding if non-ASCII present):

133 J

205 Python

209 Ruby

313 Haskell

345 C89

382 F#

14条回答
The star\"
2楼-- · 2019-01-31 07:25

Perl, 69 74 77 78 79 chars

$a=<>;s:(?{$z=substr$a,$-[0]/4,1})z|#:$z:g,print for`figlet -f3x5 $a`

Sample output:

% echo ABCDEFGHIJKLMNOPQRS | perl banner.pl

 A  BB   CC DD  EEE FFF  GG H H III  JJ K K L   M M NNN  O  PP   Q  RR   SS 
A A B B C   D D E   F   G   H H  I    J K K L   MMM N N O O P P Q Q R R S   
AAA BB  C   D D EE  FF  G G HHH  I    J KK  L   MMM N N O O PP  Q Q RR   S  
A A B B C   D D E   F   G G H H  I  J J K K L   M M N N O O P    QQ R R   S 
A A BB   CC DD  EEE F    GG H H III  J  K K LLL M M N N  O  P     Q R R SS  
% echo TUVWXYZ \! | perl banner.pl 

TTT U U V V W W X X Y Y ZZZ      !  
 T  U U V V W W X X Y Y   Z      !  
 T  U U V V WWW  X   Y   Z       !  
 T  U U V V WWW X X  Y  Z           
 T  UUU  V  W W X X  Y  ZZZ      !  

I assume you have figlet and this figlet font installed on your system. :)

查看更多
相关推荐>>
3楼-- · 2019-01-31 07:30

Ruby : 207 215 252 345 characters

i=gets.chomp;5.times{|t|p i.gsub(/./){|c|j=3*(c>?@?c.ord-64:0);(3*t..3*t+2).map{|d|"mini5mbmzjf2bqjmof3prl72i5pn138iuhylmkpi65i278kq3qjfaihyjb66787odp8ktiy5hwt78tmnb"[j..j+2].to_i(36)[d]==1?c:" "}.join+" "}}
查看更多
贪生不怕死
4楼-- · 2019-01-31 07:31

Python: 259 chars

Not the shortest, but considering it was my first Python script, I'm more than satisfied.

k=raw_input()
for i in range(5):print' '.join(''.join((' ',x)[int(z)]for z in bin(int(''.join('%02d'%(ord(q)-43)for q in'xwxvxabxwxbbdqbXWX@axbbUb_^qxXwd@kddcsbssqbXWvDabav7bs9+dwuvxdbuvssvxq')[i*28+'ABCDEFGHIJKLMNOPQRSTUVWXYZ! '.find(x)],8))[2:])for x in k)
查看更多
smile是对你的礼貌
5楼-- · 2019-01-31 07:32

Figlet (0 chars)

wget -q 3.ly/gzkv;figlet -f b

Sample output:

% wget -q 3.ly/gzkv;figlet -f b ABCDEFGHIJKLMNOPQRS

 A  BB   CC DD  EEE FFF  GG H H III  JJ K K L   M M NNN  O  PP   Q  RR   SS 
A A B B C   D D E   F   G   H H  I    J K K L   MMM N N O O P P Q Q R R S   
AAA BB  C   D D EE  FF  G G HHH  I    J KK  L   MMM N N O O PP  Q Q RR   S  
A A B B C   D D E   F   G G H H  I  J J K K L   M M N N O O P    QQ R R   S 
A A BB   CC DD  EEE F    GG H H III  J  K K LLL M M N N  O  P     Q R R SS 
% wget -q 3.ly/gzkv;figlet -f b TUVWXYZ \!

TTT U U V V W W X X Y Y ZZZ     !!! 
 T  U U V V W W X X Y Y   Z     !!! 
 T  U U V V WWW  X   Y   Z      !!! 
 T  U U V V WWW X X  Y  Z           
 T  UUU  V  W W X X  Y  ZZZ     !!! 
查看更多
Summer. ? 凉城
6楼-- · 2019-01-31 07:34

J, 133 135 79 83 84 88 characters (utf-8 encoding)

;/5 3$"1(' ',.s){~"1#:3 u:(ucp'翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧'){~0>.64-~a.i.s=:

Usage:

    ;/5 3$"1(' ',.s){~"1#:3 u:(ucp'翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧'){~0>.64-~a.i.s=:'ABCDEFGHIJKLMNOPQRSTUVWXYZ !'
┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┐
│AAA│BBB│CCC│DD │EEE│FFF│GGG│H H│III│JJJ│K K│L  │M M│N N│OOO│PPP│QQQ│RR │SSS│TTT│U U│V V│W W│X X│Y Y│ZZZ│   │!!!│
│A A│B B│C  │D D│E  │F  │G  │H H│ I │  J│K K│L  │MMM│NNN│O O│P P│Q Q│R R│S  │ T │U U│V V│W W│ X │Y Y│  Z│   │!!!│
│AAA│BBB│C  │D D│EE │FF │G G│HHH│ I │  J│KK │L  │M M│NNN│O O│PPP│Q Q│RR │SSS│ T │U U│V V│WWW│ X │YYY│ Z │   │!!!│
│A A│B B│C  │D D│E  │F  │G G│H H│ I │J J│K K│L  │M M│N N│O O│P  │QQQ│R R│  S│ T │U U│V V│WWW│ X │  Y│Z  │   │   │
│A A│BBB│CCC│DD │EEE│F  │GGG│H H│III│JJJ│K K│LLL│M M│N N│OOO│P  │QQQ│R R│SSS│ T │UUU│ V │WWW│X X│YYY│ZZZ│   │!!!│
└───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┴───┘

    ;/5 3$"1(' ',.s){~"1#:3 u:(ucp'翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧'){~0>.64-~a.i.s=:'this is incorrect input.'
|index error

Explanation (NB. is comment in J):

;/              NB. String together along the third dimension...
5 3$"1          NB. ... reshape each line to 5x3...
(' ',.s)        NB. ... a space before each letter of the input string...
{~"1            NB. ... indexed using...
#:              NB. ... the (15 bit) binary representation of ...
3 u:            NB. ... the integer representation of...
(ucp'翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧')  ... the unicode versions of these code points...
{~              NB. ...indexed using...
0>.             NB. ...the max of 0 and...
64-~            NB. ...64 less than...
a.i.            NB. the ascii indexes of s
s=:             NB. Assign the input string to the variable s.
查看更多
时光不老,我们不散
7楼-- · 2019-01-31 07:36

Delphi, 397 chars

Ok, with all the begin/end statements Delphi probably will never be shorter than any other languages, but I do see a challenge in getting it as short as possible.

vvar s:String;i,j,k:Word;const F:Array[65..92]of Word=($5BEA,$3AEB,$624E,$3B6B,$72CF,$12CF,$6B4E,$5BED,$7497,$2B26,$5AED,$7249,$5BFD,$5B6F,$2B6A,$12EB,$4D6A,$5AEB,$388E,$2497,$7B6D,$2B6D,$5FED,$5AAD,$24AD,$72A7,$2092,$0000);begin S:=ParamStr(1);for j:=0 to 4 do begin for k:=1 to Length(S)do begin for i := 0 to 2 do Write((' '+S[k])[1+(F[ord(S[k])]shr(i+j*3))and 1]);Write(' ');end;WriteLn;end;end.

The font is built up like this:

010 110 011 110 111 111 011 101 111 011 101 100 101 111 010 110 010 110 011 111 101 101 101 101 101 111 010 000
101 101 100 101 100 100 100 101 010 001 101 100 111 101 101 101 101 101 100 010 101 101 101 101 101 001 010 000
111 110 100 101 110 110 101 111 010 001 110 100 111 101 101 110 101 110 010 010 101 101 111 010 010 010 010 000
101 101 100 101 100 100 101 101 010 101 101 100 101 101 101 100 011 101 001 010 101 101 111 101 010 100 000 000
101 110 011 110 111 100 011 101 111 010 101 111 101 101 010 100 001 101 110 010 111 010 101 101 010 111 010 000

The characters in this 5x3 font take up 15 bits and are stored in word (UINT16) in this order:

00 01 02
03 04 05
06 07 08
09 10 11
12 13 14

Formatted code:

var
  s:String;    
  i, j, k: Word;

const
  F: Array [65 .. 92] of Word = (
    $5BEA,$3AEB,$624E,$3B6B,    $72CF,$12CF,$6B4E,$5BED,
    $7497,$2B26,$5AED,$7249,    $5BFD,$5B6F,$2B6A,$12EB,
    $4D6A,$5AEB,$388E,$2497,    $7B6D,$2B6D,$5FED,$5AAD,
    $24AD,$72A7,$2092,$0000);

begin
  S := ParamStr(1);
  for j := 0 to 4 do
  begin
    for k := 1 to Length(S) do
    begin
      for i := 0 to 2 do
        Write((' '+S[k])[1+(F[ord(S[k])]shr(i+j*3))and 1]);
      Write(' ');
    end;
    WriteLn;
  end;
end.
查看更多
登录 后发表回答