Skip to content

Instantly share code, notes, and snippets.

@derekmhewitt
Created September 16, 2016 16:45
Show Gist options
  • Save derekmhewitt/11e8e990aac053f972a62bea2335ef70 to your computer and use it in GitHub Desktop.
Save derekmhewitt/11e8e990aac053f972a62bea2335ef70 to your computer and use it in GitHub Desktop.
Code Fellows 401 Challenge Gist
# -*- coding: utf-8 -*-
"""
Code Challenge for this morning, an exercise in matching things up.
Write a function 'matcher()', takes two arguments. First is a string
with some parenthesies. Second string should take pairs of characters
that should be matched, examples: "()" or "()[]" or "''" < single
quotes. The inputs for matching will always be paired.
The return value of this function should be True or False, True if all
pairs of matching things above have a matching element, False otherwise.
"""
SOME_MATCHING_ELEMENTS = "{}[]()""''<>"
def matcher(input_string, matching_pairs):
"""takes a string input and a 'matcher' input of paired things, returns
True if those matching things pair up and open/close properly, else
it returns False."""
counter = 0
apostrophy_count = 0
quote_count = 0
separated_pairs = list(matching_pairs)
left_pairs = separated_pairs[0:][::2]
right_pairs = separated_pairs[1:][::2]
sliced_input = list(input_string)
for character in sliced_input:
for match in left_pairs:
if character == match:
counter += 1
for match in right_pairs:
if character == match:
counter -= 1
if counter == 0:
return True
else:
return False
"""
This is as far as I got.. It doesn't handle " or ' very well , I started to
give them their own counters and ran out of time. It also doesn't
handle the ( [ ) ] problem, it passes in that case. I'm really not sure how
to make that fail at the moment.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment