At codewars kata you can learn programming and practical English at the same time. It is a genius site. If you have a github account, you can get started in 30 seconds If you are interested, start now start here
By the way, you can challenge in many languages other than python.
Function of how many cakes can be made
Make def cakes (recipe, available):
cakes({flour: 500, sugar: 200, eggs: 1}, {flour: 1200, sugar: 1200, eggs: 5, milk: 200})
2
{apples: 3, flour: 300, sugar: 150, milk: 100, oil: 100}, {sugar: 500, flour: 2000, milk: 2000})
0
MyAnswer
list = []
for key,value in recipe.items():
if key in available:
list.append(available[key]//value)
else:
list.append(0)
return min(list)
It seems that you can get the contents with items () if you gg with the first dictionary type, so get it with for Save the ingredients // recipes in the list for each ingredient (key) If there is no material, save 0 Change the minimum value of list
Straightforward code
BestAnswers
def cakes(recipe, available):
return min(available.get(k, 0)/recipe[k] for k in recipe)
It seems that you can get the key with for k in dict
Get value with dict.get (k, 0), get second argument if no key
Almost the same with min (available.get (k, 0) // v for k, v in recipe.items ())
I have to use dict.get
return min ([available [i] // recipe [i] if i in available else 0 for i in recipe])
and if can be included
If you think that a tic tac game is something, it seems to be an OX game If you pass the board and classify the results, it's OK -1: Incomplete 1: X wins 2: O wins 0: Draw (it's a cat's game) It looks difficult ...
[[0, 0, 1], [0, 1, 2], [2, 1, 0]]
-1
MyAnswer
def ox(board):
lines = []
for i in range(3):
lines.append(board[i])
lines.append([board[j][i] for j in range(3)])
lines.append([board[0][0],board[1][1],board[2][2]])
lines.append([board[0][2],board[1][1],board[2][0]])
yetfin = 0
for k in range(8):
if lines[k] == [1,1,1] or lines[k] == [2,2,2]:
return lines[k][0]
if set(lines[k]) == {0,1} or set(lines[k]) == {0,2} :
yetfin = 1
if yetfin:
return -1
else:
return 0
It is a program that saves the numerical values of each of the 8 vertical and horizontal columns in lines and then searches for which of the following Win: [111] [222] Incomplete: {0,1} {0,2} Draw: Other than that
BestAnswers
def ox2(board):
for i in range(0,3):
if board[i][0] == board[i][1] == board[i][2] != 0:
return board[i][0]
elif board[0][i] == board[1][i] == board[2][i] != 0:
return board[0][i]
if board[0][0] == board[1][1] == board[2][2] != 0:
return board[0][0]
elif board[0][2] == board[1][1] == board[2][0] != 0:
return board[0][0]
elif 0 not in board[0] and 0 not in board[1] and 0 not in board[2]:
return 0
else:
return -1
What you are doing is almost the same except that you have not made lines until the winning decision (This is smart because it saves memory) But I'm just looking for 0 in the draw decision ... In other words XXO OOX XO_ The behavior has changed with a stacking game with 0 like
board = [[1,1,2],
[2,2,1],
[1,2,0]]
print('MyAnswer:',ox(board))
print('BestPractice:',ox2(board))
MyAnswer: 0
BestPractice: -1
It will be.
that? If you look closely at the problem statement
-1 if the board is not yet finished (there are empty spots),
In other words, if there is even one empty, it is incomplete, so the latter seems to be the correct Best Practice.
I don't feel like leaving the house before noon on Saturday, so I can't eat anything
Template for yourself
Q
MyAnswer
BestAnswers
Recommended Posts