Q: Algorithm to pick 3 or less gift cards from a list that are greater than or equal to total order amount 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).

A: 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, "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
}

Test:
package main
import "fmt"

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.
