This repository has been archived by the owner on Mar 21, 2022. It is now read-only.
forked from thomasjachmann/ruby_golf_hamburg_2013
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruby_golf.rb
117 lines (101 loc) · 5.08 KB
/
ruby_golf.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# encoding: UTF-8
module RubyGolf
##############################################################################
# Hole 1: RubyGolf.x_out_numbers #
##############################################################################
# input: n - numbers string to be x'ed out,
# v - number of visible trailing numbers
# output: x'ed out string
def self.x_out_numbers(n, v)
n[/\d{,#{v}}$/].rjust(n.size, 'x')
end
##############################################################################
# Hole 2: RubyGolf.underscore #
##############################################################################
# input: an identifier string in camel case notation. i.e. 'SchinkenWurst'
# ouput: a 'ruby style' version of the identifier: all lowercase, former case
# changes to upper case get a prepended underscore
def self.underscore(s)
s.gsub(/[-]{,1}([A-Z])/) { "_"+$1.downcase }.gsub(/^_/,'')
end
##############################################################################
# Hole 3: RubyGolf.symbolize_keys #
##############################################################################
# input: a hash
# output: a hash where each key is a symbol.
# The values stay the same except they're hashes too.
# Values that are hashes contain only smybols as keys too, this
# condition is maintained recursivley
def self.symbolize_keys(h)
Hash[h.map { |k,v| [k.to_sym, (v.is_a? Hash) ? symbolize_keys(v) : v] }]
end
##############################################################################
# Hole 4: RubyGolf.grid_computing #
##############################################################################
# input: a grid of numbers between 00 and 99, separated by spaces, each row
# ending in a \n
# output: the maximum value found by calculating the sums of all rows and
# columns
def self.grid_computing(g)
m = g.split("\n").map { |x| x.split(" ").map(&:to_i) }
(m.transpose.map { |l| eval(l*'+') } + m.map { |l| eval(l*'+') }).max
end
##############################################################################
# Hole 5: RubyGolf.reformat_hash #
##############################################################################
# input: a string containing a hash expression possibly containing hash
# rockets
# output: a string describing the same hash but without hash rockets, but
# otherwise with the same formatting
def self.reformat_hash(s)
s.gsub(/\:([^"]+) =>/,'\1:')
end
##############################################################################
# Hole 6: RubyGolf.pretty_hash #
##############################################################################
# input: a recursive hash mapping of strings or symbols ultimately to lists
# of strings or symbols
# output: a formatted string containing the information of the hash where:
# * every line contains exactly one key or value
# * values are prefixed by a hyphen and a space
# * keys a suffixed with a colon
# * all keys or values that are contained in a hash that is not top
# level are prepended by two additional spaces per level away from
# the top level
def self.pretty_hash(h)
case h
when Hash
h.map { |k,v| "#{k}:\n#{pretty_hash(v)}" } * "\n"
when Array
h.map { |v| pretty_hash(v) } * "\n"
else
"- #{h}"
end
end
##############################################################################
# Hole 7: RubyGolf.word_letter_sum #
##############################################################################
# input: a string containing a list of space separated, letter-only words
# output: independent of the case of the characters of the words the following
# value is calculated:
# * take the sum of the alphabetical positions of the characters of a
# word (A=1, B=2..)
# * sort the words by that sum - the one with the largest sum will be
# the first
# * multiply each sum with the position of its word in the list (first
# word 1* ...)
# * sum all products
def self.word_letter_sum(s)
end
##############################################################################
# Hole 8: RubyGolf.bob_ross #
##############################################################################
# input: a string defining an image, each line consisting of three values:
# * x coordinate
# * y coordinate
# * ascii value for that coordinate
# output: an ascii art string ready for output where there aren't any trailing
# spaces after the last character in each line
def self.bob_ross(s)
end
end