A simple solution for water overflow problem in Python

Related tags

Miscellaneous python
Overview

Water Overflow problem

There is a stack of water glasses in a form of triangle as illustrated. Each glass has a 250ml capacity.

When a liquid is poured into the top most glass any overflow is evenly distributed between the glasses in the next row. That is, half of the overflow pours into the left glass while the remainder of the overflow pours into the right glass

Write a program that is able to calculate and illustrate how much liquid is in the j’th glass of the i’th row when K litres are poured into the top most glass

        |_|         i = 0
       |_||_|       i = 1
     |_||_||_|      i = 2
    |_||_||_||_|    i = 3
    ...

Test first

This problem is related to Pascal's triangle that could be drafted like below:

        1
       1 1
      1 2 1
     1 3 3 1
    1 4 6 4 1
    ...

It would be better to write some test cases:

  • Pour 7 unit of water, glass at position 0 in row 1 must be 1

  • Pour 15 unit of water, glass at position 2 in row 3 must be 3 1 (overflow)

Problem solving

To solve this problem in machine code, we could translate the total rows and total glasses in row to a 2D arrays:

|_|             row = 0, glasses = [[0][0]]
|_||_|          row = 1, glasses = [[1,0], [1,1]]
|_||_||_|       row = 2, glasses = [[2,0], [2,1], [2,2]]
|_||_||_||_|    row = 3, glasses = [[3,0], [3,1], [3,2], [3,3]]
...
  • Pour the water in row(0).

  • If glass[0][0] is full and the water still remain, the remain water will be flowed to row(1).

  • Glass[1,0] will receive half of remain water, glass [1,1] also receive half of remain water.

  • If any of glass in row(1) is full and water still remain, repeat above process until no water remain.

Testing


python -m unittest

Running

python water_overflow/main.py {1} {2} {3} {4}

with {1} is total water in Litres, {2}, {3} is the row & position of glass to find. To see simple illustrate of the glasses triangle, put the {4} to true

Example

python water_overflow/main.py 5.2 6 2 true
               \▇/
             \▇/ \▇/
           \▇/ \▇/ \▇/
         \▇/ \▇/ \▇/ \▇/
       \▂/ \▇/ \▇/ \▇/ \▂/
     \_/ \▅/ \▇/ \▇/ \▅/ \_/
   \_/ \_/ \▅/ \▇/ \▅/ \_/ \_/
 \_/ \_/ \_/ \▂/ \▂/ \_/ \_/ \_/
When pouring 5.2L of water, the level of glass in row 6 at pos 2 is 210.9375ml
You might also like...
Python Common things by Problem Fighter Library, (Exception, Debug Log, etc.)

In the name of God, the Most Gracious, the Most Merciful. PF-PY-Common Documentation Install and update using pip: pip install -U xxxx Please find the

Euler 021 Py - Euler Problem 021 solved in Python

Euler_021_Py Euler Problem 021 solved in Python Let d(n) be defined as the sum o

Fix Eitaa Messenger's Font Problem on Linux

Fix Eitaa Messenger's Font Problem on Linux

nbsafety adds a layer of protection to computational notebooks by solving the stale dependency problem when executing cells out-of-order
nbsafety adds a layer of protection to computational notebooks by solving the stale dependency problem when executing cells out-of-order

nbsafety adds a layer of protection to computational notebooks by solving the stale dependency problem when executing cells out-of-order

Online HackerRank problem solving challenges

LinkedListHackerRank Online HackerRank problem solving challenges This challenge is part of a tutorial track by MyCodeSchool You are given the pointer

Ant Colony Optimization for Traveling Salesman Problem

tsp-aco Ant Colony Optimization for Traveling Salesman Problem Dependencies Python 3.8 tqdm numpy matplotlib To run the solver run main.py from the p

Verification of Monty Hall problem by experimental simulation.

Verification of Monty Hall problem by experimental simulation. |中文|English| In the process of learning causal inference, I learned about the Monty Hal

Nag0mi ctf problem 2021 writeup

Nag0mi ctf problem 2021 writeup

Problem 5: Fermat near-misses
Problem 5: Fermat near-misses

Problem 5: Fermat near-misses fermatnearmiss This is a script that computes fermat nearm misses when the -f option is set and requires users to input

Comments
  • Remove unused rows

    Remove unused rows

    It would be better if the program can itself calculate the max row based on total water and glass's capacity. Or remove unused row of glasses when no remain water

    opened by ltdangkhoa 0
Owner
Kris
Kris
30DaysOfCode-PhoenixClub - Solution of everyday coding problem given in 30DaysofCode contest held on Hackerrank

30DaysOfCode-PhoenixClub ??‍?? Every day problems solution given in 30DaysOfCode

Urveshkumar 8 Jan 30, 2022
Buffer overflow example for python

Buffer overflow example for python

Mehmet 1 Jan 4, 2022
Amitkumar Mishra 2 Jan 14, 2022
Suite of tools for retrieving USGS NWIS observations and evaluating National Water Model (NWM) data.

Documentation OWPHydroTools GitHub pages documentation Motivation We developed OWPHydroTools with data scientists in mind. We attempted to ensure the

null 36 Dec 11, 2022
A water drinking notification every hour to keep you healthy while coding :)

Water_Notification A water drinking notification every hour to keep you healthy while coding. ?? ?? Stay Hydrated Stay Healthy ?? ?? Authors @CrazyCat

Arghya Banerjee 1 Dec 22, 2021
A simple program to run through inputs for a 3n+1 problem

Author Tyler Windemuth Collatz_Conjecture A simple program to run through inputs for a 3n+1 problem Purpose: doesn't really have a purpose, did this t

null 0 Apr 22, 2022
This repository requires you to solve a problem by writing some basic python code.

Can You Solve a Problem? A beginner friendly repository that requires you to solve familiar problems with python. This could be as simple as implement

Precious Kolawole 11 Nov 30, 2022
"Cambio de monedas" Change-making problem with Python, dynamic programming best solutions,

Change-making-problem / Cambio de monedas Entendiendo el problema Dada una cantidad de dinero y una lista de denominaciones de monedas, encontrar el n

Juan Antonio Ayola Cortes 1 Dec 8, 2021
Manually Install Python 2.7 pip without any problem !

Python2.7_install_pip Manually Install Python 2.7 pip without any problem ! Download installPip.py to your system and Run the code using this Command

Ali Jafari 1 Dec 9, 2021