8
\$\begingroup\$

You must take input in the form of

title|line1|line2|(...)|line[n]

And output an information card. It's hard to explain how to make the card, so here's a quick example:

Input

1234567890|12345|1234567890123|pie|potato|unicorn

Output

/===============\
|  1234567890   |
|===============|
| 12345         |
| 1234567890123 |
| pie           |
| potato        |
| unicorn       |
\---------------/

Detailed specifications:

  • The title must be centered (if there are an odd number of characters you can decide whether to put an extra space after or before it).
  • The remaining lines must be left-aligned.
  • All of them must have at least one space of padding before and after.
  • Each line must be the same length.
  • The lines must be the smallest length possible in order to fit all of the text.
  • The first and last character of each row (except for the first and last rows) must be a |.
  • There must be a /, a row of =s, and a \ in the line right before the title. (the first line)
  • There must be a |, a row of =s. and a | in the line right after the title. (the third line)
  • There must be a \, a row of -s, and a / in the last line.
  • For the example input provided, your program's output must exactly match the example output provided.
  • The input will always contain at least one |; your programs behaivior when a string like badstring is input does not matter.

This is so the shortest code in character count wins.

\$\endgroup\$
2
  • \$\begingroup\$ In python input() interprets the input as python code. Is it ok to use input and take the input in quotes, if so do we need to add 2 for the quotes? \$\endgroup\$
    – user8777
    Commented Sep 25, 2013 at 0:00
  • \$\begingroup\$ @LegoStormtroopr I suppose that's okay, but add +2 for quotes then \$\endgroup\$
    – Doorknob
    Commented Sep 25, 2013 at 1:27

9 Answers 9

4
\$\begingroup\$

Ruby, 153 147 140 characters

h,*d=s=gets.chop.split(?|)
puts"/%s\\
|%s|
|%s|"%[e=?=*k=2+l=s.map(&:size).max,h.center(k),e]
d.map{|x|puts"| %%-%ds |"%l%x}
$><<?\\+?-*k+?/

Double format string in the second-to-last line :D

\$\endgroup\$
12
  • \$\begingroup\$ l=s.map(&:size).max saves 4 \$\endgroup\$ Commented Sep 24, 2013 at 5:28
  • \$\begingroup\$ Also no need for parentheses around k=l+2 (2 chars), center(k) (2 chars), k=2+l=s.... (2 chars). \$\endgroup\$
    – Howard
    Commented Sep 24, 2013 at 7:39
  • \$\begingroup\$ @Howard I fixed the first one, but I'm not sure where you're seeing the second or third ones...? \$\endgroup\$
    – Doorknob
    Commented Sep 24, 2013 at 12:15
  • \$\begingroup\$ "| %s |"%h.center(l) -> "|%s|"%h.center(k) which removes 2 spaces. And l=s.map(&:size).max;e=?=*k=l+2" -> e=?=*k=2+l=s.map(&:size).max \$\endgroup\$
    – Howard
    Commented Sep 24, 2013 at 17:13
  • \$\begingroup\$ @How Thanks, but the last one will break if the title is the longest word. Fixed the first. \$\endgroup\$
    – Doorknob
    Commented Sep 24, 2013 at 17:23
4
\$\begingroup\$

Python 2.7 - 168 = 166 + 2 for quotes.

This assumes we can accept the input with surrounding quotes, hence the possible +2. Either way +2 is better than the +4 required if you use raw_input() vs. just input().

b="|"
s=input().split(b)
w=max(map(len,s))
x=w+2
S="| %s |"
print"\n".join(["/"+"="*x+"\\",S%s[0].center(w),b+"="*x+b]+[S%i.ljust(w)for i in s[1:]]+["\\"+"-"*x+"/"])

Thanks to Volatility for the max(map(len,s)) tip. A very cool tip.

edit: Corrected top row problem.

And the above outputs this:

> python i.py
"1234567890|12345|1234567890123|pie|potato|unicorn"
/===============\
|   1234567890  |
|===============|
| 12345         |
| 1234567890123 |
| pie           |
| potato        |
| unicorn       |
\---------------/
\$\endgroup\$
9
  • 2
    \$\begingroup\$ This doesn't work in either Python 2 or 3. You should either change input to raw_input or use print as a function. Also, change len(max(s,key=len)) into max(map(len,s)) and get rid of the space before for i in s[1:] to save 5 chars \$\endgroup\$
    – Volatility
    Commented Sep 24, 2013 at 6:31
  • \$\begingroup\$ I've checked in the past using input() and assuming the text is entered as a string is usually permissable. I'll squeeze your other suggestions in later. Thanks :) \$\endgroup\$
    – user8777
    Commented Sep 24, 2013 at 6:57
  • 2
    \$\begingroup\$ @LegoStormtroopr If you assume the input in a slightly different format you should: 1) Ask if it is admissable to the OP, 2) State it somewhere. Also, when doing this, I think it would be correct to also provide a sample that uses the format explained in the question. \$\endgroup\$
    – Bakuriu
    Commented Sep 24, 2013 at 13:11
  • \$\begingroup\$ @Bakuriu Done!! \$\endgroup\$
    – user8777
    Commented Sep 25, 2013 at 1:41
  • \$\begingroup\$ I count 165 characters (+2=167 total). \$\endgroup\$
    – Doorknob
    Commented Sep 25, 2013 at 13:00
