-
Notifications
You must be signed in to change notification settings - Fork 0
/
rhyme-harder.rs
161 lines (141 loc) · 2.67 KB
/
rhyme-harder.rs
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
use std;
#[test]
fn testLettersDontRepeat_passIfTrue()
{
assert lettersDontRepeat("ab".chars());
}
#[test]
fn testLettersDontRepeat_failIfFalse()
{
assert !lettersDontRepeat("aba".chars());
}
#[test]
fn testAFirstIfPresent_passIfIs()
{
assert aIsFirstIfPresent("ab".chars());
}
#[test]
fn testAFirstIfPresent_passIfNotPresent()
{
assert aIsFirstIfPresent("bc".chars());
}
#[test]
fn testAFirstIfPresent_failIfIsNot()
{
assert !aIsFirstIfPresent("ba".chars());
}
#[test]
fn testZLastIfPresent_passIfIs()
{
assert zIsLastIfPresent("az".chars());
}
#[test]
fn testZLastIfPresent_passIfNotPresent()
{
assert zIsLastIfPresent("ab".chars());
}
#[test]
fn testZLastIfPresent_failIfIsNot()
{
assert !zIsLastIfPresent("za".chars());
}
#[test]
fn testContainsAllLetters_succeedIfDoes()
{
assert containsAllLetters("abcdefghijklmnopqrstuvwxyz".chars());
}
#[test]
fn testContainsAllLetters_failIfNot()
{
assert !containsAllLetters("za".chars());
}
#[test]
fn testLettersRhyme_succeedIfDo()
{
assert lettersRhyme('a', 'h');
}
#[test]
fn testLettersRhyme_failIfDoNot()
{
assert !lettersRhyme('a', 'b');
}
#[test]
fn testPathIsValid_succeedIfIs()
{
assert pathIsValid("a".chars());
assert pathIsValid("ab".chars());
assert pathIsValid("az".chars());
}
fn pathIsValid(path : [char]) -> bool
{
let mut isValid = lettersDontRepeat(path)
&& aIsFirstIfPresent(path)
&& zIsLastIfPresent(path);
let mut index = 0u;
while index < path.len() - 1u
{
isValid &= !lettersRhyme(path[index], path[index + 1u]);
index += 1u;
}
isValid
}
fn fullPathIsValid(path : [char]) -> bool
{
pathIsValid(path) && containsAllLetters(path)
}
fn lettersDontRepeat(letters : [char]) -> bool
{
for letter in letters
{
if letters.count(letter) > 1u
{
ret false;
}
}
true
}
fn aIsFirstIfPresent(letters : [char]) -> bool
{
!letters.contains('a') || letters[0] == 'a'
}
fn zIsLastIfPresent(letters : [char]) -> bool
{
!letters.contains('z') || letters[letters.len() - 1u] == 'z'
}
fn containsAllLetters(letters : [char]) -> bool
{
let allLetters = "abcdefghijklmnopqrstuvwxyz".chars();
for letter in allLetters
{
if !letters.contains(letter)
{
ret false;
}
}
true
}
fn lettersRhyme(first : char, second : char) -> bool
{
let rhymeSets = [set("ahjk"),set("iy"),set("bcdegptvz"),set("flmnxs"),set("quw"),set("o"),set("r")];
for rhymeSet in rhymeSets
{
if rhymeSet.contains(first) && rhymeSet.contains(second)
{
ret true;
}
}
false
}
fn set(input : str) -> [char]
{
input.chars()
}
impl str_utils for str
{
fn chars() -> [char] {str::chars(self)}
}
impl vec_utils<T> for [T]
{
fn count(value:T) -> uint {vec::count(self, value)}
fn contains(value:T) -> bool {vec::contains(self, value)}
}