master
bel 2022-04-10 16:42:10 -06:00
parent ef3815273e
commit 696b23d18c
1 changed files with 23 additions and 6 deletions

View File

@ -2,15 +2,32 @@ from sys import stdin, stdout
import time
class Bucket:
def __init__(self, N, R):
def __init__(self, N, R, T, CB):
self.q = 0.0
self.N = N
self.R = R
self.__last = 0
self.T = T
self.CB = CB
self.__last_pop = 0
self.__last_state = False
def push(self):
return self.__push_c(1)
def pop(self):
return self.__pop()
def __mod(self, foo):
result = foo()
self.__cb()
def __cb(self):
new_state = self.q > self.T
if new_state == self.__last_state:
return
self.__last_state = new_state
self.CB(new_state)
def __push_c(self, c):
self.__pop()
if self.q+c > self.N:
@ -20,21 +37,21 @@ class Bucket:
def __pop(self):
now = self.__now()
remove_up_to = (now - self.__last) / self.R
remove_up_to = (now - self.__last_pop) / self.R
if remove_up_to > self.q:
remove_up_to = self.q
self.q -= remove_up_to
self.__last = now
self.__last_pop = now
def __now(self):
return time.time()
def main(stream):
bucket = Bucket(3, 1)
bucket = Bucket(3, 1, 2, lambda state: print("state is now", state))
while readline():
print(bucket.push(), file=stdout)
print(f'q={bucket.q}, N={bucket.N}, R={bucket.R}', file=stdout)
#print(f'q={bucket.q}, N={bucket.N}, R={bucket.R}', file=stdout)
stdout.flush()
def readline():