Skip to content

Commit

Permalink
[Python]Challenge_7(Unreviewed) (YearOfProgramming#432)
Browse files Browse the repository at this point in the history
[Python]Challenge_7(Reviewed)
  • Loading branch information
ajschrier authored and MJUIUC committed Jan 20, 2017
1 parent 18e9143 commit 3f71adb
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
23 changes: 23 additions & 0 deletions challenge_7/python/ajschrier/MissingNumber.py
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()
23 changes: 23 additions & 0 deletions challenge_7/python/ajschrier/README.md
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.
108 changes: 108 additions & 0 deletions challenge_7/python/ajschrier/Test_MissingNumber.py
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()

0 comments on commit 3f71adb

Please sign in to comment.