Checking strings

79 views Asked by At

The function is supposed to return true only if all letters have "+" on both sides, but I always get true as my value... What's the flaw in my code?

def SimpleSymbol?(str)
str_array = str.split("")
i = 0
while i < str_array.length-1
    if ((str_array[i]  >= "a") && (str_array[i] <= "Z"))
        if  ((i == 0) || (i == str_array.length-1))
            return false
        end

        if ((str_array[i-1] != "+") && (str_array[i+1] != "+"))
            return false 
        end

    end
i += 1
end

return true
end

puts SimpleSymbol?("ads++d+")
3

There are 3 answers

1
Karoly Horvath On BEST ANSWER
  • "Z" is smaller than "a".

  • If both sides must to be +, and you check for mismatch, the logical connection is OR (= if either of those things are true).

    if ((str_array[i-1] != "+") || (str_array[i+1] != "+"))
    

note: you don't need to split, you can index into strings.

0
tadman On

This is best handled by a regular expression:

def simple_symbol?(str)
  !!str.match(/\A(\+[a-zA-Z](?:\+[a-zA-Z])*\+)\z/)
end

[
  '+a+b+c+',
  '+a+b+c',
  'a+b',
  'abc++d+',
  '+',
  'abc',
  ''
].each do |string|
  puts '%-12s %s' % [ string, simple_symbol?(string) ]
end

# => +a+b+c+      true
# => +a+b+c       false
# => a+b          false
# => abc++d+      false
# => +            false
# => abc          false
# =>              false

Note that Ruby's convention for method names is to use underscore_type_names, as CamelCase is reserved for classes and modules.

0
Cary Swoveland On

I agree with @tadman that a regular expression should be used here. Here's another way, that looks for a letter that is not bracketed by +'s:

R = /
    (?:     # begin a non-capture group
      \A    # match beginning of string
      |     # or
      [^+] # match any character other than +
    )       # end non-capture group
    [a-b]   # match a letter
    |       # or
    [a-b]   # match a letter
    (?:     # begin a non-capture group
      [^+] # match any character other than +
      |     # or
      \z    # match end of string
    )       # end non-capture group
    /xi     # use extended mode (x) and make case insensitive (i)

def letters_bracketed?(str)
   str !~ R
end

letters_bracketed?('+a+')         #=> true 
letters_bracketed?('+a+b+')       #=> true 
letters_bracketed?('+a+937+b+!!') #=> true 
letters_bracketed?('a+b+')        #=> false 
letters_bracketed?('+a+b')        #=> false 
letters_bracketed?('+?a+b+')      #=> false 
letters_bracketed?('+ab+')        #=> false 

Note that + need not be escaped in a character class.