Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
Given any array of numbers, similar in nature to the following:
[48, 60, 64, 67, 72]
I want to be able to evaluate this(or any) series of numbers and look for musically meaningful patterns, and I'm not sure how to start going about it.
For example, if I subtract 48 from every number I would get this:
[0, 12, 16, 19, 24]
...one will notice that the 1st, 2nd and 5th numbers are 12 apart from each other, which is a meaningful pattern in music, as 12 steps is one octave. This is one of the things I would want to be able to figure out by just looking at the numbers.
Now, if I take the middle three numbers and subtract 12 from each one I get:
[0, 4, 7]
...which is the numerical pattern of steps on a keyboard that would create a Major Triad, which is another example of something I would want to be able to evaluate from a series of numbers.
So, in the end, the original series of numbers:
[48, 60, 64, 67, 72]
...represents the following musical notes:
[`C2`, `C3`, `E3`, `G3`, `C4`]
...which is a C Major Triad C3, E3, G3 with the root doubled down one octave C2 and doubled up one octave C4 as well.
What I want to do is write some code that takes in any series of midi message numbers, like the example above, evaluates them, then tells the user what musical entity they have played, including inversions, doublings, other possible interpretations, etc...
There would be a database/dictionary/object/array/data-thing of some type that contains many different patterns, then the code would compare the incoming numbers to this set of patterns to find the correct one(s)...
Not looking for code answers as much as an idea as to how to go about even getting started with such a thing in javascript, as this would be for the web...
I just don't really have any idea how to start going about this, but any thoughts or examples or resources that could point me in the right direction would be most appreciated.
Thanks! XD
Related
This question already has answers here:
How does '&' work in relation to odd and even? In JS
(3 answers)
Closed last month.
I'm working through a problem on CodeSignal and trying to understand some of the solutions that other people have submitted. One of the solutions was as follows, and I don't understand what the ampersand is doing.
(a) => a.reduce((p,v,i) => (p[i&1]+=v,p), [0,0])
The problem is:
Several people are standing in a row and need to be divided into two teams. The first person goes into team 1, the second goes into team 2, the third goes into team 1 again, the fourth into team 2, and so on.
You are given an array of positive integers - the weights of the people. Return an array of two integers, where the first element is the total weight of team 1, and the second element is the total weight of team 2 after the division is complete.
Example
For a = [50, 60, 60, 45, 70], the output should be
solution(a) = [180, 105].
In this solution, the & operator is used to perform a bitwise AND operation. In JavaScript, the & operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
In the given solution, the & operator is used to determine whether the index i of the current element in the array is even or odd. If i is even, the result of i & 1 will be 0. If i is odd, the result of i & 1 will be 1.
Let's say I have a space ranging from 0-100 and along this space certain areas are occupied. Say 20-30 and 50-70.
Now let's say someone comes along and wants fill spaces 80-90. How would I best check if those spaces are unoccupied?
I could store the free space in nested arrays (which is what I'm currently doing):
[[0, 19], [31, 49], [71, 100]
and the loop an if statement checking if 80-90 will fit in any of these free spaces. This seems very inefficient. Worst case I could be talking thousands of items to loop through.
I could also use an array 100 items long with info on if each number is free:
(100) 0: "free" 1: "free" 2: "free" 3: "free" 4: "free" ...
This way I would not have to loop through multiple items but could just run the if statement on items 80-90 and see if they return free. On the other hand this would mean populating and storing an array with billions (100 is just an example, my use case involves around 1 billion)of items just to find free space.
How would one go about doing this in a better way? Or should I be using these methods I described?
Edit: I am using 0-100 as an example. In my use case, the number is actually around 1 billion.
You could take an array of free ranges and perform a binary search.
This would result in a max iteration of 20 by having a million of items.
2 ** 20 === 1048576
This is a simpler version of knapsack, which I am having trouble wrapping my head around.
In my version I don't care how valuable the items are. I just want to get as close to the weight capacity as possible, and order doesn't matter because I'm doing it multiple times and shuffling in between.
So to be clear:
I have an array of values like: weights = [{44, 52, 100, 33, 33, 22, 25, 4, 6, 77, 88, 45}] and a capacity of, for example:capacity: 204
I want the closest combination of array values to that capacity number without repeating any, I'm not super great at math, and the wikipedia article has completely lost me.
Can someone explain how to get this?
Naive approach: cycle through all subsets of N numbers, and check the sum of weights. Running time is O(2^N*N)
You can try dynamic programming.
The problem can be divided into 2 subproblems, to check whether the sum of set is equal to or less than the capacity.
1) Include the current element in subset, and recur for the remaining items with remaining sum.
2) Exclude the current element from the subset, recur remaining items.
The base case of the recursion would be when no items are left. Finally, we output the items included in the subset.
Running time is O(n*capacity) in O(n)
I'm struggling with increasing the speed of my pseudo branch & bound algo & thought a trie structure might be a good fit, but I'm stuck.
The problem (simplified & isolated best I can):
I've got 9 nodes & 3 vehicles. Each vehicle must visit 3 unique nodes. So, I created every possible trip (9 choose 3 = 84) & stuck it in an array. Now, I want to find every combination.
For example, trip 1 could be 111000000. trip 2 would be 000110001 and trip 3 would be 000001110. (84^3 = 592704 combinations).
To find out if they match, I just do a bitwise & and accept the trip combination if the value is 0.
I can't use nested loops since the # of vehicles may change, so I keep track of combinations with a counter that ticks up depth-wise like an odometer (e.g. 0,0,82, 0,0,83, 0,1,0).
I reduce combinations by keeping the following digit greater than the one that just increased (e.g. 11,83,83 goes to 12,13,14 because anything less than a 12 in the 2nd column would be a repeat like 12,1,1 is a duplicate of 1,1,12).
I also perform the bitwise AND check at each change (e.g. if (val[12] & val[13]) > 0 don't bother checking the 71 possibilities in the 3rd column, because the 12 and 13 invalidate the route. This reduces the combinations to 24720.
Since I'm doing depth-first, it's really space efficient (no queue to save), but computationally expensive. What I'd like to do is use a width-first approach to create subsets and minimize the search space. For example, assume the counter was at 11,19,20. Currently, it would check 20 - 83 in the 3rd column before incrementing the 19 to a 20. I would like to compute the AND for 19 - 83 in the 2nd column before moving on. In doing so, I would know all the values that don't work with the 11 in the first column and could use that subarray for the 3rd column (e.g. if (val[11] & val[45]) > 0, don't bother checking 11 & 19 & 45, rather use an array that excludes 45 from the 3rd column.
My idea is to create a trie, where each key is the result of the AND operation with an end key that would be the subarray.
For example:
doc = {
1: {
5: {
end: [8,9,10]
},
7: {
end: [11,14,42]
},
end: [81, 13, 42]
}
}
My problem is I can't for the life of me figure out how to iterate over my data to grow the trie. Or maybe there's a better approach? Any advice or code snippets would be great, and thanks for reading through the wall of text.
So I've managed to make a simple and easy quiz with Javascript, mainly with an array with both questions and answers (as of now, only numbers).
['How many moons does Earth have?', 1],
['How many moons does the saturn have?', 31],
['How many moons does venus have?',0]
I also have a function that adds to the score if the answer is right.
function asQuestion(question) {
var answer = prompt(question[0], '');
if (answer == question[1]) {
score++;
}
For now, I only have prompts, which is a bit tacky and not quite what I wanted. What I'm looking for is, how can I store those questions with multiple answers (only one of which is correct)? And is it possible to, instead of using prompts, use a <p>, literal or a form, to write these questions, hit button, fadeOut the answered question, and give a new one?
(Example: Question 1: Radio1(What is the capitol of Finland) Radio2(What is the biggest ocean). I check radio 1, and hit 'Next Question'.
Just looking for some advice, tips and guidance as I am quite new to Javascript and jQuery. I really appreciate all the help I can get. =)
Your question is a bit too broad for Stack Overflow, but here are a few guidelines.
Yes, all you said is possible! You can start from something as simple as:
['How many moons does Earth have?', 1, 0, 2, 3]
knowing that the first answer is correct and shuffling them for your presentation.
You can even make a separate button for each answer (improving the UX by getting rid of a redundant click) but beware of accidental clicks in that case. It's all up to you.
Here is a super-simple quiz I made for you: http://jsfiddle.net/rTv97/ There are millions of ways to improve it, of course. But it should get you started.
Note the system I used:
['How many moons does the saturn have?', 1, 31, 35, 29, 48]
\ / ^ \ /
text correct answers
It goes: question text, correct answer index (one-based), answers.
So, number 1 here means the the first of the 4 answers (31, 35, 29, 48) is correct.