-
-
Save latentflip/987222 to your computer and use it in GitHub Desktop.
L/SH/ScotRug Code Golf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g=Golf=Hash | |
def g.method_missing s, a | |
k=[] | |
case s.to_s[-1] | |
when ?1 | |
a.reduce :* | |
when ?2 | |
a.split.sort_by{|i| i[1] }*' ' | |
when ?3 | |
h1 1..a | |
when ?4 | |
a.map { |i| i. | |
sub(/(man.*)/,'hat(\1)'). | |
sub(/(dog.*)\)/,'\1(bone))'). | |
sub 'cat','dead' | |
} | |
when ?5 | |
a.size.times { |i| k += a.each_cons(i+1).to_a } | |
k | |
when ?6 | |
f="fizz" | |
b="buzz" | |
(1..a).map { |s| s%3 > 0 ? s%5 > 0 ? s : b : s%15>0 ? f : f+b } | |
when ?7 | |
*r=0 | |
l,x=a | |
m=l | |
a.map {|a| | |
x ? x=$_ : (m+=1) == a ? r[-1]=[l,a]*'-' : r<<l=m=a | |
} | |
r | |
when ?8 | |
*i=0,1 | |
a.times { i << i[-1]+i[-2] } | |
i[1..-2] | |
when ?9 | |
b = open(a).map {|e| e.chomp.split ', '} | |
g = z b | |
while (c,d = g.minmax_by {|e,s| s.size})[0][1].size*2 < b.size | |
b.select {|e,| e==c[0]}.map &:shift | |
b -= [k] | |
g = z b | |
end | |
d[0] | |
when ?z | |
a.group_by {|e,| e} | |
end | |
end |
Under 600! (590)
In hole9 we can use minmax by instead of max_by then min_by.
586
We can use
[1,2,3]*' '
instead of
[1,2,3].join ' '
582
- hole2: don't need parentheses around ranges
- hole3:
a.times.size {}
is more efficient than
(1..a.size).map {}
579!
Hole 9: we can do the double association of minmax_by on a single line
576
- Hole 6: testing for
x>0
is more efficient than
x==0
566
- Hole 9:
array -= [[]]
is more efficient than
array.reject! &:empty?
Hole 6 if used x<1 instead of ==0 would not have had to rearrange order.
@morganp Heh, good spot. D'oh.
549
Seriously, make this end.
A step to far, but I guess all is fair in golf.
Added hole7 from @andrewmcdonough's repo in and back up to 617.
- My favourite bits are:
l,*x=a;
m=l
and
r<<l=m=a
- Also refactored method missing to check the last char instead of using a regex. Nicely obfuscates it since you have to use ?1 instead of /1/
615
- Lose the brackets in final sub method on hole 4
614
- Hole7: We can use
$_ #The last input line of string by gets or readline.
instead of nil
since we aren't using gets/readline.
613
- Pull multiple
<variable>=[]
into a singlek=[]
at top of method.
I updated my rake file to this
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.rspec_opts = ["--fail-fast"]
spec.pattern = 'spec/**/*_spec.rb'
end
So will only show first fail, faster to work through the first build. Relies on RSpec 2.1 though.
612
- Hole7: Since we are just setting x to anything, we don't need the *. - with thanks to @jonmountjoy
606
- Fun with parallel assignments
*e=0 #=> e = [0]
*e=0,1 #=> e = [0,1]
e, = [1,2,3] #=> e = 1
602 is about as good as it gets for this route I think
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
601
is shorter than