Generate information cards with titles

6

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.

Doorknob

Posted 2013-09-24T02:20:14.050

Reputation: 68 138

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? – None – 2013-09-25T00:00:08.803

@LegoStormtroopr I suppose that's okay, but add +2 for quotes then – Doorknob – 2013-09-25T01:27:00.853

Answers

3

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       |
\---------------/

Howard

Posted 2013-09-24T02:20:14.050

Reputation: 23 109

§ that's an interesting variable name. :P – Doorknob – 2014-02-23T22:59:59.467

4

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

Doorknob

Posted 2013-09-24T02:20:14.050

Reputation: 68 138

l=s.map(&:size).max saves 4 – Cary Swoveland – 2013-09-24T05:28:28.093

Also no need for parentheses around k=l+2 (2 chars), center(k) (2 chars), k=2+l=s.... (2 chars). – Howard – 2013-09-24T07:39:34.953

@CarySwoveland Thanks, edited – Doorknob – 2013-09-24T12:15:16.610

@Howard I fixed the first one, but I'm not sure where you're seeing the second or third ones...? – Doorknob – 2013-09-24T12:15:33.720

"| %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 – Howard – 2013-09-24T17:13:06.660

@How Thanks, but the last one will break if the title is the longest word. Fixed the first. – Doorknob – 2013-09-24T17:23:21.040

I can't see that it breaks anything. Also you can save more chars if you move the assignments (e.g. s= into the second line - 1 char and e=.. into the %[e=... thing - 2 chars). – Howard – 2013-09-24T18:44:49.830

For the second, I think Howard means change center(l) to center l, which saves a space. – Cary Swoveland – 2013-09-24T19:38:50.033

@Howard If the title is the longest word with your improvement, it will lose the padding on both sides. The s= cannot be moved because it gives an error, but I did use your e= thing. – Doorknob – 2013-09-24T23:30:37.033

@CarySwoveland Then it parses as h.center(l,e) – Doorknob – 2013-09-24T23:37:31.347

@Doorknob but isn't k always 2 more than the length of the longest string? So it should still be padded. – Volatility – 2013-09-25T02:28:05.080

@Vola Oh yes, good point. Edited – Doorknob – 2013-09-25T02:44:11.210

The s= thing can be fixed if you put parenthesis around, i.e. h,*d=s=gets.chop.split(?|) which is still one char shorter. – Howard – 2013-09-25T05:15:32.807

4

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       |
\---------------/

user8777

Posted 2013-09-24T02:20:14.050

Reputation:

2This 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 – Volatility – 2013-09-24T06:31:20.613

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 :) – None – 2013-09-24T06:57:39.077

2@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. – Bakuriu – 2013-09-24T13:11:50.473

@Bakuriu Done!! – None – 2013-09-25T01:41:36.437

I count 165 characters (+2=167 total). – Doorknob – 2013-09-25T13:00:20.310

@Doorknob Top row should be = symbols, not -, correct? – asteri – 2013-09-30T18:56:27.417

@Jeff Yes, that's what it says in the spec. – Doorknob – 2013-09-30T19:44:56.563

@Doorknob Thats been fixed, no change in score! – None – 2013-09-30T23:35:28.563

@Lego Ok, but I still count 165, not 166. – Doorknob – 2013-09-30T23:36:03.150

2

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

Ry-

Posted 2013-09-24T02:20:14.050

Reputation: 5 283

1

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, '-'));

vrluckyin

Posted 2013-09-24T02:20:14.050

Reputation: 261

5You can save a LOT of characters by using one-char variable names and removing all that whitespace... – Doorknob – 2013-09-24T23:31:22.050

1I 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. – Igby Largeman – 2013-09-25T00:00:53.290

2Brought 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,'-')); – Doorknob – 2013-09-25T01:58:29.560

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. – vrluckyin – 2013-09-25T17:28:51.053

3

The whole point of code golf is to solve a task in the least amount of characters... http://en.wikipedia.org/wiki/Code_golf

– Doorknob – 2013-09-25T21:08:39.990

Please somebody edit the initial answers. This is god damned goal of the code golf to use a minimum amount of chars. Not to preserve readability and maintainability over the years – abatishchev – 2013-09-26T03:59:20.883

1

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 =)

C5H8NNaO4

Posted 2013-09-24T02:20:14.050

Reputation: 1 340

There should be a lot of space for golfing left – C5H8NNaO4 – 2013-09-25T12:13:53.120

2Some things I see: o={length:0} --> o=[] (-8 chars), p="| " --> p=c+m (-1 char) – Doorknob – 2013-09-25T12:19:52.477

@Doorknob Oh... thanks =), i just wonder right now why i added length at all, that makes no sense. – C5H8NNaO4 – 2013-09-25T12:23:16.743

0

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);
    }
}

asteri

Posted 2013-09-24T02:20:14.050

Reputation: 824