Created
September 24, 2019 11:34
-
-
Save GeorgeK-zn/023097b811271120eefa52e9eab9323a to your computer and use it in GitHub Desktop.
coding question solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# in_string = 'ABC' / 'AAA' / 'ABAF' | |
# delay = 5 | |
# res 3, 13, | |
from collections import deque | |
def calc_processing_time(in_str: str, delay: int) -> int: | |
""" | |
calculating running time for jobs in in_str with delay between consequent jobs | |
:param in_str: string of input jobs | |
:param delay: number of cycles to wait between 2 jobs of same type | |
:return: number of cycles it will take to work through the input sequence | |
""" | |
if len(in_str) <= 1: | |
return len(in_str) | |
pipe_of_jobs = deque(["" for _ in range(delay + 1)]) | |
num_operations, job_idx = 0, 0 | |
# going through all jobs once, worst case all jobs are the same O(delay * len(in_str)) = O(n) | |
while job_idx < len(in_str): | |
try: | |
if pipe_of_jobs.popleft() != '': | |
num_operations += 1 | |
except IndexError: | |
pass | |
# assuming delay is a const, running time of this check is O(1) | |
if in_str[job_idx] in pipe_of_jobs: | |
pipe_of_jobs.append('wait') | |
else: | |
pipe_of_jobs.append(in_str[job_idx]) | |
job_idx += 1 | |
# emptying pipe of jobs O(1), delay is const | |
while len(pipe_of_jobs) > 0: | |
if pipe_of_jobs.popleft() != '': | |
num_operations += 1 | |
return num_operations | |
if __name__ == '__main__': | |
assert calc_processing_time('ABC', 5) == 3 | |
assert calc_processing_time('ABCA', 5) == 7 | |
assert calc_processing_time('AAA', 5) == 13 | |
assert calc_processing_time('AAA', 13) == 29 | |
assert calc_processing_time('AAA', 2) == 7 | |
assert calc_processing_time('AAA', 0) == 3 | |
assert calc_processing_time('AAA', 1) == 5 | |
assert calc_processing_time('A', 110) == 1 | |
assert calc_processing_time('AA', 110) == 112 | |
assert calc_processing_time('AABBCC', 1) == 9 | |
assert calc_processing_time('AABBCC', 0) == 6 | |
assert calc_processing_time('A1234567890A', 20) == 22 | |
assert calc_processing_time('', 2) == 0 | |
assert calc_processing_time('A', 2) == 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment