-
Notifications
You must be signed in to change notification settings - Fork 2
Proper Parenthetics for Jonathan Stallings *Do Not Merge* #4
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
Open
jonathanstallings
wants to merge
13
commits into
master
Choose a base branch
from
jonathan/parenthetics
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
49e2830
Add files for parenthetics.
jonathanstallings 760fa6a
Sketch docstring for parenthetics functions
jonathanstallings 6b77321
Add simple tests.
jonathanstallings ce2d5ea
Complete first pass of parenthical function
jonathanstallings 81a5a4e
Add coding utf-8 encoding for tests
jonathanstallings a146356
Add __len__ methods to linked list and stack
jonathanstallings 007137b
Fix typo in test
jonathanstallings 10925ef
Update docstring
jonathanstallings e0de980
Simplify function
jonathanstallings 58d614b
Update README
jonathanstallings 643c1a4
Fix typos
jonathanstallings d07f1d2
Fix typo
jonathanstallings baa42f1
Fix more typos
jonathanstallings File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,27 @@ | ||
from __future__ import unicode_literals | ||
|
||
from stack import Stack | ||
|
||
|
||
def parenthetical(string): | ||
"""Examine string for valid use of parentheses. | ||
args: | ||
string: the unicode string to Examine | ||
returns: | ||
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: | ||
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 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exactly correct. |
This file contains hidden or 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
This file contains hidden or 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,37 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
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 ()') == 0 | ||
|
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good test using actual unicode input. |
||
|
||
|
||
def test_bad_input(): | ||
with pytest.raises(TypeError): | ||
parenthetical(123) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice touch adding this. Good use of special methods!