Skip to content

Instantly share code, notes, and snippets.

@agaase
Created April 17, 2017 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agaase/9d0c0e0938a43f1b8f2558287243a8b6 to your computer and use it in GitHub Desktop.
Save agaase/9d0c0e0938a43f1b8f2558287243a8b6 to your computer and use it in GitHub Desktop.
Coding
// You have L, a list containing some digits (0 to 9). Write a function answer(L) which finds the largest number that can be made from some or all of these digits and is divisible by 3. If it is not possible to make such a number, return 0 as the answer. L will contain anywhere from 1 to 9 digits. The same digit may appear multiple times in the list, but each element in the list may only be used once.
var a = [3,1,4,1,5,9];
var max = 0;
var pattern = function(arr){
if(arr.length==1){
return arr;
}
var patterns = [];
for(var i=0;i<arr.length;i++){
debugger;
var copyArr = arr.slice(0,arr.length);
copyArr.splice(i,1)
var sub_patterns = pattern(copyArr);
for(var j=0;j<sub_patterns.length;j++){
var num = arr[i]*Math.pow(10,Math.floor(Math.log10(sub_patterns[j]))+1)+sub_patterns[j];
if(num%3 ==0 && num > max){
max = num;
}
patterns.push(num);
}
}
return patterns;
}
pattern(a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment