Creation of a random vector

Asked

Viewed 1,139 times

1

from random import randint # para gerar os nums aleatorios

def criaVetor(L, H, tam):
    vec = []
    for i in range(tam): # vamos fazer isto tam (N) vezes
        vec.append(randint(L, H)) # gerar numero aleatorio entre L e H, e colocar na nossa lista
    return vec


def pegaExtremosUnicos(vec):
    i = 1
    for i in range(len(vec)):
        atual = vec[i]
        j = i - 1;
        while (j >=0) and (atual < vec[j]):
            vec[j +1] = vec[j]
            j = j -1
        vec[j + 1] = atual
    i = 1
    while i < len(vec):
        if vec[i] == vec[i-1]:
            del vec[i]
            del vec[i-1]
        i = i +1
    d = [vec[0],vec[len(vec)-1]]
    return tuple(d)

vec = []
L = int(input('Informe o valor inteiro minimo da faixa:'))
H = int(input('Informe o valor inteiro maximo da faixa:'))
tam = int(input('Informe a quantidade de valores a serem sorteados:'))
print(criaVetor(L, H, tam))
print (pegaExtremosUnicos(criaVetor(L, H, tam)))    

What would be wrong? Where there are no single minimum or maximum values, a tuple of None's must be returned. For example:

  • [3, 5, 3, 10, 8] returns (5, 10) - return ok

  • [13] returns (13, 13) d = [vec[0],vec[Len(vec)-1]] Indexerror: list index out of range

  • [13, 13, 8, 8] returns (None, None) Enter the minimum integer value of the range:8 Enter the maximum integer value of the range:13 Enter the amount of values to be drawn:4 [10, 10, 12, 10] (9, 11)

Upon return from the subprogram, the main program must display the tuple produced.

  • Absoares, you must ask the question that you cannot answer. It is important for those who want to help you to know this. http://answall.com/posts/148249/edit. Welcome to stack overflow

  • A question: why does it have to be without using the Random function? Whatever way is done here is very extensive complexae, and nor does it approach the effectiveness of this function

  • @Miguel hum... ok! I’m a beginner ....... Continuing what you did , as would the item (c) .... I got to do something , but this giving error!

  • ?? I didn’t realize... I put my answer down

1 answer

1


From what I realized all inputs of the user should be passed parameters ("...receives as parameters the size of the vector and the range of values."):

from random import randint # para gerar os nums aleatorios

def criaVetor(L, H, tam):
    vec = []
    for i in range(tam): # vamos fazer isto tam (N) vezes
        vec.append(randint(L, H)) # gerar numero aleatorio entre L e H, e colocar na nossa lista
    return vec

L = int(input('Informe o valor inteiro minimo da faixa:'))
H = int(input('Informe o valor inteiro maximo da faixa:'))
tam = int(input('Informe a quantidade de valores a serem sorteados:'))
print(criaVetor(L, H, tam))
  • I need to keep doing what you did! c) Via subprogram, search for the smallest () and largest (that is, non-repeatable) values contained in the vector. The subprogram should receive as parameter only the vector and return such values in a tuple in format (, ). If there are no single minimum or maximum values, a tuple of None’s should be returned. For example: [3, 5, 3, 10, 8] returns (5, 10) [13] returns (13, 13) [13, 13, 8, 8] returns (None, None) Upon return from the subprogram, the main program must display the tuple produced.

  • @Absoares About getting the minor and the greater vector value, see this Miguel response: http://answall.com/a/145998/6454

  • @Miguel giving error when running the tests the function minmax def minmax(L, H): For example: [3, 5, 3, 10, 8] returns (5, 10) [13] returns (13, 13) [13, 13, 8, 8] returns (None, None)

  • That function is supposed to do what? @Absoares

  • def minmax(L, H): vec = [] i = 1 for i in range(Len(vec)): current = vec[i] j = i - 1 while (j >=0) and (current < vec[j]): vec[j +1] = vec[j] j = j -1 vec[j + 1] = current i = 1 while i < Len(vec): if vec[i] == vec[i-1]: del vec[i] del vec[i-1] i = i +1 d = [vec[0],vec[Len(vec)-1]] Return tuple(d) print (minmax(L, H))

  • So I don’t understand @Absoares, say what this function is supposed to do, I’ve seen that it gets both values (min, max) and then? What will you do with them

  • fetch the smallest () and largest () unique values (i.e., not repeated) contained in the vector. You should take as parameter only the vector and return such values in a tuple in the format (, ). If there are no unique minimum or maximum values, a tuple of None’s should be returned. For example: [3, 5, 3, 10, 8] returns (5, 10) [13] returns (13, 13) [13, 13, 8, 8] returns (None, None) .

Show 2 more comments

Browser other questions tagged

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