-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatm.py
More file actions
88 lines (65 loc) · 2.3 KB
/
atm.py
File metadata and controls
88 lines (65 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import pickle
NOTES_PATH = '_notes'
class PersistentDict(dict):
def __init__(self, path):
self._path = path
if os.path.exists(self._path):
with open(self._path, 'r') as f:
self.update(pickle.load(f))
def sync(self):
with open(self._path, 'w') as f:
pickle.dump(dict(self), f)
class NotesStore(object):
def __init__(self):
self._notes = PersistentDict(NOTES_PATH)
self.denomitations = self._mk_denomitations()
def _mk_denomitations(self):
return sorted(self._notes.keys(), reverse=True)
def init(self, notes):
"""
Initialize store with notes
* notes - dict with note counts, indexed by note denomination
"""
self._notes.clear()
self._notes.update(notes)
self._notes.sync()
self.denomitations = self._mk_denomitations()
def get_count(self, note):
"""Get count for provided note"""
return self._notes[note]
def inc_count(self, note, inc=1):
"""Increase note count"""
self._notes[note] += inc
def dec_count(self, note, dec=1):
"""Decrease note count"""
self._notes[note] -= dec
def get_notes(self):
"""Return dict with note counts"""
return dict(self._notes)
def sync(self):
"""Sync counts to file"""
self._notes.sync()
class Atm(object):
def __init__(self):
self.notes = NotesStore()
def _dispense(self, remaining, result, pos=0):
if remaining == 0 and result:
return True
elif remaining > 0:
curr_note = self.notes.denomitations[pos]
if remaining >= curr_note and self.notes.get_count(curr_note) > 0:
result.append(curr_note)
self.notes.dec_count(curr_note)
if self._dispense(remaining - curr_note, result, pos):
return result
result.pop()
self.notes.inc_count(curr_note)
if pos + 1 < len(self.notes.denomitations):
if self._dispense(remaining, result, pos + 1):
return result
def dispense(self, amount):
"""Dispense money from ATM"""
result = self._dispense(amount, [])
self.notes.sync()
return result