-
Notifications
You must be signed in to change notification settings - Fork 0
/
d1e04_lexiconomitron.rb
66 lines (50 loc) · 2.83 KB
/
d1e04_lexiconomitron.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
#encoding: utf-8
require 'awesome_print'
#require_relative 'd2e01_lexiconomitron.rb'
# OC1. The Lexiconomitron
# We are going to create an awesome class called Lexiconomitron. This class is the goddamn master when it comes to phrases, words, and
# this kind of stuff, and it has a Ruby interface so we can speak to IT. The only downside to this class is one thing: it loves T letters,
# so it devores them. If it has to return, let’s say “trigonometry”, it only gives “rigonomery” back.
# One of the methods of this Ruby interface is one called “shazam”. It takes an array of words (for example [“This”, “is”, “a”, “boring”,
# “test”]). With this array, it reverses the letters within the words (that is, [“sihT”, “si”, “a”, “gnirob”, “tset”]) and ends up returning the first and last. But Lexiconomitron eats T letters, so it returns [“sih, “se”].
# Another method of Lexiconomitron is called “oompa_loompa”. Also taking an array of words (boy, it loves words), lets say for example
# [“if”, “you”, “wanna”, “be”, “my”, “lover”], it takes the ones which size is three characters of less in the same order, so: [“if”, you”,
# “be”, “my”]. After passing the eat-all-t-letters filter, it’s… the same!
# The third method will be called "all_in_line". This will take an array of words and a string X, sort them from longer to shorter,
# and then print them all in a string, using the X string as the delimiter. For example, if we pass ["words", "are", "tremendously", "nice"] and
# "$" as a separator, it will print: "tremendously$words$nice$are". And yes, Lexiconomitron is still hungry and will eat all your Ts.
# The final method is called “supercounter”. Taking (as you might imagine) ¡an array of words!, it returns the number of letters, within
# all those words, that appear before “t” in the alphabet. For example, given [“I”, “am”, “getting”, “tired”, “of”, “your”, “words”],
# it would return 20.
# Using RSpec and TDD, write some tests for this class, for all three methods, exploring different scenarios and spec data.
# And remember that Lexiconomitron devores T letters!
class Lexiconomitron
def initialize(words)
@words_array = words.split(' ')
end
def oompa_lompa
@words_array.select! {|word| word.size <= 3}
eat_the_tt
end
def shazam
@words_array.map! {|word| word.reverse}
eat_the_tt
end
def all_in
@words_array.sort_by! {|word| word.size}.reverse!
#@words_array.join ("$")
@words_array.map
#@words_array* "$"
# {|word| word.gsub(/""/,'$')}
eat_the_tt
end
def supercounter
end
def eat_the_tt
@words_array.map {|word| word.delete("T").delete("t")}
#Other way is using gsub (/[T,t]/, '')
end
end
words = "The target is get the truth"
lexi = Lexiconomitron.new(words)
ap lexi.all_in