2025-05-12 09:52:51 +07:00
2025-05-12 09:52:51 +07:00
2025-05-12 10:21:40 +07:00

penjumlahan_yang_dibatalkan

def proses_penjumlahan(urutan): stack = [] for item in urutan: if item == 'B': if len(stack) >= 2: stack.pop() # hapus penjumlahan terakhir else: angka = int(item) if stack: angka += stack[-1] stack.append(angka) return stack[-1] if stack else 0

Input data dari soal

data_input = [ ["2", "2", "B", "10", "7", "B", "5", "B", "11", "B"], # h#1 ["1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1"], # h#2 ["10", "B", "2", "B", "2", "B", "2"] # h#3 ]

Menjalankan program dan mencetak hasil

for idx, data in enumerate(data_input, 1): hasil = proses_penjumlahan(data) print(f"h#{idx}: {hasil}")

Description
No description provided
Readme MIT 25 KiB