How does the code work in Triage? (Python)

I was able to figure out this level easily but I am wondering how the rest of the code works.

What are [0] doing in the first 3 lines of the code?
doctor = hero.findByType("paladin")[0]
are they to just set 0s to the doctor array? why doesn’t the soldier array have [0]?

what are the last 3 lines doing?

mage["patients"] = magePatients
doctor["patients"] = doctorPatients
helper["patients"] = helperPatients

it’s assigning the array of different patients into?

The method hero.findByType() returns an array, but you can switch it to return an object by adding [0] at the end and it will assign the first object in that array to the variable.

Code Visualisation

input = [ 0, 1, 2, 3, 4, 5]
def findOdds(arrInput):
    arr = [];
    for i in arrInput:
        if i%2:
    return arr;
output =  findOdds(input)

for i in range(len(output)):
    print(i, output[i])
magePatients = ['mPatient1', 'mPatient2']
doctorPatients = ['dPatient1', 'dPatient2','dPatient3', 'dPatient4']
helperPatients = ['hPatient1', 'hPatient2', 'hPatient3']

# <This is wrong 
mage = {}
doctor = {}
helper = {}
# This is wrong />

mage["patients"] = magePatients
doctor["patients"] = doctorPatients
helper["patients"] = helperPatients

The function findOdds() is similar to findByType() and you are getting the first element of its output as brooksy has said. See the visualization for all the code from the link at the top.

Edit: NO, this is not the right explanation in the second part! I didn’t consult the original code, so it’s my fault.
mage, doctor, helper are not empty objects , they are more like

mage = {'id': "Name", 'pos' : {'x' : 5, 'y' : 15 }}#and all other keys and values

Sounds interesting, but why would i want to return an object for mage, doctor and helper but not soldiers?

soldier is what it iterates through later so it needs to be in a list, but what does

mage["patients"] = magePatients
doctor["patients"] = doctorPatients
helper["patients"] = helperPatients

this do?

I’ m a python noob and started learning it with CodeCombat and then quit and started javascript I gave a little bit different explanation at What is "mage["patients"] = magePatients" in triage level? more than a year ago and I suppose both can be wrong. @brooksy125 - please help :slight_smile:

Since you only have one of the mage, doctor and helper; you will only need to refer to the specific one through this code so we make it an object variable. You want an array or list of the soldiers so you can use a loop to go through all of them and assign them to the different arrays (magePatients, doctorPatients, helperPatients).

The last section isn’t really covered, but I believe you are assigning the lists you created with the soldiers to the different people, using a keyword reference “patients” and making it a dictionary, I think and adding it to the current values of that character.

like @xython stated: mage, doctor, helper are not empty objects , they are more like

mage = {'id': "Name", 'pos' : {'x' : 5, 'y' : 15 }}#and all other keys and values

this last part adds {‘patients’ : [Bond, Louis, Ann-Maria, Mary, Andrea]} to mage

Like when you check for you get the name, you can check mage.patient and it gives you the list you now added. I traded out mage["patients"] for mage.patients and it works.

mage.patients = magePatients
doctor["patients"] = doctorPatients
1 Like

I’m not sure where to post this but it’s a continuation of what I’ve been asking:
I am on another level “power points” and there’s a line

wizard = hero.findNearest(hero.findFriends())
powerMap = wizard.powerMap

how come It doesn’t need [0] behind findNearest? I thought to access the wizard object i need to use [0]? but the code runs fine. Both findNearest and findByType say it returns an array.

1 Like

Hi hyperian.
hero.findNearest(array) doesn’t find an array, it finds an object from an array.
e.g. hero.findNearest(hero.findFriends())
the array is hero.findFriends() and the hero.findNearest() find the one nearest one.
I hope this solves your problem, and it’s fine to post something about arrays/something similar, on a topic about an array level.
:lion: :lion: :lion:


Ah, that make sense now. I find the methods description confusing sometimes.