Code wars kata starting from zero

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.

Today's code

Q1 First dictionary type Pete, the baker [5kyu]

Function of how many cakes can be made Make def cakes (recipe, available):

Example 1

Input: Recipe and ingredients

cakes({flour: 500, sugar: 200, eggs: 1}, {flour: 1200, sugar: 1200, eggs: 5, milk: 200})

Output: Number that can be made

2

Example 2

Input: Recipe and ingredients

{apples: 3, flour: 300, sugar: 150, milk: 100, oil: 100}, {sugar: 500, flour: 2000, milk: 2000})

Output: Number that can be made

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

Q2 OX game Tic-Tac-Toe Checker [5kyu]

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 ...

Example 1

Input: Board

[[0, 0, 1], [0, 1, 2], [2, 1, 0]]

Output: Result

-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.

Afterword

I don't feel like leaving the house before noon on Saturday, so I can't eat anything

Template for yourself

Today's code

Q

Example 1

input:
output:

Example 2

input:
output:

MyAnswer


BestAnswers


Recommended Posts

Code wars kata starting from zero
Code wars kata starting from zero Sudoku, Nampre
Let Code Day58 Starting from Zero "20. Valid Parentheses"
Let Code Day49 starting from zero "1323. Maximum 69 Number"
Let Code Day89 "62. Unique Paths" Starting from Zero
Let Code Day 55 "22. Generate Parentheses" Starting from Zero
Let Code Table of Contents Starting from Zero
Let Code Day18 starting from zero "53. Maximum Subarray"
Let Code Day 13 "338. Counting Bits" Starting from Zero
Let Code Day71 Starting from Zero "1496. Path Crossing"
Let Code Day 61 "7. Reverse Integer" starting from zero
Let Code Day 82 "392. Is Subsequence" Starting from Zero
Let Code Day51 Starting from Zero "647. Palindromic Substrings"
Let Code Day 50 "739. Daily Temperatures" Starting from Zero
Let Code Day 15 "283. Move Zeroes" starting from zero
Let Code Day14 starting from zero "136. Single Number"
ChIP-seq analysis starting from zero
[Python] Django Source Code Reading View Starting from Zero ①
Let Code Day 43 "5. Longest Palindromic Substring" Starting from Zero
Let Code Day74 starting from zero "12. Integer to Roman"
Let Code Day 42 "2. Add Two Numbers" Starting from Zero
Let Code Day57 Starting from Zero "35. Search Insert Position"
Let Code Day47 Starting from Zero "14. Longest Common Prefix"
Let Code Day78 Starting from Zero "206. Reverse Linked List"
Let Code Day 44 "543. Diameter of Binary Tree" starting from zero
Let Code Day 64 starting from zero "287. Find the Duplicate Number"
Let Code Day 84 starting from zero "142. Linked List Cycle II"
Let Code Day24 Starting from Zero "21. Merge Two Sorted Lists"
Let Code Day12 Starting from Zero "617. Merge Two Binary Trees"
Let Code Day2 Starting from Zero "1108. Defanging an IP Address"
Let Code Day70 Starting from Zero "295. Find Median from Data Stream"
Let Code Day81 "347. Top K Frequent Elements" Starting from Zero
Let Code Day48 Starting from Zero "26. Remove Duplicates from Sorted Array"
Let Code Day87 Starting from Zero "1512. Number of Good Pairs"
Let Code Day56 Starting from Zero "5453. Running Sum of 1d Array"
Let Code Day7 starting from zero "104. Maximum Depth of Binary Tree"
Let Code Day86 Starting from Zero "33. Search in Rotated Sorted Array"
Let Code Day92 Starting from Zero "4. Median of Two Sorted Arrays"
Let Code Day5 starting from zero "1266. Minimum Time Visiting All Points"
Let Code Day 35 "160. Intersection of Two Linked Lists" Starting from Zero
Let Code Day83 Starting from Zero "102. Binary Tree Level Order Traversal"
Let Code Day75 starting from scratch "15.3 Sum"
Let Code Day 29 "46. Permutations" starting from scratch
Let Code Day 91 "153. Find Minimum in Rotated Sorted Array" starting from zero
Let Code Day59 starting from zero "1221. Split a String in Balanced Strings"
Let Code Day 11 Starting from Zero "1315. Sum of Nodes with Even-Valued Grandparent"
Let Code Day6 Starting from Zero "1342. Number of Steps to Reduce a Number to Zero"
Let Code Day 27 "101. Symmetric Tree" starting from scratch
Python explosive environment construction starting from zero (Mac)
Let Code Day 41 "394. Decode String" starting from scratch
Let Code Day 25 "70. Climbing Stairs" starting from scratch
Let Code Day69 starting from scratch "279. Perfect Squares"
Let Code Day 34 starting from scratch "118. Pascal's Triangle"
Let Code Day85 starting from scratch "6. ZigZag Conversion"
Let Code Day20 starting from scratch "134. Gas Station"
Let Code Day 88 "139. Word Break" starting from scratch
Let Code Day 28 "198. House Robber" starting from scratch
Zero-based code wars kata yield and Fibonacci sequence
Let Code Day 39 "494. Target Sum" starting from scratch
Let Code Day 36 "155. Min Stack" starting from scratch
Let Code Day 17 "169. Majority Element" starting from scratch