CodeSolved

A comprehensive source of programming questions and exercises

Voting Counting System

Practice Easy 1622/ Download 679 Views

Write a program that receives a file containing different names and prints the number of repetition of each name in the output

Example: If the name file is as follows:

Tip »The first line is the number of names available

5
sara
Hamid
Ali
sara
sara

The output should be as follows:

ali 1
Hamid 1
sara 3

7 Answers

a=["sara","hamid","ali","sara","sara"]  #<<===فایل اسم های ورودی 
for b in set(a):   
 print(b,a.count(b))
Ramsin Download Python
file_name = input("Enter the file name: ")

with open(file_name, "r", encoding="utf-8") as file:
    lines = file.readlines()

n = int(lines[0].strip())
names = [line.strip() for line in lines[1:] if line.strip()]

name_count = {}
for name in names:
    name_count[name] = name_count.get(name, 0) + 1

for name, count in sorted(name_count.items()):
    print(name, count)
Reznum Download Python

Your answer was very complicated. You can get out of two lines Ramsin


list_asame = []
moshabeh = set()
count = int(input('vared kon esm ro : '))
counts = 0
for i in range(count):
    x = input('vared kon esm ro : ')
    if x in moshabeh:
        counts += 1
    else:
        moshabeh.add(x)
    list_asame.append(x)
print(list_asame)
print(count)
print(moshabeh)
Reza.avaze Download Python
# Voting program
a=[]
d=["ramsin","ramtin","nilan","hasti"]        # Names of candidates
while True:            
    b=input("name:")     # So that you can enter the name of the candidates
    a.append(b)
    
    if b=="finish":      # Type in the number of each name "Finish" after voting
        for i in range(0,len(d)):
         print(d[i],a.count(d[i]))
Ramsin Download Python
string = input("Enter a your list: ")
ray_dict = {}

strings = string.split()
for i in strings:
    if (ray_dict.keys().__contains__(i)):
        ray_dict.update(
            {
                i:ray_dict[i]+1
            }
            )
    else:
        ray_dict.update(
            {
                i:1
            }
            )

print(ray_dict)
User 3116 Download Python
from collections import Counter
with open(r'C:\Users\Amir hossein\Desktop\matn.txt','r',encoding = 'utf-8') as file:
    txt = file.read()
sp = txt.split()
print(f'all count char: {len(sp)}')
cont = Counter(sp)
for x , y in cont.items():
    print(f'{x} → {y}')

Emrimo Download Python
names = []
def input_names(a_number_of_names):
  for _ in range(a_number_of_names):
    name = input('Please enter your name :')
    names.append(name)

def number_of_names():
  number = int(input('Enter your desired name :'))
  input_names(number)

number_of_names()

number_of_repetitions_of_names = {}

def Repetition_of_names():
  for n in names:
    repetition = names.count(n)
    number_of_repetitions_of_names[n] = repetition

Repetition_of_names()

from pandas import Series
DataFrame_number_of_repetitions_of_names = Series(number_of_repetitions_of_names)

print(DataFrame_number_of_repetitions_of_names)

User 313 Download Python

Submit answer

Submitting answers is currently unavailable.

×
×
Close