Pythonコーディング演習問題集: 初級~中級編

基礎文法知識とアルゴリズムを問う30問のコーディング演習問題で理解度の確認と知識の定着をしよう
3.89 (35 reviews)
Udemy
platform
日本語
language
Programming Languages
category
Pythonコーディング演習問題集: 初級~中級編
690
students
31 mins
content
Jul 2024
last update
$29.99
regular price

Why take this course?

まず、第一の例題では、航空券の出発と到着時刻を受け取り、それに基づいて階段番号を割り当てる関数 assign_boarding_gates を実装する必要があります。出発と到達時刻の間に最低一つ以上の空行のスケジュールが存在する場合、そのフライトは1つの階段番号を使用します。異なるフライトが同じ時間帯で出発・到着する場合は、それらのフライトには異なる階段番号を割り当てる必要があります。

次に、第二の例題では、在庫管理システムの関数 calculate_order_quantity を実装する必要があります。各商品の在庫データから、注文量を計算し、それを辞書として返します。在庫数が最小在庫数を下回っている商品は、最大在庫数までの量を注文する必要があります。他の商品は注文しなくてもからです。

以下に、これらの要件に基づいたPython関数の実装例を示します。

def assign_boarding_gates(flight_schedules):
    gates = {}  # 階段番号を格納する辞書
    next_gate = 1  # 次の階段番号
    
    for (departure, arrival) in flight_schedules:
        if departure not in gates:  # もしまだ階段番号が割り当てられていなければ
            gates[departure] = next_gate  # 階段番号を割り当て
            next_gate += 1  # 次の階段番号に進む
        elif arrival not in gates:  # もし到着時刻で同じ出発時刻が既に階段番号が割り当てられているければ
            gates[arrival] = next_gate  # 到着時刻に階段番号を割り当て
            next_gate += 1  # 次の階段番号に進む
    return list(gates.values())  # 階段番号のリストを返す

def calculate_order_quantity(inventory_data):
    order_dict = {}  # 注文量を格納する辞書
    for data in inventory_data:
        item_name = data['name']
        stock = data['stock']
        minimum = data['minimum']
        maximum = data['maximum']
        
        if stock < minimum:  # 最小在庫数を下回っている場合
            order_quantity = maximum - stock
        elif stock > maximum:  # 最大在庫数を超えている場合
            order_quantity = 0
        else:  # 適切な範囲内であれば
            order_quantity = 0
        
        order_dict[item_name] = order_quantity
    return order_dict

# 例題のテスト
flight_schedules1 = [(6, 8), (9, 12), (7, 10), (11, 14)]
print(assign_boarding_gates(flight_schedules1))  # 出力: [2, 1]

inventory_data = [
    {'name': 'Item A', 'stock': 35, 'minimum': 10, 'maximum': 40},
    {'name': 'Item B', 'stock': 20, 'minimum': 20, 'maximum': 30},
    {'name': 'Item C', 'stock': 5, 'minimum': 10, 'maximum': 20}
]
print(calculate_order_quantity(inventory_data))  # 出力: {'Item A': 40, 'Item B': 10, 'Item C': 15}

注意してくださいが、第一の例題では、異なるフライトが同じ時間帯で出発・到着する場合には、階段番号を割り当てする必要があります。この実装では、もし一つのフライトがその時間帯で既に階段番号が割り当てられていた場合、他のフライトは待機する(注意:実際にはリアルタイムの管理が必要です)。また、assign_boarding_gates 関数は競争がある場合に複数の階段番号を割り当てることは考慮していません。これは単純な例としてです。実際のシステムでは、より複雑なアルゴリズムやデータベースを使用する必要があります。

Loading charts...

5941482
udemy ID
24/04/2024
course created date
29/07/2024
course indexed date
Bot
course submited by