مسئله · پردازش داده متوسط JavaScript ·
Bank ledger
Process bank operations in order and return the final balances. Operations are lists: - ["open", name] opens an account with balance 0 (ignored if it is already open); - ["deposit", name, amount]; - ["withdraw", name, amount]; - ["transfer", from_name, to_name, amount].
An operation is rejected (has no effect) if an account it names isn't open, if the amount isn't positive, or if a balance would go below zero; a transfer to the same account is rejected too. Return a dictionary of every open account's name to its balance.
امضای تابع
/**
* @param {Array[]} ops
* @returns {Object<string, number>}
*/
function balances(ops)نمونهها
balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]]) // -> {"ann": 70, "bob": 30}بههمراه 9 تست مخفی و 1 تست با ورودی بزرگ با محدودیت زمانی؛ ورودیهای آنها نمایش داده نمیشوند. مسئلهها به زبان انگلیسی به مدلها داده میشوند.
رتبه 1MiniMax M3.1 Flashحلشده · 11 از 11 تستپاسخ در 2.1 ثانیه · زمان اجرا 477 میلیثانیه · $0.0003 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])مورد انتظار {"ann": 70, "bob": 30} 3 میلیثانیه ✓ 3 میلیثانیه✓ <1 میلیثانیه✓ 1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ 2 میلیثانیه✓ 4 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه⏱ 460 میلیثانیهکد
function balances(ops) { const accounts = Object.create(null); for (let i = 0; i < ops.length; i++) { const op = ops[i]; const type = op[0]; if (type === "open") { const name = op[1]; if (!Object.prototype.hasOwnProperty.call(accounts, name)) { accounts[name] = 0; } continue; } if (type === "deposit" || type === "withdraw") { const name = op[1]; const amount = op[2]; if (!Object.prototype.hasOwnProperty.call(accounts, name)) continue; if (!(amount > 0)) continue; if (type === "deposit") { accounts[name] += amount; } else { if (accounts[name] < amount) continue; accounts[name] -= amount; } continue; } if (type === "transfer") { const from = op[1]; const to = op[2]; const amount = op[3]; if (!Object.prototype.hasOwnProperty.call(accounts, from)) continue; if (!Object.prototype.hasOwnProperty.call(accounts, to)) continue; if (from === to) continue; if (!(amount > 0)) continue; if (accounts[from] < amount) continue; accounts[from] -= amount; accounts[to] += amount; } } return accounts; }رتبه 1Google Gemini 3.8 Flashحلشده · 11 از 11 تستپاسخ در 5.8 ثانیه · زمان اجرا 493 میلیثانیه · $0.0041 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])مورد انتظار {"ann": 70, "bob": 30} 2 میلیثانیه ✓ 2 میلیثانیه✓ 1 میلیثانیه✓ 1 میلیثانیه✓ <1 میلیثانیه✓ 1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ 1 میلیثانیه✓ 2 میلیثانیه⏱ 480 میلیثانیهکد
/** * @param {Array[]} ops * @returns {Object<string, number>} */ function balances(ops) { const accounts = new Map(); for (let i = 0; i < ops.length; i++) { const op = ops[i]; const type = op[0]; if (type === "open") { const name = op[1]; if (!accounts.has(name)) { accounts.set(name, 0); } } else if (type === "deposit") { const name = op[1]; const amount = op[2]; if (amount > 0 && accounts.has(name)) { accounts.set(name, accounts.get(name) + amount); } } else if (type === "withdraw") { const name = op[1]; const amount = op[2]; if (amount > 0 && accounts.has(name)) { const bal = accounts.get(name); if (bal >= amount) { accounts.set(name, bal - amount); } } } else if (type === "transfer") { const fromName = op[1]; const toName = op[2]; const amount = op[3]; if ( fromName !== toName && amount > 0 && accounts.has(fromName) && accounts.has(toName) ) { const fromBal = accounts.get(fromName); if (fromBal >= amount) { accounts.set(fromName, fromBal - amount); accounts.set(toName, accounts.get(toName) + amount); } } } } const result = {}; for (const [name, bal] of accounts) { result[name] = bal; } return result; }رتبه 1DeepSeek V4.1 Flashحلشده · 11 از 11 تستپاسخ در 750 میلیثانیه · زمان اجرا 537 میلیثانیه · $0.0003 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])مورد انتظار {"ann": 70, "bob": 30} 3 میلیثانیه ✓ <1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ 1 میلیثانیه✓ <1 میلیثانیه⏱ 527 میلیثانیهکد
function balances(ops) { const accounts = Object.create(null); const has = (name) => Object.prototype.hasOwnProperty.call(accounts, name); for (let i = 0; i < ops.length; i++) { const op = ops[i]; const type = op[0]; if (type === "open") { const name = op[1]; if (!has(name)) accounts[name] = 0; } else if (type === "deposit") { const name = op[1], amount = op[2]; if (has(name) && amount > 0) accounts[name] += amount; } else if (type === "withdraw") { const name = op[1], amount = op[2]; if (has(name) && amount > 0 && accounts[name] >= amount) accounts[name] -= amount; } else if (type === "transfer") { const from = op[1], to = op[2], amount = op[3]; if ( from !== to && has(from) && has(to) && amount > 0 && accounts[from] >= amount ) { accounts[from] -= amount; accounts[to] += amount; } } } return Object.assign({}, accounts); }رتبه 1OpenAI GPT-6 Lunaحلشده · 11 از 11 تستپاسخ در 2.7 ثانیه · زمان اجرا 552 میلیثانیه · $0.0002 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])مورد انتظار {"ann": 70, "bob": 30} 4 میلیثانیه ✓ 1 میلیثانیه✓ 3 میلیثانیه✓ 2 میلیثانیه✓ 2 میلیثانیه✓ <1 میلیثانیه✓ <1 میلیثانیه✓ 1 میلیثانیه✓ <1 میلیثانیه✓ 1 میلیثانیه⏱ 534 میلیثانیهکد
function balances(ops) { const accounts = new Map(); for (const op of ops) { const type = op[0]; if (type === "open") { if (!accounts.has(op[1])) accounts.set(op[1], 0); } else if (type === "deposit") { const name = op[1], amount = op[2]; if (accounts.has(name) && amount > 0) { accounts.set(name, accounts.get(name) + amount); } } else if (type === "withdraw") { const name = op[1], amount = op[2]; if (accounts.has(name) && amount > 0 && accounts.get(name) >= amount) { accounts.set(name, accounts.get(name) - amount); } } else if (type === "transfer") { const from = op[1], to = op[2], amount = op[3]; if (from !== to && amount > 0 && accounts.has(from) && accounts.has(to) && accounts.get(from) >= amount) { accounts.set(from, accounts.get(from) - amount); accounts.set(to, accounts.get(to) + amount); } } } return Object.fromEntries(accounts); }
هزینه توکنها: $0.0048 · نحوه برگزاری راندها و امتیازدهی ←