How to verify and print repeating values in an algorithm vector

Asked

Viewed 152 times

-4

Create an algorithm that given a sequence of n real numbers, determine the numbers that make up the sequence and the number of times each of them occurs in it.

Example: n = 8

Sequel:

-1.7,  
3.0,  
0.0,  
1.5,  
0.0,  
-1.7,  
2.3,  
-1,7  

Exit:

-1.7 ocorre 3 vezes

 3.0 ocorre 1 vez

 0.0 ocorre 2 vezes

 1.5 ocorre 1 vez

 2.3 ocorre 1 vez

I did it in the Visualg Code:

ALGORITMO "QUESTAO 9"
VAR
C,X,SEQ,I: INTEIRO
VSR: VETOR[1..100] DE REAL
SEQUENCIA: VETOR[1..100] de REAL

INICIO
ESCREVA("QUANTOS NÚMEROS REAIS COMPÕEM A SEQUÊNCIA? ")
LEIA(SEQ)
ESCREVAL("INFORME A SEQUÊNCIA: ")
PARA I DE 1 ATE SEQ FACA
LEIA(SEQUENCIA[I])
SE (SEQUENCIA[I])<>(VSR[I]) então
VSR[I]<-SEQUENCIA[I]
fimse
fimpara
para X de 1 ate SEQ faca
C <- 0
para I de 1 ate SEQ faca
se (VSR[X] = SEQUENCIA[I]) então
C <- C + 1
fimse
fimpara
escreval(VSR[X], " OCORRE ", C, " VEZES")
fimpara
escreval(" ")
escreval(VSR[X], " OCORRE ",SEQUENCIA[SEQ], " VEZES")


fimalgoritmo
  • Welcome to Stack Overflow. It seems your question contains some problems. We won’t do your college work or homework, but if you’re having difficulty somewhere specific, share your code, tell us what you’ve tried and what the difficulty is, so you increase your chances of getting a good answer. Be sure to read the Manual on how NOT to ask questions to have a better experience here.

  • 1
  • Unless a typo has occurred the value -1.7 occurs twice, not three, in your list of 9 elements.

  • The method Counter of lib collections fix it. See here

  • @Overpeas, it’s hard to see the code on comments. Update your post, please.

1 answer

0


I think that’s what you seek.

Update

N = int(input("Quantos números? "))

lista = []
for i in range(1, N+1):
    lista.append(float(input(f"{i} - Entre com o número: ")))

NOTE: Typing errors are not being handled

from collections import Counter

c = Counter(lista)

Example of Results

print(f"Lista de numeros: {list(c.keys())}")

Lista de numeros: [-1.7, 3.0, 0.0, 1.5, 2.3]
for numero, repeticoes in c.items():
    print(f" O numero {numero} repete {repeticoes} vezes")

 O numero -1.7 repete 3 vezes
 O numero 3.0 repete 1 vezes
 O numero 0.0 repete 2 vezes
 O numero 1.5 repete 1 vezes
 O numero 2.3 repete 1 vezes

I hope it helps

  • That’s right @Paulo Marques, thank you very much.

Browser other questions tagged

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