Build a chain out of integer pairs - javascript

I have a set of integers pairs:
[4, 3], [0, 1], [0, 4], [2, 3], [2, 1]
The task is to chain it to get the output:
[0, 1], [1, 2], [2, 3], [3, 4], [4, 0]
Are there any existed optimised algorithms to do it?

Make a graph where numbers are vertices and pairs are edges.
Check whether Eulerian path exists (all vertex degrees are even (perhaps except for two vertices))
If yes, build this path

Related

Dissolve GeoJSON polygons with Turf using Leaflet

I have one GeoJSON with about 100 polygons. I need a way to filter several polygons and dissolve them to put on a Leaflet map. Instead of creating a new GeoJSON with the dissolve polygons I want to try to use Turf.js, try to follow their examples but result are blank and no errors. What is missing or is it a wrong approach?
var poly = new L.layerGroup();
$.getJSON("/geodata/polygons.geojson", function(json) {
L.geoJson(json, {
turf: function(feature, latlng){
var select = (feature.properties.id >= 65 && feature.properties.id <= 68);
features = turf.featureCollection([select], {combine: 'yes'});
var dissolved = turf.dissolve(features, {propertyName: 'combine'}).addTo(poly);
}
});
});
Example from turf website for dissolve:
var features = turf.featureCollection([
turf.polygon([[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]], {combine: 'yes'}),
turf.polygon([[[0, -1], [0, 0], [1, 0], [1, -1], [0,-1]]], {combine: 'yes'}),
turf.polygon([[[1,-1],[1, 0], [2, 0], [2, -1], [1, -1]]], {combine: 'no'}),
]);
var dissolved = turf.dissolve(features, {propertyName: 'combine'});

Subset Sum Variation - subset has X number of items

I'm looking for an algorithm that will allow me to quickly access (not necessarily print) every X item subset of a 300 item set that, when the values of the items in the subset are added up, equals Y. Repetition is allowed and order is important. All values of the 300 item set are positive.
So for example,
300 item set: [1.5, 1.34, 3, .25, 2.333, 1.75, .125, .675, 2, 4, .75, ....]
X = 5
Y = 6
Algorithm generates:
[2, 2, 1.5, .25, .25]
[2, 2, .25, 1.5, .25]
[2, 1.5, 2, .25, .25]
[1.5, 2, 2, .25, .25]
[2, 1.75, .5, .5, 1.25]
[2, 1.75, .5, 1.25, .5]
[2, 1.75, 1.25, .5, .5]
[2, 1.25, .5, .5, 1.75]
[1.25, 2, .5, .5, 1.75]
[.5, 2, .5, 1.25, 1.75]
[3, 1, 1, .5, .5]
[1, 3, 1, .5, .5]
[1, 1, 3, .5, .5]
[1, 1, .5, 3, .5]
[1, 1, .5, .5, 3]
And so on....
[2, 2, 1.5, .25, .25] is allowed and
[2, 1.75, .5, .5, 1.25] is not the same thing as [1.25, .5, .5, 1.75, 2].
I realize that this is a variation of the Subset Sum problem but I can't seem to find any helpful examples of this variation anywhere online. Right now my current solution implements nested loops (the number of nested loops is determined by the value of X) That works fine when X is small but quickly get very slow when X starts going up. Any input would be appreciated!
An approach is to collect subsets and check if the length of the subset and sum fits.
If necessary, you could permutate the subsets.
function subsetSum(array, sum, items) {
function iter(taken, index, subsum) {
if (taken.length === items && subsum === sum) return result.push(taken);
if (taken.length === items || subsum === sum || index === array.length) return;
iter([...taken, array[index]], index, subsum + array[index]);
iter(taken, index + 1, subsum);
}
var result = [];
iter([], 0, 0);
return result;
}
var result = subsetSum([1.5, 1.34, 3, .25, 2.333, 1.75, .125, .675, 2, 4, .75], 6, 5);
console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to play a specific frequency with Javascript?

I want a function that works like this:
playSound(345, 1000)
Which would play a tone of 345 hz for 1000 milliseconds. What is the simplest way to achieve this in JavaScript? I don't mind if it uses a sample (maybe of a sin wave, or piano), or uses the computer's hardware to generate the sound.
As already pointed out in the comments, the way to do it is through the OscillatorNode.
// create web audio api context
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
function playNote(frequency, duration) {
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.value = frequency; // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.start();
setTimeout(
function() {
oscillator.stop();
playMelody();
}, duration);
}
function playMelody() {
if (notes.length > 0) {
note = notes.pop();
playNote(note[0], 1000 * 256 / (note[1] * tempo));
}
}
notes = [
[659, 4],
[659, 4],
[659, 4],
[523, 8],
[0, 16],
[783, 16],
[659, 4],
[523, 8],
[0, 16],
[783, 16],
[659, 4],
[0, 4],
[987, 4],
[987, 4],
[987, 4],
[1046, 8],
[0, 16],
[783, 16],
[622, 4],
[523, 8],
[0, 16],
[783, 16],
[659, 4]
];
notes.reverse();
tempo = 100;
playMelody();
There is a library called simpleTones.js that greatly simplifies the Web Audio API to do exactly what you are attempting.
Once the library is included in your project, playing a timed frequency is as easy as calling
playTone(345, sine, 1)
345 being the frequency in Hz, sine being the wave pattern(there are other wave pattern options as well) and "1" being one second, or 1000 milliseconds.
You can download the library and read the documentation here: https://github.com/escottalexander/simpleTones.js
Best of luck on your project.

Jquery Flot get ticks data for tooltip

I am using the jquery flot to show charts with tooltip. I am using ticks to get custom xaxis data. SO
var data = [ [0, 3], [4, 8], [8, 5], [9, 13] ];
var ticksData = [ [0, "A"], [4, "B"], [8, "C"], [9, "D"] ];
......
xaxis: {
ticks: ticksData,
tickLength: 0
},
But when I want to show the tooltip, I got the none ticks data.
$("#placeholder").bind("plothover", function(event, pos, item) {
var x = item.datapoint[0], y = item.datapoint[1];
//x and y are 4,8 instead of B,8
Complete sample http://jsfiddle.net/z0u6rqhe/
Is there any way to get to fix tool tip to show ticks data, or I should loop through ticksData and re find the value
please check this link, i hope this will helpful for you.
jsfiddle.net/z0u6rqhe/5/
code:
var x = item.series.xaxis.ticks[item.dataIndex];

Comprehensions in Python and Javascript are only very basic?

Looking at comprehensions in Python and Javascript, so far I can't see some of the main features that I consider most powerful in comprehensions in languages like Haskell.
Do they allow things like multiple generators? Or are they just a basic map-filter form?
If they don't allow multiple generators, I find them quite disappointing - why have such things been left out?
Python allows multiple generators:
>>> [(x,y,x*y) for x in range(1,5) for y in range(1,5)]
[(1, 1, 1), (1, 2, 2), (1, 3, 3), (1, 4, 4),
(2, 1, 2), (2, 2, 4), (2, 3, 6), (2, 4, 8),
(3, 1, 3), (3, 2, 6), (3, 3, 9), (3, 4, 12),
(4, 1, 4), (4, 2, 8), (4, 3, 12), (4, 4, 16)]
And also restrictions:
>>> [(x,y,x*y) for x in range(1,5) for y in range(1,5) if x*y > 8]
[(3, 3, 9), (3, 4, 12), (4, 3, 12), (4, 4, 16)]
Update: Javascript's syntax is similar (results from using the javascript shell on firefox):
var nums = [1, 2, 3, 21, 22, 30];
var s = eval('[[i,j] for each (i in nums) for each (j in [3,4]) if (i%2 == 0)]');
s.toSource();
[[2, 3], [2, 4], [22, 3], [22, 4], [30, 3], [30, 4]]
(For some reason, something about the context stuff is evaluated in in the javascript shell requires the eval indirection to have list comprehensions work. Javascript inside a <script> tag doesn't require that, of course)
Yes, you can have multiple iterables in a Python list comprehension:
>>> [(x,y) for x in range(2) for y in range(3)]
[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
Add an if statement as well...
>>> [(x,y) for x in range(5) for y in range(6) if x % 3 == 0 and y % 2 == 0]
[(0, 0), (0, 2), (0, 4), (3, 0), (3, 2), (3, 4)]
Comprehensions is very powerful in Haskell to a large extent because Haskell is functional, so it makes extremely much sense for them to be. Python is not functional so it makes less sense.
You can make a lot of complex things with comprehensions in Python but it quickly becomes hard to read, thereby defeating the whole purpose (meaning you should do it some other way).
However, as pointed out here, python does allow multiple generators in comprehensions.

Categories

Resources