first-fit, best-fit and Worst-fit python

Asked

Viewed 1,100 times

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.

1 answer

1

Expanding on Leonardo’s idea:

Save the position of free space you are considering. For example, your first free space is at position 1. There you have two variables-let’s say, posicaoEmConsideracao and qtdDeEspacosVazios. The first you will initialize with that position - 1 (because it starts at 0). The second you initialize as 1, because you just found an empty space so far.

If the next position is a space, increment qtdDeEspacosVazios. If you’re busy, invalidate posicaoEmConsideracao and start again.

At the moment qtdDeEspacosVazios is equal to tamanho, filled with posicaoEmConsideracao until posicaoEmConsideracao + tamanho with x. You can do it with a while, you already know.

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

Ah, and you might as well be wearing one chain list to manage memory. It would greatly facilitate the search of empty spaces.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.