first-fit, best-fit and worst-fit python

I have to make a software that implements the memory management algorithmsfirst-fit, best-fit and worst-fit, I know their concept, first-fit puts the die in the first space that fits, best-fit reads all and selects the smallest space that fits and 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 is 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 a 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 would be able to find the position in the vector that are empty, but as the size of the information is 2 (hypothetically):

How do I catch 2 positions at the same time to switch?

If someone can take this doubt for me I would be very grateful and sorry if this doubt is half noob for I am learning a short time programming.

Author: pss1suporte, 2017-12-04

1 answers

Expanding the idea of Leonardo:

Save the position of the free space you are considering. For example, your first free space is at Position 1. There you have two variables {say, posicaoEmConsideracao and qtdDeEspacosVazios. The first you will boot with this position - 1 (because it starts at 0). The second you boot as 1, because you only found an empty space so far.

If the next position is a space, Increment qtdDeEspacosVazios. If it is busy, invalidate posicaoEmConsideracao and start new.

When qtdDeEspacosVazios equals tamanho, Fill from posicaoEmConsideracao to posicaoEmConsideracao + tamanho with x. You can do this with a while, which you already know.

Of course in each case you have a different management to do. That's just pro first-fit. But it's just a matter of saving positions in variables, changing the strategy in each case.

Oh, and you could also be using a chained list to manage memory. It would greatly facilitate the search of the empty spaces.

 1
Author: Pablo Almeida, 2017-12-04 04:29:16