Subscribe to MentalFloss - PyBlog        RSS Feed
-----

Rambling About Data Structures

Icon 4 Comments
I'm sorry that my posts are so vapid these days. I just don't know what to talk about that's actually interesting but I want to talk about stuff as it comes up. Furthermore, I want to have something noting my struggles and successes and such. You're just going to end up wasting your time reading about it is all...

Anyway, when it comes to python, I feel like I am constantly gasping for breath. It's like there's some piece or feature or concept that I'm missing and it really is the cornerstone to some new idea or way of thinking but I just can't seem to find it. Well, I think I found a really important one and so hopefully I can share it with you and we can have fun learning something. You learn by doing too ok? So, do it with me if you want to learn it.

Your problem: Jack, Jill, Tom, Jerry, and Timmy are all playing a game. Jack has 34 points. Jill has 50. Tom has 10. Jerry has 86. Timmy has 86 too.

How will you store these players and print a scoreboard as follows:

1. Jerry.................86
2. Timmy.................86
3. Jill..................50
4. Jack..................34
5. Tom...................10



Now, we don't care what game it is or what the rules are or anything like that. The only thing that we are concerned with is printing that scoreboard. What do we know? The scores are ordered descending while the names are ordered alphabetically. We know this because Jerry comes before Timmy even though they are tied score. We know that the alphabetical order comes first in sorting because the scores are in order. So, how would you do this? Open a script:

# PRINT THE FOLLOWING SCOREBOARD
# 1. Jerry.................86
# 2. Timmy.................86
# 3. Jill..................50
# 4. Jack..................34
# 5. Tom...................10

def print_scoreboard():
    pass

if __name__ == '__main__':
    print_scoreboard()



So, think about it a bit and try a few things. Try to come up with a solution and then come back. We'll talk about it.

Spoiler

4 Comments On This Entry

Page 1 of 1

AdamSpeight2008 Icon

17 March 2014 - 03:19 PM
In vb.net (linq query style)
Spoiler
1

andrewsw Icon

17 March 2014 - 05:24 PM
Python is object-oriented so for a class:

class Score:
    def __init__(self, name, score):
        self.name = name
        self.score = score

scores = []
scores.append(Score('Jack', 34))
scores.append(Score('Jill', 50))
scores.append(Score('Tom', 10))
scores.append(Score('Timmy', 86))
scores.append(Score('Jerry', 86))

def key1(a): return a.name
def key2(a): return a.score

scores.sort(key=key1)
scores.sort(key=key2, reverse=True)

for i, s in enumerate(scores):
    print("%d. %s%d" % ((i+1), s.name.ljust(15, '.'), s.score))

# output
#1. Jerry..........86
#2. Timmy..........86
#3. Jill...........50
#4. Jack...........34
#5. Tom............10

You can use ljust to pad with '.'.

I believe string.format() is now preferred to the % operator, but I still like the % operator ;)
1

andrewsw Icon

17 March 2014 - 05:29 PM
The sorting could also be pushed into the class itself, possibly using __lt__ etc..
1

MentalFloss Icon

18 March 2014 - 03:33 AM

andrewsw, on 18 March 2014 - 12:24 AM, said:

You can use ljust to pad with '.'.


Awesome! Thanks. I did not know that.
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

November 2014

S M T W T F S
      1
2345678
9101112131415
16171819202122
2324252627 28 29
30      

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    3 user(s) viewing

    3 Guests
    0 member(s)
    0 anonymous member(s)