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.
[Python]Challenge_7(Unreviewed) (YearOfProgramming#432)
[Python]Challenge_7(Reviewed)
- Loading branch information
Showing
3 changed files
with
154 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,23 @@ | ||
def builtInMethod(inputList): | ||
for i in xrange(len(inputList)): | ||
# Check if i is in list | ||
if i not in inputList: | ||
# Return number | ||
return i | ||
# Return 0 if none of the numbers meet the condition | ||
return 0 | ||
|
||
|
||
def sumMethod(inputList): | ||
# find (n*n+1)/2 | ||
fullSum = (len(inputList) * (len(inputList) + 1)) / 2 | ||
# subtract sum of full range from supplied range | ||
return fullSum - sum(inputList) | ||
|
||
|
||
def main(): | ||
pass | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,23 @@ | ||
# Challenge 7 - Find the Missing Number | ||
|
||
*Python Version:* 2.7 | ||
|
||
In a list 0..N, find which digit is missing. | ||
|
||
# Functions | ||
|
||
## builtInMethod | ||
|
||
**Input:** List | ||
**Output:** Integer | ||
|
||
Finds the integer 0 < x < N not present in list. The 'not in' syntax performs at O(N) according to [the Python Wiki](https://wiki.python.org/moin/TimeComplexity), so that is used to check the list for the value in the range. | ||
|
||
**UPDATE**: 'not in' syntax is O(N), but the fact that it's in another loop makes the solution O(N^2). Thanks erocs. | ||
|
||
## sumMethod | ||
|
||
**Input:** List | ||
**Output:** Integer | ||
|
||
Uses the principle of sequential ranges (n*n+1)/2 to find the missing element. The sum of the input list is subtracted from the sum of the full range, exposing the missing element. |
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,108 @@ | ||
import MissingNumber | ||
import unittest | ||
|
||
|
||
class BuiltInTests(unittest.TestCase): | ||
"""tests for builtInMethod""" | ||
|
||
def testMissing3(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([0, 1, 2, 4]), 3) | ||
|
||
def testMissing2(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([1, 3, 4, 0]), 2) | ||
|
||
def testNoMissingNumber(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([1, 2, 3]), 0) | ||
|
||
def testLongerList(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, | ||
12, 13, 14, 15, 16, 17, 18, 19, | ||
20, 21, 22, 23, 24]), 7) | ||
|
||
def testEvenLongerRandomList(self): | ||
self.assertEqual(MissingNumber. | ||
builtInMethod([78, 53, 118, 99, 154, 5, 52, 184, | ||
73, 17, 92, 107, 34, 159, 101, 30, | ||
171, 95, 100, 54, 63, 169, 37, 45, | ||
174, 158, 57, 0, 89, 61, 71, 188, | ||
83, 18, 133, 149, 62, 151, 90, 3, | ||
141, 126, 165, 179, 103, 166, 68, | ||
177, 163, 190, 77, 55, 31, 187, 16, | ||
91, 47, 93, 146, 147, 74, 185, 135, | ||
102, 41, 125, 164, 12, 19, 168, 96, | ||
23, 86, 194, 9, 132, 104, 122, 24, | ||
144, 161, 117, 183, 196, 176, 155, | ||
108, 172, 120, 160, 182, 50, 75, 22, | ||
15, 49, 80, 127, 106, 20, 148, 139, | ||
38, 134, 67, 123, 56, 88, 40, 186, | ||
131, 76, 59, 21, 43, 4, 14, 115, | ||
162, 199, 48, 128, 81, 39, 11, 97, | ||
137, 153, 10, 65, 138, 114, 79, 6, | ||
195, 156, 180, 8, 28, 60, 192, 42, | ||
119, 64, 2, 200, 116, 129, 51, 178, | ||
191, 121, 82, 143, 26, 110, 33, 136, | ||
32, 175, 27, 150, 111, 105, 70, 157, | ||
44, 13, 46, 170, 58, 69, 198, 193, | ||
167, 25, 29, 181, 66, 72, 36, 87, 7, | ||
98, 130, 152, 94, 189, 1, 84, 109, | ||
113, 197, 145, 35, 124, 112, 173, | ||
140, 142]), 85) | ||
|
||
|
||
class SumMethodTests(unittest.TestCase): | ||
"""tests for sumMethod""" | ||
|
||
def testMissing3(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([0, 1, 2, 4]), 3) | ||
|
||
def testMissing2(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([1, 3, 4, 0]), 2) | ||
|
||
def testNoMissingNumber(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([1, 2, 3]), 0) | ||
|
||
def testLongerList(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, | ||
12, 13, 14, 15, 16, 17, 18, 19, | ||
20, 21, 22, 23, 24]), 7) | ||
|
||
def testEvenLongerRandomList(self): | ||
self.assertEqual(MissingNumber. | ||
sumMethod([78, 53, 118, 99, 154, 5, 52, 184, | ||
73, 17, 92, 107, 34, 159, 101, 30, | ||
171, 95, 100, 54, 63, 169, 37, 45, | ||
174, 158, 57, 0, 89, 61, 71, 188, | ||
83, 18, 133, 149, 62, 151, 90, 3, | ||
141, 126, 165, 179, 103, 166, 68, | ||
177, 163, 190, 77, 55, 31, 187, 16, | ||
91, 47, 93, 146, 147, 74, 185, 135, | ||
102, 41, 125, 164, 12, 19, 168, 96, | ||
23, 86, 194, 9, 132, 104, 122, 24, | ||
144, 161, 117, 183, 196, 176, 155, | ||
108, 172, 120, 160, 182, 50, 75, 22, | ||
15, 49, 80, 127, 106, 20, 148, 139, | ||
38, 134, 67, 123, 56, 88, 40, 186, | ||
131, 76, 59, 21, 43, 4, 14, 115, | ||
162, 199, 48, 128, 81, 39, 11, 97, | ||
137, 153, 10, 65, 138, 114, 79, 6, | ||
195, 156, 180, 8, 28, 60, 192, 42, | ||
119, 64, 2, 200, 116, 129, 51, 178, | ||
191, 121, 82, 143, 26, 110, 33, 136, | ||
32, 175, 27, 150, 111, 105, 70, 157, | ||
44, 13, 46, 170, 58, 69, 198, 193, | ||
167, 25, 29, 181, 66, 72, 36, 87, 7, | ||
98, 130, 152, 94, 189, 1, 84, 109, | ||
113, 197, 145, 35, 124, 112, 173, | ||
140, 142]), 85) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |