Castle Paradox Forum Index Castle Paradox

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Gamelist   Review List   Song List   All Journals   Site Stats   Search Gamelist   IRC Chat Room

Java help? =P

 
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP!
View previous topic :: View next topic  
Author Message
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Wed Apr 07, 2010 7:01 pm    Post subject: Java help? =P Reply with quote

Okay, I'm being cheeky, but if anyone does know the slightest bit of coding, they might be able to help.

You see I've got a slight problem. I've produced a piece of code and I think I may need to condense it somewhat. Anyway.

Code:
if (numeral[0] != numeral[1]
         && numeral[0] != numeral[2]
         && numeral[0] != numeral[3]
         && numeral[0] != numeral[4]
         && numeral[0] != numeral[5]
         && numeral[0] != numeral[6]
         && numeral[0] != numeral[7]
         && numeral[0] != numeral[8]
         && numeral[0] != numeral[9]
         && numeral[0] != numeral[10]
         && numeral[0] != numeral[11]
         && numeral[0] != numeral[12])
         {   
            System.arraycopy(numeral, 0, finalNum, 0, 1);
            System.out.println([b]new[/b] String(finalNum));
         }


This is supposed to search the array for duplicates (yes I have multiple if statements for each position). I just want to know if anyone has an idea of how to condense this to less than five lines >_>.

Edit

I've highlighted a small problem area as well, anyone know why it's not accepting?
_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
Moogle1
Scourge of the Seas
Halloween 2006 Creativity Winner
Halloween 2006 Creativity Winner



Joined: 15 Jul 2004
Posts: 3377
Location: Seattle, WA

PostPosted: Wed Apr 07, 2010 8:47 pm    Post subject: Reply with quote

Use a for loop. I think Java also has an array search function. The way you are doing it is way too much typing.
_________________
Back to top
View user's profile Send private message Visit poster's website AIM Address
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Thu Apr 08, 2010 8:10 am    Post subject: Reply with quote

I don't know much java, but here is how I would remove duplicates from an array in pseudocode.

Code:

array original[filled with data that might have duplicates]
array cleaned[empty]

var found_dup
for i in original {
  found_dup = false
  for j in original {
    if original[i] = original[j] then found_dup = true
  }
  if found_dup == false then cleaned.append(original[i])
}



There are probably more efficient ways of doing that, but I think that method is reasonable clear.

Sorry I didn't write the example in an actual real language :)
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Thu Apr 08, 2010 8:32 am    Post subject: Reply with quote

Damn right too much typing....but at least my method works. (Copy and paste and the replace function has never had so much usage. )

This is what I'm working with now, just trying to condense before I port to Haskell.


Don't worry about it not being in pseudo code. Unfortunately, I'm not allowed to use internal functions. (Tedious...)



Code:
if (numeral[0] != -1) {
         System.arraycopy(numeral, 0, finalNum, 0, 1); //Copies original number.
         
         if (numeral[0] == numeral[1]) {      //Get rid of extra num
            numeral[1] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[2]) {      //Get rid of extra num
            numeral[2] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[3]) {      //Get rid of extra num
            numeral[3] = -1;
         } //and if not true...
         if (numeral[0] == numeral[4]) {      //Get rid of extra num
            numeral[4] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[5]) {      //Get rid of extra num
            numeral[5] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[6]) {      //Get rid of extra num
            numeral[6] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[7]) {      //Get rid of extra num
            numeral[7] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[8]) {      //Get rid of extra num
            numeral[8] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[9]) {      //Get rid of extra num
            numeral[9] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[10]) {      //Get rid of extra num
            numeral[10] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[11]) {      //Get rid of extra num
            numeral[11] = -1;
         } //and if not true...
         
         if (numeral[0] == numeral[12]) {      //Get rid of extra num
            numeral[12] = -1;
         } //END check. No more duplicates to strip.
      }

_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Thu Apr 08, 2010 8:47 am    Post subject: Reply with quote

Ravenshade wrote:

Don't worry about it not being in pseudo code. Unfortunately, I'm not allowed to use internal functions. (Tedious...)


