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

[Python] Challenge 7 (Unreviewed) #361

Merged
merged 5 commits into from
Jan 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
challenge 7 in python
  • Loading branch information
sarcodian committed Jan 11, 2017
commit ccd15a08b81b7bcaab646dee7c1d58d03deb7459
12 changes: 12 additions & 0 deletions challenge_7/python/sarcodian/src/challenge_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def missing_int(L):

sum_n = 0
sum_n_less_1 = 0

for i in range(len(L)+1):
sum_n += i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try the more pythonic sum([i for i in range(len(L)+1)])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, can you provide a link or something that provides a deeper explanation of <i for i in range(len(L)+1)> syntax. I have seen it a lot in other code as well, but I don't get the logic behind how it works so I am wary of just using it. Or even if you provide the name for what that type of syntax is called I can google it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As sent to you in slack, here's a great video on the topic (and also for the reference of others)
https://www.youtube.com/watch?v=ZoWgzG_r2qo


for i in L:
sum_n_less_1 += i
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try the more pythonic and readable sum(L).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, thanks, didn't know it existed :)


return sum_n - sum_n_less_1
23 changes: 23 additions & 0 deletions challenge_7/python/sarcodian/src/challenge_7_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# we start by importing the unittest module
import unittest

# 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_7 import missing_int

# lets define our suite of tests as a class and lets inherit from unittest.TestCase
class TestBinaryMethods(unittest.TestCase):

def test_missingInt(self):
""" Tests from the READ.me """

self.assertEqual(missing_int([1,3,4,0]), 2)
self.assertEqual(missing_int([1,2,3]), 0)
self.assertEqual(missing_int([0,1,2,4]), 3)

# 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()