最終課題:アカウント移行
Coddyの「Rust開発者のためのGDScript」コースのレッスン 48/48。
チャレンジ
簡単balance を持つ FundsAccount(initial:int) を実装してください。static FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool を実装してください。null アカウント、負の金額、または source の資金不足の場合は、状態を変更せずに拒否してください。それ以外の場合は、一度だけ送金してください。残高が足りていれば、自己送金は成功し、残高は変化しません。solve は [a,b,n] を受け取り、success:a:b を返します。初期残高は負でないものとします。
入力は stdin 上の 1 つの JSON 配列です。編集できない main はこれを解析し、公開 API を呼び出して、返されたテキストの前に result= を付けて改行とともに出力します。ヘルパー関数は何も出力しないでください。入力ケースの前に、独立した固定のテストケースで必須の API 動作を検証します。これらのテストケースは実装スタイルを規定するものではありません。
コードは funds_account.gd、funds_transfer.gd、exercise.gd に記述してください。main.gd には入力/出力用のコードが用意されており、編集できません。各 class_name 型は、名前に対応するファイルに記述してください。
入力の範囲:初期残高は 0 から 1000000 までの整数で、指定する金額は -1000000 から 1000000 までの整数です。公開 API は、説明したとおり null アカウントと自己送金も処理します。
Rust の完全な参照プログラムです。同じ JSON 配列を stdin から読み取り、同じ結果の行を書き出します。
Rust ファイル account.rs:
pub struct Account { pub balance:i64 }
impl Account { pub fn new(balance:i64)->Self { Self{balance} } }
Rust ファイル transfer.rs:
use std::{rc::Rc,cell::RefCell};
use crate::account::Account;
pub fn move_funds(source:Option<&Rc<RefCell<Account>>>,target:Option<&Rc<RefCell<Account>>>,amount:i64)->bool {
let (Some(source),Some(target))=(source,target) else { return false; };
if amount<0 || amount>source.borrow().balance { return false; }
source.borrow_mut().balance-=amount;
target.borrow_mut().balance+=amount;
true
}
Rust ファイル main.rs:
mod account; mod transfer;
use std::{rc::Rc,cell::RefCell,io::{self,Read}};
fn main() {
let probe=Rc::new(RefCell::new(account::Account::new(5)));
assert!(transfer::move_funds(Some(&probe),Some(&probe),3));
assert_eq!(probe.borrow().balance,5);
assert!(!transfer::move_funds(None,Some(&probe),0));
let mut input=String::new(); io::stdin().read_to_string(&mut input).expect("stdin");
let inner=input.trim().strip_prefix('[').and_then(|s|s.strip_suffix(']')).expect("array");
let values:Vec<i64>=inner.split(',').map(|s| {
let value=s.trim().parse::<f64>().expect("number");
assert!(value.is_finite() && value.fract()==0.0 && value.abs()<=1_000_000.0);
value as i64
}).collect();
assert_eq!(values.len(),3);
let source=Rc::new(RefCell::new(account::Account::new(values[0])));
let target=Rc::new(RefCell::new(account::Account::new(values[1])));
let success=transfer::move_funds(Some(&source),Some(&target),values[2]);
println!("result={}:{}:{}",success,source.borrow().balance,target.borrow().balance);
}
自分で試してみよう
extends Node
func contract_ok(data: Array) -> bool:
var a: FundsAccount = FundsAccount.new(6)
var b: FundsAccount = FundsAccount.new(2)
if FundsTransfer.move(null,b,0) or FundsTransfer.move(a,null,0) or FundsTransfer.move(a,b,7) or FundsTransfer.move(a,b,-1): return false
if a.balance != 6 or b.balance != 2: return false
if not FundsTransfer.move(a,a,4) or a.balance != 6: return false
if not FundsTransfer.move(a,b,3) or a.balance != 3 or b.balance != 5: return false
return true
func _ready() -> void:
var data: Array = JSON.parse_string(OS.read_string_from_stdin())
if not contract_ok(data):
print("API_CONTRACT_FAILED")
return
print("result=" + Exercise.solve(data))
Rust開発者のためのGDScriptのすべてのレッスン
自分で練習してみよう: GDScriptオンラインコンパイラ