Menu
CoddyTech

Final: Account Transfer

Lesson 48 of 48 in Coddy's GDScript for C Developers course.

challenge icon

Challenge

Easy

Implement FundsAccount(initial:int) with balance; static FundsTransfer.move(source:FundsAccount,target:FundsAccount,n:int)->bool. Reject null accounts, negative amount or insufficient source funds without mutation. Otherwise transfer once. Self-transfer succeeds without net change when affordable. Solve takes [a,b,n] and returns success:a:b. Initial balances nonnegative.

Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.

Complete C reference. Save these files together and compile main.c with a C17 compiler. The headers provide the helper API; main reads the same JSON array and prints the same result protocol. No external library is required.

C file account.h:

#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <stdint.h>
struct Account { int64_t balance; };
static inline struct Account account_make(int64_t initial) {
    return (struct Account){initial};
}
#endif

C file transfers.h:

#ifndef TRANSFERS_H
#define TRANSFERS_H
#include <stdbool.h>
#include "account.h"
static inline bool transfer_move(struct Account *source,
                                 struct Account *target, int64_t amount) {
    if (!source || !target || amount<0 || amount>source->balance) return false;
    if (source==target) return true;
    /* Match the reference's unchecked 64-bit arithmetic without C overflow. */
    uint64_t sum=(uint64_t)target->balance+(uint64_t)amount;
    source->balance-=amount;
    target->balance=sum <= INT64_MAX ? (int64_t)sum : -1-(int64_t)(UINT64_MAX-sum);
    return true;
}
#endif

C file main.c:

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "transfers.h"
int main(int argc, char **argv) {
    if (argc>1 && strcmp(argv[1],"--probe")==0) {
        struct Account a=account_make(6);
        if (transfer_move(NULL,&a,0) || transfer_move(&a,NULL,0) ||
            !transfer_move(&a,&a,4) || a.balance!=6 ||
            transfer_move(&a,&a,7) || a.balance!=6) return 1;
        puts("contract-ok"); return 0;
    }
    int64_t a,b,n;
    if (scanf(" [ %" SCNd64 " , %" SCNd64 " , %" SCNd64 " ]",&a,&b,&n)!=3) return 1;
    struct Account source=account_make(a),target=account_make(b);
    bool moved=transfer_move(&source,&target,n);
    printf("result=%s:%" PRId64 ":%" PRId64 "\n",
           moved?"true":"false",source.balance,target.balance);
    return 0;
}

Your code goes in funds_account.gd, funds_transfer.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.

Try it yourself

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))

All lessons in GDScript for C Developers

Practice on your own: Online GDScript compiler