What? You can't use internal functions like "for"?
That is absolutely insane. Why can't you use internal functions?



This question caused me to research what was the best way to remove duplicates from a list in python. turns out it is:

Code:

cleaned = list(set(original))


Where "original" is the list that might have duplicates, and "cleaned" is the resulting list with the duplicates removed.

...of course that method assumes you don't care if the list of gets re-sorted.

EDIT: a quick search of the Haskell documentation suggests that the best way to remove duplicates from a list is the "nub" command.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Thu Apr 08, 2010 8:56 am    Post subject: Reply with quote

Code:
/*Documentation
  a) Given a list of integers, removes all the duplicates from the list. For example, the list:

   17   6   45   2   3   17   6   9   8   6    1   4   12
   after removing the duplicates becomes:

   17   6    45   2   3   9   8   1   4   12
 
 This program solves the problem for all positive integers.
 
null = -1
    */
   
class problem1b {
   public static void main(String[] args)
   {
      //Declare Array
      int[] numeral = { 17, 6, 45, 2, 3, 17, 6, 9, 8, 6, 1, 4, 12};
      int[] finalNum = new int[13];
      
      //Check Array
      for(int a = 0; a < numeral.length; a++){ //Search array
         //CHECK pos 1
      
         if (numeral[a] != -1) {      //If array hasn't already been cleared.
            System.arraycopy(numeral, 0, finalNum, 0, 1); //Copies original number. 
            for (int b = 1; b < numeral.length; b++){
               if (numeral[a] == numeral[b]) {
                  numeral[b] = -1;
               } //If search not true, repeat for next
            }
         }//repeat for next number in the array.
      
      }
      
      //Print array
      for(int i = 0; i < finalNum.length; i++){
         if (finalNum[i] != 0) {
            System.out.println(finalNum[i]);
         }
      }
   }
}


Tried condensing.... not working out for me. >_>

I'm allowed to use 'for' 'while' etc, but anything further than that such as a function in java that removes duplicates automatically isn't allowed.
_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Thu Apr 08, 2010 2:02 pm    Post subject: Reply with quote

You are getting pretty close.

I don't actually see any reason to be setting values in numeral[] to -1 when you find a duplicate.

And I think you are copying values into finalNum[] in the wrong place. You should be doing it when numeral[a] != numeral[b]
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Thu Apr 08, 2010 3:15 pm    Post subject: Reply with quote

Since I can't use Null and I'm including integers of the set N-1, meaning the only number I can choose is -1 (or any number below that) to be sure of a valid value that won't be entered by a user.

Secondly, you would think that wouldn't you. But I actually get the opposite values I want if I put them that side of the equations. I want the first number and then strip out any duplicates after that.

