Created
January 9, 2020 05:20
-
-
Save dhaninugraha/6316dddfe5e99768202015359e3d1690 to your computer and use it in GitHub Desktop.
Represent X amount as IDR currency. Python version is banknotes-only, while Go versions are banknotes-only and banknotes + coins.
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
package main | |
import ( | |
"fmt" | |
) | |
var ( | |
bankNotes = []int{100000, 50000, 20000, 10000, 5000, 2000, 1000} | |
) | |
func _represent(amount int, bankNotes []int) { | |
mapDivisorQuotient := map[int]int{} | |
dqPair := []int{} | |
for _, bn := range bankNotes { | |
mapDivisorQuotient[bn] = amount / bn | |
} | |
for divisor, quotient := range mapDivisorQuotient { | |
if quotient == 0 { | |
continue | |
} | |
if len(dqPair) == 0 || quotient < dqPair[1] { | |
dqPair = []int{divisor, quotient} | |
} | |
} | |
if len(dqPair) == 0 { | |
fmt.Println("Excess:", amount) | |
} else { | |
fmt.Println(dqPair[0], "x", dqPair[1]) | |
amount -= dqPair[0] * dqPair[1] | |
_represent(amount, bankNotes) | |
} | |
} | |
func represent(amount int) { | |
_represent(amount, bankNotes) | |
} | |
func main() { | |
represent(163500) | |
/* | |
100000 x 1 | |
50000 x 1 | |
10000 x 1 | |
2000 x 1 | |
1000 x 1 | |
Excess: 550 | |
*/ | |
} |
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
bank_notes = [100000, 50000, 20000, 10000, 5000, 2000, 1000] | |
def _represent(amount, bank_notes): | |
map_divisor_quotient = {} | |
for _, bn in enumerate(bank_notes): | |
map_divisor_quotient[bn] = int(amount / bn) | |
dq_pair = () | |
for divisor, quotient in map_divisor_quotient.items(): | |
if quotient == 0: | |
continue | |
if dq_pair == () or quotient < dq_pair[1]: | |
dq_pair = (divisor, quotient) | |
if dq_pair == (): | |
print(f"Excess: {amount}") | |
else: | |
print(f"{dq_pair[0]} x {dq_pair[1]}") | |
amount -= dq_pair[0] * dq_pair[1] | |
_represent(amount, bank_notes) | |
def represent(amount): | |
_represent(amount, bank_notes) | |
represent(163550) | |
''' | |
100000 x 1 | |
50000 x 1 | |
10000 x 1 | |
2000 x 1 | |
1000 x 1 | |
Excess: 550 | |
''' |
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
package main | |
import ( | |
"fmt" | |
) | |
type representation struct { | |
d int | |
q int | |
p int | |
} | |
const ( | |
priorityBankNote = 0 | |
priorityCoin = 1 | |
) | |
var ( | |
priorities = map[int]string{ | |
priorityBankNote: "banknote", | |
priorityCoin: "coin", | |
} | |
currency = map[int][]int{ | |
priorityBankNote: []int{100000, 50000, 20000, 10000, 5000, 2000, 1000}, | |
priorityCoin: []int{1000, 500, 100}, | |
} | |
) | |
func _represent(amount int, currency map[int][]int) { | |
mapPriorityDivisorQuotient := map[int]map[int]int{} | |
repr := representation{} | |
for priority, currKind := range currency { | |
mapPriorityDivisorQuotient[priority] = map[int]int{} | |
for _, currvValue := range currKind { | |
mapPriorityDivisorQuotient[priority][currvValue] = amount / currvValue | |
} | |
} | |
for priority, divisorQuotient := range mapPriorityDivisorQuotient { | |
for divisor, quotient := range divisorQuotient { | |
if quotient == 0 { | |
continue | |
} | |
if repr == (representation{}) || (priority < repr.p || quotient < repr.q) { | |
repr.p = priority | |
repr.d = divisor | |
repr.q = quotient | |
} | |
} | |
} | |
if repr == (representation{}) { | |
fmt.Println("Excess:", amount) | |
} else { | |
fmt.Printf("%d x %d in %s(s)\n", repr.d, repr.q, priorities[repr.p]) | |
amount -= repr.d * repr.q | |
_represent(amount, currency) | |
} | |
} | |
func represent(amount int) { | |
_represent(amount, currency) | |
} | |
func main() { | |
represent(163550) | |
/* | |
100000 x 1 in banknote(s) | |
50000 x 1 in banknote(s) | |
10000 x 1 in banknote(s) | |
2000 x 1 in banknote(s) | |
1000 x 1 in banknote(s) | |
500 x 1 in coin(s) | |
Excess: 50 | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment