I have to do a software that implements the memory management algorithms first-fit
, best-fit
and worst-fit
, I know their concept, first-fit
places the data in first space that fits, best-fit
reads all and selects the smallest space that fits and worst-fit
le all and select the largest space that fits, here is my code:
import random
memoria = [' '] * 100
opcao = 0
tamanho = 0
letra = ''
for i in range(100):
if(random.randint(0,11) >= 5):
memoria[i] = 'x'
else:
memoria[i] = ' '
while(opcao != 4):
#Menu do programa
print("1 - Primeira Escolha")
print("2 - Melhor Escolha")
print("3 - Pior Escolha")
print("4 - Sair")
print("Escolha o algoritmo pelo numero")
opcao = int(input())
print("Digite o tamanho da informacao")
tamanho = int(input())
print("Digite a letra a ser utiliada")
letra = input()
if(opcao == 1):
#Implemente aqui a lógica da primeira escolha
pass
else:
if (opcao == 2):
#Implemente aqui a lógica da melhor escolha
pass
else:
if(opcao == 3):
#Implemente aqui a lógica da pior escolha
pass
# Aqui você deve imprimir todo o conteúdo da variável memória
My doubt is this: let's say the memory is this:
x| |x| |x|x|x|x| | |x| |x|x| | | |x| | |
x|x|x|x| |x| |x| | |x|x| |x|x| |x|x| |x|
|x|x|x| |x| | | |x|x| |x|x| | |x|x| |x|
x|x|x|x|x| |x|x|x|x| | | |x| |x|x|x| |x|
| | |x| | |x|x|x| | |x|x| |x|x|x| | | |
The size of the information is 2 and the letter to be used is O, let's say option 1 is used, the memory should look like this:
x| |x| |x|x|x|x|O|O|x| |x|x| | | |x| | |
x|x|x|x| |x| |x| | |x|x| |x|x| |x|x| |x|
|x|x|x| |x| | | |x|x| |x|x| | |x|x| |x|
x|x|x|x|x| |x|x|x|x| | | |x| |x|x|x| |x|
| | |x| | |x|x|x| | |x|x| |x|x|x| | | |
To find a space that has nothing I could use
if memoria[i] == ' ':
This way I could find the position in the vector that is empty, but as the size of the information is 2 (hypothetically):
How do I get 2 positions at the same time to change?
If anyone can ask this question to me I would be very grateful and sorry if this doubt is half noob because I am learning the programming time soon.