From 49e2830137e10895679205b6563cc0e5620196ca Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 18:51:20 -0700 Subject: [PATCH 01/13] Add files for parenthetics. --- parenthetics.py | 0 test_parenthetics.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 parenthetics.py create mode 100644 test_parenthetics.py diff --git a/parenthetics.py b/parenthetics.py new file mode 100644 index 0000000..e69de29 diff --git a/test_parenthetics.py b/test_parenthetics.py new file mode 100644 index 0000000..e69de29 From 760fa6ac8a9d2a371d1ac3dadd60e789028eb483 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 18:56:56 -0700 Subject: [PATCH 02/13] Sketch docstring for parenthetics functions --- parenthetics.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/parenthetics.py b/parenthetics.py index e69de29..93830fd 100644 --- a/parenthetics.py +++ b/parenthetics.py @@ -0,0 +1,15 @@ +from __future__ import unicode_literals + +from stack import Stack + + +def parenthetics(string): + """Examine string for valid use of paretheses. + args: + string: the unicode string to Examine + returns: + 1 if the string is "open" + 0 if the string is "balanced" + -1 if the string is "broken" + """ + pass From 6b77321ce73e3f30b315899f4bf047d2c6cf47ef Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 19:29:41 -0700 Subject: [PATCH 03/13] Add simple tests. --- test_parenthetics.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test_parenthetics.py b/test_parenthetics.py index e69de29..79100a6 100644 --- a/test_parenthetics.py +++ b/test_parenthetics.py @@ -0,0 +1,35 @@ +from __future__ import unicode_literals +import pytest + +from parenthetics import parenthetical + + +def test_open_parens(): + assert parenthetical('(') == 1 + assert parenthetical('( (()) ') == 1 + assert parenthetical(' ()() (') == 1 + assert parenthetical('( other ) characters (') + + +def test_balanced_parens(): + assert parenthetical('((()))') == 0 + assert parenthetical('()()()') == 0 + assert parenthetical('( other ) characters ()') + + +def test_broken_parens(): + assert parenthetical(')') == -1 + assert parenthetical(') )(()))') == -1 + assert parenthetical(' ()()())') == -1 + assert parenthetical('( other ) characters )') == -1 + + +def test_special_unicode_character_input(): + assert parenthetical(' (パイソン) (') == 1 + assert parenthetical(' (パイソン) ') == 0 + assert parenthetical(') (パイソン) ') == -1 + + +def test_bad_input(): + with pytest.raises(TypeError): + parenthetical(123) From ce2d5ea4e72f7488f9d51657e20f2c29a2afa611 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 19:42:53 -0700 Subject: [PATCH 04/13] Complete first pass of parenthical function --- parenthetics.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/parenthetics.py b/parenthetics.py index 93830fd..a4efd4d 100644 --- a/parenthetics.py +++ b/parenthetics.py @@ -3,7 +3,7 @@ from stack import Stack -def parenthetics(string): +def parenthetical(string): """Examine string for valid use of paretheses. args: string: the unicode string to Examine @@ -12,4 +12,16 @@ def parenthetics(string): 0 if the string is "balanced" -1 if the string is "broken" """ - pass + open_parens = Stack() + for char in unicode(string): + if char == '(': + open_parens.push(char) + elif char == ')': + try: + open_parens.pop() + except IndexError: + return -1 + if len(open_parens) > 1: + return 1 + else: + return len(open_parens) From 81a5a4ec8abbda63e0cad0f1835661d0bacc6c96 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 19:55:23 -0700 Subject: [PATCH 05/13] Add coding utf-8 encoding for tests --- test_parenthetics.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test_parenthetics.py b/test_parenthetics.py index 79100a6..60ac142 100644 --- a/test_parenthetics.py +++ b/test_parenthetics.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + from __future__ import unicode_literals import pytest From a146356c474b2f94fc2614bc603face85f42ec63 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 19:56:03 -0700 Subject: [PATCH 06/13] Add __len__ methods to linked list and stack --- linked_list.py | 4 ++++ stack.py | 3 +++ 2 files changed, 7 insertions(+) diff --git a/linked_list.py b/linked_list.py index f482d72..63e0763 100644 --- a/linked_list.py +++ b/linked_list.py @@ -29,6 +29,10 @@ def __repr__(self): node = node.next return "({})".format(output.rstrip(' ,')) + def __len__(self): + """Return the length of the LinkedList.""" + return self.size() + def insert(self, val): """Insert value at head of LinkedList. diff --git a/stack.py b/stack.py index 11d387c..eae6825 100644 --- a/stack.py +++ b/stack.py @@ -12,6 +12,9 @@ def __init__(self, iterable=()): def __repr__(self): return repr(self.other) + def __len__(self): + return self.other.size() + def push(self, value): """Add a value to the head of the stack. From 007137bfb9af473da651d17e5d5b7d348f03fd80 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 19:56:43 -0700 Subject: [PATCH 07/13] Fix typo in test --- test_parenthetics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_parenthetics.py b/test_parenthetics.py index 60ac142..41691ec 100644 --- a/test_parenthetics.py +++ b/test_parenthetics.py @@ -16,7 +16,7 @@ def test_open_parens(): def test_balanced_parens(): assert parenthetical('((()))') == 0 assert parenthetical('()()()') == 0 - assert parenthetical('( other ) characters ()') + assert parenthetical('( other ) characters ()') == 0 def test_broken_parens(): From 10925efadfbd534db31b0b53bfe51458650b0f9f Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 20:00:58 -0700 Subject: [PATCH 08/13] Update docstring --- parenthetics.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/parenthetics.py b/parenthetics.py index a4efd4d..660a50c 100644 --- a/parenthetics.py +++ b/parenthetics.py @@ -8,12 +8,12 @@ def parenthetical(string): args: string: the unicode string to Examine returns: - 1 if the string is "open" - 0 if the string is "balanced" - -1 if the string is "broken" + 1 if the string is "open" -- unclosed paretheses + 0 if the string is "balanced" -- equal number of paretheses + -1 if the string is "broken" -- closing paretheses before opening """ open_parens = Stack() - for char in unicode(string): + for char in string: if char == '(': open_parens.push(char) elif char == ')': From e0de9801163b1891c7afae127d36a331cc18a427 Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 20:20:37 -0700 Subject: [PATCH 09/13] Simplify function --- parenthetics.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parenthetics.py b/parenthetics.py index 660a50c..5ddc975 100644 --- a/parenthetics.py +++ b/parenthetics.py @@ -21,7 +21,7 @@ def parenthetical(string): open_parens.pop() except IndexError: return -1 - if len(open_parens) > 1: + if len(open_parens) >= 1: return 1 else: - return len(open_parens) + return 0 From 58d614bd39eaddb744664c03caeca7b02b5703ba Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 20:20:55 -0700 Subject: [PATCH 10/13] Update README --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 3406eb5..d45872e 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,28 @@ Available methods include: * pop() See the doc strings for implementation details. + +##Parenthetics +Contains the parenthetical function which checks the validity of parenthical usage. + +The function will examine a unicode string as its sole argument and return as follows: + +* Return 1 if the string is "open" (there are open parens that are not closed) +* Return 0 if the string is "balanced" (there are an equal number of open and closed parentheses in the string) +* Return -1 if the string is "broken" (a closing parens has not been proceeded by one that opens) + +###Exammples + +Open Case: +* '( ()' + +Balanced Case: +* '(())' + +Broken Case: +* ') ()' + +###Notes +This function was written with inspiration from the work of: +* [Jason Tyler](https://github.com/jay-tyler) +*[Jim Grant](https://github.com/MigrantJ) From 643c1a40c83631aae9570fd6de7a0221596d8c5c Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 20:46:49 -0700 Subject: [PATCH 11/13] Fix typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d45872e..e52fc90 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Available methods include: See the doc strings for implementation details. ##Parenthetics -Contains the parenthetical function which checks the validity of parenthical usage. +Contains the parenthetical function which checks the validity of parenthetical usage. The function will examine a unicode string as its sole argument and return as follows: @@ -44,4 +44,4 @@ Broken Case: ###Notes This function was written with inspiration from the work of: * [Jason Tyler](https://github.com/jay-tyler) -*[Jim Grant](https://github.com/MigrantJ) +* [Jim Grant](https://github.com/MigrantJ) From d07f1d2fcd52753b570172ecd94a0a89e761a5cd Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 20:47:20 -0700 Subject: [PATCH 12/13] Fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e52fc90..022952b 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ The function will examine a unicode string as its sole argument and return as fo * Return 0 if the string is "balanced" (there are an equal number of open and closed parentheses in the string) * Return -1 if the string is "broken" (a closing parens has not been proceeded by one that opens) -###Exammples +###Examples Open Case: * '( ()' From baa42f17813a136962e2425e9987f020d0098cce Mon Sep 17 00:00:00 2001 From: Jonathan Stallings Date: Sun, 28 Jun 2015 21:16:38 -0700 Subject: [PATCH 13/13] Fix more typos --- parenthetics.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/parenthetics.py b/parenthetics.py index 5ddc975..4b48daf 100644 --- a/parenthetics.py +++ b/parenthetics.py @@ -4,13 +4,13 @@ def parenthetical(string): - """Examine string for valid use of paretheses. + """Examine string for valid use of parentheses. args: string: the unicode string to Examine returns: - 1 if the string is "open" -- unclosed paretheses - 0 if the string is "balanced" -- equal number of paretheses - -1 if the string is "broken" -- closing paretheses before opening + 1 if the string is "open" -- unclosed parentheses + 0 if the string is "balanced" -- equal number of parentheses + -1 if the string is "broken" -- closing parentheses before opening """ open_parens = Stack() for char in string: