Python, matrices/vectors and dictionaries

Asked

Viewed 389 times

3

Hello, I’m new to python and I don’t know how it should be done:

1) I have a 2x6 matrix generated by random numbers, for example:

matriz = [11, 4,  50, 8,  9,  78]
         [10, 33, 44, 57, 80, 90]   

What I need to do is this: by drawing a value in the range of line 2 [10, 33, 44 ...] I must return its respective value of line 1, for example if the drawn number for 85, I must return the value 9 of line 1. In case the value 38 came out in the draw, I must return the value 4 of line 1.

value from 10 to 32 of the second row corresponds to 11 of row 1 value from 33 to 43 of the second row corresponds to the value 4 of line 1 and so on, but the matrix is generated can be 2x10, 2x30 and so on

Code:

import random

Pessoa = random.sample(range(1,25),10)
Nota   = random.sample(range(1,25),10)

Pessoa.sort()
Nota.sort()
tmp = sum(Nota)

# sorteando um valor
n_escolhido=random.randrange(1,tmp)

print('Numero sorteado: ', n_escolhido)

I did using list instead of matrix but I stopped at this point because I can not make equivalence of the person list with the ranges of notes.

  • Is the first intention code really? It’s a syntax error. And is there a problem you’re having in your code? where is it?

  • Put the code you are trying to execute, the other members will help you solve and not solve for you.

  • Got the exercise, where are you crashing? Which part are you crashing? Add the code you tried to ask in the question to help you

  • I put what I’ve done so far, I still can’t figure out a way to check the intervals, I’m taking a look at dictionaries...

2 answers

1

Using the data of your is example can be simple, remembering that this code will only work if the values of the second row of the matrix are in ascending order.

I used Numpy to create an array 2x6 ( the same array as your example), the algorithm logic is simple, create a loop and compare the column position atual of the line 2 matrix and column position atual+1, if your number is between the two values print the value of the corresponding column of the row 1

the Code

import numpy as np

#criando array do exemplo
matriz = np.array([[11, 4,  50, 8,  9,  78],[10, 33, 44, 57, 80, 90]])

#entre 10 e 32 = 11
#entre 33 e 43 = 4
#entre 44 e 56 = 50
#entre 57 e 79 = 8
#entre 80 e 89 = 9
#entre 90 e inf = 78

#numero para testar o algoritmo ou pegar número randômico entre 10 e 90
numero=68
numero=np.random.randint(10,90)

#imprime qual número estamos usando
print(numero)

#anda na segunda linha da matriz comparando a posição atual com a próxima posição
for i in range(0, matriz[1:,].shape[1]-1):
    if (numero >= matriz[1,i]) and (numero < matriz[1,i+1]):
        print(matriz[0,i])

#o ultimo índice foi deixado de fora no loop, comparar agora        
if numero >= matriz[1,i+1]:
    print(matriz[0,i+1])

0

With the number drawn, present in the second row of the matrix

numero_sorteado = 90 # Exemplo

You will find the index of that number in the line in which it is present

index_numero_sorteado = matriz[1].index(numero_sorteado)

With this number just access the first row of the matrix and return the corresponding value

numero_correspondente = matriz[0][index_numero_sorteado]
  • But if the drawn value is 68, which input it will return?

  • In this case 68 is not in the second row of the matrix, so the example I gave will give error, then in this case it is necessary to check if the drawn number is really in the second row.

Browser other questions tagged

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