Practice: KelloggCoin - Solution
The Problem
Below is the problem again, which you can also find here.
Cryptocurrency is all the rage. You have decided to create your own, KelloggCoin, that will massively disrupt financial markets at the Global Hub.
Don't change the following code, which represents a series of transactions, each of which represents a transfer of KelloggCoin from one user to another – transactions with no from_user are from the "ICO" (Initial Coin Offering).
blockchain = [
{ "from_user" => nil, "to_user" => "ben", "amount" => 20000 },
{ "from_user" => nil, "to_user" => "brian", "amount" => 20000 },
{ "from_user" => "ben", "to_user" => "evan", "amount" => 9000 },
{ "from_user" => "brian", "to_user" => "anthony", "amount" => 7000 },
{ "from_user" => "evan", "to_user" => "anthony", "amount" => 400 },
{ "from_user" => "ben", "to_user" => "anthony", "amount" => 1500 },
{ "from_user" => "anthony", "to_user" => "ben", "amount" => 4500 },
{ "from_user" => "anthony", "to_user" => "evan", "amount" => 1750 }
]
Write code that returns the number of KelloggCoin that each user has in their KelloggCoin "wallet" after these transactions.
It should print out:
Ben's KelloggCoin balance is 14000
Brian's KelloggCoin balance is 13000
Evan's KelloggCoin balance is 10350
Anthony's KelloggCoin balance is 2650
The Solution
The goal is to track the changes to each user's balance so that we can display the final amounts after the transactions. Here's just 1 possible solution.
Since this is the ICO, we can assume all balances begin at 0
. So let's start by setting each user's starting balance to 0
- we can't track changes to a balance unless the balance exists.
# create 4 variables to represent each user's balance
bens_balance = 0
brians_balance = 0
evans_balance = 0
anthonys_balance = 0
Next, loop through the transactions. For each to_user
, add the transaction amount to their balance; for each from_user
, subtract the transaction amount from their balance.
# loop through the "blockchain" and increment/decrement the balances accordingly
for transaction in blockchain
# each "transaction" in the "blockchain" array is a Hash
# if the user is the "from_user", decrement their balance
if transaction["from_user"] == "ben"
bens_balance = bens_balance - transaction["amount"]
elsif transaction["from_user"] == "brian"
brians_balance = brians_balance - transaction["amount"]
elsif transaction["from_user"] == "evan"
evans_balance = evans_balance - transaction["amount"]
elsif transaction["from_user"] == "anthony"
anthonys_balance = anthonys_balance - transaction["amount"]
end
# if the user is the "to_user", increment their balance
if transaction["to_user"] == "ben"
bens_balance = bens_balance + transaction["amount"]
elsif transaction["to_user"] == "brian"
brians_balance = brians_balance + transaction["amount"]
elsif transaction["to_user"] == "evan"
evans_balance = evans_balance + transaction["amount"]
elsif transaction["to_user"] == "anthony"
anthonys_balance = anthonys_balance + transaction["amount"]
end
end
Lastly, display the final balance amounts:
# finally, print out the result
puts "Ben's KelloggCoin balance is #{bens_balance}"
puts "Brian's KelloggCoin balance is #{brians_balance}"
puts "Evan's KelloggCoin balance is #{evans_balance}"
puts "Anthony's KelloggCoin balance is #{anthonys_balance}"
Congrats! You're on your way to mastery of the programming fundamentals: data types, variables, conditional logic, arrays, hashes, and loops!
Final Code
# 1. create 4 variables to represent each user's balance
bens_balance = 0
brians_balance = 0
evans_balance = 0
anthonys_balance = 0
# 2. loop through the "blockchain" transactions
for transaction in blockchain
# each "transaction" in the "blockchain" array is a Hash
# 3. if the user is the "from_user", decrement their balance
if transaction["from_user"] == "ben"
bens_balance = bens_balance - transaction["amount"]
elsif transaction["from_user"] == "brian"
brians_balance = brians_balance - transaction["amount"]
elsif transaction["from_user"] == "evan"
evans_balance = evans_balance - transaction["amount"]
elsif transaction["from_user"] == "anthony"
anthonys_balance = anthonys_balance - transaction["amount"]
end
# 4. if the user is the "to_user", increment their balance
if transaction["to_user"] == "ben"
bens_balance = bens_balance + transaction["amount"]
elsif transaction["to_user"] == "brian"
brians_balance = brians_balance + transaction["amount"]
elsif transaction["to_user"] == "evan"
evans_balance = evans_balance + transaction["amount"]
elsif transaction["to_user"] == "anthony"
anthonys_balance = anthonys_balance + transaction["amount"]
end
end
# 5. finally, print out the result
puts "Ben's KelloggCoin balance is #{bens_balance}"
puts "Brian's KelloggCoin balance is #{brians_balance}"
puts "Evan's KelloggCoin balance is #{evans_balance}"
puts "Anthony's KelloggCoin balance is #{anthonys_balance}"
Challenge!
The above solution gets the job done and there's no need to go any further. However, if this solution is leaving you a bit dissatisfied, then you're starting to think like a programmer!
Why might you be feeling dissatisfied - i.e. what are the drawbacks?
Well, this solution only works with these 4 specific users. But what if we added someone new to the blockchain? We would need to duplicate a lot of the code to accommodate other user balances. Are there other ways to implement the solution that are not dependent on knowing who and how many users there are in the transactions?
See if you can challenge yourself!