A read the title laugh oneself hahaha, recently pig elder brother (play knock good Peng you) ask me to help solve his homework hahaha, their company out of the pen test hahaha, hope not to be they set the question to see hahaha, recall the pig elder brother said a word, in case this is later you meet the test hahaha. Suddenly I realized that I hadn’t played Python for almost half a year, and my hands had already been born.

Blog.csdn.net/hanhanwangh… Treasure Girl welcomes your attention! Welcome to pay attention to wechat public number: Treasure girl’s growth diary let this lovely treasure girl walk with you on the road of hard work! If reproduced, please indicate the source (if not indicated, the thief will investigate)

My humble opinion on tian Ji’s horse racing

  • Use python functions to implement Tian Ji horse racing
    • Request a
    • For two
    • For three

Use python functions to implement Tian Ji horse racing

Request a

Now we have abstracted tian ji’s horses into a list [2,5,8], and qi wang’s horses into another list [3,6,9], representing the lower, middle and upper horses respectively (I remember that the higher the value is, the better the horse is, so that I can understand correctly). Design a function race() that passes two lists as arguments to Race (), abstracting the strategy into a code that allows Tian Ji to win the race. The function returns each round match: the code

# coding=utf-8
from typing import *


def race(tian: List[int], qi: List[int]) - >List[List[int]] :
    # Sort the numbers of the two lists
    tian.sort()
    qi.sort()

    # Save the weakest for last
    The first index is not the first element (i.e. the index is 0).
    The second is to take the first element and use it as an element in a list
    tian = tian[1:] + [tian[0]]

    # return matchup status for each round
    return list(zip(tian, qi))


if __name__ == '__main__':
    tianji = [2.5.8]
    qiwang = [3.6.9]
    result = race(tianji, qiwang)
    print(result)
Copy the code

For two

Now the horse is divided into inferior, inferior, middle, upper, excellent, etc., the best of five rounds system, abstract as the list [2,4,6,8,10] and [1,3,5,7,9], other conditions remain unchanged (the king of qi still abide by the rules, tian ji continues not to follow the routine card), calculate how many kinds of tian ji may win the game on the code

# coding=utf-8
import itertools


def race(qiwang, tianji) :
    ['13579',5]
    tianji_l = list(itertools.permutations(tianji, len(tianji)))
    # All results
    result = []
    # Go through all the horse racing ways
    for i in tianji_l:
        # Round results
        result_1 = []
        # In the round of the match, the horses of both sides are against each other. I is tianji(I is obtained from Tianji_L).
        for horses in zip(i, qiwang):
            If Tian Ji's horse is worth less than King Qi's
            if horses[0] < horses[1] :# Then this round is Tian Ji to lose
                result_1.append('lose')
            else:
                # Otherwise, tian Ji wins this round
                result_1.append('win')
        # If you win three or more times in this round
        if result_1.count('win') > =3:
            # tian Ji will win, add the win to result
            result.append('win')
    return len(result)


if __name__ == '__main__':
    tian = [1.3.5.7.9]
    qi = [2.4.6.8.10]
    all_result = race(qi, tian)
    print(all_result)
Copy the code

For three

If you are the king of Qi and you know that your horse is superior to tian Ji’s in the same class, you do not know the dispatch order of the other side in advance, but you can make the selection of this round according to the horses dispatched by the other side in the last round. Develop a dispatch strategy that maximizes your chances of winning the game.

The third I am really do not know how to do (think this brain all long grass! It may not be the best. I need some smart brains. If you have a better idea, please send me a private message and welcome to discuss.

Tian Ji’s strategy of winning by inferior is that when he loses, he loses more often, but when he wins, he only wins by a small margin, such as 10-1, 8-9, 6-7, 4-5, 2-3 (The King of Qi 1: the Field Chicken 4). Then the way to make The King of Qi win as much as possible is to open this “small margin”.

Method After the first round of 2, the king of Qi divided two situations each time. 1. Tian Ji won, then we can take out tian Ji’s X-1 to participate in the next round. 2. Tian Ji lost, can continue to take the current smallest out. If the x minus one is gone, the worst one is chosen, and so on

This method will still fail as Tian Ji follows 3, 5, 7, 9, 1

In the code

# coding=utf-8

from itertools import permutations
from typing import *

tian = [1.3.5.7.9]
qi = [2.4.6.8.10]


def play() - >float:
    global qi
    win_count = 0
    cnt = 0
    for a, b, c, d, e in permutations(tian, 5):
        qi = [2.4.6.8.10]  # recovery
        cnt += 1
        tian_rank = [a, b, c, d, e]
        qi_rank = [qi.pop(0)]  # initialization
        for index, last_tian in enumerate(tian_rank[:4]):
            qi_rank.append(strategy(last_tian, qi_rank[index] > tian_rank[index]))  # Strategize based on last Tian's wins and losses
        # count wins and losses by two ranks
        win_count += winner(qi_rank, tian_rank)
    # return probability
    return float(win_count) / float(cnt)


def strategy(last_tian: int, is_win: bool) - >int:
    global qi
    # If King Qi wins the last round, continue to take the smallest one out
    if is_win:
        return qi.pop(0)
    # If King Qi loses in the last round
    else:
        # If x minus 1 exists, take it out
        if last_tian - 1 in qi:
            return qi.pop(qi.index(last_tian - 1))
        Otherwise, take the smallest
        else:
            return qi.pop(0)


def winner(qi: List[int], tian: List[int]) - >bool:
    cnt = 0
    for q, t in zip(qi, tian):
        cnt += q > t
    return cnt >= 3


if __name__ == '__main__':
    print(play())
Copy the code

That gives us a probability of.075,

I feel that the first two algorithms can be written, and the third one is really difficult (of course, it’s a piece of cake for big guys).

Blog.csdn.net/hanhanwangh… Treasure Girl welcomes your attention! Welcome to pay attention to wechat public number: Treasure girl’s growth diary let this lovely treasure girl walk with you on the road of hard work! If reproduced, please indicate the source (if not indicated, the thief will investigate)