Skip to content

Instantly share code, notes, and snippets.

@sXakil
Last active May 16, 2020 19:40
Show Gist options
  • Save sXakil/47ecd71a57db343d9f1c38d4ed522ec9 to your computer and use it in GitHub Desktop.
Save sXakil/47ecd71a57db343d9f1c38d4ed522ec9 to your computer and use it in GitHub Desktop.
# 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 = []
page_indices = []
page_faults = 0
page_hits = 0
for page in pages:
if (page not in current_pages):
if (len(current_pages) < capacity):
current_pages.append(page)
page_indices.append(len(current_pages) - 1)
else:
idx = page_indices.pop(0)
current_pages[idx] = page;
page_indices.append(idx);
page_faults += 1
else:
page_indices.append(page_indices.pop(page_indices.index(current_pages.index(page))))
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