Created
December 30, 2013 02:37
-
-
Save mwerner/8177182 to your computer and use it in GitHub Desktop.
golf_challenge.rb
This file contains 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
# 9 hole challenge - http://codegolf.stackexchange.com/questions/16707/9-hole-challenge | |
# 1 Greenway | |
# f(c:string, n:integer) | |
# Prints a line containing n instances of c. | |
def a(n,c) | |
puts n*c | |
end | |
a('a',3) | |
# 2. Somewhere in the rough | |
# f(t:string, s:string, n:integer) -> i | |
# Where i is the index of the nth instance of s in t. | |
def b(t,s,n) | |
r=0;t.length.times{|x| r+=1 if t[x]==s;return x if r==n};r | |
end | |
puts b('onomotopoeia','e',1) | |
# 3. Curry for Dinner | |
# f(x:function, y: function) -> g | |
# Where g is a function that will call y, n times; where n is the return value of x | |
def c(x,y) | |
lambda{x.call.times{y.call}} | |
end | |
c(lambda{4}, lambda{puts 'a'}).call | |
# 4. Spew | |
# f(p:string) | |
# Writes to file at p and fills it with a random sized grid of random characters (ascii) | |
def d(p) | |
File.open(p, 'w') do |f| | |
x=rand(10).floor | |
x.times do |i| | |
s = '' | |
x.enum_for(:times).inject(s){s << rand(93) + 33} | |
f.write("#{s}\n") | |
end | |
end | |
end | |
d('./test.txt');puts `cat ./test.txt` | |
# 5. Treasure Hunt | |
# f(p:string, c:char) -> (x, y) | |
# Reads file at p which contains a grid of symbols and returns the | |
# x and y coordinates of the symbol within the grid. | |
def e(p,c) | |
s=File.read(p).split("\n") | |
y = 0 | |
s.each do |r| | |
r.length.times{|x| return [x,y] if r[x] == c} | |
y += 1 | |
end | |
end | |
puts `cat t.txt`;puts e('./t.txt','%').inspect | |
# 6. Bridge on the River Kwai | |
# f(l:list[int]) Prints difference bridges diagram for l. E.g for [1,7,3,17,1] | |
# /+6\ /-4\ /+14\ /-16\ | |
# 1 7 3 17 1 | |
# Catch: Somewhere, your code must spell trousers (Must have at least 1 non-alphanumeric delimiters. | |
# E.g. tr(ou,se)(rs) | |
def f(l) | |
def trous(e,r,s);print "/#{e[s]-r}\\";end | |
y=0;l.each do |x| | |
print x | |
break if l[y+1].nil? | |
trous(l,x,y+1) | |
y += 1 | |
end | |
end | |
f([1,6,2,7,3,9,9,3,30]) | |
#7. Time Flies When You're Playing Golf | |
# f(p:string) -> [h, m] Reads file at p which contains | |
# an ASCII representation of an analogue clock, where the | |
# hour hand is represented with two lines, and the minutes by one. | |
# Output a list containing two elements: the hours and minutes shown | |
# on the clock. If only one hand is visible, assume both point to | |
# that position. | |
# Here are all the possible combinations for a hand. | |
# \ | / | |
# \|/ | |
# --o-- | |
# /|\ | |
# / | \ | |
# These positions, respectively are (12, 1, 3, 5, 6, 7, 9, 11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment