Algorithm to pick 3 or less gift cards from a list that are greater than or equal to total order amount

huangapple go评论116阅读模式
英文:

Algorithm to pick 3 or less gift cards from a list that are greater than or equal to total order amount

问题

我有一个礼品卡列表,存储在一个数组中。每张礼品卡都有一个由冒号分隔的PIN码和金额(示例如下)。

  1. 1u1xz94r1tb:30
  2. p5aoveerv2c:100
  3. uy1gpevdlq:71.44
  4. f5w9r82h34g:10
  5. kj1nbawqm6:125.33

我们还有一个订单总额。

我希望算法能够选择3张或更少的适用礼品卡,使其金额等于或超过订单总额。

我需要管理客户的购物车,购物车会推荐适用的礼品卡。

一些测试案例:

  1. 总额:$196.77
  2. 礼品卡:
  3. 1. kj1nbawqm6:125.33
  4. 2. uy1gpevdlq:71.44
  1. 总额:$140
  2. 礼品卡:
  3. 1. p5aoveerv2c:100
  4. 2. 1u1xz94r1tb:30
  5. 3. f5w9r82h34g:10
  1. 总额:$400
  2. 礼品卡:
  3. 空(空数组)

我不知道从哪里开始。这个问题是在Go语言中实现的,但是任何语言的解决方案都可以,我可以根据我的需求进行翻译(即使是简单的伪代码/数学解释也可以)。

英文:

I have a list of gift cards in an array. Each gift card has the pin and the amount on it separated by a colon (example below).

  1. 1u1xz94r1tb:30
  2. p5aoveerv2c:100
  3. uy1gpevdlq:71.44
  4. f5w9r82h34g:10
  5. kj1nbawqm6:125.33

We also have an order total.

I want the algorithm to pick 3 or less applicable gift cards to equal or exceed the order total.

I have to manage a customer's cart, the cart will recommend gift cards to apply.

Some test cases:

  1. Total: $196.77
  2. Gift cards:
  3. 1. kj1nbawqm6:125.33
  4. 2. uy1gpevdlq:71.44
  1. Total: $140
  2. Gift cards:
  3. 1. p5aoveerv2c:100
  4. 2. 1u1xz94r1tb:30
  5. 3. f5w9r82h34g:10
  1. Total: $400
  2. Gift cards:
  3. null (empty array)

I simply do not know where to start. This is being implemented in Go, but a solution in any language will fit, I will be able to translate it for my needs (even a simple pseudocode/mathematical explanation will do).

答案1

得分: 1

这不完全符合你的输出,但它确实遵循了规则:

  1. package main
  2. type card struct {
  3. amount float32
  4. pin string
  5. }
  6. var cards = []card{
  7. {125.33, "kj1nbawqm6"},
  8. {100, "p5aoveerv2c"},
  9. {71.44, "uy1gpevdlq"},
  10. {30, "1u1xz94r1tb"},
  11. {10, "f5w9r82h34g"},
  12. }
  13. func newCards(f float32) []card {
  14. var cs []card
  15. for _, c := range cards {
  16. if f <= 0 {
  17. return cs
  18. }
  19. cs = append(cs, c)
  20. f -= c.amount
  21. }
  22. return nil
  23. }

测试:

  1. package main
  2. import "fmt"
  3. func main() {
  4. tests := []float32{196.77, 140, 400}
  5. for _, test := range tests {
  6. c := newCards(test)
  7. fmt.Println(c)
  8. }
  9. }

结果:

  1. [{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
  2. [{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
  3. []

我不会进一步解释这个例子,因为它只是为了让你入门。

英文:

This doesn't exactly match your output, but it does follow the rules as decribed:

  1. package main
  2. type card struct {
  3. amount float32
  4. pin string
  5. }
  6. var cards = []card{
  7. {125.33, &quot;kj1nbawqm6&quot;},
  8. {100, &quot;p5aoveerv2c&quot;},
  9. {71.44, &quot;uy1gpevdlq&quot;},
  10. {30, &quot;1u1xz94r1tb&quot;},
  11. {10, &quot;f5w9r82h34g&quot;},
  12. }
  13. func newCards(f float32) []card {
  14. var cs []card
  15. for _, c := range cards {
  16. if f &lt;= 0 {
  17. return cs
  18. }
  19. cs = append(cs, c)
  20. f -= c.amount
  21. }
  22. return nil
  23. }

Test:

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. tests := []float32{196.77, 140, 400}
  5. for _, test := range tests {
  6. c := newCards(test)
  7. fmt.Println(c)
  8. }
  9. }

Result:

  1. [{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
  2. [{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
  3. []

I won't take this example further, as it's only meant to get you started.

huangapple
  • 本文由 发表于 2021年7月2日 07:38:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/68218008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定