Skip to content

Instantly share code, notes, and snippets.

@cbarraford
Created May 9, 2014 14:15
Show Gist options
  • Save cbarraford/76a298b36513d2e8fc8c to your computer and use it in GitHub Desktop.
Save cbarraford/76a298b36513d2e8fc8c to your computer and use it in GitHub Desktop.
Celebrity, and only the celebrity, meets these criteria:
- celebrity does not know anyone at the party
- everyone at the party knows the celebrity
knows(a, b) => true if a knows b
false if a does not know b
knows(a, b) does not imply knows(b, a)
@mattwhite
Copy link

def find_celebrity(people)
  people.find { |pc| (people - [pc]).none? { |p| knows(pc, p) } }
end

@mattwhite
Copy link

def find_celebrity(people)
  people.delete_at(knows(people[0], people[1]) ? 0 : 1) while people.length > 1
  people.first
end

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