1
I have to make one software implementing the memory management algorithms first-fit
, best-fit
and worst-fit
, I know their concept, the first-fit
puts the dice in the first space that fits, best-fit
reads all and selects the smallest space that fits and the worst-fit
le all and selects 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 goes like 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 we use option 1, 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 are empty, but as the size of the information is 2 (hypothetically):
How I take 2 positions at once to change?
If anyone can clear that doubt for me I would be very grateful and sorry if that doubt is half Noob because I am learning the programming little time.
You can store the positions you found free in a temporary variable. Then go back to them and make the memory allocation.
– Leonardo Pessoa