关于数据格式转换及羽毛球比赛输赢概率的面试题 - 杰普的博客

Posted lijianming180

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于数据格式转换及羽毛球比赛输赢概率的面试题 - 杰普的博客相关的知识,希望对你有一定的参考价值。

1
2
3
4
5
6
7
8
9
10
11
12
#input
listd = [
 {"id": 1, "level": 0, "parent_id": 0, "name": "PROVINCE_A"},
 {"id": 2, "level": 1, "parent_id": 1, "name": "CITY_B"},
 {"id": 3, "level": 2, "parent_id": 2, "name": "DISTRICT_C"},
 {"id": 4, "level": 3, "parent_id": 3, "name": "VILLAGE_D"},
 {"id": 5, "level": 3, "parent_id": 3, "name": "VILLAGE_E"},
 {"id": 6, "level": 0, "parent_id": 0, "name": "PROVINCE_F"},
 {"id": 7, "level": 1, "parent_id": 6, "name": "CITY_G"},
 {"id": 8, "level": 2, "parent_id": 7, "name": "DISTRICT_H"},
 {"id": 9, "level": 3, "parent_id": 8, "name": "VILLAGE_I"}
]
1
2
3
4
5
6
7
8
9
10
11
12
13
# expected return
{
"PROVINCE_A": {
"CITY_B": {
"DISTRICT_C": ["VILLAGE_D", "VILLAGE_E"]
}
},
"PROVINCE_F": {
"CITY_G": {
"DISTRICT_H": ["VILLAGE_I"]
}
}
}
1
2
def search(ids):
    return next(item for item in listd if item['id'] == ids)
1
2
3
4
5
6
7
8
9
10
'''主要思路是通过构建一个空的dict,然后按照需要转化的格式按逻辑填充'''
s = dict()
for i in listd:
    if i['level'] == 1:
         大专栏  关于数据格式转换及羽毛球比赛输赢概率的面试题 - 杰普的博客s[search(i['parent_id'])['name']] = {i['name']:{}}
    if i['level'] == 2:
        s[search(search(i['parent_id'])['parent_id'])['name']][search(i['parent_id'])['name']] = {i['name']:[]}
    if i['level'] == 3:
        s[search(search(search(i['parent_id'])['parent_id'])['parent_id'])['name']][search(search(i['parent_id'])['parent_id'])['name']][search(i['parent_id'])['name']].append(i['name'])    
print(s)
1
{'PROVINCE_A': {'CITY_B': {'DISTRICT_C': ['VILLAGE_D', 'VILLAGE_E']}}, 'PROVINCE_F': {'CITY_G': {'DISTRICT_H': ['VILLAGE_I']}}}

problem 2

In a simple badmintom game, one player wins the opponent by: 1. scoring at least 21 points 2. scoring at least 2 points ahead of the opponent

Now we want to generalize the problem. In each independent round, player A has probalility p to gain one point, and player B has probability (1-p) to gain one point. If a player needs at least N points, and K points ahead of the opponent in order to win the game, can you write a function to ESTIMATE the probability for A to win B? It is guaranteed that 1<=N<=10, 1<=K<=5 and 0<p<1. Only the first three significant figures of your answer are considerd for corretnees.

由推理可得一局打球次数n的范围:N+K <= n <= 2(N+K)-1
A获胜的一般公式为:

1
from scipy.special import comb
1
2
3
4
5
6
7
'''markdown中的指数符号为 ^, python中为 **, 写的时候别混了。
由于在一定输赢概率下,一局打球的次数是有上限的,超过这个上线必有一方胜出,因此可以通过次数的范围计算每个次数条件下一方赢的概率'''
def f(N,K,p):   
    s = 1.0
    for i in range(1,N+K):
        s += (1-p)**(i)*comb(N+K-1+i,i)
    return p**(N+K)*s
1
f(2,3,0.5)
1
0.5


以上是关于关于数据格式转换及羽毛球比赛输赢概率的面试题 - 杰普的博客的主要内容,如果未能解决你的问题,请参考以下文章

python羽毛球竞技模拟

面试题:关于概率

关于羽毛球规则的英文词汇

关于Ajax和Json的面试题

水题讲解:瑞士轮

面试题LRU算法及编码实现LRU策略缓存