r/dailyprogrammer • u/oskar_s • Jun 26 '12
[6/26/2012] Challenge #69 [easy]
Write a program that takes a title and a list as input and outputs the list in a nice column. Try to make it so the title is centered. For example:
title: 'Necessities'
input: ['fairy', 'cakes', 'happy', 'fish', 'disgustipated', 'melon-balls']
output:
+---------------+
| Necessities |
+---------------+
| fairy |
| cakes |
| happy |
| fish |
| disgustipated |
| melon-balls |
+---------------+Bonus: amend the program so that it can output a two-dimensional table instead of a list. For example, a list of websites:
titles: ['Name', 'Address', 'Description']
input: [['Reddit', 'www.reddit.com', 'the frontpage of the internet'],
['Wikipedia', 'en.wikipedia.net', 'The Free Encyclopedia'],
['xkcd', 'xkcd.com', 'Sudo make me a sandwich.']]
output:
+-----------+------------------+-------------------------------+
| Name | Address | Description |
+-----------+------------------+-------------------------------+
| Reddit | www.reddit.com | the frontpage of the internet |
+-----------+------------------+-------------------------------+
| Wikipedia | en.wikipedia.net | The Free Encyclopedia |
+-----------+------------------+-------------------------------+
| xkcd | xkcd.com | Sudo make me a sandwich |
+-----------+------------------+-------------------------------+- Thanks to Medicalizawhat for suggesting this problem at /r/dailyprogrammer_ideas (a version of this problem was originally posted here). If you have a problem you think would be good for us, head over there and post it!
17
Upvotes
2
u/JensjD Jun 28 '12 edited Jun 28 '12
first attempt at solving one of these, its close but not quite. Firstly I am having troubles the condition that perhaps the title is longer and formatting that correctly. the second thing is why do i have a single space b/n the + and --- characters.
p.s. whats the trick to tabbing in 4 spaces with having to physically press spacebar each time?
1
u/pyronautical Jun 29 '12
I usually hang out over at program euler, But I love Oskar's work so I'll join in over here too :)
Here is my solve in C#
Running here : http://ideone.com/FRmp4
2
u/_Daimon_ 1 1 Jun 26 '12 edited Jun 26 '12
c++
If anyone could tell me how to do printf("| %-13s |\n", item); where 13 is replaced by an variable or preprocessor value, then I'd be much appreciative :)
EDIT: Did find a solution. See code. Updated code below with the improved solution.
2
u/mrpants888 Jun 26 '12
Ruby. Weee
2
u/Erocs Jun 28 '12
Python 2.7, handles both the challenge and the bonus.
2
u/kais58 Jun 27 '12
It sort of works, hacked together from part of a coursework I did earlier this year, in Java.
1
u/oohay_email2004 Jun 26 '12
My attempt, in Python: https://github.com/hitbox/dailyprogrammer-challenge-69-easy
1
u/loonybean 0 0 Jun 26 '12 edited Jun 26 '12
Python 2.5:
You can control padding, the character used for borders, and the minimum length of a list.
Output for the first question (with minimum length 10):
Answer for bonus question:
Output for bonus question (you need to pass the names together, addresses together etc.):
1
u/acero Jun 26 '12
Python:
Sample output:
1
u/SwimmingPastaDevil 0 0 Jun 26 '12
Feels like a bad hackjob.
Output:
1
u/_Daimon_ 1 1 Jun 26 '12
Python
Solution of bonus question
1
u/scurvebeard 0 0 Jun 27 '12
Wow. Usually I can think of a way to hack it or approximate the desired result with my limited Python skill.
This time I am completely out of my element. >< Oh well, next time then. Thanks for the post!
1
u/DashAnimal Jun 27 '12
I've only started programming last week... so I'm not sure how 'efficient' this code or anything. I'd love some words of advice if anybody has any.... even if it is how to easily paste code on reddit :P.
First Problem: http://ideone.com/KOFi0 Bonus: http://ideone.com/Rho2A
1
u/[deleted] Jun 28 '12 edited Jun 28 '12
1
u/aimlessdrive Jul 05 '12
C++:
First attempt at one of these. Took me forever. Works and gets 'titles' line and 'input' line from the file specified in run arguments. Very clunky. Majority of my time was spent figuring out how to read the data in:
1
u/Th3D0ct0r Jul 08 '12
Python:
1
u/NattyBroh Jul 13 '12
Got a little carried away with this. It's my first submission to this subreddit and I only just started learning Python a few days ago so I'm sure this is extremely sloppy (especially after looking at the others)...
Sample output:
1
u/cdelahousse Jul 28 '12
Javascript:
1
u/[deleted] Jun 26 '12
This is blatantly cheating; "boxes" are a built-in J type for grouping nested data and stuff. They're drawn using +-| internally.
Note that the code really comes down to this:
The titles aren't centered because that would require actual effort :D