-
Notifications
You must be signed in to change notification settings - Fork 0
Branch1 #1
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
StudentMAGe
wants to merge
14
commits into
master
Choose a base branch
from
branch1
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
Branch1 #1
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
a2d8a98
Add files via upload
StudentMAGe 68aa9ac
Update README.md
StudentMAGe b095ad3
Update README.md
StudentMAGe b0c5bd2
Create __init__.py
StudentMAGe a0dd06d
Create __init__.py
StudentMAGe d539f59
Add files via upload
StudentMAGe 45c890a
Update README.md
StudentMAGe 36ab966
Removed bool flag and changed init for generator
StudentMAGe 4af24ca
Test changed according to Reviewer's comments
StudentMAGe 88873a6
Removed Jupiter debris
StudentMAGe bad986b
Minor style changes
StudentMAGe 02b0ed8
Removed unnecessary code
StudentMAGe cece13d
Added additional tests
StudentMAGe ffd96ec
Added several additional tests
StudentMAGe 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,9 @@ | ||
| # PythonHW_Iterator_Generator | ||
| ### PythonHW_Iterator_Generator | ||
|
|
||
| Contains Python 3 code for classes IteratorEnhancedRange and GeneratorEnhancedRange. | ||
|
|
||
| Both work similar to standard Python range(int) function, except they are able work with float arguments. | ||
| Both classes require at least one argument. All arguments are positional: | ||
| start (default = 0), end (no default, required argument), step (default = 1). | ||
| One specified argument (x) is interpreted as (0, x, 1). | ||
| Two arguments are interpreted as start and end, respectively. |
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,84 @@ | ||
| class IteratorEnhancedRange: | ||
|
|
||
| def __init__(self, *args): | ||
| #Arguments are: start (default = 0), end (no default, required argument), step (default = 1) | ||
| self.args = args | ||
| for arg in args: | ||
| if not (type(arg) == int or type(arg) == float): | ||
| raise Exception("Arguments must be numeric") | ||
| if len(args) == 1: | ||
| self.start = 0 | ||
| self.end = self.args[0] | ||
| self.step = 1 | ||
| elif len(args) == 2: | ||
| self.start = self.args[0] | ||
| self.end = self.args[1] | ||
| self.step = 1 | ||
| elif len(args) == 3: | ||
| if self.args[2] == 0: | ||
| raise Exception("Unable to range with step = 0") | ||
| self.start = self.args[0] | ||
| self.end = self.args[1] | ||
| self.step = self.args[2] | ||
| else: | ||
| raise Exception("IteratorEnhancedRange requires at least one, but no more than three arguments") | ||
| self.startediter = False | ||
|
agbragin marked this conversation as resolved.
|
||
|
|
||
| def __iter__(self): | ||
| return self | ||
|
|
||
| def __next__(self): | ||
| if (self.step / abs(self.step)) * self.start >= (self.step / abs(self.step)) * self.end: | ||
| raise StopIteration | ||
| else: | ||
| if self.startediter is False: | ||
| self.startediter = True | ||
| return self.start | ||
| else: | ||
| if (self.step / abs(self.step)) * (self.start + self.step) >= (self.step / abs(self.step)) * self.end: | ||
| raise StopIteration | ||
| else: | ||
| self.start += self.step | ||
|
Collaborator
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.
Owner
Author
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. Sorry, I don't understand this one. |
||
| return self.start | ||
|
|
||
| def __call__(self): | ||
| return self | ||
|
|
||
|
|
||
| class GeneratorEnhancedRange: | ||
|
|
||
| def __init__(self, *args): | ||
| #Arguments are: start (default = 0), end (no default, required argument), step (default = 1) | ||
| self.args = args | ||
|
agbragin marked this conversation as resolved.
|
||
| for arg in args: | ||
| if not (type(arg) == int or type(arg) == float): | ||
| raise Exception("Arguments must be numeric") | ||
| if len(args) == 1: | ||
| self.start = - 1 | ||
| self.end = self.args[0] | ||
| self.step = 1 | ||
| elif len(args) == 2: | ||
| self.start = self.args[0] - 1 | ||
| self.end = self.args[1] | ||
| self.step = 1 | ||
| elif len(args) == 3: | ||
| if self.args[2] == 0: | ||
| raise Exception("Unable to range with step = 0") | ||
| self.start = self.args[0] - self.args[2] | ||
| self.end = self.args[1] | ||
| self.step = self.args[2] | ||
| else: | ||
| raise Exception("GeneratorEnhancedRange requires at least one, but no more than three arguments") | ||
| self.startediter = False | ||
|
|
||
| def __iter__(self): | ||
| return GeneratorEnhancedRange.gener(self) | ||
|
|
||
| def gener(self): | ||
| while (self.step / abs(self.step)) * (self.start + self.step) < (self.step / abs(self.step)) * self.end: | ||
| self.start += self.step | ||
| yield self.start | ||
|
|
||
| def __call__(self): | ||
| return self | ||
|
|
||
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 @@ | ||
|
|
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,36 @@ | ||
| import unittest | ||
| from RangeEnhancedIterator_Generator import GeneratorEnhancedRange | ||
|
|
||
|
|
||
| class GeneratorTests(unittest.TestCase): | ||
|
|
||
| def test_on_range_w_one_arg(self): | ||
| self.assertEqual([n for n in GeneratorEnhancedRange(2)], [0, 1]) | ||
|
|
||
| def test_on_range_w_two_args(self): | ||
| self.assertEqual([n for n in GeneratorEnhancedRange(2, 5)], [2, 3, 4]) | ||
|
|
||
| def test_on_range_w_three_args(self): | ||
| self.assertEqual([n for n in GeneratorEnhancedRange(2, 7, 2)], [2, 4, 6]) | ||
|
|
||
| def test_on_nonnumeric_args(self): | ||
| with self.assertRaises(Exception, msg = "Arguments must be numeric"): | ||
| example = GeneratorEnhancedRange(5, "o", -1) | ||
|
|
||
| def test_on_zero_step(self): | ||
| with self.assertRaises(Exception, msg = "Unable to range with step = 0"): | ||
| example = GeneratorEnhancedRange(5, 6, 0) | ||
|
|
||
| def test_on_wrong_number_of_args(self): | ||
| with self.assertRaises(Exception, | ||
| msg = "GeneratorEnhancedRange requires at least one, but no more than three arguments"): | ||
| example = GeneratorEnhancedRange() | ||
| with self.assertRaises(Exception, | ||
| msg = "GeneratorEnhancedRange requires at least one, but no more than three arguments"): | ||
| example = GeneratorEnhancedRange(2, 7, 2, 0) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
|
|
||
|
|
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,49 @@ | ||
| import unittest | ||
| from RangeEnhancedIterator_Generator import IteratorEnhancedRange | ||
|
|
||
|
|
||
| class IteratorTests(unittest.TestCase): | ||
|
|
||
| def test_on_range_w_one_arg(self): | ||
|
agbragin marked this conversation as resolved.
|
||
| example = IteratorEnhancedRange(2) | ||
| self.assertEqual(next(example), 0) | ||
| self.assertEqual(next(example), 1) | ||
| self.assertRaises(StopIteration, next, example) | ||
|
|
||
| def test_on_range_w_two_args(self): | ||
| example = IteratorEnhancedRange(2,5) | ||
| self.assertEqual(next(example), 2) | ||
| self.assertEqual(next(example), 3) | ||
| self.assertEqual(next(example), 4) | ||
| self.assertRaises(StopIteration, next, example) | ||
|
|
||
| def test_on_range_w_three_args(self): | ||
| example = IteratorEnhancedRange(2, 7, 2) | ||
| self.assertEqual(next(example), 2) | ||
| self.assertEqual(next(example), 4) | ||
| self.assertEqual(next(example), 6) | ||
| self.assertRaises(StopIteration, next, example) | ||
|
|
||
| def test_on_nonnumeric_args(self): | ||
| with self.assertRaises(Exception, msg = "Arguments must be numeric"): | ||
| example = IteratorEnhancedRange(5, "o", -1) | ||
|
|
||
| def test_on_zero_step(self): | ||
| with self.assertRaises(Exception, msg = "Unable to range with step = 0"): | ||
| example = IteratorEnhancedRange(5, 6, 0) | ||
|
|
||
| def test_on_wrong_number_of_args(self): | ||
| with self.assertRaises(Exception, | ||
| msg = "IteratorEnhancedRange requires at least one, but no more than three arguments"): | ||
| example = IteratorEnhancedRange() | ||
| with self.assertRaises(Exception, | ||
| msg = "IteratorEnhancedRange requires at least one, but no more than three arguments"): | ||
| example = IteratorEnhancedRange(2, 7, 2, 0) | ||
|
|
||
| def tearDown(self): | ||
| print ('Tear down called') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
|
|
||
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 @@ | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.