Skip to content

Commit

Permalink
Merge pull request YearOfProgramming#364 from sarcodian/python_10
Browse files Browse the repository at this point in the history
[Python] Challenge 10 (reviewed and merged)
  • Loading branch information
Steven Landau committed Jan 23, 2017
2 parents 5f337c6 + c4c8950 commit 69725c0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
8 changes: 8 additions & 0 deletions challenge_10/python/sarcodian/READ.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Takes a string and checks to see every openning {,(,[
has a corresponding closing ],),}.

Does this by appending a list of every openning and deletes
the last element when it comes accross a matching closing.

If the closing is off a different type or if the list has
elements remaining then returns False.
32 changes: 32 additions & 0 deletions challenge_10/python/sarcodian/src/challenge_10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def brackets():
'''
Asks for a string and returns True/False.
True if all openning brackets {([ have a matching ])},
else returns False
'''

a_string = input("Please enter a string to process: ")

brac_dict = {
'}' : '{',
']' : '[',
')' : '(',
}

brac_store = []

for i in a_string:
if i in brac_dict.values():
brac_store.append(i)
elif i in brac_dict.keys():
if len(brac_store) == 0:
return False
if brac_dict[i] == brac_store[-1]:
del brac_store[-1]
else:
return False

if len(brac_store) > 0:
return False
else:
return True
45 changes: 45 additions & 0 deletions challenge_10/python/sarcodian/src/challenge_10_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# we start by importing the unittest module
import unittest
from unittest import mock

# Next lets import our function that we intend to do testing on
#
# **We could also import all functions by using * or just import the module
# itself but for now lets just import the function
from challenge_10 import brackets

# lets define our suite of tests as a class and lets inherit from unittest.TestCase
class TestBinaryMethods(unittest.TestCase):

def test_Brackets(self):
""" Tests from the READ.me """

test_list = [
'{{{{{{{{{adfkjaefia}}}}}}}',
'{{{{{{{{{[[[[[[kadfa{{{{{{{((({daljfdaf({{{[]}}kaldjfs})})))}}}}}}}]]]]]]}kjfela}}}}}}}}',
'{{{[}}}}dafda',
'{{{{{{{{{}}}}}}}}}',
'[[[[[[[[[kafjalfeianfailfeja;fjai;efa;sfj]]]]]]]]]kjajdain',
'< blank >',
'((((((fjdalfeja((((alefjalisj(())))))))))))d',
')))(((d',
'({)} '
]

with mock.patch('builtins.input', side_effect=test_list):
self.assertEqual(brackets(), False)
self.assertEqual(brackets(), True)
self.assertEqual(brackets(), False)
self.assertEqual(brackets(), True)
self.assertEqual(brackets(), True)
self.assertEqual(brackets(), True)
self.assertEqual(brackets(), True)
self.assertEqual(brackets(), False)
self.assertEqual(brackets(), False)



# if the python file is ran by itself run unittest
# This allows us to import the members of the file without running main if we need to
if __name__ == '__main__':
unittest.main()

0 comments on commit 69725c0

Please sign in to comment.