Giter VIP home page Giter VIP logo

cakes-apple-problem's Introduction

Problem 3

  • Type: Closed problem

Scenario

Ainun have 20 cakes and 25 apples. She want to bundle that cakes and apples into boxes and give them to her friends. How many boxes that Ainun can make? And how many cakes and apples every box have?

Terms and conditions

  • Solve this problem using language you know and confident
  • Create function to count boxes Ainun can make
  • Create function to count how many cake and apple in a box
  • Create unit test for the function
  • Apples and cakes divided evenly every box
  • Don’t forget to make documentation

Bonus point

  • Implementation in Go.

Solution

  1. Find the greatest common factor (GCF) between cakes and apples to get the number of boxes
  2. Divide cakes and apples with the GCF value to get the number of cakes and apples each box

Example:

cakes = 20 apples = 25

the factors of each arguments are:

20 = 1, 2, 4, 5, 10, 20

25 = 1, 5, 25

To get the GCF, we can loop through reading the numbers from the smaller argument (in this case is 20) to 2. If the number is divisible by both arguments, then that number is the GCF. If until 2 there's no number found, it means the GCF is 1.

We find that 5 is the first number that is divisible by both arguments, so the GCF (total number of boxes) is 5.

Then, we can get the cakes and apple for each box:

cakes = 20 / 5 = 4

apples = 25 / 5 = 5

Test the code solution

Run all the unit test

go test ./... -v

Run example case

go run main.go

cakes-apple-problem's People

Contributors

guntoroyk 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.