Skip to content

Instantly share code, notes, and snippets.

@sXakil
Created May 16, 2020 19:39
Show Gist options
  • Select an option

  • Save sXakil/3933fed2e018aa4f2b5c6febdfaa26e9 to your computer and use it in GitHub Desktop.

Select an option

Save sXakil/3933fed2e018aa4f2b5c6febdfaa26e9 to your computer and use it in GitHub Desktop.
from queue import Queue
# capacity = 3
# pages = [1, 2, 3, 2, 1, 5, 2, 1, 6, 2, 5, 6, 3, 1, 3, 6, 1, 2, 4, 3]
capacity = int(input("Capacity: "))
pages = [int(x) for x in input('Page References (separated by space): ').split(' ')]
current_pages = set()
page_indices = Queue()
page_faults = 0
page_hits = 0
for page in pages:
if (page not in current_pages):
if (len(current_pages) < capacity):
current_pages.add(page)
page_indices.put(page)
page_faults += 1
else:
current_pages.remove(page_indices.get())
current_pages.add(page)
page_indices.put(page)
page_faults += 1
else:
page_hits += 1
print(f"Total {page_faults} page faults and {page_hits} page hits")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment