31
\$\begingroup\$

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 where start and end 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 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.

\$\endgroup\$
  • 1
    \$\begingroup\$ Point 3 seems clear. But piint 5 (To make it clear...) is not clear at all. \$\endgroup\$ – edc65 Jun 29 '15 at 14:37
  • \$\begingroup\$ Alright, 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 that arguments[0] is an array of tasks. \$\endgroup\$ – Jacob Jun 29 '15 at 14:43
  • 8
    \$\begingroup\$ Why not simply allow input as an array / list / vector / etc. for all languages? Personal preference seems like a pretty weak reason. \$\endgroup\$ – Doorknob Jun 29 '15 at 14:58
  • 1
    \$\begingroup\$ Varargs 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. \$\endgroup\$ – JohnE Jun 29 '15 at 15:03
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – Adám Jan 29 '18 at 12:28

40 Answers 40

0
\$\begingroup\$

Perl 5 with -054naF/->/ -M5.010, 24 bytes

say"-"x$F[1]."\x0d".$"x"@F"

Try it online!

Only works in terminal because of the use of "\x0d" to overwrite the -s with s, but here's a cast of the script in action.

To recreate the script, use the below hexdump:

00000000: 7361 7922 2d22 7824 465b 315d 2e22 0d22  say"-"x$F[1]."."
00000010: 2e24 2278 2240 4622                      .$"x"@F"
\$\endgroup\$
0
\$\begingroup\$

VBA, 75 bytes

A declared routine, f(a), which takes input as an array of arrays, and outputs to the console.

Sub f(a)
For Each t In a
Debug.?Spc(t(0));String(t(1)-t(0),45)
Next
End Sub

Example I/O

f Array(Array(28,35),Array(34,40),Array(39,44))
                            -------
                                  ------
                                       -----
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 12 bytes

ε`sDð×?-'-×,

Try it online!

Takes input in the form [[5,20],[5,20],[2,10],[15,19]].

Explanation:

ε`sDð×?-'-×,    IMPLICITLY INPUT ARRAY OF ARRAYS
ε               For each array [a, b] in input:
 `sD                Push stack b, a, a (a on top)
    ð               Push space to stack : [b, a, a, ' ']
     ×?             Print a spaces without newline, stack: [b, a]
       -'-×,        Print (b - a) dashes with newline, stack empty
                Implicit end of loop
\$\endgroup\$
0
\$\begingroup\$

C++, 112*

#include<iostream>
#include<string>
int a(){int a,b;while(cin>>a>>b){cout<<string(a,' ')+string(b-a,'-')+"\n";}}

*~~or 76 depending on whether you count the include statements~~ just completely wrong... Didn't include std:: as pointed out in comments

Test

Input: 5 20 5 20 2 10 15 19

Output:

     ---------------
     ---------------
  --------
               ----
\$\endgroup\$
  • \$\begingroup\$ Don't you need include files for this? \$\endgroup\$ – Reto Koradi Jun 29 '15 at 19:27
  • \$\begingroup\$ Yes iostream and string... Should I add those to my code? \$\endgroup\$ – sudo rm -rf slash Jun 29 '15 at 19:32
  • \$\begingroup\$ I only started participating here relatively recently, so I'm not much of a rules expert. But it was my understanding that even if only a function is required, you still need to count everything needed to compile the function. You may want to ask one of the "authorities". \$\endgroup\$ – Reto Koradi Jun 29 '15 at 19:40
  • \$\begingroup\$ I'll just put it in \$\endgroup\$ – sudo rm -rf slash Jun 29 '15 at 19:41
  • \$\begingroup\$ Your input method of space separated values is just fine. And it's my understanding (though I'm often wrong so take it with a grain of salt) that you don't need the #includes for functions, but I'll defer that to someone else. \$\endgroup\$ – Alex A. Jun 29 '15 at 19:56
0
\$\begingroup\$

Japt -R, 14 10 9 bytes

If the character used for the timeline can be anything we want then one byte can be saved replacing -<space> with Ê, which will use l instead of dashes.

®Ìç- hZÎî

Try it


Explanation

®             :Map over each sub-array Z
 Ì            :  Get the last element of Z
  ç-          :  Repeat "-" that many times
     h        :  Set the characters starting at index 0 to
      ZÎ      :    Get the first element of Z
        î     :    Repeat <space> that many times
              :Implicitly join with newlines and output
\$\endgroup\$
0
\$\begingroup\$

Perl 6, 28 bytes

*.map: (' ','-'Zx*).join.say

Try it online!

Input is a list of 2-element lists:

(<0 7>,<5 6>,<3 6>)

Essentially we are using the 'b' x 3 = 'bbb' operator x between the lists ' ', '-' and * (the line input as a list), zipped via Z, hence the combined operator Zx.

In p6 golfing this seems... suboptimal.

\$\endgroup\$
0
\$\begingroup\$

Tcl, 74 bytes

proc G L {lmap s\ e $L {puts [format %$e\s [string repe - [expr $e-$s]]]}}

Try it online!


Tcl, 76 bytes

proc G L {lmap s\ e $L {puts [format %$e\s [string repeat - [expr $e-$s]]]}}

Try it online!

Tcl, 82 bytes

proc G L {lmap s\ e $L {puts [string repeat \  $s][string repeat - [expr $e-$s]]}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Momema, 75 bytes

m00*-8m+1-=+1*01+*-8-*0s0s+1-=*00+*0-1-9 32s1d0d+1-=*1 1+*1-1-9 45d1-9 10m1

Try it online!

Ungolfed:

main 0
0    *-8      # read user input
main +1-=+1*0 # terminate if zero

    1     +*-8-*0 # calculate number of '-' to print

    space 0
    space +1-=*0  # stop?
        0  +*0-1      # decrement counter
        -9 32         # print ' '
    space  1      # go back to start
    dash  0
    dash  +1-=*1  # stop?
        1  +*1-1      # decrement counter
        -9 45         # print '-'
    dash  1

    -9 10

main 1        # go to beginning
\$\endgroup\$
0
\$\begingroup\$

Canvas, 10 bytes

{┤∔α ×P-×p

Try it here!

\$\endgroup\$
0
\$\begingroup\$

K (ngn/k), 13 bytes

{" -"@&-':x}'

Try it online!

{ } function with argument x

{ }' apply to each - in this case to each pair of numbers from the input

-': subtract each prior - transform (a;b) into (a;b-a)

& "where" - transform (a;b-a) into a list of a zeroes followed by b-a ones

" -"@ use as indices in " -"

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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