-3

Here is the code:

get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]

It is in the context of a tik tac toe program, and if you want to see it, here it is:

starting_message = 'Greetings, X Shall Go First And O Shall Go Second. '      

prompt = 'Shall We Begin?(Y Or N)  '   

instructions = 'Pick A Number(1-9)'

box = ["1", "2", "3", "4", "5", "6", "7", "8" ,"9"]     

               
print(starting_message)

get_indexes = lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]

player_begin = raw_input(prompt)

if player_begin == 'Y':
    print(instructions)

else:
    if player_begin == 'N':
        quit()



player = 'X'

def is_winner(boxes):
    if 6 in boxes and 7 in boxes and 8 in boxes:    
        return True
    elif 5 in boxes and 4 in boxes and 3 in boxes:    
        return True
    elif 2 in boxes and 1 in boxes and 0 in boxes:
        return True
    elif 0 in boxes and 4 in boxes and 8 in boxes:
        return True 
    elif 1 in boxes and 4 in boxes and 7 in boxes:
        return True 
    elif 0 in boxes and 3 in boxes and 6 in boxes:
        return True 
    elif 2 in boxes and 5 in boxes and 8 in boxes:
        return True
    elif 2 in boxes and 4 in boxes and 6 in boxes:
        return True 
    else:
        return False

def is_tie(boxes):
    if all boxes == 'X' or 'O'
    and is_winner(boxes) is false:
        return True
    else return False
    
    
def is_occupied(index,boxes):
    return boxes[index - 1] == 'X' or boxes[index - 1] == 'O'
 
while True: 
    
    if player == 'X':   
        print('Your Turn X')

    else:
        print('Your Turn O')
   
    board = '''                ___________________
                |     |     |     |
                |  '''+box[0]+'''  |  '''+box[1]+'''  |  '''+box[2]+'''  |  
                |     |     |     |
                |-----------------|
                |     |     |     |
                |  '''+box[3]+'''  |  '''+box[4]+'''  |  '''+box[5]+'''  |
                |     |     |     |
                |-----------------|
                |     |     |     |
                |  '''+box[6]+'''  |  '''+box[7]+'''  |  '''+box[8]+'''  |
                |     |     |     |
                ___________________  '''
                                

    player_board = raw_input(board)

            
    if is_occupied(int(player_board), box):
        print('This Box Is Not Avaliable') 
    else:
        if player == 'X':
            box[int(player_board) - 1] = "X" 
            player = 'O' #False
        else: 
            box[int(player_board) - 1] = "O"
            player = 'X' #True
    
        if is_winnerX(get_indexes('X', box)):   
            print('X WINS!!')
            quit()
        elif is_winnerX(get_indexes('O', box)):   
            print('O WINS!!')
            quit()
        elif is_tie(boxes):
            print()
            quit()
        else:
            'return'
   


    
    


 




    
  • 2
    You should take a look at what ```lambda``` is how to use/understand it. Here for example : https://www.w3schools.com/python/python_lambda.asp – Panda50 Apr 21 '21 at 13:59
  • See [Lambda Expressions](https://docs.python.org/3/tutorial/controlflow.html#lambda-expressions) in the docs. –  Apr 21 '21 at 14:01
  • To cite the [Style Guide for Python Code](https://www.python.org/dev/peps/pep-0008/#id51): "Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier" – Matthias Apr 21 '21 at 15:05
  • 1
    BTW, according to the usage of `raw_input` this is code for Python 2, even if `print` uses parentheses. Python 2 is dead since the beginning of 2020 and that death was announced over 10 years ago. Switch to Python 3. Now! – Matthias Apr 21 '21 at 15:08

1 Answers1

1

It finds the indexes where there are is a X or an O, depending on how you call it.

Lets say that box = ['1', '2', 'X', 'O', '5', 'X', '7', '8', 'O']. Now, you call get_indexes, passing 'X', meaning that you want to find the indexes of all the X's, and passing in box, like so: get_indexes('X', box).

The first thing that happens is the zip. To see what is happening, let's print it out:

print([i for i in zip(box, range(len(box)))])
# and we get:
[('1', 0), ('2', 1), ('X', 2), ('O', 3), ('5', 4), ('X', 5), ('7', 6), ('8', 7), ('O', 8)]

Let's look at the first tuple, ('1', 0). Well, where did the '1' come from? Well, if you look at box you will see that it is the first index. That means that the 0 must be the index of '1'!

The next part is here :

lambda x, xs: [i for (y, i) in zip(xs, range(len(xs))) if x == y]
#     this part ---> (y, i) <--- this part

What's happening here is y and i are beging set to ('1', 0). So y is being set to '1', and i to 0.

Now we come to if x == y. If you remember from the beginning, x = 'X'. Well, y is equal '1' not 'X', so i, or the index of '1', is not appended to the list.

Skip to the third tuple ('X', 2). y is set to 'X'. if x == y will return True, and the index of y (i) will be appended to the list. So after looping through all the tuples, get_indexes will return [2, 5].

You could also accomplish the same thing with enumerate(xs) instead of using zip(xs, range(len(xs))). Like this:

get_indexes = lambda x, xs: [idx for idx, ele in enumerate(xs) if ele == x]
# idx is the index, and ele is current item/element
Have a nice day
  • 1,007
  • 5
  • 11