Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # Compare two strings by comparing the sum of their letter-Values

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 145
Comment on it

### Description:

Compare two strings by comparing the sum of their letter-Values (char-Value).
For comparing treat all letters as UpperCase.

Empty and null-Strings should be treated as they are equal.
If the string contains other characters than letters, treat the whole string as it would be empty.

Examples:

"gf","FG" -> equal

"zz1","" -> equal

"ZzZz", "ffPFF" -> equal

"kl", "lz" -> not equal

null, "" -> equal

##### Test Cases:
``````Test.describe("Basic tests") do
Test.assert_equals(compare("gf", "FG"), true, "\'gf\' vs \'FG\'")
Test.assert_equals(compare("zz1", ""), true, "\'zz1\' vs \'\'")
Test.assert_equals(compare("ZzZz", "ffPFF"), true, "\'ZzZz\' vs \'ffPFF\'")
Test.assert_equals(compare("kl", "lz"), false, "\'kl\' vs \'lz\'")
Test.assert_equals(compare(nil, ""), true, "\'<null>\' vs \'\'")
Test.assert_equals(compare("!!", "7476"), true, "\'!!\' vs \'7476\'")
Test.assert_equals(compare("##", "1176"), true, "\'##\' vs \'1176\'")
end``````

### Solution:

``````def compare(s1,s2)
alphabet = ("A".."Z").to_a
s1_arr = s1.upcase.split(//)
s2_arr = s2.upcase.split(//)
s1_val = 0
for count in 0...s1_arr.length
if !(alphabet.include? s1_arr[count])
s1_val = 0
break
end
for counter in 0...26
if s1_arr[count] == alphabet[counter]
#s1_val += counter+1
s1_val += alphabet[counter].ord
end
end
end
s2_val = 0
for count in 0...s2_arr.length
if !(alphabet.include? s2_arr[count])
s2_val = 0
break
end
for counter in 0...26
if s2_arr[count] == alphabet[counter]
#s2_val += counter+1
s2_val += alphabet[counter].ord
end
end
end
s1_val == s2_val
end``````

## 0 Comment(s)

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General