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条回答
做自己的国王
2楼-- · 2019-01-31 07:37

C#, 239 231 229 chars (292 bytes)

I'm a bit late but this just looked like fun.

using C=System.Console;class P{static void Main(){var t=C.ReadLine();for(int
b=15,s;b>0;b-=3){foreach(var c in t)for(s=0;s++<4;)C.Write(s>3||c<33?' ':((
"翇篭篯礧歮禧禤祯寭璗牯宭䤧彭忭筯篤筿殭秏璒孯孪寿咕寏犧"[c<34?0:c-64])&1<<b-s)>0
?c:' ');C.WriteLine();}}}
查看更多
等我变得足够好
3楼-- · 2019-01-31 07:40

Python 2.6, 251 - 243 - 227 characters

I tried a slightly different approach (bitpacking the parts the letters are made of) ...

  • handles uppercase letters, spaces, exclamation mark.
  • 2 unnecessary linebreaks added here for readability (the for loop could be one line)
  • be sure to save this as UTF-8 with BOM!

As always, all comments and suggestions welcome! Contender for shortest Python solution (again a few characters behind at the moment)...

w=raw_input()
for l in range(5):print''.join("1111 11  11  1   1   "
[int(("%05d"%ord(u"<ϳϲࢬ禉ऐऒ࠾⬃ᅘᖆⰯ囌❿✛іϾь穏ࠂᅜ⭦⭪⫸㡩⬪㰼"
[max(0,ord(c)-64)]))[l])*3:][:3].replace("1",c)+" "for c in w)
查看更多
Emotional °昔
4楼-- · 2019-01-31 07:44

C89, 345 characters

Newlines added for your sanity (they are not included in the character count and can/should be deleted):

char o[5][99];
d[]={0x2df7fbef,0x3927bb6b,0x396792cf,0x3da7dbed,0x3a4bfb27,0x2d76f249,0x2dbedbfd,0x3db793ef,0x3fb7daeb,0x3ce7a497,0x3db6ab6d,0x3ff6d495,0x3cf6f2a7,0x38ff8000};
c,i,j;
main(){memset(o,32,495);
while((c=getchar())>0){
for(j=0;j<15;j++)
o[j/3][i+j%3]=d[c-33?(c-65)/2:13]>>((c&1)*15+j)&1?c:32;i+=4;}
for(j=0;j<5;j++)printf("%.*s\n",i,o[j]);}
查看更多
成全新的幸福
5楼-- · 2019-01-31 07:44

F#, 382 chars

I compressed two letters from each row into an ascii-printable byte, and handled space and exclamation point specially.

let s,(!)=stdin.ReadLine(),printf"%s"
for n in 0..4 do
 for c in s do if c=' '||n=3&&c='!'then !"    "elif c='!'then !"!!! "else for x in 0..3 do printf"%c"(if(Array.collect(fun b->let B n=int b&&&n=0 in[|(B 64)||not(B 8);B 32;B 16;true;B 4;B 2;B 1;true|])"wvwuwTUwvwUUWUEDEiTwUUBURQwEfWidWWVrUrrUEDUmTUTuZUr\\WvtuwWUturruw"B).[n*104+(int c-int 'A')*4+x]then ' 'else c)
 !"\n"

Sample I/O below:

HELLO WORLD!!!
H H EEE L   L   OOO     W W OOO RR  L   DD  !!! !!! !!!
H H E   L   L   O O     W W O O R R L   D D !!! !!! !!!
HHH EE  L   L   O O     WWW O O RR  L   D D !!! !!! !!!
H H E   L   L   O O     WWW O O R R L   D D
H H EEE LLL LLL OOO     WWW OOO R R LLL DD  !!! !!! !!!

ABCDEFGHIJKL
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

MNOPQRSTUVWXYZ
M M N N OOO PPP QQQ RR  SSS TTT U U V V W W X X Y Y ZZZ
MMM NNN O O P P Q Q R R S    T  U U V V W W  X  Y Y   Z
M M NNN O O PPP Q Q RR  SSS  T  U U V V WWW  X  YYY  Z
M M N N O O P   QQQ R R   S  T  U U V V WWW  X    Y Z
M M N N OOO P   QQQ R R SSS  T  UUU  V  WWW X X YYY ZZZ
查看更多
神经病院院长
6楼-- · 2019-01-31 07:46

