-2
\$\begingroup\$

Given the integers N and M, can you print all numbers from N to M comma-seperated? The hitch is to use one single expression.

The output for N=23 and M=42 should look like this:

23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42

I'll start with Ruby:

puts (23..42).to_a.join(', ')
\$\endgroup\$
8
  • \$\begingroup\$ related: codegolf.stackexchange.com/questions/8588/… \$\endgroup\$
    – ardnew
    Jun 27, 2013 at 20:50
  • 5
    \$\begingroup\$ @YMMD what do you consider a single expression in golfscript? \$\endgroup\$ Jun 27, 2013 at 21:05
  • 6
    \$\begingroup\$ so now that the code golf tag has been removed, is there even an objective or winning criteria anymore? \$\endgroup\$
    – ardnew
    Jun 27, 2013 at 21:16
  • 1
    \$\begingroup\$ Quite poorly worded task. As you can see the answers, there are too many variations: n..m vs 23..42, “,” vs “, ”, quoted vs unquoted output, maybe with or without trailing separator. I know, it is not code-golf anymore, but I think the answer still should be somehow comparable. \$\endgroup\$
    – manatwork
    Jun 28, 2013 at 7:15
  • 1
    \$\begingroup\$ @manatwork, that's the least of its problems. The biggest problem is that, as Jan pointed out, "expression" is not well defined. \$\endgroup\$ Jun 28, 2013 at 10:06

8 Answers 8

3
\$\begingroup\$

k

17 chars. Expression returns 1 but prints output to stdout

1@", "/:$23_!1+42

To return a string with the comma seperated values, simply use:

", "/:$23_!1+42
\$\endgroup\$
2
\$\begingroup\$

Sage CLI, 14

Edit: Sage has a range shorthand.

`[n..m]`[1:-1]
\$\endgroup\$
1
\$\begingroup\$

Ruby: 18 characters

$><<[*23..42]*", "

Sample run:

bash-4.1$ ruby -e '$><<[*23..42]*", "'
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42

Forcing the rules: 10 characters

As seen in the other answers:

  • variables instead of numeric literals
  • comma only instead of comma and space
  • no output, just generated value
[*n..m]*?,

Sample run:

irb(main):001:0> n=23
=> 23

irb(main):002:0> m=42
=> 42

irb(main):003:0> [*n..m]*?,
=> "23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42"
\$\endgroup\$
1
\$\begingroup\$

Bash, 21 bytes

echo {23..42}|tr \  ,
\$\endgroup\$
2
  • \$\begingroup\$ what about seq -s, 23 42 \$\endgroup\$
    – marcosm
    May 30, 2017 at 13:13
  • \$\begingroup\$ @marcosm seq is distribution specific and not available everywhere. \$\endgroup\$ May 30, 2017 at 16:50
1
\$\begingroup\$

Tcl, 63 bytes

puts "$n,[join [lmap a [lrepeat [expr {$m-$n}] 0] {incr n}] ,]"
\$\endgroup\$
3
  • \$\begingroup\$ My answer has the problem of putting new lines: while {$n<$m} {puts $n,;incr n};puts $m \$\endgroup\$
    – sergiol
    Dec 18, 2016 at 22:43
  • \$\begingroup\$ Fixed: while {$n<$m} {lappend l $n,;incr n};lappend l $m;puts $l \$\endgroup\$
    – sergiol
    Dec 18, 2016 at 22:49
  • \$\begingroup\$ OK I forgot there is the requirement of 1 single expression; my answers are not acceptable! \$\endgroup\$
    – sergiol
    Dec 18, 2016 at 22:50
0
\$\begingroup\$

Python: 39

print(",".join(map(str,range(23,42))))
\$\endgroup\$
0
\$\begingroup\$

J, 28 characters

echo(,', ',])&":/[23}.i.>:42
  • i.43 creates an array of 43 values 0..42. Unfortunately, we have to compute 43.
  • 23}. drops first 23 of them, leaving 23..42. If precomputation is allowed, 23+i.20 fares better byte-wise (-3 characters)
  • ,', ',] concatenates both arguments with ', ' between them
  • ...&": does that after converting both arguments to strings
  • .../ does that as a reduction step.
  • echo is a built-in to perform standard output. If simply returning is enough, you can drop this (-4 characters)
\$\endgroup\$
0
\$\begingroup\$

R: 17 characters

cat(n:m,sep=", ")

Example:

> n=23
> m=42
> cat(n:m,sep=", ")
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42
\$\endgroup\$

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