Skip to content

Instantly share code, notes, and snippets.

@TomColBee
Created July 15, 2018 14:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomColBee/add079c82f68ee3b51dfbd2731208ce2 to your computer and use it in GitHub Desktop.
Save TomColBee/add079c82f68ee3b51dfbd2731208ce2 to your computer and use it in GitHub Desktop.
Coderbyte Python Challenge: Simple Symbols
# 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 different 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.
# Examples:
# +t+a+b+ returns True
# +++a+s++++e+ returns True
# t+a+b+ returns False
# ta+b+s++++r+t returns False
def SimpleSymbols(str):
# import re to remove certain characters from string
import re
# create alphabet list
alphabet = list('abcdefghijklmnopqrstuvwxyz')
# convert string to lower
str = str.lower()
# set n = 0 for loop
n = 0
# create new var letters based on number of letters in string
letter_str = re.sub("[^abcdefghijklmnopqrstuvwxyz]", "", str)
letters = len(letter_str)
# if first or last element in the string is in the alphabet then return False
if str[0] in alphabet or str[-1] in alphabet:
return False
# otherwise, loop from second element to second to last element
else:
for x in range(1,len(str)-1):
# get letter
index = alphabet[x]
# if letter in alphabet and the character before and after is a "+"
# then add one to n, otherwise, dont add anything
if index in alphabet and str[x-1] == "+" and str[x+1] == "+":
n += 1
else:
n += 0
# if n is equal to number of letters in string, then each letter in the string
# has a "+" both before and after the letter
if n == letters:
return True
else:
return False
print(SimpleSymbols(input()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment