forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#364 from sarcodian/python_10
[Python] Challenge 10 (reviewed and merged)
- Loading branch information
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |