<br /> <br /> sorry for java i tried below:- <br /> <br />Stephan van Hulst wrote:CarefullyChooseOneForum <-- read this page <br /> <br /> It appears you want to write Python, not Java.
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:My code snippets work for me, without a hiccup, and my code is giving correct outcomes. What compile errors do you get?
I added some println's to your code, so that you can follow the development of your given array, so that with some luck you can see what is going wrong with your code, giving you a clue where to look for it.
Your code needs much more explanation for me to understand. What is the idea behind the pairs-array? Why do things need to be reset after recursion? What is the meaning of this cryptic line:
A problem I have is that I cannot run your code, because I do not understand how to form the pairs array from the given array.
How do you debug your code?
Piet Souris wrote:I didn't mention the required imports. Normally, an IDE will do the imports for you at the click of a mousebutton. Here are the imports:
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:hi John,
I can't run python code, so I stick to java.
Indeed, your code does not work correctly. I do not know what causes this, but then again, your code is pretty complex to follow. So, to start, I added some print commands to your code, so at least you can follow the development of the array to be swapped. I see no clear development, so you need to add more prints, and use a singlestepper to see if your logic works. Here is that code:
Here is a version that is somewhat simpler than yours. It does not use recursion, but the idea is the same. I have an array, next I make a map (dictionary in python) with keys the values of the array and values the indices of the elements in that array.
I have also a method that, given an index and the array value, determines what the next element should be. For instance, if arr[0] = 4, then we know that arr[1] must be 5, and if arr[0] = 5, then arr[1] must be 4. If arr[1] = 12, then I look up the index of 5, swap the elements 5 and 12, and also adjust the indices-map. Maybe it is a help to spot the bug in your code. Succes!
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:Lets leave my code for a while.
I did have a look at your original java code. I said I couldn't run it, because I did not understand how you created the necessary 'pairs' array. You had this example in 'main()' in your code:
I really have no clue why your pairs-array was like that. I just changed that to this array:
int[] pairs = {1, 0, 3, 2, 5, 4}
since that is according to your description. The pairs-to-be are (0, 1), (2, 3) and (4, 5). So, according to your description, pairs[0] must be 1, pairs[1] must be 0, pairs[2] must be 3, et cetera. As you see, I left out a starting zero. With this, and some extra output statements, I ran your code again, and got this outcome:
input array: [0, 3, 1, 2, 0, 5, 4]
************ input for minSwapUtils *************
array: [0, 3, 1, 2, 0, 5, 4]
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 1 n = 6
swapping done, Array is now:
[0, 3, 2, 1, 0, 5, 4]
going to recurse
************ input for minSwapUtils *************
array: [0, 3, 2, 1, 0, 5, 4]
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 3 n = 6
No swapping needed, going to recurse
************ input for minSwapUtils *************
array: [0, 3, 2, 1, 0, 5, 4]
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 5 n = 6
No swapping needed, going to recurse
************ input for minSwapUtils *************
array: [0, 3, 2, 1, 0, 5, 4]
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 7 n = 6
finished
************ input for minSwapUtils *************
array: [0, 0, 1, 2, 3, 5, 4] // where is this coming from?
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 3 n = 6
No swapping needed, going to recurse
************ input for minSwapUtils *************
array: [0, 0, 1, 2, 3, 5, 4]
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 5 n = 6
No swapping needed, going to recurse
************ input for minSwapUtils *************
array: [0, 0, 1, 2, 3, 5, 4]
pairs: [1, 0, 3, 2, 5, 4]
index: [1, 0, 3, 2, 5, 4]
i = 7 n = 6
finished
Min swaps required is 1
As you see, it seems to work correctly, albeit that some extra work is done. So, have a look why this extra work is being done.
The disadvantage of your code is that you must first create (manually) this pairs[] array, before you can call the method 'minSwaps'. The pairs can be determined automatically. I gave a description how I did that in one of my posts. For instance, the pairs are (0, 1), (2, 3) et cetera. So, if you have a pair that starts with, say 3, then you know the next number of that pair must be 2. I used for this the method:
My advice is that you use this method instead of the pairs array, or that you create the pairs-array with the help of this method.
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:hi John,
that is because each time you run the code a new array is formed, with a different permutation of the numbers 0 - 19. That's why you get a different number of required swaps (the first line of the output is the starting array).
The creation of that array is done like this:
In general, if you have N pairs, then the maximunm number of swaps will be N - 1, since after N - 1 swaps, the last pair is okay (since there are no more pairs to swap with). And sometimes a pair is already oke, so no swap for that pair is needed.
And this topic was, as far as I am concerned, about getting you code up and running, and not to meet the requirements of the exercise. However, to achieve that is easy. Remove all the println's from the solve method (or from your swapUtils methods), and have this structure:
There are three kinds of actuaries: those who can count, and those who can't.
There are three kinds of actuaries: those who can count, and those who can't.
John Ken wrote:Thanks,But after compiling when i am executing it is not showing any result.please see attached screen shot which displays that command prompt stopped when i ran command java JohnKen4 to execute it after compiling this code.
I hope if it runs successfully then i would be able to see desired outputs.
also to convert this code in Python do we have any ready made online tool which could quickly convert it in Python
There are three kinds of actuaries: those who can count, and those who can't.
This tiny ad is suggesting that maybe she should go play in traffic.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|