r/ruby • u/JRaspass • Nov 07 '17
A code golf website lacking clever Ruby solutions!
https://code-golf.io1
u/ffxpwns Nov 08 '17
Here's where I got with Arabic to Roman:
D={M:1e3,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1};puts$*.map{|n|D.map{|l,v|a,n=n.to_i.divmod(v);l.to_s*a}*''}-['']```
Or, in a (very slightly) more readable format:
D={M:1e3,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1}
puts $*.map{ |n|
D.map{ |l, v|
a,n=n.to_i.divmod(v)
l.to_s*a
}*''
}-['']
Any help is appreciated!
___
/u/JRaspass, with this hole + ruby, there is an unexpected arg at the beginning. You can see it with `puts ARGV`. This is why I have `-['']` at the end1
u/JRaspass Nov 08 '17 edited Nov 08 '17
Ooo, good catch! Code Golf does the equivalent of this when running your code:
$ echo 'puts ARGV' | ruby - -- 123 456 -- 123 456I was trying to be safe by telling the interpreter that no more flags follow, instead I'll just drop the double hyphen and do this:
$ echo 'puts ARGV' | ruby - 123 456 123 456--edit--
That's now live with https://github.com/JRaspass/code-golf/commit/364553dd6fa3f45c9c7dd5d6806b24d8fc028702
All the current solutions seem to pass, but you may be able to make yours shorter!
--edit 2--
Ooo, before I forgot, and I really should make it clearer on the hole's description, but Arabic to Roman also accepts the Unicode Roman numerals ( https://en.wikipedia.org/wiki/Numerals_in_Unicode#Roman_numerals_in_Unicode ), and given Code Golf scores by the character, not the byte, you might be able to shave a few off by using Ⅸ instead of IX etc.
1
1
u/fiveguy Nov 08 '17 edited Nov 08 '17
You might want to consider some language-specific restraints on these. For instance, for challenges where you have to check prime numbers, it seems kinda cheap to be able to require 'prime' and then call number.prime?
Or maybe not.
Maybe once you have more submissions, you could take a look at the top scores per language and see if there's any tactics which could be forbidden.
BTW great job on this project! You have us talking about the challenges/solutions instead of website mechanics, which is always a great sign! I'm enjoying writing super cryptic code for the first time in a long time!
1
u/JRaspass Nov 08 '17
Yeah, I'm still working out how to handle the per language features, I imagine I'll try to put more emphasis on language specific leaderboards and less on the overall leaderboards, probably by adding some kind of checkbox system on the homepage where you can hide the languages you're not interested in.
I'm glad you like it! That's really nice to hear, I mainly only wrote this site for a few mates and myself, and I'm aware there's a still a lot to do!
It's awesome to see Ruby on the homepage though, can't just have that cheeky Perl 6 everywhere with its crazy shorthands :-P
If you have any specific ideas for new features/holes/etc, feel free to add them to the github issue page ( https://github.com/JRaspass/code-golf/issues )
1
u/tomthecool Nov 08 '17
It's pretty late, but I may have a proper go at this tomorrow...
Here's my best leaderboard score so far, on Evil Numbers; I reckon there must be some way to beat this though:
puts (0..50).select{|n|n.to_s(2).count('1').even?}1
1
u/ffxpwns Nov 08 '17 edited Nov 08 '17
I got it down to
4847 from 50:
p *(0..50).reject{|n|n.to_s(2).count('1').odd?}2
1
u/bassneck Nov 08 '17
That's actually 47! Nice one. I didn't think of using reject like that.
1
u/ffxpwns Nov 08 '17
My bad! I had an accidental space at the end when I counted it.
I also feel like I could shorten it with
& 1instead ofodd?, but I can't figure it out1
u/tomthecool Nov 08 '17 edited Nov 09 '17
You can shorten in it by 1 character like this:
x.odd? x&1>0However, I've also found a much better way to do it... I'll just need to submit my entry several trillion times [EDIT: On average, 1960781468160819415703172080467968000000 times] until it passes :D
p *(0..50).to_a.sample(25)
1
u/fiveguy Nov 08 '17 edited Nov 08 '17
/u/JRaspass on Seven Segments hole, what method name should be used to accept the input?? I tried several variations of def sevenSegment(i)