Skip to content

Instantly share code, notes, and snippets.

@clintonmedbery
Last active April 4, 2018 03:12
Show Gist options
  • Save clintonmedbery/7555ba4f8fa227cb4c07112dee37dfa0 to your computer and use it in GitHub Desktop.
Save clintonmedbery/7555ba4f8fa227cb4c07112dee37dfa0 to your computer and use it in GitHub Desktop.
Cet Interview Problem 1
$accountsJSON = '{
"Header": {
"Time": "2018-04-02T20:17:46.38"
},
"Current Account Balances": [
{
"AcctName" : "MassagePlace1",
"AcctNum" : 10010,
"CurrentBalance" : "24.20"
},
{
"AcctName" : "MassagePlace1",
"AcctNum" : 30500,
"CurrentBalance" : "1124.00"
},
{
"AcctName" : "MassagePlace1",
"AcctNum" : 60001,
"CurrentBalance" : "144.00"
},
{
"AcctName" : "MassagePlace2",
"AcctNum" : 10330,
"CurrentBalance" : "524.00"
},
{
"AcctName" : "MassagePlace2",
"AcctNum" : 30550,
"CurrentBalance" : "1444.77"
},
{
"AcctName" : "MassagePlace2",
"AcctNum" : 60010,
"CurrentBalance" : "304.00"
},
{
"AcctName" : "MassagePlace3",
"AcctNum" : 10530,
"CurrentBalance" : "294.00"
},
{
"AcctName" : "MassagePlace3",
"AcctNum" : 30600,
"CurrentBalance" : "324.20"
},
{
"AcctName" : "MassagePlace3",
"AcctNum" : 63000,
"CurrentBalance" : "104.01"
}
]
}';
$accountsInfo = json_decode($accountsJSON, true);
// print_r("$accountsJSON\n");
$date = new DateTime($accountsInfo['Header']['Time']);
print_r("\nTime: {$date->format('m-d-Y')}\n");
$accounts = $accountsInfo['Current Account Balances'];
// print_r($accounts);
//Reduces our balances to an array where 0 = Checking, 1 = Savings, 2 = Expense
$sumBalances = array_reduce($accounts, function($sumArray, $account) {
$acctNum = $account['AcctNum'];
(($acctNum >= 10000 && $acctNum <= 19999) and $sumArray['Checking'] = $sumArray['Checking'] + $account['CurrentBalance'])
or
(($acctNum >= 30000 && $acctNum <= 39999) and $sumArray['Savings'] = $sumArray['Savings'] + $account['CurrentBalance'])
or
(($acctNum >= 60000 && $acctNum <= 69999) and $sumArray['Expenses'] = $sumArray['Expenses'] + $account['CurrentBalance'])
;
return $sumArray;
} ,["Checking" =>0.0, "Savings" => 0.0, "Expenses" => 0.0]);
// print_r($sumBalances);
print("Checking Balance: ".number_format($sumBalances['Checking'], 2)."\n");
print("Savings Balance: ".number_format($sumBalances['Savings'], 2)."\n");
print("Expenses Balance: ".number_format($sumBalances['Expenses'], 2)."\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment