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

finished streams exercise #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
178 changes: 176 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,179 @@
# USER SPECIFIC/IDE FILES
# Windows image file caches
Thumbs.db
ehthumbs.db

.idea/

# GENERAL PYTHON FILES
# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

# =========================
# Operating System Files
# =========================

# OSX
# =========================

.DS_Store
.AppleDouble
.LSOverride

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
47 changes: 27 additions & 20 deletions stream_exercise.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io


class StreamProcessor(object):
Expand All @@ -11,9 +12,15 @@ class StreamProcessor(object):
my_stream_processor = MyStreamProcessor(f)

2. You call a `process` method of my_stream_processor.

This method:

"""

def __init__(self, stream):
self._stream = stream

def process(self):
"""
This method performs the following:

1. Reads two digits at a time from the beginning of the stream
2. Converts the two digits into a number, and adds that number
to a running total.
Expand All @@ -29,8 +36,8 @@ class StreamProcessor(object):
sum has not yet reached 200, then the method will stop and
return 10.

For example, given a stream yielding "234761640930110349378289194", the
process method will:
For example, given a stream yielding "234761640930110349378289194", the
process method will:

1. Read two digits at a time from the stream: "23", "47", "61", etc.
2. Convert these digits into a number: 23, 47, 61, etc., and make a
Expand All @@ -39,29 +46,29 @@ class StreamProcessor(object):
3. For this particular stream, the running total will exceed 200 after
5 such additions: the `process` method should return 5.

You can see the `tests.py` file for more examples of expected outcomes.
"""
You can see the `tests.py` file for more examples of expected outcomes.

def __init__(self, stream):
self._stream = stream

def process(self):
"""
TODO: Implement the `process` method, as described above.

:return: int
"""

count = 0 # How many two-digit numbers the `process` method has added
# together.
# together.
total = 0 # The running total of sums.

# TODO: WRITE CODE HERE:
while total < 200 and count < 10:

# Just some example syntax, you can read two digits from the head of the
# stream using the following code:
#
# digits = self._stream.read(2)
digits = self._stream.read(2)
if len(digits) < 2:
break
total += int(digits)

count += 1

return count


if __name__ == '__main__':
f = io.StringIO("234761640930110349378289194")
my_stream_processor = StreamProcessor(f)
result = my_stream_processor.process()
print(f"this is what was returned: {result}")