4
\$\begingroup\$

Charcoal, 66 bytes

≔∕S|ι≔⁺⌈EιLκ²ς/×=ς↓\↓⁺Lι¹←/←ς↑\↑⁺Lι¹↘M∕⁻ςL§ι⁰¦²→P§ι⁰M±²¦¹P×=ςM↘✂ι¹

My first Charcoal answer!

I wasn't getting a proper result with reflection, so I made the box manually.

Try it online!

Charcoal (Verbose), 385 bytes

Assign(Divide(InputString,"|"),i)
Assign(Add(max(Each(i,Length(k))),2),v)
Print("/")
Print(Times("=",v))
Print(:Down,"\\")
Print(:Down,Length(i)+1)
Print(:Left,"/")
Print(:Left, v)
Print(:Up, "\\")
Print(:Up,Length(i)+1)
Move(:DownRight)
Move(Minus(v,Length(AtIndex(i,0)))/2,:Right)
MultiPrint(AtIndex(i,0))
Move(Negate(2),1)
MultiPrint(Times("=",v))
Move(:DownRight)
Print(Slice(i,1))

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Nice answer! Unrelated: How many of you got my 1+ Christmas gift on 12/25/2020? \$\endgroup\$ Commented Jan 29, 2021 at 10:36
  • \$\begingroup\$ I don't remember getting one, I think \$\endgroup\$
    – Razetime
    Commented Jan 29, 2021 at 10:38
  • \$\begingroup\$ That's pretty sad, I worked hard on that one :( it's an 1+ chirstmas tree program I posted on twtxt :) \$\endgroup\$ Commented Feb 7, 2021 at 4:10
  • \$\begingroup\$ @null haven't checked twtxt in a while. I'll see it! \$\endgroup\$
    – Razetime
    Commented Feb 7, 2021 at 4:19
  • \$\begingroup\$ Tell Lyxal I'm back ;) \$\endgroup\$ Commented Jul 26, 2021 at 16:24
3
\$\begingroup\$

GolfScript, 97 characters

