Skip to content

[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
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions challenge_7/julia/DTCitron/README.md
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/
14 changes: 14 additions & 0 deletions challenge_7/julia/DTCitron/src/findmissing.jl
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))
40 changes: 40 additions & 0 deletions challenge_7/python/DTCitron/README.md
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
16 changes: 16 additions & 0 deletions challenge_7/python/DTCitron/src/main.py
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))

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.


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)