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.
Related
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!
Is there an equivalent to python's product and combination functions?
Or in other words, given a set of Integers, and REPEAT number of times to repeat,
is there a way to create a list of lists or array of arrays or something of this sort that contains all the ways to choose REPEAT objects from the set with or without choosing the same element twice.
The main issue is that the number of repetition times is not known during compilation time.
Example of combination (with choosing twice):
input [1,2,3,4,5] 3
output [[1,1,1], [1,1,2] ... ]
Example of product (without choosing twice):
input [1,2,3,4,5] 3
output [[1,2,3], [1,2,4], [1,2,5] ... ]
Posting #RC's comment as an answer for others to find this library.
Perhaps there is something a more standard way or a simple code to do it in java (which people will be able to copy and change without importing a package just for it).
But anyway, this seems like a good library to do just that.
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.
Is there a faster way to check if an item in a list is greater than, less than, equal to a certain number?
Or you just have to loop through it? Im just curious if there are pre-built functions for this.
Example:
List contains 5, 5, 10, 15, 15, 20.
I want to check if how many items are actually >= 5. So the answer is 6. If I want to check >= 15, the answer would be 3.
step 1 : sort the list
step 2 : find the index for desired element
step 3 : print length-index
I don't see any such methods in the documentation, so I would say NO. You have to iterate through the List. If it is sorted, you can do binary search for faster results.
You need to loop to check the condition for each element.
Are you trying to sort? If you are dealing with arrays you can use Arrays.sort() else if you are dealing with collections you can use Collections.sort()
No there is no pre-built function afaik. If your list items are not order critical(i.e. you are not making any priority list or LIFO/FIFO), you can improve the searching through sorting list before finding the element.
You can sort the list and then you'll have to compare only its first and the last elements with the number.
That is why I am asking a question if there is such method that currently exists within the Collection.
There is no such method in the standard collection APIs.
Write a loop. It should be quicker code and test a 5 line method to do this than scouring the internet for a 3rd party library. And your code will most likely be faster ... and certainly no slower.
Just do it. (I'd write the code for you myself, but it sounds like you need the practice ...)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android (method for checking an arrays for repeated numbers?)
I've just asked a question and got a few answers and i was very happy to, but there were very complicated answers, I'm quite new to android so can some one maybe give me some example code or some think explained not the complicated. I've tried there code and tried to make sense of it but i cant.
here is the question....
could any one help me. i am making an app, and in the java, numbers are send to a int array and i need to check if any of the numbers in the array repeated and if there are to call a method or something like that. Is there a method to check this or something similar? or would i have to do it using loops and if statements, which i have tried but is getting a bit long and confusing. Any advice would be great, thanks.
int test[] = {0,0,0,0,0,0,0}; (The Array)
(A method to check if any of the arrays numbers are repeated)
First don't make double topic.
Second you are searching for a Java answer not related to Android.
I think that maybe it's better if you first learn java (or other language like).
I would store the items in a Set if you do not want them to repeat. If add returns false then you have a repeating number
Set uniqueItems = new HashSet();
for(int i=0;i<test.length;i++)
if(!uniqueItems.add(test[a]))
System.out.println("The item is already in the set");
First, sort the array. Then search through the array comparing each node to the node on either of it's sides. Or you could store the data in a Set which cannot have duplicates.
Arrays.asList(test).contains(valueYouWantCheck).
If you want to find out for each and every value in test array, Yes I think you need to loop the array.