Skip to content

Instantly share code, notes, and snippets.

@matthewrudy
Created February 11, 2011 09:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewrudy/822139 to your computer and use it in GitHub Desktop.
Save matthewrudy/822139 to your computer and use it in GitHub Desktop.
<<-LRUG Hola El Rug,
Here's a little teaser for you...
I have two sets of photos; we'll call them urban and nature. Both sets contain about 6-9 photos, but not necessarily the same number.
I also have about 15-20 pages on a site, spread more or less evenly across three sections. Each page has a title unique to its section (and, if it helps, we can assume unique across all the pages).
On each page I want to display one photo from each set.
The constraints in descending order are:
* The code should change as little as possible when I add or remove a page or photo.
* We should use each photo at least once.
* A combination of [urban photo, nature photo] should not appear on more than one page.
* And we should try to use all the photos roughly uniformly.
Your time starts...now!
LRUG
U = ["a","b","c","d", "e", "f", "g", "h", "i"]
N = ["u", "v", "w", "x", "y", "z"]
def s(i, u, n)
u_index = i%(u.length)
n_offset = i/(u.length)
n_index = (u_index+n_offset)%(n.length)
[u[u_index], n[n_index]]
end
results = {}
0.upto(50) do |i|
value = s(i, U, N)
puts value.inspect
results[value] ||= 0
results[value] += 1
end
# every combo happens once
puts results.values.uniq
@airblade
Copy link

Lovely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment