|
| 1 | +package greedy |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +/* |
| 9 | +TestMaxNumber tests solution(s) with the following signature and problem description: |
| 10 | +
|
| 11 | + func MaxNumber(number1, number2 []int, n int) []int |
| 12 | +
|
| 13 | +Given two numbers represented as list of positive integers, and an integer n, return the |
| 14 | +largest possible integer with n digits that can be constructed by merging digits from two |
| 15 | +numbers while respecting the order of digits in each number. |
| 16 | +
|
| 17 | +For example given {5, 4, 3, 2, 1}, {9, 8, 7, 6} and 9, it should return a 9 digit number |
| 18 | +{9, 8, 7, 6, 5, 4, 3, 2, 1}. |
| 19 | +*/ |
| 20 | +func TestMaxNumber(t *testing.T) { |
| 21 | + tests := []struct { |
| 22 | + number1 []int |
| 23 | + number2 []int |
| 24 | + k int |
| 25 | + largestNumber []int |
| 26 | + }{ |
| 27 | + {[]int{}, []int{}, 1, []int{}}, |
| 28 | + {[]int{}, []int{1}, 1, []int{1}}, |
| 29 | + {[]int{0}, []int{1}, 1, []int{1}}, |
| 30 | + {[]int{0}, []int{1}, 2, []int{1, 0}}, |
| 31 | + {[]int{1}, []int{0}, 2, []int{1, 0}}, |
| 32 | + {[]int{1}, []int{2}, 4, []int{}}, |
| 33 | + {[]int{2}, []int{1}, 4, []int{}}, |
| 34 | + {[]int{3, 2, 1}, []int{1}, 3, []int{3, 2, 1}}, |
| 35 | + {[]int{6, 9}, []int{6, 0, 4}, 5, []int{6, 9, 6, 0, 4}}, |
| 36 | + {[]int{1, 2, 3}, []int{1, 2, 3}, 4, []int{3, 1, 2, 3}}, |
| 37 | + {[]int{5, 4, 3, 2, 1}, []int{9, 8, 7, 6}, 9, []int{9, 8, 7, 6, 5, 4, 3, 2, 1}}, |
| 38 | + {[]int{5, 4, 3, 2, 1}, []int{9, 8, 7, 6}, 8, []int{9, 8, 7, 6, 5, 4, 3, 2}}, |
| 39 | + } |
| 40 | + |
| 41 | + for i, test := range tests { |
| 42 | + if got := MaxNumber(test.number1, test.number2, test.k); !reflect.DeepEqual(got, test.largestNumber) { |
| 43 | + t.Fatalf("Failed test case #%d. Want %#v got %#v", i, test.largestNumber, got) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments