-
Notifications
You must be signed in to change notification settings - Fork 160
[Julia] Challenge 7 (Unreviewed) #370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dtcitron
wants to merge
2
commits into
YearOfProgramming:master
Choose a base branch
from
dtcitron:challenge_7
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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/ |
This file contains hidden or 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,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)) |
This file contains hidden or 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,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 |
This file contains hidden or 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 @@ | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need numpy to use sum.