| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | Japt mR | 180129T101445Z | Shaggy |
| 972 | AWK | 241106T162114Z | xrs |
| 004 | Vyxal j | 210618T105729Z | emanresu |
| 008 | Stax | 210618T111630Z | Razetime |
| 090 | VBA Excel | 180816T044251Z | remoel |
| 013 | K ngn/k | 180618T064737Z | ngn |
| 010 | Canvas | 180816T071925Z | dzaima |
| 075 | R | 180618T133526Z | JayCe |
| 075 | Momema | 180621T041136Z | Esolangi |
| 074 | Tcl | 180619T205448Z | sergiol |
| 028 | Perl 6 | 180620T104722Z | Phil H |
| 112 | C++ | 150629T173957Z | sudo rm |
| 4836 | PowerShell 3.0 | 180618T222121Z | Veskah |
| 012 | 05AB1E | 180618T183349Z | Geno Rac |
| 075 | VBA | 180618T144702Z | Taylor R |
| 024 | Perl 5 with 054naF/>/ M5.010 | 180618T120148Z | Dom Hast |
| 054 | JavaScript ES8 | 180129T102114Z | Shaggy |
| 021 | J | 180129T015238Z | Jonah |
| 012 | APL Dyalog Classic | 180129T002810Z | ngn |
| 009 | Jelly | 180128T185445Z | ellie |
| 101 | Java | 150630T102751Z | Nateowam |
| 069 | C++14 | 150630T074249Z | sweerpot |
| 160 | PostgreSQL | 150703T102943Z | manatwor |
| 047 | Gema | 150702T181516Z | manatwor |
| 180 | Java 8 | 150629T135558Z | SuperJed |
| 111 | Brainfuck | 150629T183404Z | Rolf ツ |
| 059 | Scala | 150629T141128Z | Jacob |
| 6166 | JavascriptES6 | 150630T113352Z | zura |
| 041 | Perl | 150629T144800Z | manatwor |
| 089 | PHP | 150629T174250Z | JPMC |
| 108 | C | 150630T023522Z | Functino |
| 063 | JavaScript ES6 | 150629T150338Z | edc65 |
| 091 | PHP | 150629T161441Z | rink.att |
| 014 | CJam | 150629T135900Z | Optimize |
| 014 | Pyth | 150629T153924Z | Alex A. |
| 065 | CoffeeScript | 150629T153553Z | rink.att |
| 068 | JavaScript ES6 | 150629T145301Z | rink.att |
| 044 | Julia | 150629T143842Z | Alex A. |
| 076 | Haskell | 150629T150611Z | nimi |
| 039 | Python 2 | 150629T134704Z | Kade |
| 018 | K | 150629T144508Z | JohnE |
| 035 | Ruby | 150629T135317Z | manatwor |
| 055 | SWIProlog | 150629T140947Z | Fatalize |
Japt -mR, 14 10 9 7 bytes
rÈç ú-Y
rÈç ú-Y :Implicit map of 2D input array
r :Reduce each pair by
È :Passing through the following function as X & Y
ç : X spaces
ú-Y : Right pad with "-" to length Y
:Implicit output joined with newlines
AWK, -vFPAT="[0-9]+" 72 bytes
{for(i=1;i<NF;i+=2){s="";for(j=0;j++<$(i+1);)s=j<=$i?s" ":s"-";print s}}
For TIO to work right add BEGIN{FPAT="[0-9]+"}
{for(i=1;i<NF;i+=2) # number pairs
{s="";for(j=0;j++<$(i+1);) # until second number
s=j<=$i?s" ":s"-"; # space if less than first
print s}} # out
Vyxal j, 4 bytes
ε¤-↳
Try it Online! Takes two lists starts, ends.
ε # Take the difference between each pair
¤- # Repeat "-" that many times
↳ # Pad to the length of ends
Stax, 8 bytes
ÇÉ⌐öç♦}k
Explanation
mzsE|r'-&
m map each pair and print with newline
z empty string
s swap with pair
E|r convert to range
'- - character
& assign at indices(extends the empty string with spaces)
VBA (Excel), 99 90 bytes
Using Immediate Window and [A1] as input eg.0-1,2-5
Thanks to @TaylorSott for cutting some bytes.
b=Split([A1]):For x=0To Ubound(b):c=Split(b(x),"-"):?Spc(c(0)-0)String(c(1)-c(0),"-"):Next
K (ngn/k), 13 bytes
{" -"@&-':x}'
{ } 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 " -"
R, 117 90 75 bytes
function(y)for(i in 1:ncol(y))cat(" "<y[1,i],"-"<diff(y)[i],"
")
"<"=strrep
Giuseppe golfed at least 29 bytes off my original answer!
The idea is straightforward: print as many " " as necessary followed by as many "-" as required. Input is a 2*L matrix with L the number of pairs. The vectorized function diff is used to get the number of "-".
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
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
Tcl, 74 bytes
proc G L {lmap s\ e $L {puts [format %$e\s [string repe - [expr $e-$s]]]}}
Tcl, 76 bytes
proc G L {lmap s\ e $L {puts [format %$e\s [string repeat - [expr $e-$s]]]}}
Tcl, 82 bytes
proc G L {lmap s\ e $L {puts [string repeat \ $s][string repeat - [expr $e-$s]]}}
Perl 6, 28 bytes
*.map: (' ','-'Zx*).join.say
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.
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:
---------------
---------------
--------
----
PowerShell 3.0, 4836 Bytes
$args|%{" "*$_[0]+"-"*($_[1]-$_[0])}
Thanks to Mazzy for saving 12 with a better way to pass in the list
Old code and explanation:
&{param($b="")$b|%{" "*$_[0]+"-"*($_[1]-$_[0])}}
Takes arguments as a list of tuples, e.g. (5,20),(5,20),(2,10),(15,19). Had to default $b to a value to take care of the empty string because it somehow entered the foreach block when called with no input.
05AB1E, 12 bytes
ε`sDð×?-'-×,
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
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))
-------
------
-----
Perl 5 with -054naF/->/ -M5.010, 24 bytes
say"-"x$F[1]."\x0d".$"x"@F"
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"
J, 21 bytes
(' -'#~{.,-~/)"1 ::''
ungolfed
(' -' #~ {. , -~/)"1 ::''
This is essentially just J's copy verb #, but its we're copying the space character head of list {. number of times, and the hyphen character "2nd list element minus 1st list element" number of times: -~/. Sadly this forces us to have to specify the rank "1 explictly, and we need to use Adverse :: to handle the empty case.
APL (Dyalog Classic), 12 bytes
↑('-'\⍨≤∘⍳)/
APL has no varargs, so the arg here is a single Nx2 matrix.
Jelly, 13 9 bytes
ạ\⁾ -xµ€Y
Takes input as [[5, 20], [5, 20], [2, 10], [15, 19]].
-4 bytes thanks to Erik
Java, 187 181 197 183 101 bytes
void g(int[][]g){for(int[]i:g)for(int j=0;j<i[1];System.out.print(j++<i[0]?" ":j==i[1]?"-\n":"-"));}
Ungolfed (sort of):
void g(int[][] g){
for(int[] i : g)
for(int j = 0; j < i[1]; System.out.print(j++ < i[0] ? " " : j == i[1] ? "-\n" : "-"));
}
Accepts input as 2d array of ints. Thanks to masterX244 for pointing out that this is allowed by the rules.
C++14, 69 bytes
[]{int a,b;for(;cin>>a>>b;){cout<<setw(b)<<string(b-a,'-')+'\n';}}();
First time golfing, this was a good problem to start with!
PostgreSQL: 160 characters
create function g(int[])returns text as
$$select string_agg(lpad(repeat('-',$1[x][2]-$1[x][1]),$1[x][2]),chr(10))from generate_subscripts($1,1)x$$
language sql;
Sample run:
manatwork=# create function g(int[])returns text as
manatwork-# $$select string_agg(lpad(repeat('-',$1[x][2]-$1[x][1]),$1[x][2]),chr(10))from generate_subscripts($1,1)x$$
manatwork-# language sql;
CREATE FUNCTION
manatwork=# select g(array[[0,7],[5,6],[3,6]]);
-------
-
---
Gema: 47 characters
<D>-\><D><y>=@left{$1;}@repeat{@sub{$2;$1};-}\n
Sample run:
bash-4.3$ gema '<D>-\><D><y>=@left{$1;}@repeat{@sub{$2;$1};-}\n' <<< '0->7,5->6,3->6'
-------
-
---
Java 8, 280 275 246 204 195 185 180 bytes
void g(String t){for(String s:t.split(",")){String[]a=s.split("->");s="";Integer i;for(i=0;i<i.valueOf(a[0]);i++)s+=" ";for(;i<i.valueOf(a[1]);i++)s+="-";System.out.println(s);};};
A method that takes a comma-seperated input string and prints the resulting ascii Gantt Chart to stdout.
Thanks to durron597 and masterX244 for helping me save 10 bytes
Brainfuck, 120 115 111 bytes
At least it's shorter than Java :) The input is a list of bytes, where each pair is a single line in the gantt.
++++[->++++++++<]>[->+>+<<]++++++++++>>+++++++++++++>+[,[->+>+<<]>>[-<<+>>],<[->-<<<<.>>>]>[-<<<.>>>]<<<<<.>>>]
Try out
Set end-of-input to char with value \0. Example input: \5\20\5\20\2\10\15\19.
Note that setting the end-of-input value to \0 will have the side effect that no more input will be read (and thus stopping the program) when the input contains the number zero. In BF there is no other way of knowing when the input is exhausted.
Explanation*
++++[->++++++++<]> #Store <space> at index 1
[->+>+<<] #Move index 1 to index 2 and 3
++++++++++ #Increment index 1 to <newline>
>> #Move to index 3
+++++++++++++ #Increment index 3 to <dash>
> #Move to (empty) index 4
+ #Increment to start the main loop
[ #Main loop
, #Read first number to index 4
[->+>+<<]>>[-<<+>>] #Copy index 4 to index 5 (index 5 can now be altered)
, #Read second number (the number pair is now stored at index 5 and 6)
< #Move to first number (index 5)
[->-<<<<.>>>] #Decrement index 5 and 6 and print <space> until index 5 equals zero
> #move to second input (index 6)
[-<<<.>>>] #Decrement index 6 and print <dash> until index 6 equals zero
<<<<<.>>> #Print <newline> and move to index 4 (original first number)
] #End of main loop
*(You won't be able to compile/run this due to the comments)
Scala, 67 63 59 bytes
(r:Seq[(Int,Int)])⇒for((s,e)←r)(println(" "*s+"-"*(e-s)))
Usage:
res0() or res0(Seq(28->35, 34->40, 39->44)) etc.
Thanks gilad for shaving 4 bytes using a for expression!
Javascript(ES6), 61/66 chars
My answer is almost similar to the one posted by @edc65 , but with some improvements.
As tasks in single array are not allowed(so function would be called like this: a([3,4], [7,15], [0,14], [10, 15])), correct one would be this(66 chars without name assignment):
a=(...x)=>x.map(([c,d])=>console.log(' '.repeat(c)+'-'.repeat(d-c)))
And if one array argument is allowed(so fn call like this: a([[3,4], [7,15], [0,14], [10, 15]])), then it would be(61 char without assignment):
a=x=>x.map(([c,d])=>console.log(' '.repeat(c)+'-'.repeat(d-c)))
Perl: 42 41 characters
Just to have at least one solution with string parsing too.
s!(\d+)->(\d+),?!$"x$1."-"x($2-$1).$/!ge
Sample run:
bash-4.3$ perl -pe 's!(\d+)->(\d+),?!$"x$1."-"x($2-$1).$/!ge' <<< '0->7,5->6,3->6'
-------
-
---
PHP, 89 characters (function body)
function gantt($x){array_walk($x,function($a){echo str_pad(str_repeat('-',$a[1]-$a[0]),$a[1],' ',0)."\n";});}
I was going to go for reading strings, but as a lot of the entries were taking arrays of integer pairs, I figured I would follow suit for the sake of brevity.
For each tuple $a in array $x I echo a string of dashes repeated $a[1] - $a[0] times, padded up to the larger number $a[1] with spaces. Then the obligatory newline.
C: 108 bytes
void g(int*l){for(int c=0;*l>=0;c=!c,l++){if(!c)l[1]-=*l;while(l[0]-->0)putchar(c?45:32);c?putchar(10):0;}}
Ungolfed:
void gantt(int*l) {
for (int c = 0; *l >= 0; c = !c, l++) {
if (!c) l[1] -= *l;
while (l[0]-- > 0) putchar(c? 45 : 32);
c? putchar(10) : 0;
}
}
Takes as a parameter a list of integers terminated by -1. For example:
int list[] = {
28, 35,
34, 40,
39, 44,
-1
};
gantt(list);
It uses c to toggle between writing spaces and dashes.
JavaScript (ES6), 63
Edit 3 byte saved thx @apsillers
63 bytes not counting the assignment to F as an anonymous function is allowed.
A function with a variable number of parameters, as requested.
A function with a list of tasks as a single parameter.
Test running the snippet below (being EcmaScript 6, Firefox only)
F=l=>l.map(t=>console.log(' '.repeat(l=t[0])+'-'.repeat(t[1]-l)))
// TEST
// for this test, redefine console.log to have output inside the snippet
console.log = (...x) => O.innerHTML += x + '\n';
console.log('* Empty'); F([]);
console.log('\n* [0,7],[5,6],[3,6]'); F([[0,7],[5,6],[3,6]])
console.log('\n* [5,20],[5,20],[2,10],[15,19]');F([[5,20],[5,20],[2,10],[15,19]]);
<pre id=O></pre>
PHP, 94 91 bytes
Takes a list of tasks (e.g. [[5,20],[5,20],[2,10],[15,19]]). Thanks @IsmaelMiguel for the reminder of variable function names.
function x($a){$r=str_repeat;foreach($a as$v){echo$r(' ',$v[0]).$r('-',$v[1]-$v[0])."\n";}}
Original attempt: 94 bytes
function x($a){foreach($a as$v){echo str_repeat(' ',$v[0]).str_repeat('-',$v[1]-$v[0])."\n";}}
CJam, 16 14 bytes
q~{S.*~'-e]N}/
This expects a list of lists as input. For example:
[[5 20] [5 20] [2 10] [5 19]]
gives:
---------------
---------------
--------
--------------
How it works
q~ e# Read the input and parse it as a list of list
{ }/ e# Go over each item in the list in a for loop
S e# S is basically this string - " "
.* e# Multiply each item of the first list with the corresponding index
e# item of the second list. This basically repeats the space
e# X times where X is the first number of the tuple. The second
e# number remains untouched as the second list was only 1 char long
~ e# Unwrap the space string and second number containing list
'- e# Put character '-' on stack
e] e# Make sure that the space is filled with - to its right so as to
e# make the total length of the string equal to the second number
N e# Put a newline. After all iterations, the result is printed
e# automatically to STDOUT
Pyth, 36 22 19 14 bytes
This is my first Pyth program. Jakube helped golf out 5 bytes!
FNQ<s*V" -"NeN
It expects input in the form [[5,20], [5,20], [2,10], [15,19]].
You can try it online.
CoffeeScript, 104 82, 65 bytes
List of tasks (ES6): 65 bytes
(a)->a.map (v)->console.log ' '.repeat(v[0])+'-'.repeat v[1]-v[0]
List of tasks (ES5 variant): 82 bytes
(a)->a.map (v)->j=-1;s='';s+=(if j<v[0]then' 'else'-') while++j<v[1];console.log s
Zero or more arguments: 104 bytes
()->[].slice.call(arguments).map((v)->j=-1;s='';s+=(if j<v[0]then' 'else'-')while++j<v[1];console.log s)
Unminified:
() -> [].slice.call(arguments).map( # convert to array-like arguments to array and loop
(v) ->
j = -1 # counter
s = '' # initialize string
s += (if j < v[0] then ' ' else '-') while ++j < v[1]
console.log s # print to STDOUT
)
JavaScript (ES6), 106 85 80 68 bytes
As per the updated requirements, a list of tasks is now acceptable
a=>a.reduce((p,v)=>p+=' '.repeat(z=v[0])+'-'.repeat(v[1]-z)+"\n",'')
Takes zero or more arguments: 80 bytes
(...a)=>{s='';a.map(v=>s+=' '[r='repeat'](z=v[0])+'-'[r](v[1]-z)+"\n");return s}
Original attempt, 106 bytes:
(...a)=>{for(i=-1,s='',r='repeat';a.length>++i;){s+=' '[r](a[i][0])+'-'[r](a[i][1]-a[i][0])+"\n"}return s}
Julia, 44 bytes
x->for t=x a,b=t;println(" "^a*"-"^(b-a))end
This creates an anonymous function that accepts an array of tuples as input and prints to STDOUT.
Ungolfed + explanation:
function f(x)
# Loop over the tasks (tuples) in x
for t in x
# Assign a and b to the two elements of t
a,b = t
# Print a spaces followed by b-a dashes on a line
println(" "^a * "-"^(b-a))
end
end
Examples:
julia> f([(5,20), (5,20), (2,10), (15,19)])
---------------
---------------
--------
----
julia> f([(0,7), (5,6), (3,6)])
-------
-
---
julia> f([])
Haskell, 76 bytes
(#)=replicate
f i=putStr$g=<<(read$'[':i++"]")
g(s,e)=s#' '++(e-s)#'-'++"\n"
Input format is a string of comma separated tuples, e.g. "(1,2),(3,4)".
Usage examples:
*Main> f "(1,2),(3,4)"
-
-
*Main> f "(0,7),(5,6),(3,6)"
-------
-
---
How it works: for input parsing I enclose the input string in [ and ] and use Haskell's native read function for lists of integer tuples. The rest is easy: for each tuple (s,e) take s spaces followed by e-s dashes followed by a newline and concatenate all into a single string. Print.
Haskell, 59 bytes
with relaxed input format:
(#)=replicate
f=putStr.(g=<<)
g(s,e)=s#' '++(e-s)#'-'++"\n"
Now it takes a list of tuples, e.g f [(0,7),(5,6),(3,6)].
Works as described above, but without input parsing.
Python 2, 39 Bytes
Straightforward solution using string multiplication :)
for x,y in input():print' '*x+'-'*(y-x)
Accepts input formatted like so:
((5,20),(5,20),(2,10),(15,19))
K, 18 bytes
`0:" -"@{&x,y-x}.'
Expects a list of pairs as input:
`0:" -"@{&x,y-x}.'(0 7;5 6;3 6)
-------
-
---
`0:" -"@{&x,y-x}.'(5 20;5 20;2 10; 15 19)
---------------
---------------
--------
----
`0:" -"@{&x,y-x}.'()
I unpack each (') tuple using dot-apply (.) so that inside the lambda I have access to the start and end value as x and y, respectively. Then I reassemble these into a (start,length) tuple (x,y-x) and apply "where" (&). This gives me output like so:
{&x,y-x}.'(0 7;5 6;3 6)
(1 1 1 1 1 1 1
0 0 0 0 0 1
0 0 0 1 1 1)
Then I simply have to index into a 2-character array using this ragged matrix (" -"@) and send it all to stdout (0:).
Ruby: 35 characters
->*t{t.map{|s,e|puts' '*s+?-*(e-s)}
Sample run:
irb(main):001:0> ->*t{t.map{|s,e|puts' '*s+?-*(e-s)}}.call [0,7], [5,6], [3,6]
-------
-
---
Updated to accept multiple two-element arrays, one for each task to display. (I think that is what the updated requirement expects.)
SWI-Prolog, 55 bytes
a([[A,B]|C]):-tab(A),writef("%r",[-,B-A]),nl,C=[];a(C).
Example: a([[5,20],[5,20],[2,10],[15,19]]). outputs
---------------
---------------
--------
----