Impractical code size requires more efficient sorting technique

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 630 Views - Last Post: 06 September 2013 - 02:04 PM Rate Topic: -----

#16 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 07:15 AM

Glad to here it.

As noted, while dict is versatile, you'd be better of with a class.

Here's another quick and dirty example:
Python 3.2.3 (default, Jun 25 2012, 23:10:56) 
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from random import randint
>>> 
>>> 
>>> class Mob(object):
...     def __init__(self, name, initMod=0):
...         self.name = name
...         self.initMod = initMod
...     
...     def rollInit(self):
...         return randint(1,10) + self.initMod
... 
>>> 
>>> def getAttackOrder(mobs):
...     order = []
...     for mob in mobs:
...         roll = mob.rollInit()
...         while roll in order:
...             roll = mob.rollInit()
...         print(mob.name,'rolls',roll)
...         order.append(roll)
...     return [ y for (x,y) in sorted(zip(order, mobs), key=lambda x:x[0], reverse=True) ]
... 
>>> 
>>> def test(mobs, n):
...     for i in range(n):
...         print(','.join(x.name for x in getAttackOrder(mobs)))
...         print('---------')
... 
>>> 
>>> mobs = [Mob('Alice',-2),Mob('Bob',1),Mob('Charlie'),Mob('Erik')]
>>> 
>>> test(mobs,4)
Alice rolls 7
Bob rolls 5
Charlie rolls 4
Erik rolls 10
Erik,Alice,Bob,Charlie
---------
Alice rolls 2
Bob rolls 9
Charlie rolls 3
Erik rolls 10
Erik,Bob,Charlie,Alice
---------
Alice rolls 0
Bob rolls 7
Charlie rolls 4
Erik rolls 1
Bob,Charlie,Erik,Alice
---------
Alice rolls -1
Bob rolls 10
Charlie rolls 3
Erik rolls 6
Bob,Erik,Charlie,Alice
---------
>>> 



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#17 JamesGE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-September 13

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 11:44 AM

Baavgai I have implemented your code but it does not seem to function as expected. Here is what I have:
def turn_rolls(mob):
    from random import randint
    roll = (randint(1,10))
    return mob['i'] + roll

def get_turn_order(mobs):
    order = []
    for mob in mobs:
        roll_tot = turn_rolls(mob) 
        while roll in order:
            roll_tot = turn_rolls(mob)
        order.append(roll_tot)
    m = zip(order, mob)

    return [y for (x, y) in sorted(m, key=lambda x:x[0])]

def turn_init(Ishido, Kentaro, Arren, Roths, Brigand_1, Brigand_2, Brigand_3):
    mobs = get_turn_order([Ishido, Kentaro, Arren, Roths, Brigand_1, Brigand_2, Brigand_3])
    print(mobs[0])



The output of printing mobs[0] gives a key name 'thrr', not an opponent name.
Any thoughts?

This post has been edited by andrewsw: 06 September 2013 - 11:50 AM
Reason for edit:: Removed huge unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#18 JamesGE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-September 13

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 12:26 PM

I forgot to remove the 'def' from turn_init (turn_init gets called from below in my program), but fixing it up this way and just running what is shown here does not change the result.
Was This Post Helpful? 0
  • +
  • -

#19 ccubed  Icon User is offline

  • It's That Guy
  • member icon

Reputation: 160
  • View blog
  • Posts: 1,403
  • Joined: 13-June 08

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 12:32 PM

Just out of curiosity, do you really mean to roll n*n times? This code rolls for every person every time. Is that what you meant to do? It's how your original code was too but it seems like an odd initiative system.
Was This Post Helpful? 0
  • +
  • -

#20 JamesGE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-September 13

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 12:38 PM

View PostJamesGE, on 06 September 2013 - 12:26 PM, said:

I forgot to remove the 'def' from turn_init (turn_init gets called from below in my program), but fixing it up this way and just running what is shown here does not change the result.

Actually sorry that is not correct. I get the unexpected incorrect return value for mobs[0] with your code when it is a part of this small program (needs the dictionaries to produce the result of course). The incorrect result is that mobs[0] is a random key name from the dictionaries provided, not anything specific to a character, such as a dictionary name which I was expecting.
def turn_rolls(mob):
    from random import randint
    roll = (randint(1,10))
    return mob['i'] + roll

def get_turn_order(mobs):
    order = []
    for mob in mobs:
        roll = turn_rolls(mob) 
        while roll in order:
            roll = turn_rolls(mob)
        order.append(roll)
    m = zip(order, mob)
    
    return [y for (x, y) in sorted(m, key=lambda x:x[0])]

def turn_init(Ishido, Kentaro, Arren, Roths, Brigand_1, Brigand_2, Brigand_3):
    mobs = get_turn_order([Ishido, Kentaro, Arren, Roths, Brigand_1, Brigand_2, Brigand_3])
    print(mobs[0])

def turn(a, b, c, d, e, f, g):
    turn_init(a, b, c, d, e, f, g)

def lvl1():
    Ishido = {'name': "Ishido", 'side': 'good', 'i': 9, 'irr': 2, 'th': 9, 'd': 9, 'thrr': 2, 'drr': 2}
    Kentaro = {'name': "Kentaro", 'side': 'good', 'i': 9, 'irr': 2, 'th': 9, 'd': 9, 'thrr': 2, 'drr': 1}
    Brigand_1 = {'name': "Brigand 1", 'side': 'evil', 'i': 7, 'irr': 1, 'th': 8, 'd': 6, 'thrr': 0, 'drr': 0}
    Brigand_2 = {'name': "Brigand 2", 'side': 'evil', 'i': 6, 'irr': 0, 'th': 7, 'd': 7, 'thrr': 1, 'drr': 0}
    Brigand_3 = {'name': "Brigand 3", 'side': 'evil', 'i': 8, 'irr': 1, 'th': 6, 'd': 8, 'thrr': 0, 'drr': 1}
    Arren = {'name': "Sir Arrencourt", 'side': 'evil', 'i': 8, 'irr': 0, 'th': 8, 'd': 7, 'thrr': 1, 'drr': 0}
    Roths = {'name': "Sir Arrencourt", 'side': 'evil', 'i': 9, 'irr': 1, 'th': 8, 'd': 7, 'thrr': 1, 'drr': 1}
    turn(Kentaro, Ishido, Arren, Roths, Brigand_1, Brigand_2, Brigand_3)

def main():
    lvl1()

main()


Was This Post Helpful? 0
  • +
  • -

#21 JamesGE  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 05-September 13

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 02:02 PM

Baavgai I know have your non-class version working now.
You had a slight error where you said;

m = zip(order, mob)

For it to be working correctly it must be;

m = zip(order, mobs)

A small fix. Thanks again! Now to learn about classes and more!
Was This Post Helpful? 0
  • +
  • -

#22 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5796
  • View blog
  • Posts: 12,631
  • Joined: 16-October 07

Re: Impractical code size requires more efficient sorting technique

Posted 06 September 2013 - 02:04 PM

Look at this very, very carefully: m = zip(order, mob). Perhaps m = zip(order, mobs)?

Now, classes. Learn to use classes.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2