#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Python List Exercise


    Heey guys!

    I was learning how to use python online and I failed to complete one of the exercises. Mainly because I don't understand how I can complete the exercise by just using what I've learned so far. Can anyone help me out?
    Note: Try to only use basic python commands

    The game of Assassin is a simple game played on university campuses where each player is assigned a target to assassinate by simply saying “you’re dead” to them. Of course with thousands of people on campus and only a few dozen in the game you never know who is looking to assassinate you. Once you assassinate someone you take on the target they were looking for. If this happens to be you then you are the winner. To ensure that this works properly the targets must form a continuous “chain.” Write a program that allows the user to enter their target assignments and output whether it is valid or not based on if there is a continuous “chain.” Each person in the list is represented by position in the list.

    I got the practice questions from programmersheaven.com.
    (Just in case anyone is wondering)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481
    Uh huh, and what is "basic python"?

    This grouping has two cycles. It is NO GOOD.

    [1,2,0],[4,3]

    I don't have time right now to figure this out.
    (Following the list starting anywhere to see if everyone is hit is no problem.) The trouble is to figure out
    1) the permutation number of a specific list.
    2) the permutation numbers that are cycles.
    3) Is this permutation in the set of 2) ?

    In j (www.jsoftware.com) these are the anagram indexes
    Code:
       (,.i.6);,.(i.6)A.'abc'
    ┌─┬───┐
    │0│abc│ no good a->a
    │1│acb│ no good a->a
    │2│bac│ bad, c->c
    │3│bca│ good
    │4│cab│ good
    │5│cba│ bad, b->b
    └─┴───┘
    The 6 permutations of 3 objects. Why are 3 and 4 full cycles?
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Hmm how would you follow the list starting anywhere to see if everyone is hit
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,997
    Rep Power
    481

    I'd do it like this.


    Code:
    '''
        command  python3 -m doctest thisfile.py  to run doctests
    '''
    
    import random
    
    def assassinate(P:list)->bool:
        '''
            Return True iff the permutation vector P is a 1 cycle.
            >>> assassinate([7, 5, 6, 1, 3, 0, 4, 2])
            True
            >>> assassinate([0])
            True
            >>> assassinate([2,1,0])
            False
        '''
        visited = set()
        k = 0
        while k not in visited:
            visited.add(k)
            k = P[k]
        return len(visited) == len(P)
    
    if __name__ == '__main__:                        # test a few cases
        A = list(range(8))
        for i in 'range()':
            random.shuffle(A)
            print('{}eah: {}'.format('ny'[assassinate(A)], A))
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo