Python Forum
Sorting Santa's List of Children
Thread Rating:
  • 3 Vote(s) - 3.33 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting Santa's List of Children
#1
Hi guys,


I'm doing a Master in Comupter science in Brasil and the problem began to be more seriously and i'm a bit lost in this problem.

It seems to be not so difficult but i'm quite new in Python and sometimes i'm confusing what i have to do to resolve even simply problems.


The prblem is:

Santa Claus is in finishing to organize the delivery of gifts to all the children of the world because Christmas is coming again. Looking at his new lists of children who will win gifts this year he realized that the trainee elf (who had been responsable for making the lists) did not put the names in alphabetical order.
As Santa is a very organized man, he wants that every list of children have, at its end, the total number of children who were well behaved this year and the total of those who were not. Then he can compare the number of children that was behaved in this year and in previous ones.
To help the good old man, your function is to create a program that reads all the names in the list and prints the same names in alphabetical order. At the bottom of the list, you should print the total number of children who were good behaved and the ones who were not.

Input
The input consists of several names. The first value N (0 ≤ N ≤ 100), indicates how many names there are on the list. The next N lines have one special character that matches with the behavior of the child (+ indicates that the child was well behaved, - indicates that the child misbehaved). After the special character, follows the name of the children with 20 characteres at maximum.
Output
For each list of children, you should print the names in alphabetical order. After print the names, you must show the total number of children that were well behaved ("Se comportaram: ") and misbehaved during the year ("Nao se comportaram: ")

My code in Python is this one.

[color=#454545][font=Ubuntu, sans-serif]import string

import operator

num_list = input() # number of row i have to print

def show_info(SantaChildren): # define a function to print a result
    print(str(SantaChildren[0])+" "+str(SantaChildren[1]))

count = 0 # define varible that count my good boy and bed boy

for SantaChildren in range(int(num_list)): #run this for to write a number of children until the number i insert

    SantaChildren_symbolname = raw_input() # write name of children
[/font][/color]
#here is getting this error and i don't know why
Error:
Traceback (most recent call last):   File "SantaChildren.py", line 16, in <module>     new_list = sorted(sorted(SantaChildren_symbolname, key=itemgetter(1)), reverse=True) NameError: name 'itemgetter' is not defined   **wall** **wall**
line 16: new_list = sorted(sorted(SantaChildren_symbolname, key=itemgetter(1)), reverse=True) #i would like to sort them in alphabetic order without the symbols starting to itemgetter position 1 because in position 0 there is a symbol right?

#here i'm lost  Huh Cry
#after order them alphabetically i would like to count the children with symbol -
for i in range(len(new_list)): 
    show_info(new_list[i])

I don't know if it is clear but if you put this code in Atom or Sublime you can understand more.

Thanks a lot

I really appreciate your help

Alberto Sironi
Reply
#2
Your post is a bit of a mess. You should be posting runnable but still minimal code, and not trying anything fancy - code tags are great, no need to try to add fonts or color.

The code you've posted doesn't include the line with your error.
Reply
#3
That's what you need to get a Master's in CS?  I should go back to school, then... I thought it was all about building operating systems and algorithms...

Quote:I don't know if it is clear but if you put this code in Atom or Sublime you can understand more.
What would whatever editor I use change the code you posted?  Just use code tags, most of us can read the language without needing an editor (...or without needing to even run it).

Your error is referencing something that's not in your code.  Which means you didn't share your code.  Which means we can't help.
Reply
#4
First, itemgetter is in the operator module. You have to import operator to use it.

Second, itemgetter(1) is going to return a function f, such that f® = r[1]. This will be the first character after the good/bad symbol, but that alone will not allow you to sort the list correctly. You probably want itemgetter(slice(1, None)).

Third, sorted(sorted(seq), reverse = True) is redundant. You just need sorted(seq, reverse = True). Although I'm not sure why you're reversing it, then it would be in reverse alphabetical order (Z to A).

Fourth, to count nice kids you can just loop through the list times, counting those that start with +. Or get the length of a list comprehension only including names that start with +. Then, since you have the total number of kids, finding the number of naughty kids is just subtraction.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks a lot to evryone, i understud the answer and the problem.

Great.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print out the children index of the search tree? longmen 7 2,133 Apr-02-2022, 06:58 AM
Last Post: Yoriz
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,353 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  Sorting list - Homework assigment ranbarr 1 2,217 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,872 May-14-2021, 07:14 AM
Last Post: perfringo
  Help with Python Inheritance (One parent, two children) b_salm 3 1,899 Jan-29-2020, 08:09 AM
Last Post: buran
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,663 Nov-14-2019, 02:44 AM
Last Post: jefsummers
  CODE for Bubble sorting an unsorted list of 5 numbers. SIJAN 1 2,275 Dec-19-2018, 06:22 PM
Last Post: ichabod801
  sorting a deck of cards (objects in a list) itmustbebunnies 1 7,179 Dec-05-2018, 02:44 AM
Last Post: ichabod801
  Help with list sorting gonzo620 1 3,105 Oct-16-2018, 02:58 PM
Last Post: j.crater
  Sorting list of names by first two characters Otbredbaron 2 3,263 May-24-2018, 03:59 PM
Last Post: Otbredbaron

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020