Programming Questions

  • Newest
  • Popular Tags
  • Ask A Question
  • Python Simple Symbols
    Hi Everybody I'm new to python and to this site. I'm having a little problem with one of the challenge. It's the Simple Symbols Challenge.
     
    def SimpleSymbols(string): 
      for letter in range(len(string)) :
        if ord((string[letter]).lower()) >= ord('a') and ord((string[letter]).lower()) <= ord('z'):
          if ord(string[letter-1]) == ord('+') and ord(string[letter+1]) == ord('+'):
             break
          else:
            return False
    
      return True
    print SimpleSymbols(raw_input())           
    
    One of the main problem is that if the first symbol is a letter, the second letter is a + and the last letter is also a + it returns True. I understand why it does it (Because when it checks for the +x+ sequence, it will check for + signs around the letter, and if the first symbole of the string is a letter it will look for -1 wich is the last letter of the string), but I'm not sure how to actually resolve it. Also, when I send the code for checks, all the results are wrong, but when I test them, they look fine. Why would that be? I don't want a direct answer, maybe just a direction on where to look Thanks a lot!
    PsYc0p4tH posted this question on 5/2/14 | python, symbol
    Answers
  • +
  • 3
  • -
  • Hey, so to sum up what you are doing with comments:
    if ord((string[letter]).lower()) >= ord('a') and ord((string[letter]).lower()) <= ord('z'):
    1. You scan for characters in the range from a-z. This is a recurring task on this site so you might want to invest the time to look up the isalpha()-function in python to prevent you from writing this check over and over again. The syntax is str.isalpha(), so in your case it would be str[letter].isalpha(). While you are at it look at the other string functions like isdigit(), isspace(), etc : en.wikibooks.org/wiki/Python_Programming/Strings
      if ord(string[letter-1]) == ord('+') and ord(string[letter+1]) == ord('+'):
             break
          else:
            return False
    
    2. You check if the alphabetical character is surrounded by a '+' character on each side. I see two problems here: why would you break out of the loop if the condition is true once? Consider the string "+a+ xxxxxx". Just because there is a character that is surrounded by '+' that does not mean it is true for subsequent characters. You are however supposed to check this for EVERY character. It may be easier to just check for the logical opposite of your condition and have the function return false in that case, i.e. once you find a character which is not surrounded by '+'.
     if ord(string[letter-1]) == ord('+') and ord(string[letter+1]) == ord('+'):
    3. Something that is specific to your code: you run out of bounds, if you check the first or the last element. You will check the first or the last element if either one is an alphabetical character. If that is the case they are not surrounded by a '+' on either side, because there is nothing towards one side or the other. Think about how to implement the fact that having a character in the beginning or the end will make the function always return false, independent of the rest of the string. You can fix this code by adding a simple check outside the loop and by adjusting the behaviour of your check for '+'. I am not going to be any clearer than that since you did not want a direct answer.
  • +
  • 3
  • -
  • "Also, when I send the code for checks, all the results are wrong, but when I test them, they look fine. Why would that be?" The test case awaits "true" as a String not as a boolean (True and False in python), your return statements must be
     
    if condition:
        return "true"
    else:
        return "false"
     
  • +
  • 0
  • -
  • I also had a problem like your first letter, second plus, last plus. The problem is we are accidentally looping around the array, because
    string[-1]
    gives us the last element. You should try paddding the string iin the beggining like this:
    string = '=' + string
    that should help.
    Log in to write an answer.