'|'/{'  ':!{1/*}:E~}%.{,}%$-1=:L'='*:§'/\\'E\(.,L\-)2/{!E}*L<'||':^E§^E@{!L*+L<^E}/L'-'*'\\/'E]n*

Example:

> 1234567890|12345|1234567890123|pie|potato|unicorn

/===============\
|   1234567890  |
|===============|
| 12345         |
| 1234567890123 |
| pie           |
| potato        |
| unicorn       |
\---------------/
\$\endgroup\$
1
  • \$\begingroup\$ § that's an interesting variable name. :P \$\endgroup\$
    – Doorknob
    Commented Feb 23, 2014 at 22:59
2
\$\begingroup\$

Haskell, 292

import Data.List.Split
z=replicate
l=length
p w s=' ':s++z(w-l s)' '
c w s=p w$z(div(w-l s-1)2)' '++s
t(i:s)=let r=z m;m=(+)1$maximum$map l(i:s)in"/="++r '='++"\\\n"++"|"++c m i++"|\n|="++r '='++"|\n"++concat['|':p m x++"|\n"|x<-s]++"\\-"++r '-'++"/"
main=do l<-getLine;putStrLn$t$splitOn"|"l

… I’m a little new to this one :D

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 60 bytes

\|/DvLG⇧D\=*\/p\\+,„h⋏\|pǏ,:\|$-Ǐ,‹£Ḣƛ¥↲ðp\|pǏ,;¥���\=*\\p\/+,

Try it Online!

Longer than I'd like...

\$\endgroup\$
1
\$\begingroup\$

C# 4.5 - 375 347 chars

;) [If someone is interested to see how C# differs from others]


var c = input.Split('|').ToList();
int l = c.Select(s => s.Length).Max() + 2;
Console.WriteLine("/{0}\\\r\n| {1} |\r\n|{0}|\r\n{2}\r\n\\{3}/\r\n", "=".PadRight(l, '='), c[0].PadLeft(c[0].Length + ((l - c[0].Length) / 2)-1).PadRight(l-2), string.Join("\r\n", c.Select(s => "| " + s.PadRight(l - 2) + " |").Skip(1)), "-".PadRight(l, '-'));

\$\endgroup\$
6
  • 6
    \$\begingroup\$ You can save a LOT of characters by using one-char variable names and removing all that whitespace... \$\endgroup\$
    – Doorknob
    Commented Sep 24, 2013 at 23:31
  • 1
    \$\begingroup\$ I might be wrong but I always assume that a complete program is required unless the OP states that a code snippet is acceptable. This is a long way from a complete program. \$\endgroup\$ Commented Sep 25, 2013 at 0:00
  • 2
    \$\begingroup\$ Brought it down to 305 simply by renaming variables and removing whitespace: var c=input.Split('|').ToList();int l=c.Select(s=>s.Length).Max()+2;Console.WriteLine("/{0}\\\r\n| {1} |\r\n|{0}|\r\n{2}\r\n\\{3}/\r\n","=".PadRight(l,'='),c[0].PadLeft(c[0].Length+((l-c[0].Length)/2)-1).PadRight(l-2),string.Join("\r\n",c.Select(s=>"| "+s.PadRight(-2)+" |").Skip(1)),"-".PadRight(l,'-')); \$\endgroup\$
    – Doorknob
    Commented Sep 25, 2013 at 1:58
  • \$\begingroup\$ Thanks Doorknob. Can't we recognize minimum functions calls instead of total chars? That helps us [includes solutions providers also]. Because after sometime [after a year], it would be hard to understand code written by me and that also helps others to understand code easily. \$\endgroup\$
    – vrluckyin
    Commented Sep 25, 2013 at 17:28
  • 4
    \$\begingroup\$ The whole point of code golf is to solve a task in the least amount of characters... en.wikipedia.org/wiki/Code_golf \$\endgroup\$
    – Doorknob
    Commented Sep 25, 2013 at 21:08
1
\$\begingroup\$

Javascript, 373 364

l="length";m=" ";a="\\";b="/";c="|";p=m+c;v="join";u="concat";o=[];q=Array;i=prompt().split(c);j=i.slice().sort(function(d,e){return e[l]-d[l]})[0][l]+3;i=i.map(function(d,e){s=q(o[l]=0|(e?j-d[l]-1:(j-d[l])/2))[v](m);return(e?p:c+s+(d[l]&1?m:""))+d+(e?s:s+m)+p});x=q(o[l]=j)[v]("=");r=[b+x+a,i.shift(),c+x+c][u](i)[u](a+q(o[l]=j)[v]("-")+b);console.log(r[v]("\n"))

Output

/===============\
|  1234567890   | 
|===============|
| 12345         | 
| 1234567890123 | 
| pie           | 
| potato        | 
| unicorn       | 
\---------------/

Edit Notes:
Thanks @Doorknob!: -9 Characters =)

\$\endgroup\$
3
  • \$\begingroup\$ There should be a lot of space for golfing left \$\endgroup\$
    – C5H8NNaO4
    Commented Sep 25, 2013 at 12:13
  • 2
    \$\begingroup\$ Some things I see: o={length:0} --> o=[] (-8 chars), p="| " --> p=c+m (-1 char) \$\endgroup\$
    – Doorknob
    Commented Sep 25, 2013 at 12:19
  • \$\begingroup\$ @Doorknob Oh... thanks =), i just wonder right now why i added length at all, that makes no sense. \$\endgroup\$
    – C5H8NNaO4
    Commented Sep 25, 2013 at 12:23
1
\$\begingroup\$

Java - 549 492 468 453 432

class a{static<T>void p(T p){System.out.println(p);}public static void main(String[]a){String b="/=";int l=0,t,i;a=a[0].split("\\|");for(String s:a)l=(t=s.length())>l?t:l;for(t=0;t<l;t++)b+="=";b+="=\\";p(b);i=a[0].length();p(String.format("| %"+(l+i)/2+"s%"+(l-i)/2+"s |",a[0]," "));b="|=";for(t=0;t<l;t++)b+="=";b+="=|";p(b);for(t=1;t<a.length;t++)p(String.format("| %-"+l+"s |",a[t]));b="\\-";for(t=0;t<l;t++)b+="-";b+="-/";p(b);}}

With line breaks and tabs

class a{
    
    static<T>void p(T p){System.out.println(p);}

    public static void main(String[]a){
        a=a[0].split("\\|");
        String b="/=";
        int l=0,t,i;

        for(String s:a)l=(t=s.length())>l?t:l;
        
        for(t=0;t<l;t++)b+="=";
        b+="=\\";
        p(b);
        
        p(String.format("| %"+(l+i)/2+"s%"+(l-i)/2+"s |",a[0]," "));
        
        b="|=";
        for(t=0;t<l;t++)b+="=";
        b+="=|";
        p(b);
        
        for(t=1;t<a.length;t++)p(String.format("| %-"+l+"s |",a[t]));
        
        b="\\-";
        for(t=0;t<l;t++)b+="-";
        b+="-/";
        p(b);
    }
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.