Count number of occurrences of a number

Asked

Viewed 513 times

1

Program that has only one function, besides the main program, that will receive as parameters an integer number x, a sequence of integers L and a natural n, representing an item x that must have quantified its occurrences in the sequence L of size n.

The function should return a natural value indicating the amount of occurrences of x in L.

Entree:

(a) the first line of the entry contains the searched item x;

(b) the second line of the entry contains the sequence size n;

(c) the next n lines represent the items that make up the sequence.

7
6
70
71
72
73
7
54

Exit:

(a) the main program shall print a natural value indicating the amount of occurrences of x in L according to the value returned by the function.

1

Code created until then:

X = input()
N = int(input())
L = [0]
Quantidade = 0

for i in range(len(L),N):
    if X [i]==X:
        Quantidade += 1
    Novo = input()
    L.insert(0,Novo)
print(Quantidade)

I’m not quite understand the implementation of this program in list, the above code is not working.

1 answer

1


You don’t need to create this list L, just read the numbers and see if they match the first number read:

x = int(input())
n = int(input())
qtd = 0
for _ in range(n):
    if int(input()) == x:
        qtd += 1
print(qtd)

Use range(n) causes the eternal loop n times, and in the variable of for used _, which is a Python convention to indicate that I will not use this variable in the loop (because I just want to iterate n times).

The list L was unnecessary and was only confusing the algorithm.

So much so that the statement does not clearly say that it is to store the values in a list, but in the comments it was said it is, so in that case it would be:

valores = []
for _ in range(n): # guarda os valores lidos na lista
    valores.append(int(input()))

qtd = 0
for num in valores: # percorre a lista e verifica quais são iguais a x
    if num == x:
        qtd += 1

Note that the list starts empty (you initialized it with an element - zero - which makes no sense, it should only save what is read).


I understand that because it’s an exercise, probably want you to manually count in a loop. But it can also be done directly, because lists have the method count, who already does that:

qtd = valores.count(x)

The method count serves to find the amount of occurrences of a single element. But if you want to count all the elements (how often each one appears), it is not good, because for each element it goes through the whole list again. If you want everyone’s count, you better use one Counter:

from collections import Counter

c = Counter(valores)
print(c[x]) # quantidade de vezes que "x" ocorre em "valores"

The Counter is a dictionary that will contain, for each element of the list, the amount that each occurs. To get the amount of a particular element, just use the element as a key, as an example above.

  • It is that in fact I would like to do as follows, the numbers that are typed by the user within this for loop, were stored in a list, after they are in the list, for example: [70,71,72,73,7,54], find how many times the number "7" occurs within that list.

  • Summarizing, after the user insert the values "X and N" all the values typed inside the for loop, by the user, would be inserted in a list, after insertion, there would be the location of how many times the number "X" of the beginning of the program occurs in the created list.

  • @I updated the answer

  • Thank you very much! Your explanations have helped me a lot to better understand the functioning of the list.

Browser other questions tagged

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