Skip to content

Commit

Permalink
renamed dogs and people, added sample and instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Jul 1, 2014
1 parent 4ab478a commit f277a72
Show file tree
Hide file tree
Showing 24 changed files with 1,644 additions and 129 deletions.
1,512 changes: 1,512 additions & 0 deletions Crypto/BigInt.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions Crypto/sieve.js
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
Binary file added Matching/img/Colleen.jpeg
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 removed Matching/img/Fido.jpeg
Binary file not shown.
File renamed without changes
File renamed without changes
Binary file modified Matching/img/Jess.jpeg
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 removed Matching/img/Sally.jpeg
Binary file not shown.
Binary file modified Matching/img/Sandra.jpeg
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 added Matching/img/Sprinkles.jpeg
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
9 changes: 5 additions & 4 deletions Matching/multipleTrials.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
</head>
<body>
<div class='container'>
<div class='results'>
<p>Trial count: <span class='trials'></span></p>
<p>Average satisfaction: <span class='satisfaction'></span>%</p>
</div>
<label>Trials: <span class='trials'>10</span></label><br>
<input class='trialsSelector' type='range' min='1' max='30' value='10'></input><br>
<label>Sample Size: <span class='size'>100</label><br>
<input class='sizeSelector' type='range' min='10' max='1000' value='100'></input>
<p>Average satisfaction: <span class='satisfaction'></span>%</p>
</div>
<script src='src/solution.js'></script>
<script src='src/scripts.js'></script>
Expand Down
47 changes: 26 additions & 21 deletions Matching/src/scripts.coffee
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
generatePrefs = (size) ->
humanNames = [
"Bob",
"Sally",
"James",
"Tadd",
"Sandra",
"Edward",
"Colleen",
"Colin",
"Peggy",
"Amir",
"Lee",
"Bethany",
"Gilford",
"Ming",
"Fredrick",
"Fanny"
"Megan"
]

puppyNames = [
"Spike",
"Rover",
"Spot",
"Buster",
"Snuggles",
"Ike",
"Bernie",
"Breakfast",
"Snoopy",
"Napoleon",
"Rex",
"Lassie",
"Jess",
"Fido"
"Louie",
"Sprinkles",
"Jess"
]

while humanNames.length < size
Expand Down Expand Up @@ -109,8 +109,8 @@ render = (solution, prefs, result) ->

$humans.append $("<td><img class='photo' src='img/#{human}.jpeg'/></td>")
$puppies.append $("<td><img class='photo' src='img/#{puppy}.jpeg'/></td>")
$humanRankings.append $("<td>#{prefs.humans[human].indexOf(puppy)}</td>")
$puppyRankings.append $("<td>#{prefs.puppies[puppy].indexOf(human)}</td>")
$humanRankings.append $("<td><strong>#{human}</strong><br><em>#{prefs.humans[human].indexOf(puppy)} points</em></td>")
$puppyRankings.append $("<td><strong>#{puppy}</strong><br><em>#{prefs.puppies[puppy].indexOf(human)} points</em></td>")

$(".happiness").text(result.happiness)
$(".satisfaction").text(result.satisfaction.toFixed(2))
Expand All @@ -123,18 +123,23 @@ window.initGraphical = () ->
render(solution, prefs, result)

window.initMultiple = () ->
size = 50
trialCount = 10
size = Number($('.sizeSelector').val())
trials = Number($('.trialsSelector').val())

$('.size').text(size)
$('.trials').text(trials)

console.log size, trials
satisfaction = 0
_(trialCount).times () ->
_(trials).times () ->
prefs = generatePrefs(size)
solution = window.solve(JSON.parse(JSON.stringify(prefs)), size)
result = evalSolution(prefs, solution, size)
satisfaction += result.satisfaction

$(".trials").text(trialCount)
$(".satisfaction").text(satisfaction/trialCount)
$(".satisfaction").text(satisfaction/trials)

$('body').on('change', '.sizeSelector, .trialsSelector', _.throttle(window.initMultiple, 1000))



Expand Down
26 changes: 15 additions & 11 deletions Matching/src/scripts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 3 additions & 48 deletions Matching/src/solution.coffee
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()


109 changes: 64 additions & 45 deletions Matching/src/solution.js
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;
};

0 comments on commit f277a72

Please sign in to comment.