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
    • 362
    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:

    "AD","BC" -> equal

    "AD","DD" -> not equal

    "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("AD", "BC"), true, "\'AD\' vs \'BC\'")
    Test.assert_equals(compare("AD", "DD"), false, "\'AD\' vs \'DD\'")
    Test.assert_equals(compare("gf", "FG"), true, "\'gf\' vs \'FG\'")
    Test.assert_equals(compare("Ad", "DD"), false, "\'Ad\' vs \'DD\'")
    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

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