Python - Detectives and Assassins

Asked

Viewed 333 times

0

I’m having difficulty understanding what this exercise in the link below is requesting:

http://www.ic.unicamp.br/~mc102/Labs/roteiro-lab06.html

What I’ve understood so far is that I should build an algorithm so that when the data was provided, that algorithm would find out who the killer is and who the detective is. Is that really what the exercise is proposing? If so, is it very sophisticated? I have basic knowledge in Python, but I can handle dictionaries and IF instructions well.

If anyone can give a north just how to start it will help me a lot.

Thank you very much from now on.

2 answers

2


You must discover killers, victim and detectives. The program is relatively simple, you only need to heed the printing rules.

A basis:

• Receber a quantidade de casos. Exemplo: 1,2,3...100
• Verificar se a quantidade é >=1 e <=100
• Guardar os dados digitados em um dicionario
    -> <assassino> <vítima> <detetive>, com essa informação você pode criar um dicionário assim:
    relat = {'assassino':[], 
             'vitima':[],
             'detetive': []}
    nomes = set() # O meu conjunto vai guardar todos os nomes digitados ordenados e sem repetição, isso vai ajudar na impressão em ordem alfabética

    relat['assassino'].append('Ze')
    relat['vitima'].append('Xico')
    relat['detetive'].append({'Rafael':1}) # coloquei como dicionário para saber quantos casos cada detetive resolveu.
• Depois de criado seu dicionário com as entradas, só passar um for em nomes e recuperar a informação no dicionário, não esqueça de atender as regras.

Search on the . split(), it will help you with the entries(it’s in the task tips).

I hope I’ve helped, any doubt just comment there.

  • Thank you very much Rafael. It helped a lot, I will start writing the code.

1

# coletando dados
N = input("Digite o numero de casos: ")
casos = []
if N < 1 or N > 100:
    print("Valor inválido na entrada.")
else:
    for i in range(N):
        caso = input("Digite assassino, vitima e detetive: ")
        caso = caso.split(' ')
        assassino, vitima, detetive = caso
        # caso = {"assassino":assassino,"vitima":vitima,"detetive":detetive} # dica do professor
        casos.append(caso)

To find out if the person is alive, there are two conditions: not to have been a victim and to be cited either as a murderer or as a detective. The list 'cases' will have 'case' lists as elements, whose index elements 0, 1 and 2 will be, respectively, names of the killer, the victim and the detective.

  • Thank you also Hog Dog, also helped me.

Browser other questions tagged

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