Finding all even factorizations of a given number n - javascript python - javascript

Currently i am implementing an algorithm that relies on finding all even factorizations of a given number n, including n.
I've tried some things, but overall i am not able to handle the problem well. Maybe its a good idea to handle it recursively, but i am not that good with javascript yet, especially with the higher level aspects of the language which might come in handy.
function evens(n) {
evens = []
for (var i = 2; i < n/2 - 1; i++){
if (i % 2 != 0){
continue;
}
else {
if ((n/i) % 2 == 0) {
evens.push([n/i, i])
}
}
}
return evens
}
This is some code that goes some of the way, but i am not yet able to recursively implement it considering all the right base cases.
I also thought that it could be done with a tree like structure in which paths are even factors, but my cs knowledge is pretty bad.
Suggestions in Python are also welcome, but javascript would be best.
Just to make everything more clear: all even factorizations of 136 for example are [[68, 2], [34, 2, 2], [34, 4], [136]].
Thankfull for any help :)

Maybe its a good idea to handle it recursively
Here's my attempt at a recursive solution in Python:
def even_factorization(n):
solutions = []
def even_divisors(n): # 136 generates [2, 4, 8, 34, 68, 136]
return (d for d in range(2, n + 1, 2) if n % d == 0)
def remove_similarities(array): # [[2, 2, 34], [2, 34, 2], [34, 2, 2]] -> [[2, 2, 34]]
return list(map(list, set(map(lambda a: tuple(sorted(a)), array))))
for divisor in even_divisors(n):
if divisor == n:
solutions.append([divisor])
else:
for solution in even_factorization(n // divisor):
solutions.append([divisor] + solution)
return remove_similarities(solutions) # return 'solutions' to see raw data
For 136 returns:
[[2, 2, 34], [4, 34], [2, 68], [136]]
for 218960 returns:
[[184, 1190], [8, 27370], [4, 54740], [2, 70, 1564], [56, 3910], [2, 2, 170, 322],
[280, 782], [70, 3128], [4, 46, 1190], [2, 2, 34, 1610], [2, 14, 34, 230],
[2, 14, 7820], [20, 34, 322], [10, 14, 34, 46], [14, 92, 170], [20, 46, 238],
[218960], [2, 322, 340], [10, 68, 322], [34, 46, 140], [10, 14, 1564],
[2, 10, 10948], [10, 92, 238], [4, 170, 322], [92, 2380], [14, 20, 782],
[10, 21896], [238, 920], [28, 34, 230], [10, 28, 782], [2, 2, 46, 1190],
[2, 28, 3910], [10, 34, 644], [34, 6440], [2, 92, 1190], [46, 4760], [2, 170, 644],
[2, 68, 1610], [4, 70, 782], [340, 644], [2, 34, 46, 70], [2, 20, 5474],
[14, 68, 230], [2, 34, 3220], [4, 34, 1610], [4, 10, 5474], [28, 7820],
[14, 34, 460], [322, 680], [10, 46, 476], [2, 2, 54740], [4, 230, 238],
[2, 2, 2, 27370], [34, 70, 92], [2, 140, 782], [14, 15640], [2, 10, 46, 238],
[2, 10, 14, 782], [2, 14, 46, 170], [2, 238, 460], [136, 1610], [2, 2, 10, 5474],
[20, 10948], [4, 14, 3910], [40, 5474], [2, 2, 70, 782], [2, 2, 230, 238],
[230, 952], [68, 3220], [2, 46, 2380], [2, 230, 476], [2, 10, 34, 322],
[140, 1564], [460, 476], [170, 1288], [2, 4, 27370], [46, 68, 70], [14, 46, 340],
[2, 109480], [28, 46, 170], [2, 2, 14, 3910]]

After cdlane correctly pointed out a flaw in my solution, I have retracted my original solution, and ported cdlane's elegant python solution to javascript.
function even_factorization(n) {
let solutions = [];
function even_divisors(n) {
var divisors = [];
for (let i = 2; i <= n; i += 2) {
if (n % i === 0) divisors.push(i);
}
return divisors;
}
function remove_similarities(combos) {
for (let i = 0; i < combos.length; i++) {
for (let j = i + 1; j < combos.length; j++) {
if (combos[i].sort((a,b) => a - b).join(" ") === combos[j].sort((a,b) => a - b).join(" ")) {
combos.splice(j--,1);
}
}
}
return combos;
}
even_divisors(n).forEach(divisor => {
if (divisor === n)
solutions.push([divisor]);
else {
even_factorization(n / divisor).forEach(solution => {
solutions.push([divisor, ...solution]);
});
}
});
return remove_similarities(solutions);
}
Running with 218960 returns...
[[2,2,2,27370],[2,2,10,5474],[2,2,14,3910],[2,2,34,1610],[2,2,46,1190],[2,2,70,782],[2,2,170,322],[2,2,230,238],[2,2,54740],[2,4,27370],[2,10,14,782],[2,10,34,322],[2,10,46,238],[2,10,10948],[2,14,34,230],[2,14,46,170],[2,14,7820],[2,20,5474],[2,28,3910],[2,34,46,70],[2,34,3220],[2,46,2380],[2,68,1610],[2,70,1564],[2,92,1190],[2,140,782],[2,170,644],[2,230,476],[2,238,460],[2,322,340],[2,109480],[4,10,5474],[4,14,3910],[4,34,1610],[4,46,1190],[4,70,782],[4,170,322],[4,230,238],[4,54740],[8,27370],[10,14,34,46],[10,14,1564],[10,28,782],[10,34,644],[10,46,476],[10,68,322],[10,92,238],[10,21896],[14,20,782],[14,34,460],[14,46,340],[14,68,230],[14,92,170],[14,15640],[20,34,322],[20,46,238],[20,10948],[28,34,230],[28,46,170],[28,7820],[34,46,140],[34,70,92],[34,6440],[40,5474],[46,68,70],[46,4760],[56,3910],[68,3220],[70,3128],[92,2380],[136,1610],[140,1564],[170,1288],[184,1190],[230,952],[238,920],[280,782],[322,680],[340,644],[460,476],[218960]]
...and running with 136 returns...
[[2,2,34],[2,68],[4,34],[136]]

Related

How do I shuffle a list of lists?

I've got a list of lists:
myList = [[67, 79], [1, 5], [63, 122], [43, 44], [2, 5], [31, 37], [16, 45], [110, 124], [60, 64], [68, 79], [37, 116], [5, 76]]
And I would like to shuffle it around, but every way I've tried to do it has ruined the structure of lists and stripped it down to bare numbers. For instance:
myShuffledList = myList.sort((a, b) => 0.5 - Math.random());
Gives me something like:
myShuffledList = [60,64,110,124,63,122,5,76,43,44,68,79,37,116,2,5,1,5,67,79,16,45,31,37]
instead of something like this:
myShuffledList = [[68, 79], [2, 5], [31, 37], [1, 5], [67, 79], [110, 124], [63, 122], [16, 45], [5, 76], [37, 116], [43, 44], [60, 64]]
Is there simple solution as to how I can avoid the list being stripped completely, and just shuffle the lists of lists?
Everything is working as you intended it to. You just got bamboozled by console.log converting your array to strings. If you use a custom toString function to avoid this you can see that everything works as intended
myList = [
[67, 79],
[1, 5],
[63, 122],
[43, 44],
[2, 5],
[31, 37],
[16, 45],
[110, 124],
[60, 64],
[68, 79],
[37, 116],
[5, 76]
]
myShuffledList = myList.sort((a, b) => 0.5 - Math.random());
var toString = (list) => {
return `${list.map((sublist) => `[${sublist}]`)}`
}
console.log(toString(myList))
console.log(toString(myShuffledList))

Counting occurrences of arrays of numbers in Javascript

How do you count the number of occurrences of number arrays in plain Javascript? Where something like this (imagine having hundreds to these number sets):
[2, 5, 66, 199]
[32, 56, 88, 109]
[13, 45, 102, 200]
[2, 5, 66, 199]
[2, 5, 66, 199]
[32, 56, 88, 109]
would give me back something like this:
2, 5, 66, 199 -> 3 times
32, 56, 88, 109 -> 2 times
13, 45, 102, 200 -> 1 time
Create string from array and use it as key in object. If you want [1, 2] != [2, 1] just remove sort before join method.
const data = [[2, 5, 66, 199],[32, 56, 88, 109],[13, 45, 102, 200],[2, 5, 66, 199],[2, 5, 66, 199],[32, 56, 88, 109]]
const result = data.reduce((r, e) => {
const arr = e.sort().join(',');
r[arr] = (r[arr] || 0) + 1;
return r;
}, {});
console.log(result)

I am stuck on a quiz in Udacity for JavaScript on Multi-dimensional arrays.

The quiz is about a multidimensional array and wants us to use a nested for loop to cycle through each element and replace the values that are divisible by 2 with the strings 'even' and 'odd' for even and odd numbers respectively. I cannot print out the array with the numbers divisible by 2 on to the console with the added string.
I have tried using .slice() and .splice() also but that did not get me the desired results either. Below is my latest attempt.
Programming Quiz - Multi-dimensional Arrays:
Question: Use a nested for loop to take the numbers array below and replace all of the values that are divisible by 2 (even numbers) with the string "even" and all other numbers with the string "odd".
var numbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
My code:
//create a nested for loop to cycle through each row and column
for(let row = 0; row < numbers.length; row++){
for(let col = 0; col < numbers.length; col++){
//if the even numbers in each row and column are divisible by 2, replace those
//numbers with the word even
if(numbers % 2 === 0){
numbers[row] += 'even';
}
}
}
console.log(numbers);
You're real close! It comes down to how you're referencing the index in the 2-dimensional array. Iterating over it in a nested for loop requires first that we iterate through the rows, then the columns. In your example, you were actually iterating over the columns twice. In order to fix this, you would simply iterate over the row index of the numbers array in the nested for loop:
for(let row = 0; row < numbers.length; row++){
for(let col = 0; col < numbers[row].length; col++){
Then finally you will need to set the target index numbers[row][col] to even. In your example you used the addition assignment operator +=, which performs what is string concatenation. Instead of simply changing the index value to 'even', the += would actually add the word 'even' to the end of the number. So the index output would actually read something like 156even. Just drop the plus sign in order to reassign the index to even:
numbers[row][col] = 'even';
var numbers = [
[243, 12, 23, 12, 45, 45, 78, 66, 223, 3],
[34, 2, 1, 553, 23, 4, 66, 23, 4, 55],
[67, 56, 45, 553, 44, 55, 5, 428, 452, 3],
[12, 31, 55, 445, 79, 44, 674, 224, 4, 21],
[4, 2, 3, 52, 13, 51, 44, 1, 67, 5],
[5, 65, 4, 5, 5, 6, 5, 43, 23, 4424],
[74, 532, 6, 7, 35, 17, 89, 43, 43, 66],
[53, 6, 89, 10, 23, 52, 111, 44, 109, 80],
[67, 6, 53, 537, 2, 168, 16, 2, 1, 8],
[76, 7, 9, 6, 3, 73, 77, 100, 56, 100]
];
for(let row = 0; row < numbers.length; row++){
for(let col = 0; col < numbers[row].length; col++){ //iterate over the n-th index of numbers[]
if(numbers[row][col] % 2 === 0){ //numbers[row][col] points to the nested index
numbers[row][col] = 'even'; //set index to 'even'
}
}
}
console.log(numbers);
This video is a nice explanation of how iterating over 2d arrays works: https://www.youtube.com/watch?v=qdT1P2qmsmU
The video is using Java, but the idea is exactly the same.
Hope this helps! Multi-dimensional arrays are tough to wrap your brain around!

Highcharts age stacking

I want to make a stacked column chart with age intervals.
I have a two-dimensional array containing an age key and a value.
The idea is: every single age is stacked on top of each other within their interval, and thereby represent the accumulated number of people within the age interval in a single column.
I want to approach this solution with highcharts' stacked column chart example.
This is my code so far:
// Age stacked column chart.
var ageData = [
[0, 0], [1, 0], [2, 0], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 4], [9, 3],
[10, 24], [11, 33], [12, 31], [13, 21], [14, 61], [15, 42], [16, 43], [17, 87], [18, 64], [19, 120],
[20, 134], [21, 142], [22, 184], [23, 221], [24, 234], [25, 211], [26, 198], [27, 94], [28, 79], [29, 34],
[30, 24], [31, 27], [32, 32], [33, 21], [34, 4], [35, 7], [36, 11], [37, 5], [38, 3], [39, 6],
[40, 3], [41, 4], [42, 13], [43, 6], [44, 4], [45, 3], [46, 1], [47, 2], [48, 2], [49, 0],
[50, 0], [51, 34]]
$('#container_stackedColumn').highcharts({
chart: {
type: 'column'
},
title: {
text: 'User age distribution'
},
xAxis: {
categories: [
'0-12',
'13-18',
'19-23',
'24-28',
'29-35',
'36-42',
'43-50',
'>50'
]
},
yAxis: {
min: 0,
title: {
text: 'Number of users'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
}
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white',
style: {
textShadow: '0 0 3px black, 0 0 3px black'
}
}
}
},
series: [{ /* --- What to do here?! --- */ }]
});
So, with my two-dimensional array in consideration, how do I visualize this stacked column chart?
Any help is greatly appreciated. :)
You need to itearate on you arrays, compare with the limits which you have (categories) and push to correct series array.
See the parser (http://jsfiddle.net/W6AFY/3/) hope that it is correct.
Or another solution, using different series for each part of bar: http://jsfiddle.net/s9qHH/48/
var ranges = [12, 18, 23, 28, 35, 42, 50, 1000],
rangesLen = ranges.length,
series = [];
$.each(ranges, function(i, e){
series.push({ //create series' containers for legend
id: e+'index',
name: e
});
});
$.each(ageData, function(i, e){
var x = e[0],
y = e[1],
index = 0;
while(ranges[index] < x){ // get category index
index ++;
}
series.push({
showInLegend: false,
linkedTo: ranges[index] +'index', // link to container series
data: [ [index, y] ] //add point at specific category
})
});
Still, I think that's Sebastian's solution is faster.

Ordering of bidimensional array

Hello I have the following question:
I have a bidimensional array which look like this:
[[2, C, 22, 22, 8]
[2, C, 22, 22, 7]
[2, C, 22, 22, 10]
[1, R, 45, 45, 4]
[1, R, 45, 45, 3]
[1, R, 45, 45, 2]
[1, R, 45, 45, 1]
[1, R, 150, 100, 6]
[1, R, 150, 100, 5]
[1, C, 22, 22, 9]]
And I want to order the data first for column 1, then for column 2, then for column 3, then for column 4, then for column 5; all in descending order. The following is the result that I want to obtain:
[[2, C, 22, 22, 10]
[2, C, 22, 22, 8]
[2, C, 22, 22, 7]
[1, R, 150, 100, 6]
[1, R, 150, 100, 5]
[1, R, 45, 45, 4]
[1, R, 45, 45, 3]
[1, R, 45, 45, 2]
[1, R, 45, 45, 1]
[1, C, 22, 22, 9]]
I use ExtendScript which is an extended version of Javascript for Adobe.
How is this possible with Javascript?
Any suggestions would be much appreciated.
Thanks in advance
Had the numbers flipped. Updated with working example.
You can use Array.prototype.sort then loop through the options. (make sure to put quotes around your strings though!)
var x = [[2, "C", 22, 22, 8],
[2, "C", 22, 22, 7],
[2, "C", 22, 22, 10],
[1, "R", 45, 45, 4],
[1, "R", 45, 45, 3],
[1, "R", 45, 45, 2],
[1, "R", 45, 45, 1],
[1, "R", 150, 100, 6],
[1, "R", 150, 100, 5] ,
[1, "C", 22, 22, 9]];
x.sort(function(a,b){
for(var i=0; i<a.length; i++) {
if(a[i] > b[i]) {
return -1;
}
if(a[i] < b[i]) {
return 1;
}
}
return 0;
})
Note that I'm assuming that all arrays are the same length and that a simple < and > is sufficient for comparison. If your needs differ it should be trivial to adapt to that.
Working example: http://jsfiddle.net/zSHw9/

Categories