Advertisement
-_Neo_-

python > Vetores

Mar 20th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Mar 12 01:18:50 2019
  4.  
  5. testes com listas.
  6. """
  7. from random import *
  8. from math import *
  9. def gera_vetor(x): # "x" é a dimensão do vetor.
  10.     x = int(x)
  11.     i = 0
  12.     vetor=[0]*x
  13.     inter = randint(1,(10*x))
  14.     while i < len(vetor):
  15.         vetor[i] = randint(-inter,inter)
  16.         i = i + 1
  17.     return vetor
  18.  
  19. def calcula_modulo(y):
  20.     y = list(y)
  21.     mod = 0
  22.     pos = 0
  23.     while pos < len(y):
  24.         mod = mod + ((y[pos])**2)
  25.         pos += 1
  26.     return 'O módulo do vetor deve ser: {0:.3f}'.format(sqrt(mod))
  27.  
  28. def inverte_lista(x):
  29.     y = gera_vetor(x)
  30.     print(y)
  31.     cont = 0
  32.     len(y)
  33.     invert = []
  34.     while cont < len(y):
  35.         invert.append(y[len(y) - 1 - cont])
  36.         cont += 1
  37.     return invert
  38.  
  39. ask_user = input('Digite a dimensão máxima do vetor: ')
  40. a = gera_vetor(ask_user)
  41. print('O vetor de dimensão {0} gerado para o teste será:\n{1}'.format(ask_user,a))
  42.  
  43. contador = 0
  44. while contador < len(a):
  45.     print('Componente da posição {0}: {1}'.format(contador,a[contador]))
  46.     contador += 1
  47.  
  48. print(calcula_modulo(a))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement