forked from AlgorithmsMeetup/PreviousAlgorithms
-
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.
renamed dogs and people, added sample and instructions
- Loading branch information
1 parent
4ab478a
commit f277a72
Showing
24 changed files
with
1,644 additions
and
129 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,19 @@ | ||
var sieve = function(n) { | ||
var isPrime = [false, false, true, true, false, true, false, true, false, false, | ||
false, true, false, true, false, false, false, true, false, true]; | ||
for (var i = 20; i < n; i++) { | ||
if ((i % 2) && (i % 3) && (i % 5) && (i % 7) && (i % 11) && (i % 13) && (i % 17) && (i % 19)) { | ||
isPrime.push(true); | ||
} else { | ||
isPrime.push(false); | ||
} | ||
} | ||
for (var i = 23, root = Math.ceil(Math.sqrt(n)); i < root; i += 2) { | ||
if (!isPrime[i]) continue; | ||
for (var m = 3*i, step = 2*i; m < n; m += step) { | ||
isPrime[m] = false; | ||
} | ||
} | ||
primes = isPrime.map(function(b, i) {return b ? i : 0}).filter(function(k) {return k}); | ||
return primes; | ||
}; |
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file not shown.
File renamed without changes
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,56 +1,11 @@ | ||
# # Random solution - match each human with a random puppy. Ignores preferences. | ||
# window.solve = (input) -> | ||
# humanPrefs = input.humans | ||
# puppyPrefs = input.puppies | ||
|
||
# humans = _.chain(humanPrefs).keys().shuffle().value() | ||
# puppies = _.chain(puppyPrefs).keys().shuffle().value() | ||
|
||
# return _.chain(humans).zip(puppies).map((pair) -> {human: pair[0], puppy: pair[1]}).value() | ||
|
||
|
||
window.solve = (input) -> | ||
humanPrefs = input.humans | ||
puppyPrefs = input.puppies | ||
|
||
humans = _.keys(humanPrefs) | ||
puppies = _.keys(puppyPrefs) | ||
|
||
matchByHuman = {} | ||
matchByPuppy = {} | ||
|
||
matchedAll = false | ||
roundNum = 0 | ||
while not matchedAll | ||
matchedAll = true | ||
for human in humans | ||
if not matchByHuman[human] | ||
matchedAll = false | ||
# get most favored puppy | ||
puppy = humanPrefs[human].pop() | ||
|
||
# puppy is not yet matched. | ||
if not matchByPuppy[puppy] | ||
matchByPuppy[puppy] = human | ||
matchByHuman[human] = puppy | ||
|
||
# already matched this puppy | ||
else | ||
existingMatch = matchByPuppy[puppy] | ||
|
||
myRank = puppyPrefs[puppy].indexOf(human) | ||
existingMatchRank = puppyPrefs[puppy].indexOf(existingMatch) | ||
|
||
if myRank >= existingMatchRank | ||
matchByHuman[existingMatch] = null | ||
matchByHuman[human] = puppy | ||
matchByPuppy[puppy] = human | ||
|
||
solution = [] | ||
for puppy, human of matchByPuppy | ||
solution.push {human: human, puppy: puppy} | ||
|
||
return solution | ||
humans = _.chain(humanPrefs).keys().shuffle().value() | ||
puppies = _.chain(puppyPrefs).keys().shuffle().value() | ||
|
||
return _.chain(humans).zip(puppies).map((pair) -> {human: pair[0], puppy: pair[1]}).value() | ||
|
||
|
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 |
---|---|---|
@@ -1,47 +1,66 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
(function() { | ||
window.solve = function(input) { | ||
var existingMatch, existingMatchRank, human, humanPrefs, humans, matchByHuman, matchByPuppy, matchedAll, myRank, puppies, puppy, puppyPrefs, roundNum, solution, _i, _len; | ||
humanPrefs = input.humans; | ||
puppyPrefs = input.puppies; | ||
humans = _.keys(humanPrefs); | ||
puppies = _.keys(puppyPrefs); | ||
matchByHuman = {}; | ||
matchByPuppy = {}; | ||
matchedAll = false; | ||
roundNum = 0; | ||
while (!matchedAll) { | ||
matchedAll = true; | ||
for (_i = 0, _len = humans.length; _i < _len; _i++) { | ||
human = humans[_i]; | ||
if (!matchByHuman[human]) { | ||
matchedAll = false; | ||
puppy = humanPrefs[human].pop(); | ||
if (!matchByPuppy[puppy]) { | ||
matchByPuppy[puppy] = human; | ||
matchByHuman[human] = puppy; | ||
} else { | ||
existingMatch = matchByPuppy[puppy]; | ||
myRank = puppyPrefs[puppy].indexOf(human); | ||
existingMatchRank = puppyPrefs[puppy].indexOf(existingMatch); | ||
if (myRank >= existingMatchRank) { | ||
matchByHuman[existingMatch] = null; | ||
matchByHuman[human] = puppy; | ||
matchByPuppy[puppy] = human; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
solution = []; | ||
for (puppy in matchByPuppy) { | ||
human = matchByPuppy[puppy]; | ||
solution.push({ | ||
human: human, | ||
puppy: puppy | ||
}); | ||
/* | ||
Greetings, algorithmics! Today, our problem is the "Best Matching Problem". | ||
Imagine n people go to the pound to adopt puppies, and there are (coincidentally) n puppies at the pound. | ||
Each person makes a list of puppies, ranking them from least to most favorite. | ||
Similarly, each puppy makes a ranking of the humans. | ||
The challenge is to pair up puppies and humans so that the maximum amount of satisfaction is achieved. | ||
This problem is open-ended - the solution I'll present at the end of class is just one possible answer. | ||
--- | ||
INPUT: (Imagine we have 3 humans and 3 puppies.) | ||
{ | ||
humans: { | ||
"human1": ['puppy2', 'puppy3', 'puppy1'], <-- | ||
"human2": ['puppy2', 'puppy1', 'puppy3'], <-- Rankings, | ||
"human3": ['puppy1', 'puppy3', 'puppy2'] <-- | ||
}, <-- from | ||
<-- WORST | ||
puppies: { <-- | ||
"puppy1": ['human1', 'human2', 'human3'], <-- to | ||
"puppy2": ['human2', 'human3', 'human1'], <-- BEST | ||
"puppy3": ['human2', 'human3', 'human1'] <-- | ||
} | ||
return solution; | ||
}; | ||
} | ||
In this situation, human1 likes puppy1 the best, and puppy2 the worst. | ||
OUTPUT: | ||
[ | ||
{human: "human1", puppy: "puppy1"}, <-- An array of objects, | ||
{human: "human2", puppy: "puppy3"}, <-- each listing a puppy | ||
{human: "human3", puppy: "puppy2"} <-- and a human. | ||
] | ||
--- | ||
NOTES: | ||
- You can use Object.keys(input.humans) to get a list of all humans (and similar for puppies) | ||
- You have use of the underscore.js library, which you may find useful. | ||
*/ | ||
|
||
// This solution pairs humans and puppies without considering their preferences. | ||
// Let's see if you can do better! | ||
window.solve = function(input) { | ||
var humanPrefs = input.humans; | ||
var puppyPrefs = input.puppies; | ||
|
||
var humans = Object.keys(humanPrefs); | ||
var puppies = Object.keys(puppyPrefs); | ||
|
||
var matches = []; | ||
|
||
for (var i = 0; i < humans.length; i++) { | ||
matches.push({ | ||
human: humans[i], | ||
puppy: puppies[i] | ||
}) | ||
} | ||
|
||
}).call(this); | ||
return matches; | ||
}; |