First of all I want to say, that I looked online for a possible or similar solution but did not find anything, might have been looking for the wrong keywords but please excuse my rather simple question.
However, currently I am trying to implement a java function that - on an input of an integer (e.g. 10) gives me every possible solutions on how to divide this by 2 or 1.
And I literally have no clue on how to even start, as there is no pattern to it or anything that I have done is relatable.
However this is an old exam from my professor, so there is a good and short solution I am just not seeing.
Thank you very much for your help and time :)
edit:
The method I am trying to imlement would look as follows:
static void possibilities (int i){ }
I would be able to give an integer (3 for example) and then as output (through Sys.out) all possibilities of all possible combinations of 1 and 2's, for example for 3:
(1,1,1)
(2,1)
(1,2)
Or for 2:
(1,1)
(2)
Here is what I think would work:
say the input is 5 so the very 1st combination will be: (1,1,1,1,1) right? now we add the first two number so it is (2,1,1,1) and store it in a string i.e. "2111" now we find all the permutations of this string (code 4 this is available), now we add the next 1,1 pair so it will look like (2,2,1) i.e "221" and again same process of permutations.
We keep repeating this till the string has just one '1' present or no '1' present, try checking for the number 4, same thing.
Related
I have this assignment where given a list of tuples where each tuple contains 2 Strings like this :
[ ("...","...") , ("...","...") , ("...","...") ... ]
I have to calculate the shortest path which will lead to an extreme-string.
An extreme-string is defined as a tuple of 2 strings where the first string is equal to the second string.
I know this might sound confusing so let me set an example.
Given :
The list [("0","100") , ("01","00") , ("110","11")]
With indices 0,1,2
The shortest path is : [2,1,2,0]
The extreme-string is equal to : "110011100"
Step by step explanation :
Starting with tuple of index 2 the initial string is : "110","11"
Appending tuple of index 1 next string is : "11001","1100"
Appending tuple of index 2 next string is : "11001110","110011"
Appending tuple of index 0 final string is : "110011100","110011100"
So say you begin with tuple ("X","Y") and then you pick tuple ("A","B") then result is ("XA","YB").
The way I approached this problem was using BFS which I already implemented and sounds right to me but there is an issue I am dealing with.
If the input is something like :
[("1","111")]
then the algorithm will never terminate as it will always be in the state "111..." - "111111111..." .
Checking for this specific input is not a good idea as there many inputs that can reproduce this result.
Having an upper bound for the iterations is also not a good idea because in some cases a finite result may actually exist after the iterations bound.
Any insight would be really useful.
Since its an assignment I can't really solve it for you, but I'll try to give tips:
BFS sounds great to me as well.
One thing that differentiates the BFS from, say, DFS is that you place the elements of level N into the queue (as opposed to stack). Since queue is FIFO, you'll process the elements of Level N before elements at the level of N + 1. So this algorithm will finish (although might occupy a lot of memory).
The interesting part is what exactly you put into the queue and how you organize the traversal algorithm. This is something that I feel you've already solved or at least you have a direction. So think about my previous paragraph and hopefully you'll come to the solution ;)
I'm extremely new to coding, and I need to solve a problem that I'm not sure what is the best approach. I'd appreciate enormously if anyone could point me towards a proper solution!
The problem is: I have a bunch of list of numbers (aprox. 2 thousand) each containing 6 digits, from 1 to 20, like this:
1 {1,13,5,16,4,19};
2 {2,5,9,15,22,8};
3 {14,23,1,13,6};
...
they never repeat within the same list.
I need to add new lists, that should be generated based on information from the 2k previous (I have it in an excel sheet). For example:
The next list (6 numbers, 1 to 20), need to match a given sum (and this sum will be the most frequent sum in my database);
They need to start with a number (for instance,1);
They must contain at least 3 odd numbers;
I have no idea what coding language would be best to use, I'm dedicating my time learning some Python and C#, and see if I can come up with a solution, but I'm really struggling to understand whether these languages are suitable for the problem or not.
Thanks a lot in advance to anyone willing to shed some light on my problem!
I have a problem in front of me that I can't grasp, even though I grasp the concept of backtracking well. I have been unable to find any information on this specific problem, and I am unaware if it goes by another name. The problem is to construct a backtracking algorithm that will print out the list of combinations of seating that N number of "presidents" can sit at a round table without sitting next to the same person twice. For example, you give it an input 8, it gives you the output,
1 2 3 4 5 6 7 8
1 3 5 2 6 8 4 7
1 4 2 7 5 8 3 6
Now I have constructed my own backtracking algorithms for simple games (checkers, n-queens, tic-tac-toe), however these that I have worked with have started with a blank canvas and didn't require this type of implementation. What I am asking for is a bit of guidance and insight, or links to more information to this problem as all that was given was about a page and a half, mostly talking about how the combination 1 2 3 4 is effectively the same as 1 4 3 2 in this particular problem. This needs to be done using a backtracking method and that's what is making me confused. If we were give free rein to find solutions I could easily do it. Any help is appreciated.
Thanks!
I'm thinking you'll have to add information about who sat next to who already to the backtracking algorithm state:
You start with, say 1. You seat 2 next to him, and record this "pairing" in the current partial solution. (Literally just an unordered pair of president IDs. An easy way of implementing an unordered pair of numbers is to have the constructor of that class store the smaller ID in the first element of the pair, and the greater ID in the second element.) So you have a partial solution 1 2 …, with the pairing 1-2.
When you reach to a complete solution, you add all its pairings to a "global" list of pairings. This will be used in future searches. I.e. if you get the solution 1 2 3 4 5 6 7 8, you save the pairings 1-2, 2-3, 3-4, 4-5, 5-6, 6-7, 7-8, 1-8.
Now, when doing a search, you have a partial solution x y z … – the last president seated so far is z, and the pairings in this partial solution are x-y and y-z. To seat the next president, you need to look at every president that's:
Not seated so far. (And thus not included in the partial solution's pairings.)
Not paired with z in the global pairings.
If no such president is available, you discard the partial solution and backtrack.
That said, I'm working off the top of my head here, so you'd be well advised to actually doublecheck if I missed any edge cases by using a straightforward brute force implementation and comparing the results.
A very rough pseudocode will look like this:
global flag // I know it is a bad practice but can't think of a better idea at this point
back (PresidentList, chairNumber, computedList)
if (PresidentList.size() == 0)
if (computedList doesn't violate any global distribution)
add computedList to globalList
add distirbution of computedList to global distirbution
set flag;
else return;
else if (flag is set and chairNumber > 2) return;
else if (flag is set and chairNumber = 2) un set flag; return;
else if (chairNumber == maxChairs-1)
// at this point there should be only one president in PresidentList
if (PresidentList.back() can sit with computedList.back() and computedList.front())
remove president from PreseidentList, add president to computedList
back(PresidenList, chairNumber+1,computedList)
else return;
else
for every president in PresidentList
if (president can sit with computedList.back())
remove president from PreseidentList, add president to computedList
back(PresidentList,chairNumber+1,computedList);
You always check if a president can sit near some other president only according to the global distribution variable.
In the main function just run a loop for PresidentList that will start back with that president being as the first one.
The idea of the flag is as follows: when you add a distribution and a list to the global list, back runs already a lot of branches which have the same prefix as the list you just added and hence are already bad. To close all those lists you force the back to stop branching and return to the point when it can set a new 2nd element in the list to compute i.e. get back to the loop if the last else branch of the function.
Ok guys, time to get creative.
I have a list of integers, and want to preserve the integers in the 5 green boxes below.
I thought about finding the 5 minimums, and then going left and right from each one until the next integer is a certain distance too far. However this may be difficult to do as the last to boxes have some scattered values that I would like to maintain.
So cliff notes,
have list of integers that i want to make 5 lists of integers from, each one of the 5 lists holding the values of one of the green boxes.
I am sorry if this question seems dumb, I was just wanting to see if other people had better ways of going about this then the one I mentioned above.
Edit:
Perhaps a Levenberg-Marquardt algorithm would help? If only I could understand math books xD. I swear math explanations are never written in English, but if someone just shows me the use, and how to, I get it and understand it. Then I can go read it out of a book again and just be confused all over again!
That could be a possible solution:
Let call your list of integer P[n].
define a new list of point, we call it MAX[n], calculated this way:
if ((MAX[n] < P[n]) || (n == 0))
MAX[n] = P[n];
else
MAX[n] = MAX[n-1];
in this way, MAX[n] list will be the green line above your graph:
FIRST SOLUTION:
to identify the points inside the green boxes, you just have to calculate the incremental ratio of your P list, and check when its absolute value is smaller than a threshold:
I = [P[n+1] - P[n]] / [T[n+1] - T[n]]
so a point is into the green box if I is below a threshold and P is below MAX:
if ((P[n] < MAX[n]) && (ABS(I) < DER_THRESHOLD))
// your point is inside the green box
from your graph, roughtly, you could put DER_THRESHOLD = 5.
SECOND SOLUTION:
calculate a new list of integers, called this time AVG[n] like that:
AVG[n] = (MAX[n] + P[n]) / 2
this will end in the red line (sorry for the ugly graph, did it by hand):
and now you can identify green boxes just checking if your P[n] is below AVG[n].
This solution could be even better using some kind of low filtering in AVG calculation.
Try finding the least squares fit line through the points, and then look at whether the points are above or below the line. That will split your points into the five green boxes below the line, and the six infrared boxes above the line. It will also include the points inbetween the boxes, but those will be easy to exclude because of the big jump before or after the point that should be excluded. But it's also looking like you may have trouble at the top right of this data set.
I've been working with linked lists and have been trying to split them. If i had methods to add nodes and to print the list such as A to add and p to print and s to split. I want to split the linked list once or twice or more times at a given index.
For example if I had an input like:
A 1 2 3 4 5 6 7 8
s 2 4 6
p
I'd want my output to be:
1 2
I know how to join split lists but I really want to know how to split them like this, any help would be much appreciated.
¿Have you tried API class LinkedList?
I suggest that you simplify your problem as much as you can by splitting the list at a single element. You probably need a method called split(). What parameters does this method need? What should its return type be? What action does the method do? Are there any side-effects?
In order to create a solution, you need to clearly understand the problem to begin with. Answering these questions can hopefully get you started in the right direction. Let me know what you come up with and we can go from there.