Code Monkey home page Code Monkey logo

secret_santa_ruby's Introduction

This is a ruby implementation of the secret santa problem.

After researching I realized the problem has many variants and multiple approaches to solutions.

I have used a simple approach to solve the problem.

Approach:

  • Create 2 lists of participants, one recievers the other senders.
  • Corresponding indexes give each others give, so person at position i in senders will give gift to person at position i in recievers
  • Check to see if the current solution is valid
    • no one is giving themselves a gift
    • no one is giving gift to their partner
    • no one is giving gift to the same person they gave it to last time
  • if the solution is not valid, randomly shuffle the senders and try again

This approach does not cater for the case when their is no solution, it will just keep on trying forever. I have tried to focus on the design and clean code rather than trying to capture all boundry cases.

How to Run

The code lives in a two classes in a single file santa.rb

  • s = SecretSantaRunner.new('file_name_to_read_input_from')
  • s.assign_santas
  • s.print_solution

A sample runner is in runner.rb, so you can directly run ruby runner.rb

Tests

  • Rspec based tests are under /test
  • To run tests rspec ./

secret_santa_ruby's People

Contributors

mabid avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.