Haskell, 313 316 320

import Data.Bits
import Data.Char
c&True=c
c&_=' '
a ' '='@'
a '!'='['
a c=c
q s=unlines[s>>= \c->take 3(drop(84*n+3*(ord(a c)-64))$map((c&).testBit(0xffdebaf79f6fbfde7bfe8062f6a979b69b55a4d368ebaf6aeefbe9717add3f8f2ab6a36dbf9b1524d368fedb6fefff69bfdffbff8::Integer))[0..])++" "|n<-[0..4]]
main=getLine>>=putStr.q

For the curious, the large number is the hex version of encoding in the following code. The number is simply used as a bitmap. I've had no success with further shortening the code by encoding the number to other bases even with non-standard character representations.

formats :: [String] -- order: [ A-Z!] <- that's a space in front of A
formats = [
    "   AAABBBCCCDD EEEFFFGGGH HIIIJJJK KL  M MN NOOOPPPQQQRR SSSTTTU UV VW WX XY YZZZ!!!"
  , "   A AB BC  D DE  F  G  H H I   JK KL  MMMNNNO OP PQ QR RS   T U UV VW W X Y Y  Z!!!"
  , "   AAABBBC  D DEE FF G GHHH I   JKK L  M MNNNO OPPPQ QRR SSS T U UV VWWW X YYY Z !!!"
  , "   A AB BC  D DE  F  G GH H I J JK KL  M MN NO OP  QQQR R  S T U UV VWWW X   YZ     "
  , "   A ABBBCCCDD EEEF  GGGH HIIIJJJK KLLLM MN NOOOP  QQQR RSSS T UUU V WWWX XYYYZZZ!!!"
  ]

charToBool :: Char -> Bool
charToBool ' ' = False
charToBool _   = True

boolToInteger :: Bool -> Integer
boolToInteger True = 1
boolToInteger _    = 0

encoding :: Integer
encoding = foldr f 0 $ zip [0..] $ map charToBool $ concat formats
  where
    f (pow, bool) z = z + ((2^pow) * boolToInteger bool)
查看更多
\"骚年 ilove
7楼-- · 2019-01-31 07:47

Python, 233 231 225 224 205 chars

Candidate for the shortest python solution here :-)
It is a two-liner - last line was broken in 3 for urrrr readability

s=raw_input()
for i in range(5):print' '.join(''.join((' ',c)[ord(
'W_E__U__QQ_QN_UQ_EA_Q]_D_Q_QYQ__D[_PP_B__F__Q__EG_Y__EZWU]A_A_P_OPO_\\_QNQWT_YUS'
[max(0,3*ord(c)-192-k)])>>i&1]for k in(2,1,0))for c in s)

ps. Thanks for comments, corrected issues and now using 7-bit ASCII only!

Test banner:

TTT H H EEE     QQQ U U III CCC K K     BBB RR  OOO W W N N     FFF OOO X X     JJJ U U M M PPP SSS     OOO V V EEE RR      TTT H H EEE     L   AAA ZZZ Y Y     DD  OOO GGG !!!
 T  H H E       Q Q U U  I  C   K K     B B R R O O W W NNN     F   O O  X        J U U MMM P P S       O O V V E   R R      T  H H E       L   A A   Z Y Y     D D O O G   !!!
 T  HHH EE      Q Q U U  I  C   KK      BBB RR  O O WWW NNN     FF  O O  X        J U U M M PPP SSS     O O V V EE  RR       T  HHH EE      L   AAA  Z  YYY     D D O O G G !!!
 T  H H E       QQQ U U  I  C   K K     B B R R O O WWW N N     F   O O  X      J J U U M M P     S     O O V V E   R R      T  H H E       L   A A Z     Y     D D O O G G    
 T  H H EEE     QQQ UUU III CCC K K     BBB R R OOO WWW N N     F   OOO X X     JJJ UUU M M P   SSS     OOO  V  EEE R R      T  H H EEE     LLL A A ZZZ YYY     DD  OOO GGG !!!
查看更多
登录 后发表回答