牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Pythonで小2算数のくり下がりのあるひき算のひっ算の問題を生成するメモ

(python3.7.x)

前置き

この動画のような問題をいくつか生成してみる


小2算数 くり下がりのあるひき算のひっ算

コード

雑に書いちゃったから後で見直すかも
ちなみに最大2桁

import random


def get_num():
    num_list = range(10)
    one1 = random.sample(num_list, 1)[0]
    one2 = random.sample(num_list[one1:], 1)[0]
    if one1 == one2 and 0 < one1:
        one1 -= 1
    elif one1 == one2 and len(num_list) > one1:
        one2 += 1
    return [one1, one2]


amount = 10
max_ten1 = 9
for _ in range(amount):
    one1, one2 = get_num()
    ten1 = random.sample(range(1, max_ten1), 1)[0]
    ten2 = 0
    if 1 < ten1 and ten1 < max_ten1:
        ten2 = random.sample(range(0, ten1), 1)[0]
    answer = (ten1 * 10 + one1) - (ten2 * 10 + one2)
    print('{}{}-{}{} = {}'.format(ten1, one1, ten2, one2, answer))

実行結果例

40-12 = 28
46-38 = 8
26-08 = 18
20-02 = 18
38-09 = 29
46-38 = 8
28-09 = 19
53-38 = 15
81-04 = 77
70-31 = 39

Pythonでランキングで同順位を考慮して順位を取得するメモ

(python3.7.x)

{id:score} の入った配列 id_score_listid:2 が score 降順で順位が何番めなのかを取得したいメモ

id_score_list = [{3: 180}, {5: 150}, {10: 150}, {1: 120}, {8: 120}, {9: 120}, {2: 110}, {7: 90}, {6: 70}, {4: 60}]
target_id = 2
ids = [list(d.items())[0][0] for d in id_score_list]
scores = [list(d.items())[0][1] for d in id_score_list]
target_index = ids.index(target_id)
print('total_count', len(scores))
print('target_id', target_id)
ranking = len(scores[:target_index]) + 1
print('ranking', ranking)
before_duplicate_ranking_count = len(set(scores[:target_index]))
print('before_duplicate_ranking_count', before_duplicate_ranking_count)
fix_ranking = before_duplicate_ranking_count + 1
print('fix_ranking', fix_ranking)

# total_count 10
# target_id 2
# ranking 7
# before_duplicate_ranking_count 3
# fix_ranking 4

id:2 のランキング7位だったが、同順位を考慮すると4位になった。

参考