Last active
September 26, 2019 10:20
-
-
Save KevinSia/80eb1bc9d0d6d5a1185b61dc60ef4cd7 to your computer and use it in GitHub Desktop.
Assessment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
slips = { | |
"slip_23" => | |
{ | |
transactions: [123, 456], | |
shop: 1 | |
}, | |
"slip_42" => | |
{ | |
transactions: [789], | |
shop: 2 | |
} | |
} | |
transactions = [ | |
{ | |
id: 123, | |
amount: 10.01, | |
payout: false | |
}, | |
{ | |
id: 456, | |
amount: 5.01, | |
payout: true | |
}, | |
{ | |
id: 789, | |
amount: 20.10, | |
payout: false | |
} | |
] | |
shops = [ | |
[1, 'Zalando.de'], | |
[2, 'Amazon.com'] | |
] | |
################################ | |
# Main code | |
def calculate_transaction_total(transaction_ids, transactions) | |
total = 0 | |
transaction_ids | |
.map do |trx_id| | |
# find transaction hash | |
transactions.find { |trx| trx[:id] == trx_id } || {} | |
end | |
.each do |trx| | |
# sum up transaction amounts, payout amt will be deducted from total | |
total += trx[:amount] * (trx[:payout] ? -1 : 1) | |
end | |
total | |
end | |
def find_shop(shop_id, shops) | |
# find shop data with id, then take the second item in array | |
_shop_id, shop_name = shops.find { |shop| shop[0] == shop_id } | |
shop_name | |
end | |
SLIP_FORMAT = "slip_".freeze | |
def get_slip_number(slip_id) | |
slip_id.sub(SLIP_FORMAT, '').to_i | |
end | |
result = Hash.new | |
slips.each do |slip_id, slip| | |
slip_number = get_slip_number(slip_id) | |
result[slip_number] = { | |
number_transactions: slip[:transactions].length, | |
total_amount: calculate_transaction_total(slip[:transactions], transactions), | |
shop: find_shop(slip[:shop], shops) | |
} | |
end | |
################################ | |
# Test | |
expected_result = { | |
23 => { | |
number_transactions: 2, # no of transactions per slip | |
total_amount: 5, # total amount of transactions (a payout must be subtracted instead of added!) | |
shop: 'Zalando.de' # Shop Title | |
}, | |
42 => { | |
number_transactions: 1, | |
total_amount: 20.10, | |
shop: 'Amazon.com' | |
} | |
} | |
p result | |
puts "Expected result == result => #{expected_result == result}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
slips = { | |
"slip_23" => | |
{ | |
transactions: [123, 456], | |
shop: 1 | |
}, | |
"slip_42" => | |
{ | |
transactions: [789], | |
shop: 2 | |
} | |
} | |
transactions = [ | |
{ | |
id: 123, | |
amount: 10.01, | |
payout: false | |
}, | |
{ | |
id: 456, | |
amount: 5.01, | |
payout: true | |
}, | |
{ | |
id: 789, | |
amount: 20.10, | |
payout: false | |
} | |
] | |
shops = [ | |
[1, 'Zalando.de'], | |
[2, 'Amazon.com'] | |
] | |
################################ | |
# Main code | |
result = Hash.new | |
slips.each do |slip_id, slip| | |
result[ | |
slip_id[5..-1].to_i | |
] = { | |
number_transactions: slip[:transactions].length, | |
total_amount: slip[:transactions].reduce(0) { |total, trx_id| | |
trx = transactions.find { |trx| trx[:id] == trx_id} | |
total + trx[:amount] * (trx[:payout] ? -1 : 1) | |
}.round(2), | |
shop: shops.find { |shop| shop[0] == slip[:shop] }.last | |
} | |
end | |
################################ | |
# Test | |
expected_result = { | |
23 => { | |
number_transactions: 2, # no of transactions per slip | |
total_amount: 5, # total amount of transactions (a payout must be subtracted instead of added!) | |
shop: 'Zalando.de' # Shop Title | |
}, | |
42 => { | |
number_transactions: 1, | |
total_amount: 20.10, | |
shop: 'Amazon.com' | |
} | |
} | |
p result | |
puts "Expected result == result => #{expected_result == result}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment