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

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

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

问题

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

1u1xz94r1tb:30
p5aoveerv2c:100
uy1gpevdlq:71.44
f5w9r82h34g:10
kj1nbawqm6:125.33

我们还有一个订单总额。

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

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

一些测试案例:

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

我不知道从哪里开始。这个问题是在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).

1u1xz94r1tb:30
p5aoveerv2c:100
uy1gpevdlq:71.44
f5w9r82h34g:10
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:

Total: $196.77
Gift cards:
1. kj1nbawqm6:125.33
2. uy1gpevdlq:71.44
Total: $140
Gift cards:
1. p5aoveerv2c:100
2. 1u1xz94r1tb:30
3. f5w9r82h34g:10
Total: $400
Gift cards:
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

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

package main

type card struct {
   amount float32
   pin string
}

var cards = []card{
   {125.33, "kj1nbawqm6"},
   {100, "p5aoveerv2c"},
   {71.44, "uy1gpevdlq"},
   {30, "1u1xz94r1tb"},
   {10, "f5w9r82h34g"},
}

func newCards(f float32) []card {
   var cs []card
   for _, c := range cards {
      if f <= 0 {
         return cs
      }
      cs = append(cs, c)
      f -= c.amount
   }
   return nil
}

测试:

package main
import "fmt"

func main() {
   tests := []float32{196.77, 140, 400}
   for _, test := range tests {
      c := newCards(test)
      fmt.Println(c)
   }
}

结果:

[{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
[{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
[]

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

英文:

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

package main

type card struct {
   amount float32
   pin string
}

var cards = []card{
   {125.33, &quot;kj1nbawqm6&quot;},
   {100, &quot;p5aoveerv2c&quot;},
   {71.44, &quot;uy1gpevdlq&quot;},
   {30, &quot;1u1xz94r1tb&quot;},
   {10, &quot;f5w9r82h34g&quot;},
}

func newCards(f float32) []card {
   var cs []card
   for _, c := range cards {
      if f &lt;= 0 {
         return cs
      }
      cs = append(cs, c)
      f -= c.amount
   }
   return nil
}

Test:

package main
import &quot;fmt&quot;

func main() {
   tests := []float32{196.77, 140, 400}
   for _, test := range tests {
      c := newCards(test)
      fmt.Println(c)
   }
}

Result:

[{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
[{125.33 kj1nbawqm6} {100 p5aoveerv2c}]
[]

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:

确定