Answorth
Answorth

Reputation: 147

Navigating around a string in python

Pardon the incredibly trivial/noob question, at least it should be easy to answer. I've been working through the coderbyte problems and solving the easy ones in python, but have come across a wall. the problem is to return True if a string (e.g. d+==f+d++) has all alpha characters surrounded by plus signs (+) and if not return false. I'm blanking on the concept that would help navigate around these strings, I tried doing with a loop and if statement, but it failed to loop through the text entirely, and always returned false (from the first problem):

def SimpleSymbols(str):
    split = list(str)
    rawletters = "abcdefghijklmnopqrstuvwxyz"
    letters = list(rawletters)

    for i in split:
        if i in letters and (((split.index(i)) - 1) == '+') and (((split.index(i)) + 1) == '+'):
            return True
        else:
            return False


print SimpleSymbols(raw_input())

Also editing to add the problem statement: "Using the Python language, have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter."

Any assistance would be greatly appreciated. Thank you!

Upvotes: 1

Views: 1762

Answers (2)

Adam Smith
Adam Smith

Reputation: 54213

More of a note than an answer, but I wanted to mention regular expressions as a solution not because it's the right one for your scenario (this looks distinctly homework-ish so I understand you're almost certainly not allowed to use regex) but just to ingrain upon you early that in Python, almost EVERYTHING is solved by import foo.

import re

def SimpleSymbols(target):
    return not (re.search(r"[^a-zA-Z+=]",target) and re.search(r"(?<!\+)\w|\w(?!\+)",target))

Upvotes: 0

mgilson
mgilson

Reputation: 309949

Here's how I would do the first part (if I weren't using regex):

import string

LOWERCASE = set(string.ascii_lowercase)

def plus_surrounds(s):
    """Return True if `+` surrounds a single ascii lowercase letter."""
    # initialize to 0 -- The first time through the loop is guaranteed not
    # to find anything, but it will initialize `idx1` and `idx2` for us.
    # We could actually make this more efficient by factoring out
    # the first 2 `find` operations (left as an exercise).
    idx2 = idx1 = 0

    # if the indices are negative, we hit the end of the string.
    while idx2 >= 0 and idx1 >= 0:
        # if they're 2 spaces apart, check the character between them
        # otherwise, keep going.   
        if (idx2 - idx1 == 2) and (s[idx1+1] in LOWERCASE):
            return True
        idx1 = s.find('+', idx2)
        idx2 = s.find('+', max(idx1+1, 0))
    return False

assert plus_surrounds('s+s+s')
assert plus_surrounds('+s+')
assert not plus_surrounds('+aa+')

I think that if you study this code and understand it, you should be able to get the second part without too much trouble.

Upvotes: 2

Related Questions