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

0

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.

    
asked by anonymous 04.12.2017 / 03:59

1 answer

0

Expanding Leonardo's idea:

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

If the next position is a space, increase qtdDeEspacosVazios . If busy, invalidate posicaoEmConsideracao and start over.

When qtdDeEspacosVazios is equal to tamanho , fill from posicaoEmConsideracao to posicaoEmConsideracao + tamanho with x . You can do this with 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 List_list"> linked list to manage memory. It would make it much easier to search for empty spaces.

    
04.12.2017 / 05:29