Rosetta Stone Code Challenges are challenges where you must complete the task in as many languages as possible.
The Challenge
You will be given two numbers, n and s, in a list as input. The n will be a number you have to count up to. n will be any number from 2 to 10000. s will be the number you skip by when counting up to n. s will be from 1 to n/2 (half of n). If n was 600, s could not be greater 300. You have to output all of the numbers up to n skipping by s starting with 1. Each output number should either be on a newline, on the same line separated by spaces, or in a list.
The Twist
For this Rosetta Stone Code Challenge, each snippet you write the program in must be the same length. ie: If your Python program has 56 characters then your Java program must also be 56 characters. Spaces and comments do not count as characters. Variable/function names can't be more than 10 characters. Remember, this is also code golf, so try to keep your code short.
Example
Input
[100, 7]
Output
1
8
15
22
29
36
43
50
57
64
71
78
85
92
99
Note: Output could be in single line separated by spaces or list format
Winning Criterion
The winner will be decided by this formula:
numberOfLanguages/codeSize
codeSize is not the total code size but the average code size of a snippet.
The user with the highest score will win. ie: An answer with 4 languages and a size of 31 bytes would win over an answer with 7 languages and a size of 80 bytes. A winner will be chosen at 2:00 UTC on March 9, 2015.
Scoreboard
All scores are rounded to the nearest thousandth
Maltysen - 14 languages / 24 characters = 0.583 ---- Winner
mbomb007 - 15 languages / 29 characters = 0.517
Jakube - 5 languages / 11 characters = 0.455
Teun Pronk - 4 languages / 51 characters = 0.078
captncraig - 1 language / 56 characters = 0.018
6 Answers 6
23 languages, 15 characters = 1.5333333
+---------------+----------------------+----------------+
| Language | Code | Kind |
+---------------+----------------------+----------------+
| gs2 | B/4[RRRRRRRRRRR | (stack) |
| Pyth | M%+Z+Z+Z+Z+ZHSG | (definition) |
| GolfScript | \),000000001>%p | (stack) |
| CJam | \),000000001>%p | (stack) |
| zsh | seq 00000001 2ドル 1ドル | (script) |
| Bash | seq 00000001 2ドル 1ドル | (script) |
| FlogScript | \),0000001>\%Pa | (stack) |
| APL | ×ばつ⍳⌈⍺÷⍵} | (lambda) |
| Mathics | 00001~Range~##& | (lambda) |
| Mathematica | 00001~Range~##& | (lambda) |
| J | ({.00001円+i.)~- | (lambda) |
| Matlab | @(a,b)00001:b:a | (lambda) |
| Octave | @(a,b)00001:b:a | (lambda) |
| Burlesque | jrojcoq-]m[m]uN | (stack) |
| K | {0001+&~(!x)!y} | (lambda) |
| Pip | {_%b=001FI,++a} | (lambda) |
| Haskell | ff a b=[1,b+1..a] | (definition) |
| Clean | ff a b=[1,b+1..a] | (definition) |
| Curry | ff a b=[1,b+1..a] | (definition) |
| Frege | ff a b=[1,b+1..a] | (definition) |
| Julia | ff(a,b)=[1:b:a] | (definition) |
| Scotch | f(a,b)=[1..a,b] | (definition) |
| Perl6 | {1,$^b+1...$^a} | (lambda) |
+---------------+----------------------+----------------+
-
1\$\begingroup\$ Since you have Matlab, why not add Octave? \$\endgroup\$alephalpha– alephalpha2015年10月22日 13:12:48 +00:00Commented Oct 22, 2015 at 13:12
-
\$\begingroup\$ That might work. Would the code be exactly the same? \$\endgroup\$lynn– lynn2015年10月22日 13:17:33 +00:00Commented Oct 22, 2015 at 13:17
-
\$\begingroup\$ Yes, it's exactly the same. \$\endgroup\$alephalpha– alephalpha2015年10月22日 13:25:57 +00:00Commented Oct 22, 2015 at 13:25
-
\$\begingroup\$ Neat. I've added it! \$\endgroup\$lynn– lynn2015年10月22日 13:41:52 +00:00Commented Oct 22, 2015 at 13:41
-
\$\begingroup\$
jrojcoq-]m[m]uNwould be a Burlesque Version that outputs number and doesn't push dummy data to the stack just to pop it again. \$\endgroup\$mroman– mroman2015年10月22日 14:13:29 +00:00Commented Oct 22, 2015 at 14:13
14 Languages - 24 bytes = 0.58333333333
**Facepalm**. I forgot not to count the space in my Python answer. So I'm at 24 bytes now.
Learned about a lot of new languages from this challenge, which I will probably using in golfs. A site that really helped me find these languages was http://www.tutorialspoint.com/codingground.htm. Just don't expect my programs to run in it, it requires a full program format, and I just used it to learn about the languages and used other REPLs to run them.
1. Pyth:
Pyth doesn't have a range by step function unlike python, so I used regular range and (削除) chopped (削除ここまで) (thanks @Jakube) used mod operator on it.
M%Hr+Z+Z+Z+Z+Z+Z+Z+Z1+1G
Defines a function g(until, step)
M Define g(G, H)
%H Every H elements
r1+1G Range 1->G+1
2. Golfscript:
This is my first golfscript program. Probably not the best way to do it.
000000000000000;\),1>\%p
\ swap
) increment
, range
1> all but first element
\ swap
% take every top of stack elements
p print array
3. Bash Utils:
Uses the seq command which has INCREMENT param.
seq 00000000000000001 2ドル 1ドル
4,5. Python, Sage:
Self-explanatory code.
lambda a,b:range(1,a+1,b)
Sage is a language based on Python used for math stuff.
6. Mathematica:
Again, self-explanatory.
r[a_,b_]:=Range[001,a,b]
7. Matlab:
Uses Matlab's range notation and defines an anonymous function.
@(a,b)00000000000001:b:a
8. Livescript:
Like Coffeescript it is a JS derivative. Has cool range syntax. Arrow notation again.
r=((((a,b)->[1 to a by b])))
9. Erlang:
Learned erlang just for this. Pretty self explanatory, defines function r.
r(a,b)->lists:seq(1,a,b)
10. F#:
Kind of obvious, F#'s function definitions are like variables.
let r a b=seq{000001..b..a}
11. Haskell:
Haskell's range notation has you give the first two in the range for the increment, and not an explicit increment.
r a b=[00000000001,a+1..b]
12. R:
Very cool language, even if function definitions are a little verbose.
function(a,b)seq(01,a,b)
13. Julia:
Awesome language, will be using for golfing in the future.
r(a,b)=[00000000001:b:a]
14. CJam:
Like my old golfscript answer except with input parsing and padding.
0000000000000;q~\),1>\%p
-
\$\begingroup\$ How do I run this?
Mmhdcr1GH100 7doesn't work like I thought it would. \$\endgroup\$mbomb007– mbomb0072015年02月24日 18:57:48 +00:00Commented Feb 24, 2015 at 18:57 -
1\$\begingroup\$ @mbomb007
Mdefines a functiong, which you have to call first. TryMmhdcr1GHg100 7. \$\endgroup\$Jakube– Jakube2015年02月24日 20:03:13 +00:00Commented Feb 24, 2015 at 20:03 -
\$\begingroup\$
%is the same thing as python's[::a]. No need for chopping. And I'm pretty sure your range has to go up to includingG, e.g. usingr1hG. \$\endgroup\$Jakube– Jakube2015年02月24日 21:41:13 +00:00Commented Feb 24, 2015 at 21:41 -
\$\begingroup\$ Please put a space before
7. Negative doesn't look right. \$\endgroup\$mbomb007– mbomb0072015年02月24日 22:10:02 +00:00Commented Feb 24, 2015 at 22:10 -
\$\begingroup\$ @mbomb007 fixed, srry \$\endgroup\$Maltysen– Maltysen2015年02月24日 22:10:47 +00:00Commented Feb 24, 2015 at 22:10
5 languages 11 characters = 0.4545
Took this challenge as an opportunity to check out some new languages (CJam, Burlesque, zsh, gs2). First time I've worked with these languages.
The method behind creating the output is different in nearly each language. Only the CJam and the gs2 method are identical. The 4 methods are, creating a range [1, 100] and slice it with modulo (CJam and gs2), creating the range [1, ..., 100] and filter for elements that are 1 mod 7 (Pyth), creating the range [1, ..., 100], divide it into chunks of len 7 and use the first element of each chunk (Burlesque), and creating the sequence directly (zsh).
I tried to design each program in such a way, that it looks golfed to someone, who is not familiar with the language.
edit 1: added Burlesque, previous 2 languages with 10 characters = 0.2
edit 2: added gs2, previous 3 languages with 11 characters = 0.2727
edit 3: added zsh, previous 4 languages with 11 characters = 0.3636
CJam
q~1円+,0-\%p
Test it online with the input 100 7.
q~ push the evaluated input (100 7)
\ swap (7 100)
1 push 1 (7 100 1)
+ add last to elements (7 101)
, create the range (7, [0, 1, ..., 100])
0 push 0 (7, [0, 1, ..., 100], 0)
- delete 0 from the range (7, [1, 2, ..., 100])
\ swap ([1, 2, ..., 100], 7)
% use every 7th entry ([1, 8, ..., 99])
p print pretty
Burlesque
jrojcoq-]m[
Beside zsh, this is the only golfed program. Burlesque is quite awesome, it has a very large range of different functions, like ro. Sadly there's nothing like the Python [::a] slicing operator.
There is no online interpreter, but you can get Burlesque here. Since Burlesque doesn't support functions or IO, you have to put the values onto the stack first, like 100 7 jrojcoq-]m[
j Swap (7 100)
ro Range (7 {1 2 ... 100})
j Swap ({1 2 ... 100} 7)
co ChunksOf ({{1 2 .. 7} {8 ... 14} ... {99 100}})
q Create Block ({{1 2 .. 7} {8 ... 14} ... {99 100}} {})
-] head (first element) ({{1 2 .. 7} {8 ... 14} ... {99 100}} {-]})
m[ map the chunks to the head-block
gs2
read-nums dup head inc range tail swap reverse head mod unlines
Yep, this is only 11 chars. In gs2 each byte has a different meaning. But because writing in bytes is quite hard and not any fun, you can also write in mnemonics, which you can compile to the actual gs2 code. The gs2 code, which gets executed is
W@!'."B !4+
or as hex-dump:
57 40 21 27 2e 22 42 20 21 34 2b
Fun fact: I didn't write in mnemonics, but directly the numbers of the hex-dump.
You can test it by downloading the compiler. Compile the mnemonics file with python gs2c.py < mnemonics.txt > compiled or simply copy W@!'."B !4+ to a file named compiled and then execute it with echo 100 7 | python gs2.py compiled.
The mnemonics is pretty self explainable, but here's what's going on in the stack.
57 40 21 27 2e 22 42 20 21 34 2b
57 read-nums ([100 7])
40 dup ([100 7] [100 7])
21 head ([100 7] 100)
27 inc ([100 7] 101)
2e range ([100 7] [0 1 ... 100])
22 tail ([100 7] [1 2 ... 100])
42 swap ([1 2 ... 100] [100 7])
20 reverse ([1 2 ... 100] [7 100])
21 head ([1 2 ... 100] 7)
34 mod ([1 8 ... 99])
2b unlines ('1\n8\n...\n99')
everything on the stack gets printed
Btw, 8 bytes are possible, maybe even less.
edit: I forked gs2, slightly manipulated a command and made a pull request, which is already merged. So now the task can be done with 6 bytes: read-nums extract-array swap range1 mod unlines, which translates to 57 0e 42 2f 34 2b, which is even better than the optimal pyth.
zsh
f() seq 1 2ドル 1ドル
This defines a function f, that creates the a sequence. To use it on Windows, I installed the babun shell, where you can simply type these commands. Simply write f() seq 1 2ドル 1ドル and in the next line f 100 7.
First I tried to write this in Bash, but there you have to write () around the code block, like f() ( seq 1 2ドル 1ドル ). So I switched to zsh instead.
Pyth
fq1%TeQUhhQ
This really hurts, since of course I see the obvious 7 byte golf. You can test it online with the input 100, 7.
Q = input()
UhhQ the range [0, 1, ..., Q[0]]
f filtered (only use those elements T, which satisfy the equation
q1%TeQ 1 == T % Q[1]
-
\$\begingroup\$ Technically Burlesque has an "EveryNth"-Command. Like
100ro7en?iwhich produces{8 15 22 29 36 43 50 57 64 71 78 85 92 99}. You can go even shorter by using7mo14.+?i. \$\endgroup\$mroman– mroman2015年05月12日 12:28:00 +00:00Commented May 12, 2015 at 12:28 -
\$\begingroup\$ @mroman Quite a long time since I wrote this code. But as far as I remembered the "EveryNth"-Command doesn't work like the
[::a]command in Python.EveryNth([1,2,3,4,5,6,7,8, 9], 3)returns[3, 6, 9]and not[1,4,7], like it was required in this challenge. \$\endgroup\$Jakube– Jakube2015年05月12日 12:33:02 +00:00Commented May 12, 2015 at 12:33 -
\$\begingroup\$ Yes, you're right. The
enworks different than one would expect from other languages. I curse the day I implemented it this way :D. Common workarounds for that are either using themoI mentioned above or by usingcoas in9ro3co)-]which produces{1 4 7}. PS: I had an online interpreter back in the old days but my new hoster doesn't support CGI anymore but I might be able to convince someone who has a Server with CGI support to host the online interpreter again :) \$\endgroup\$mroman– mroman2015年10月22日 14:38:33 +00:00Commented Oct 22, 2015 at 14:38
15 languages, 29 characters = (15/29) ≈ 0.517
I've fixed all my code so far. Now I'm searching for other languages.
1. Python (24 golfed)
f=lambda n,s:range(0001,n+1,s)
2.><> (Fish) - (19 + junk chars)
This program assumes that the number and the step size were are on the stack beforehand, pushed in that order. Uses ao to print newlines between numbers.
1 v
$:@+:{:}(?!;>:nao
~~~~~~~~~~
Explanation (code, stack after preceding code executes, comments):
1 1 s n start n0=1. stack is (top)1 s n(bottom)
(skip to print code - last line of explanation, then come back)
$:@ s 1 s n swap, duplicate, rotate top 3 (this all is OVER in Forth)
+: n1 n1 s n add step to x and dup
{:} n n1 n1 s n compare to n
( n1>n n1 s n if !(n1 > n), JUMP: dup and print (continue)
?!; n1 s n else exit
:nao n1 s n dup, print n1, newline, loop around
3. APL (19 golfed)
fffffffff←{(⌈⍵÷⍺)×ばつ(⍳⍵)}
Golfed. Paste into here: http://ngn.github.io/apl/web/
f←{(⌈⍵÷⍺)×ばつ⍳⍵}
Call like this: 7 f 100. This creates an array from 1 to n, multiplies every element by s, adds one to each element, then takes the first ceil(n/s) elements.
4-7. Haskell, Clean, Curry, Frege (14 golfed - spaces don't count)
This program is valid in Haskell, Clean, Curry, and Frege.
ffffffffff nnnn s=[1,s+1..nnnn]
Golfed. Run here: https://ideone.com/Ii0pgP
f n s=[1,s+1..n]
8. Scotch (15 golfed)
fffffffff(nnnn,s)=[1..nnnn,s]
9. Jaskell (24 golfed) - Built based on documentation.
Filter the list where the remainder modulo s is 1.
ffffff n s=filter(.%s==1)[1..n]
10. CoffeeScript (25 golfed) Run here
fffff=(n,s)->x for x in[1..n]by s
11. R (25 golfed) Run here: http://www.r-fiddle.org/#/fiddle?id=k3vRnCOW&version=2
fffff=function(n,s)seq(1,n,s)
12. Cobra (26 golfed)
def ffff(n,s)
for i in 1:n:s
print i
13. S-Lang (28 golfed)
define ff(n,s){return [1:n:s];}
14. Boo (29)
Not sure if range function can take 3 parameters...
def f(n,s):return range(n)[::s]
15. Funge-98 (29, any help golfing?)
As close as I could get to my><> program. Since there's no "rotate" stack instruction, I had to use "put" and "get" instead. Befunge really needs some stack rotation/shift instructions, or even an "over" instruction.
22p1v
>:.a,\:33p33円g+:22g\`!j@
Explanation:
n s
22p s (store n at 2,2)
1 1 s (push 1)
(v and > are direction changes, so the above only happens once)
\: s s 1 (swap, dup)
33p s 1 (store s at 3,3)
33円g s 1 s (get s)
+: n1 n1 s (add, dup)
22g n n1 n1 s (get n)
\`! !(n1>n) n1 s (swap, >, not)
j@ n1 s (conditional exit)
:.a, n1 s (print n1, newline)
NOT USING AT THE MOMENT, TOO LONG - My score is improved by not including these.
Ruby (35) Run here: http://ideone.com/yFZELR
def f(n,s)
(1..n).select{|x|x%s==1}
end
PowerShell (38)
function f($n,$s){(1..$n|?{$_%$s-eq 1})}
Perl (39) Run here: https://ideone.com/HGoleN
sub f{join(" ",grep $_%@_[1]==1, 1..@_[0])}
-
\$\begingroup\$ Notice that your ><> program counts backwards from n to one by skipping s, thus it produces 2 9 16 23 30 37 44 51 58 65 72 79 86 93 100 for n=100, s=7 instead of 1 8 15 22 29 36 43 50 57 64 71 78 85 92 99 as specified in OP's example. \$\endgroup\$cirpis– cirpis2015年02月24日 20:14:52 +00:00Commented Feb 24, 2015 at 20:14
-
\$\begingroup\$ @cirpis I fixed ><>, still have to fix Befunge. \$\endgroup\$mbomb007– mbomb0072015年02月24日 20:51:56 +00:00Commented Feb 24, 2015 at 20:51
-
\$\begingroup\$ Rather than assuming everything's on the stack for ><>, I think you can use the -v flag to supply input. Normally flags add a byte, but I'm not sure whether that means you need to remove a junk char or not (since apparently we only care about "code size") \$\endgroup\$Sp3000– Sp30002015年02月24日 20:58:20 +00:00Commented Feb 24, 2015 at 20:58
-
\$\begingroup\$ I see people assuming stuff is on the stack all the time for Befunge, GolfScript, etc. But I don't know how to input, other than
i, which pushes a character. Also, while we're on the topic of stuff, is it acceptable for the program to crash after successfully completing its output (by popping an empty stack)? \$\endgroup\$mbomb007– mbomb0072015年02月24日 21:05:46 +00:00Commented Feb 24, 2015 at 21:05 -
1\$\begingroup\$ @mbomb007 With
seq.int(). It works the same asrangein Python. Then you can get it to 29. \$\endgroup\$freekvd– freekvd2015年02月25日 19:41:23 +00:00Commented Feb 25, 2015 at 19:41
4 languages 51 characters = (4/51) = 0,0784313725490196
No idea how many numbers after comma you want so i'll just leave it like this.
All snippets are 51 characters long when you remove all the whitespace.
Working on more and shorter code but it's a start.
Log
Initial answer
Submitted Python(3.4), Lua(5.1) and Javascript.
Score: 3/51 = 0,0588235294117647
Update 1
Added Ruby
Score: 4/51 = 0,0784313725490196.
Python 3.4
def f(NUMB,ST):
CNT=1
while CNT<=NUMB:
print(CNT)
CNT+=ST
Lua 5.1
function f(num,st)
for cn=1,num,st do
print(cn);
end;
end;
Javascript
function f(n,s){
for(i=1;i<=n;i+=s){
console.log(i);
}
}
Ruby
def faa(numb,st)
for i in 1..numb
if i%st==1
puts i
end
end
end
-
\$\begingroup\$ If you want another language, here's STATA:
di _reque(s) _reque(n) gl a=1 while $a<=$n{ di $a gl a=$a+$s }\$\endgroup\$bmarks– bmarks2015年02月24日 14:35:37 +00:00Commented Feb 24, 2015 at 14:35 -
\$\begingroup\$ @bmarks Thanks, seems a bit lame to add someone else's code to the answer though ;) \$\endgroup\$Teun Pronk– Teun Pronk2015年02月24日 14:36:33 +00:00Commented Feb 24, 2015 at 14:36
-
\$\begingroup\$ In the javascript function, I think you need i<=n instead of i<n. Also, I think you can shorten most of these by using the alert function in javascript and shortening variable names in the others. \$\endgroup\$bmarks– bmarks2015年02月24日 14:56:16 +00:00Commented Feb 24, 2015 at 14:56
-
\$\begingroup\$ @bmarks the variable names used to be 1 character each, same for function names. I just padded them a bit to get them to be the same length. and youre right about the javascript. Ill change it. \$\endgroup\$Teun Pronk– Teun Pronk2015年02月24日 14:58:40 +00:00Commented Feb 24, 2015 at 14:58
-
\$\begingroup\$ I only count 50 chars in the Ruby solution. \$\endgroup\$Jakube– Jakube2015年02月25日 12:28:09 +00:00Commented Feb 25, 2015 at 12:28
1 language - 56 characters = 0.01785714285
piet (2x28) -
http://www.pietfiddle.net/img/JpdU9zE1lR.png?cs=15
NumberOfLanguages / CodeSize? \$\endgroup\$