Skip to content

Commit 6e0f069

Browse files
Added fire-and-fury.rb
1 parent 3a42f21 commit 6e0f069

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ruby/6kyu/fire-and-fury.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=begin
2+
Codewars. 31/05/20. 'FIRE and FURY'. 6kyu. The President's phone is broken, the only letters still working are upper case EFIRUY. We must
3+
decipher the president's tweets. FIRE = "You are fired!". FURY = "I am furious". In our tweet, any letters not spelling FIRE or FURY are
4+
simply ignored. If consecutives of the same word are found, plural rules apply e.g. FIRE x2 = "You and you are fired!", FIRE x3 = "You and
5+
you and you are fired!", FURY x2 = "I am really furious.", FURY x3 = "I am really really furious." If FIRE or FURY is found, or unexpected
6+
characters are encountered, it must be a "Fake tweet." Here is the solution I developed to solve the challenge.
7+
1) We define our method fire_and_fury_ms, which takes a tweet in the form of a string as its argument.
8+
2) If the tweet contains any character that is not E, F, I, R, U or Y, we have encountered unexpected characters so we return "Fake tweet."
9+
3) So that we can remove all expected characters which don't form FIRE or FURY, we generate an array of every occurrence - in order of
10+
appearance in the string - of FIRE and FURY using scan.
11+
4) If our array of FIRE and FURY is empty, we return "Fake tweet."
12+
5) Now we group our array according to same consecutives using chunk_while, then map over the chunks.
13+
6) If the first word in the sub-array is FIRE, we turn this sub-array into our deciphered string. Within this deciphered string we use
14+
string interpolation and do "and you " multiplied by the size of the sub-array minus 1. So if there's 1 FIRE in the sub-array, "and you "
15+
* 0 = "" so the chunk becomes "You are fired!". If there's 3 FIRE's, "and you " * 2 = "and you and you " so the chunk becomes
16+
"You and you and you are fired!".
17+
7) If the first word in the sub-array is not FIRE, it must be fury, so in our false portion of the ternary operator, we do the same for
18+
" really" but this time we place the space at the start of really, putting no space between "am" and " really". We did the opposite for
19+
the FIRE cases.
20+
8) Now the tweet is deciphered, we join it back into a string, with each sentence delimited by a space.
21+
=end
22+
23+
def fire_and_fury_ms(t)
24+
return "Fake tweet." if t.match(/[^EFIRUY]/)
25+
t = t.scan(/FIRE|FURY/) ; return "Fake tweet." if t.empty?
26+
t.chunk_while(&:==).map {|w|
27+
w[0] =~ /FIRE/ ? "You #{"and you " * (w.size-1)}are fired!" :
28+
"I am#{" really" * (w.size-1)} furious."}.join(" ")
29+
end

0 commit comments

Comments
 (0)