-
Notifications
You must be signed in to change notification settings - Fork 0
/
2021-animals_who_ate.rb
167 lines (131 loc) · 2.93 KB
/
2021-animals_who_ate.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
=begin
Animals who Ate
form: https://programmingpraxis.com/2021/01/12/animal-txt/
Today's task is from a beginning programmer, who starts with an input file
called animal.txt:
There once was a Dog
Wednesday he ate Apples
Thursday he ate Apples
Friday he ate Apples
Saturday he ate carrots
There once was a Bear
Tuesday he ate carrots
Wednesday he ate carrots
Thursday he ate chicken
He wants to create this output:
Food: Apples Animals who ate it: 1
=======
Dog
Food: Carrots Animals who ate it: 2
========
Bear
Dog
Food: Chicken Animals who ate it: 1
========
Bear
He gave a complicated awk solution that didn't work; it produced duplicate
lines of output in those cases where the same animal ate the same food on
multiple days.
Your task is to write a program to produces the desired transformation form
input to output.
=end
require 'yaml'
require 'test/unit'
def aha1(str)
str = str.clone
animal_rg = /\AThere once was a (\w+)/
food_rg = /\A\w+ he ate (\w+)/
h = {} # { food: [animal...] }
str.strip!
while str =~ animal_rg
animal = Regexp.last_match[1]
str.sub!(animal_rg,'')
str.strip!
while str =~ food_rg
food = Regexp.last_match[1]
str.sub!(food_rg,'')
str.strip!
if h[food]
h[food] |= [animal]
else
h[food] = [animal]
end
end
end
render(h)
end
def aha2(str)
h = {} # { food: [animal...] }
s = str.clone.strip
while m = s.match(/\AThere once was a (\w+)/)
animal = m[1]
s = s[m.end(1)..].strip
while m = s.match(/\A\w+ he ate (\w+)/)
food = m[1]
s = s[m.end(1)..].strip
if h[food]
h[food] |= [animal]
else
h[food] = [animal]
end
end
end
render(h)
end
def aha3(str)
yml = str
.gsub(/^There once was a (\w+)/) { $1 + ":" }
.gsub(/^\w+ he ate (\w+)/) { "- " + $1 }
animals_foods = YAML.load(yml)
foods_animals = animals_foods.reduce({}) do |memo, pair|
animal, foods = pair
foods.uniq.each do |food|
if memo[food]
memo[food] << animal
else
memo[food] = [animal]
end
end
memo
end
render(foods_animals)
end
def render(hash)
hash.reduce("") do |out, pair|
food, animals = pair
out << "Food: #{food} Animals who ate it: #{animals.size}\n"
out << "========\n"
out << animals.sort.join("\n")
out << "\n\n"
end
end
class SubjectTest < Test::Unit::TestCase
def test_the_thing
subject = <<~TXT
There once was a Dog
Wednesday he ate Apples
Thursday he ate Apples
Friday he ate Apples
Saturday he ate Carrots
There once was a Bear
Tuesday he ate Carrots
Wednesday he ate Carrots
Thursday he ate Chicken
TXT
target = <<~TXT
Food: Apples Animals who ate it: 1
========
Dog
Food: Carrots Animals who ate it: 2
========
Bear
Dog
Food: Chicken Animals who ate it: 1
========
Bear
TXT
assert_equal target, aha1(subject)
assert_equal target, aha2(subject)
assert_equal target, aha3(subject)
end
end