Skip to content

Commit

Permalink
Merge pull request YearOfProgramming#361 from sarcodian/python_7 revi…
Browse files Browse the repository at this point in the history
…ewed by ningyuansg

[Python] Challenge 7 (reviewed and merged)
  • Loading branch information
Steven Landau committed Jan 23, 2017
2 parents cfff9aa + 1db18a9 commit db4481b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
5 changes: 5 additions & 0 deletions challenge_7/python/sarcodian/READ.me
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Assume we are giving a list of unorder consective ints. We have to find
which int is missing. Since we know a single int is missing, we know
that the original list must have a length of given list + 1.
Therefore, we sum all elements of range( lenght_of_given_list + 1) and
subtract the sum of all elements in the given list.
16 changes: 16 additions & 0 deletions challenge_7/python/sarcodian/src/challenge_7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def missing_int(L):
'''
Ask for a list and return the missing value
'''

sum_n = 0
sum_n_less_1 = 0

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

sum_n_less_1 = sum(L)

return sum_n - sum_n_less_1


22 changes: 22 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,22 @@
# 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):

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

0 comments on commit db4481b

Please sign in to comment.