Skip to content

Commit 45a8627

Browse files
committed
feat(day 19): implement candle recycling calculator for maximum burn count
1 parent cf6385b commit 45a8627

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
===============================================> Candlelight <======================================================
3+
4+
Given an integer representing the number of candles you start with, and an integer representing how many burned candles it takes to create a new one, return the number of candles you will have used after creating and burning as many as you can.
5+
6+
For example, if given 7 candles and it takes 2 burned candles to make a new one:
7+
8+
Burn 7 candles to get 7 leftovers,
9+
Recycle 6 leftovers into 3 new candles (1 leftover remains),
10+
Burn 3 candles to get 3 more leftovers (4 total),
11+
Recycle 4 leftovers into 2 new candles,
12+
Burn 2 candles to get 2 leftovers,
13+
Recycle 2 leftovers into 1 new candle,
14+
Burn 1 candle.
15+
You will have burned 13 total candles in the example.
16+
17+
===========================================================================
18+
O/P :
19+
1. burn_candles(7, 2) should return 13
20+
2. burn_candles(10, 5) should return 12
21+
3. burn_candles(20, 3) should return 29
22+
23+
"""
24+
25+
def burn_candles(candles, make_new):
26+
27+
total_burned = candles
28+
leftovers = candles
29+
30+
while leftovers >= make_new:
31+
new_candles = leftovers // make_new
32+
total_burned += new_candles
33+
leftovers = leftovers % make_new + new_candles
34+
35+
return total_burned
36+
37+
38+
39+
if __name__ == "__main__":
40+
41+
print(burn_candles(7,2))
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
from candleLight import burn_candles
3+
4+
5+
class candleLightTest(unittest.TestCase):
6+
7+
def test1(self):
8+
self.assertEqual(burn_candles(7,2),13)
9+
10+
def test2(self):
11+
self.assertEqual(burn_candles(10,5),12)
12+
13+
def test3(self):
14+
self.assertEqual(burn_candles(20,3),29)
15+
16+
def test4(self):
17+
self.assertEqual(burn_candles(17,4),22)
18+
19+
def test5(self):
20+
self.assertEqual(burn_candles(2345,3),3517)
21+
22+
23+
24+
if __name__ == "__main__":
25+
unittest.main()
26+
27+

0 commit comments

Comments
 (0)