-
Notifications
You must be signed in to change notification settings - Fork 0
/
howManyNumbers.js
148 lines (73 loc) · 3.6 KB
/
howManyNumbers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// We want to generate all the numbers of three digits where:
// the sum of their digits is equal to 10.
// their digits are in increasing order (the numbers may have two or more equal contiguous digits)
// The numbers that fulfill the two above constraints are: 118, 127, 136, 145, 226, 235, 244, 334
// Make a function that receives two arguments:
// the sum of digits value
// the desired number of digits for the numbers
// The function should output an array with three values: [1,2,3]
// 1 - the total number of possible numbers
// 2 - the minimum number
// 3 - the maximum number
// The example given above should be:
// findAll(10, 3) => [8, "118", "334"]
// If we have only one possible number as a solution, it should output a result like the one below:
// findAll(27, 3) => [1, "999", "999"]
// If there are no possible numbers, the function should output the empty array.
// findAll(84, 4) => []
// The number of solutions climbs up when the number of digits increases.
// findAll(35, 6) => [123, '116999', '566666']
// Features of the random tests:
// Number of tests: 112
// Sum of digits value between 20 and 65
// Amount of digits between 2 and 17
const f = (arr1, arr2) => [].concat(...arr1.map(element1 => arr2.map(element2 => [].concat(element1, element2))));
const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);
function findAll2(n, k) {
if(n === 0 || k === 0) return [];
let zeroToNine = Array(10).fill("").map(( _, i) => i + "");
let cartesianList = Array(k).fill(zeroToNine);
let possibleCombos = cartesian.apply(zeroToNine, cartesianList );
let out = possibleCombos.filter(e => e.reduce((accum, val) => parseInt(accum) + parseInt(val)) === n).map(e => e.join("")).sort();
out = out.filter(e => e === e.split("").sort().join("") && e.charAt(0) != "0");
return out.length === 0 ? [] : [out.length, out[0], out[out.length - 1]];
}
function findAll3(n, k) {
if(n === 0 || k === 0) return [];
let zeroToNine = Array(9).fill("").map(( _, i) => i + 1 + "");
let cartesianList = Array(k).fill(zeroToNine);
let possibleCombos = cartesian.apply(zeroToNine, cartesianList );
let out = [...new Set(possibleCombos.filter(e => e.reduce((accum, val) => parseInt(accum) + parseInt(val)) === n && e.sort()[0] != "0").map(e => e.sort().join("")))];
console.log(out);
return out.length === 0 ? [] : [out.length, out[0], out[out.length - 1]];
}
function findAll4(n, k) {
if(n === 0 || k === 0) return [];
let zeroToNine = Array(10).fill("").map(( _, i) => i + "");
let cartesianList = Array(k).fill(zeroToNine);
const cartesian = xs => ys => xs.flatMap(x => ys.map(y => [x, y]));
let possibleCombos = cartesian.apply(zeroToNine, cartesianList);
console.log(possibleCombos);
// let possibleCombos = cartesian.apply(zeroToNine, cartesianList );
// let out = [...new Set(possibleCombos.filter(e => e.reduce((accum, val) => parseInt(accum) + parseInt(val)) === n && e.sort()[0] != "0").map(e => e.sort().join("")))];
// return out.length === 0 ? [] : [out.length, out[0], out[out.length - 1]];
}
function findAll(n, k){
const count = (digits, sum) => {
if(digits === 0) return sum === 0;
if(sum === 0) return 1;
let ans = 0;
for(let i=0; i < 10; i++){
if(sum-i >= 0) ans += count(digits - 1, sum - i);
}
return ans;
}
let out = 0;
for(let i=1; i < 10; i++){
if(n - i >= 0) out += count(k - 1, n - i);
console.log(out);
}
return out;
}
//[8, '118', '334']
console.log(findAll3(17, 63));