-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathStringSum.rb
47 lines (33 loc) · 929 Bytes
/
StringSum.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
=begin
Compare two strings by comparing the sum of their values (ASCII character code).
For comparing treat all letters as UpperCase.
Null-Strings should be treated as if they are empty strings.
If the string contains other characters than letters, treat the whole string as
it would be empty.
Examples:
"AD","BC" -> equal
"AD","DD" -> not equal
"gf","FG" -> equal
"zz1","" -> equal
"ZzZz", "ffPFF" -> equal
"kl", "lz" -> not equal
null, "" -> equal
Your method should return true, if the strings are equal and false if they are
not equal.
=end
# My Solution
def compare(s1,s2)
return true if s1 == "" || s2 == ""
s1.gsub! /[\W\d]/ , ""
s2.gsub! /[\W\d]/ , ""
p s1
p s2
s1Sum = 0 ; s2Sum = 0
s1.split("").each {|x| s1Sum += x.upcase.ord}
s2.split("").each {|x| s2Sum += x.upcase.ord}
s1Sum == s2Sum
end
# Better Solution
def compare *s
s.map{|s| s.to_s.upcase[/^[A-Z]*$|/].sum }.reduce &:==
end