forked from YearOfProgramming/2017Challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YearOfProgramming#361 from sarcodian/python_7 revi…
…ewed by ningyuansg [Python] Challenge 7 (reviewed and merged)
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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. |
This file contains 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,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 | ||
|
||
|
This file contains 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,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() |