...(that and I don't have numeral[a]!=numeral[b])
_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Tue Apr 13, 2010 5:55 am    Post subject: Reply with quote

okay, I've stripped down my code, changed the loops to whiles. (For loops while essentially the same, are harder to read and I need the while because I potentially have to add more numbers...sigh)

Code:
/*Documentation
  a) Given a list of integers, removes all the duplicates from the list. For example, the list:

   17   6   45   2   3   17   6   9   8   6    1   4   12
   after removing the duplicates becomes:

   17   6    45   2   3   9   8   1   4   12
 
 This program solves the problem for all positive integers.
 
null = -1
    */
   
class problem1c {

   public static void main(String[] args) {
   
      //initialise while -1 loop.
      int a = 0;
      int b = 1;
      int i = 1; //start while loop at 1.
      
      //Declare the initial array
      int[] numeral = { 17, 6, 45, 2, 3, 17, 6, 9, 8, 6, 1, 4, 12};
      int[] finalNum = new int[13]; //define as empty to be filled.
      
      //check the array.
      while (numeral[a] < (numeral.length)) {
         if (numeral[a] != -1) { //if not equal to minus 1, continue, else increment.
            while (numeral[b] <= (numeral.length)) {
                           //What about numeral B? what if it already is -1?
               if (numeral[a] == numeral[b]) { //Check if numeral a and subsequent are similar
                  numeral[b] = -1;    //if TRUE then erase secondary number.
               }
                           //else
               b++;            //Increment b to check
                           //the next number in he sequence
            }
               System.arraycopy(numeral, a, finalNum, a, 1); //copy over
               a++;
               //increment a for the next number in the sequence
         } else {
         
         a++;
         }
         
      }
      
      while (i < finalNum.length) {
      
            //System.out.println(finalNum[i]);
            System.out.println(finalNum[i]);
         
         
         i++; //increment while loop to end!
      }
   
   }

}


my results.... 0 0 0 0 0 0 0 0 0 0 0 0. Now...discounting the fact that there should in theory be a thirteenth digit.... I can't for the life of me figure out why I'm getting 0's.

I can only assume that System.arraycopy isn't accepting the variables.

I've left in all the comments, plus some of my thoughts in the program.


Edit...

Found the error. It's me defining the variables. For some reason nothing wants to tell me how to look at a position in the array.
_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
Bob the Hamster
OHRRPGCE Developer




Joined: 22 Feb 2003
Posts: 2526
Location: Hamster Republic (Southern California Enclave)

PostPosted: Tue Apr 13, 2010 7:18 am    Post subject: Reply with quote

I don't know much about Java, but I am wondering if instead of:

Code:
System.arraycopy(numeral, a, finalNum, a, 1)


you can just do:

Code:
finalNum[a] = numeral[a]


I suspect that the main point of System.arraycopy is to quickly copy really gigantic arrays all at once. If you are just copying one array element at a time, there is probably no reason to use it.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Wed Apr 14, 2010 11:41 am    Post subject: Reply with quote

I did, in fact, I managed to get a solution. (Which works remarkably well)


Code:
class problem1d {
   
    public static void main(String[] args) {

        //Declare the initial array
       
        int[] numeral = { 17, 6, 45, 2, 3, 17, 6, 9, 8, 6, 1, 4, 12};
        int[] finalNum = new int[13]; //define as empty to be filled.
       
        boolean hasBeenCopied = false;
        int numDuplicated = 0;
       
        //Check all numeral's items
        for(int i=0; i<numeral.length; i++){
           hasBeenCopied = false;
           //Cycle destination array
           for(int j=0; j<numDuplicated; j++){
              //Check if current item in destination array is equals to current item in source array
              //This means that this number has been copied before
              if( numeral[i]==finalNum[j]){
                 hasBeenCopied=true;
                 break;
              }
           }
           if(!hasBeenCopied){
              //Copy current number into destination array
              finalNum[numDuplicated++] = numeral[i];
           }
        }
        int i=0;
        while (i < finalNum.length) {
            //System.out.println(finalNum[i]);
            System.out.println(finalNum[i]);
            i++; //increment while loop to end!
        }
   
    }

}

_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
TMC
On the Verge of Insanity




Joined: 05 Apr 2003
Posts: 3240
Location: Matakana

PostPosted: Thu Apr 15, 2010 12:12 am    Post subject: Reply with quote

That'll print a few extra zeroes though, shouldn't the while loop be this:

Code:
        for (int i=0; i< numDuplicated; i++) {
            System.out.println(finalNum[i]);
        }


A hint. Don't write comments like this:

Quote:
i++; //increment while loop to end!


They are painful to read. Usually you want to describe what code is doing a high level, not just repeat what it already plainly says.
_________________
"It is so great it is insanely great."
Back to top
View user's profile Send private message Send e-mail
Ravenshade




Joined: 03 Apr 2010
Posts: 50

PostPosted: Thu Apr 15, 2010 1:33 am    Post subject: Reply with quote

Good point Cacti. Works even better now. Never thought of using NumDuplicated in that way.
_________________
Campaigning to change the name of OHRRPGCE to Orph anage, because it's just better and easier to pronounce!

Campaigning for Linux! Down with Win.exe!

PS. It's mean to replace words just because you don't like them ¬_¬ moderators...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Castle Paradox Forum Index -> HELP! All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group