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'