Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Challenge 10 (Unreviewed) #364

Merged
merged 5 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()