Testing Greedy Coin Change Algorithm Implementation in hello-algo
This test suite evaluates the greedy coin change algorithm implementation in Go, examining both optimal and sub-optimal scenarios. It validates the algorithm’s behavior with different coin denominations and target amounts, demonstrating cases where greedy approach succeeds and fails to find the global optimum.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
krahets/hello-algo
codes/go/chapter_greedy/coin_change_greedy_test.go
// File: coin_change_greedy_test.go
// Created Time: 2023-07-23
// Author: Reanon ([email protected])
package chapter_greedy
import (
"fmt"
"testing"
)
func TestCoinChangeGreedy(t *testing.T) {
// 贪心:能够保证找到全局最优解
coins := []int{1, 5, 10, 20, 50, 100}
amt := 186
res := coinChangeGreedy(coins, amt)
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
fmt.Printf("凑到 %d 所需的最少硬币数量为 %d\n", amt, res)
// 贪心:无法保证找到全局最优解
coins = []int{1, 20, 50}
amt = 60
res = coinChangeGreedy(coins, amt)
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
fmt.Printf("凑到 %d 所需的最少硬币数量为 %d\n", amt, res)
fmt.Println("实际上需要的最少数量为 3 ,即 20 + 20 + 20")
// 贪心:无法保证找到全局最优解
coins = []int{1, 49, 50}
amt = 98
res = coinChangeGreedy(coins, amt)
fmt.Printf("coins = %v, amt = %d\n", coins, amt)
fmt.Printf("凑到 %d 所需的最少硬币数量为 %d\n", amt, res)
fmt.Println("实际上需要的最少数量为 2 ,即 49 + 49")
}