31
2
This is a simple one: print an ASCII Gantt chart.
Given tasks' ranges (start-time - end-time Tuples), print a Gantt timeline in the form of -
characters for each task duration - each task in a new line.
Example
Say my tasks ranges are 28->35, 34->40, 39->44
, the Gantt will look like this:
-------
------
-----
Specifications
- You can write a full program, a named function or an anonymous function.
- Your program/function should accept the tasks via STDIN or as arguments.
- Each task should be represented as a string of
start->end
wherestart
andend
are Integers. Tasks are separated by spaces or commas. Alternatively, you may get it as a Tuple of Integers, or as an Array/Collection of 2 Integers. (For example, in JavaScript you can get it as[start,end]
- this is allowed). - Any non-negative number of tasks (arguments) should be supported.
To make it clear, a single argument of tasks collection is not allowed. You can either parse a single string argument, or support zero-or-more tasks arguments. Where task is a tuple or a collection of size 2.- You can assume only valid input will be given. That means, each task has a positive duration.
- Return value does not matter, your code must print the timeline on STDOUT.
- Output: per task,
start
spaces followed by(end-start)
dashes and a\n
. - Needless to say, output lines should be ordered correspondingly with the input (tasks) order.
- Trailing spaces before the
\n
are allowed, if that helps you.
Test cases
Input:
(empty)
Output:
(empty)
Input:
0->7,5->6,3->6
Output:
-------
-
---
Input:
5->20,5->20,2->10,15->19
Output:
---------------
---------------
--------
----
Winning
- This is code-golf so the least code length (in bytes) wins.
- Traditionally, tie breaker is earlier post.
- "Standard loopholes are no longer funny".
-----
EDIT
As many of you understood that it is allowed to have a single tasks collection argument, and since there's no much different between that and the original varargs requirement, it is now allowed to have a single collection argument, if you don't want to use the varargs option, or in case your language does not support varargs.
1Point 3 seems clear. But piint 5 (
To make it clear...
) is not clear at all. – edc65 – 2015-06-29T14:37:52.837Alright, let me rephrase that: You cannot write a function that accepts exactly one argument unless it's a string. If it's a bunch of tuples we're talking about, they may be sent to your function as arguments, not wrapped in a collection. For example, in JavaScript: You may iterate
arguments
within the function, but you may not assume thatarguments[0]
is an array of tasks. – Jacob – 2015-06-29T14:43:01.603I don't understand the purpose of this limitation. In many languages accepting a list is the way you create a function with a variable number of arguments. There is often no equivalent to Javascript's
arguments
list available. – JohnE – 2015-06-29T14:49:18.403There is no hidden purpose or reason behind it. I happen to like the "varargs" coding style. I'm sorry that your language does not support it - but not all golfing challenges are suitable for all languages! You can still choose the string parsing option if you insist on a language that does not support varargs. I don't think it will be fair to allow it on some languages and not on others. – Jacob – 2015-06-29T14:54:47.660
8Why not simply allow input as an array / list / vector / etc. for all languages? Personal preference seems like a pretty weak reason. – Doorknob – 2015-06-29T14:58:55.947
1Varargs versus an argument list is a purely syntactic distinction and leaves the rules of this question up to an unnecessary and arbitrary degree of interpretation, in my opinion. – JohnE – 2015-06-29T15:03:09.297
Fine. I will edit now the question to allow it. – Jacob – 2015-06-29T15:03:48.213
Is is acceptable to use MySQL with a table containing the data? – Ismael Miguel – 2015-06-30T14:12:31.103
I'm not sure. You may use SQL / T-SQL and define a
FUNCTION
or aPROCEDURE
. – Jacob – 2015-06-30T14:13:56.327That's what I wanted to do. but I'm not sure if those SQL 'flavors' have arrays or not. And how that works. – Ismael Miguel – 2015-06-30T14:33:14.040
As far as I know, a Table-Typed Parameter is the Array equivalence in SQL :) However, please consume the table as a Procedure's input parameter, do not count on existing table in a database. You can state in your answer something like: Usage
DECLARE @G TABLE(x INT, y INT); INSERT INTO @G VALUES( ... )
and thenEXEC
your procedure on@G
. – Jacob – 2015-06-30T14:38:29.410I'm not having luck in any way. But thanks a lot for the help. I hope it is useful to someone else. – Ismael Miguel – 2015-06-30T15:48:03.240
Given that: 1)you can accept whenever you like,2)14 bytes seems impossible to beat, 3)anyway you can change your mind later ... Givel all this, It seems too early accepting an asnwer just 1 day after the question. It gives the message: ok, the challenge is over, go think about somthing else .... – edc65 – 2015-07-01T07:17:51.320
May we take two list arguments; all the start-times and all the end-times? – Adám – 2018-01-29T10:25:05.210
I’m sorry @Adám but the answer is no. It won’t be fair for existing answers. – Jacob – 2018-01-29T11:23:09.397
2@Jacob Makes sense. For future challenges, I'd recommend as lax an input spec as possible: Mangling input shouldn't be part of the challenge. – Adám – 2018-01-29T12:28:07.733
Can we take a 1-indexed input? ie
29->36, 35->41, 40->45
in place of28->35, 34->40, 39->44
– JayCe – 2018-06-18T13:07:02.223May there be zero-duration tasks? This should be mentioned in the description. – Jakob – 2018-06-18T16:39:43.660