From 447573624cd13d2528d55ec2df14110d3504c2b4 Mon Sep 17 00:00:00 2001 From: Daniel T Citron Date: Wed, 11 Jan 2017 12:11:52 -0500 Subject: [PATCH 1/2] [Python] Challenge 7 --- challenge_7/python/DTCitron/README.md | 40 +++++++++++++++++++++++++ challenge_7/python/DTCitron/src/main.py | 16 ++++++++++ 2 files changed, 56 insertions(+) create mode 100644 challenge_7/python/DTCitron/README.md create mode 100644 challenge_7/python/DTCitron/src/main.py diff --git a/challenge_7/python/DTCitron/README.md b/challenge_7/python/DTCitron/README.md new file mode 100644 index 000000000..1b64bc93d --- /dev/null +++ b/challenge_7/python/DTCitron/README.md @@ -0,0 +1,40 @@ +Find The Missing Number +====== +Idea +------ +Given a list of numbers from 0 to N-1, your job is to find the missing number. Your program should take in a list of integers and return a single integer (the missing number). + +Note: This is a popular interview question. Bloomberg in particular, really likes this question. + +Input +----- +* The list of integers will not be sorted +* Each value in the list will be between 0 and N +* Each value will only appear once in the list +* Only one number will be missing + +Requirements +------------ +* You should not sort the list of integers +* Your solution should use O(1) space and complete in O(N) time +* O(1) space means that your algorithm should not need to allocate an additional additional array of N elements to solve the problem +* O(N) times means that your algorithm can not contain nested loops + +Notes +----- +* The test cases will not be sorted +* The fact that the values range from 0 to N is important + +Example 1 +--------- +Given the list [0,1,2,4], your program should return the number 3 + +Example 2 +--------- +Given the list [1,2,3], your program should return the 0. + +## Implementation + +- Call `python main.py` in the `src` directory to demonstrate +- Works by exploiting the known sum of the first n+1 integers from 0 to n, comparing the sum of the input list with a missing integer to that known sum +- Requires numpy to perform the sum over the input list diff --git a/challenge_7/python/DTCitron/src/main.py b/challenge_7/python/DTCitron/src/main.py new file mode 100644 index 000000000..1fb55979f --- /dev/null +++ b/challenge_7/python/DTCitron/src/main.py @@ -0,0 +1,16 @@ +import numpy as np + +def findMissing(l): + n = len(l) + return n*(n+1)/2 - np.sum(np.array(l)) + +if __name__=="__main__": + t1 = [0,1,2,4]; + print "Input: ", t1 + print "Output: ", findMissing(t1) + t2 = [1,2,3]; + print "Input: ", t2 + print "Output: ", findMissing(t2) + t3 = [1,3,4,0]; + print "Input: ", t3 + print "Output: ", findMissing(t3) \ No newline at end of file From ba63062f5f6526392e4bc0f65beb07d4170e6a50 Mon Sep 17 00:00:00 2001 From: Daniel T Citron Date: Wed, 11 Jan 2017 12:18:40 -0500 Subject: [PATCH 2/2] [Julia] challenge_7 --- challenge_7/julia/DTCitron/README.md | 45 +++++++++++++++++++ challenge_7/julia/DTCitron/src/findmissing.jl | 14 ++++++ 2 files changed, 59 insertions(+) create mode 100644 challenge_7/julia/DTCitron/README.md create mode 100644 challenge_7/julia/DTCitron/src/findmissing.jl diff --git a/challenge_7/julia/DTCitron/README.md b/challenge_7/julia/DTCitron/README.md new file mode 100644 index 000000000..2a8d25b7e --- /dev/null +++ b/challenge_7/julia/DTCitron/README.md @@ -0,0 +1,45 @@ +Find The Missing Number +====== +Idea +------ +Given a list of numbers from 0 to N-1, your job is to find the missing number. Your program should take in a list of integers and return a single integer (the missing number). + +Note: This is a popular interview question. Bloomberg in particular, really likes this question. + +Input +----- +* The list of integers will not be sorted +* Each value in the list will be between 0 and N +* Each value will only appear once in the list +* Only one number will be missing + +Requirements +------------ +* You should not sort the list of integers +* Your solution should use O(1) space and complete in O(N) time +* O(1) space means that your algorithm should not need to allocate an additional additional array of N elements to solve the problem +* O(N) times means that your algorithm can not contain nested loops + +Notes +----- +* The test cases will not be sorted +* The fact that the values range from 0 to N is important + +Example 1 +--------- +Given the list [0,1,2,4], your program should return the number 3 + +Example 2 +--------- +Given the list [1,2,3], your program should return the 0. + +Testing +------ +Testing for this challenge is fairly straight forward. Simply create a list of +size N, filled with integers from 0 to N, with one integer missing. + +## Implementation + +- Call `python main.py` in the `src` directory to demonstrate +- Works by exploiting the known sum of the first n+1 integers from 0 to n, comparing the sum of the input list with a missing integer to that known sum +- More on basic functions on arrays in julia: http://docs.julialang.org/en/release-0.4/stdlib/arrays/ diff --git a/challenge_7/julia/DTCitron/src/findmissing.jl b/challenge_7/julia/DTCitron/src/findmissing.jl new file mode 100644 index 000000000..0d6b99db7 --- /dev/null +++ b/challenge_7/julia/DTCitron/src/findmissing.jl @@ -0,0 +1,14 @@ +function findMissing(l::Array) + n = length(l) + return Int(n*(n+1)/2) - sum(l) +end + +t1 = [0,1,2,4]; +println( "Input: ", t1) +println( "Output: ", findMissing(t1)) +t2 = [1,2,3]; +println( "Input: ", t2) +println( "Output: ", findMissing(t2)) +t3 = [1,3,4,0]; +println( "Input: ", t3) +println( "Output: ", findMissing(t3)) \ No newline at end of file