Results 1 to 7 of 7
  1. #1
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Homework question on swapping array values

    Hello I am confused on how to get these test cases to adhere to my code.

    Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4.

    fix34([1, 3, 1, 4]) → [1, 3, 4, 1]
    fix34([1, 3, 1, 4, 4, 3, 1]) → [1, 3, 4, 1, 1, 3, 4]
    fix34([3, 2, 2, 4]) → [3, 4, 2, 2]


    That is my assignment. Here are the test cases



    Expected

    Run



    fix34([1, 3, 1, 4]) → [1, 3, 4, 1] [1, 3, 4, 1]OK
    fix34([1, 3, 1, 4, 4, 3, 1]) → [1, 3, 4, 1, 1, 3, 4] [1, 3, 4, 1, 4, 3, 1] X
    fix34([3, 2, 2, 4]) → [3, 4, 2, 2] [3, 4, 2, 2] OK
    fix34([3, 2, 3, 2, 4, 4]) → [3, 4, 3, 4, 2, 2] [3, 4, 3, 4, 2, 2] OK
    fix34([2, 3, 2, 3, 2, 4, 4]) → [2, 3, 4, 3, 4, 2, 2] [2, 3, 4, 3, 4, 2, 2] OK
    fix34([3, 1, 4]) → [3, 4, 1] [3, 4, 1] OK
    fix34([3, 4, 1]) → [3, 4, 1] [3, 4, 1] OK
    fix34([1, 1, 1]) → [1, 1, 1] [1, 1, 1] OK
    fix34([1]) → [1] [1] OK
    fix34([]) → [] [] OK
    fix34([7, 3, 7, 7, 4]) → [7, 3, 4, 7, 7] [7, 3, 4, 7, 7] OK
    fix34([3, 1, 4, 3, 1, 4]) → [3, 4, 1, 3, 4, 1] [3, 4, 1, 3, 4, 1] OK
    fix34([3, 1, 1, 3, 4, 4]) → [3, 4, 1, 3, 4, 1] [3, 4, 1, 3, 4, 1]



    and here is my code


    Java Code:
    public int[] fix34(int[] nums) {
      
      for(int i = 0; i < nums.length; i++){
          if(nums[i] == 3){
            for( int j = i; j<nums.length; j++){
                 if(nums[j]== 4)
                 {
                   int temp = nums[i+1];
                   nums[i+1]= nums[j];
                   nums[j] = temp;
                 }
            }
          }
      }
      return nums;
    }
    Right now I can satisfy all test cases but the one with the X next to it. Can I get some input on how to overcome that test case?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Homework question on swapping array values

    As far as I can tell, the problem seems to be that your algorithm assumes that for each three, if there is a
    four, it must be somewhere ahead of it in the array. So you simply cascade the array down from right to left,
    filling in the numbers. In your case that didn't work because there are no fours to the right of the last three. So it
    won't cascade properly.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: Homework question on swapping array values

    So should I do the first sweep through, get all the 3s that come before 4s together, and then do another separate algorithm to sweep through looking for 4s before 3s and then rearrange accordingly? I feel that this should be able to be done with more concise code, I feel that I am making it too long and unnecessary.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Homework question on swapping array values

    The algorithm is up to you. But don't worry about concise code. Get it working with the existing test cases. You may want to add a few of your own.
    When it is working, then you can focus on improving it. Trying to optimize code on the fly is a waste of energy and usually unproductive.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: Homework question on swapping array values

    Okay I have now come up with the solution. It is not pretty but it works. Thanks.
    Java Code:
    public int[] fix34(int[] nums) {
      
      for(int i = 0; i < nums.length; i++){
          if(nums[i] == 3){
            for( int j = i; j<nums.length; j++){
                 if(nums[j]== 4)
                 {
                   int temp = nums[i+1];
                   nums[i+1]= nums[j];
                   nums[j] = temp;
                 }
            }
          }
      }
      
      for(int i = 0; i< nums.length; i++){
        if(i!=0){
        if(nums[i] == 4 && nums[i-1] !=3){
          for(int j=0; j<nums.length; j++){
            if(nums[j] == 3){
              int temp = nums[j+1];
                nums[j+1]= nums[i];
                nums[i] = temp;
            }
          }
        }
        }
      }
      
      return nums;
    }

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Homework question on swapping array values

    I haven't checked out the algorithm. But try this one. fix34(1, 3, 1, 4, 4, 4, 3, 1, 3);

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    camel-man is offline Member
    Join Date
    Jan 2016
    Posts
    19
    Rep Power
    0

    Default Re: Homework question on swapping array values

    I initially was looking at the test cases where 3 comes last and 4s come first in the array, however, when I look at the requirements of the assignment: "Return an array that contains exactly the same numbers as the given array, but rearranged so that every 3 is immediately followed by a 4. Do not move the 3's, but every other number may move. The array contains the same number of 3's and 4's, every 3 has a number after it that is not a 3 or 4, and a 3 appears in the array before any 4."

    It excludes those test cases although I can always add it in for extra practice.

Similar Threads

  1. Array checking and element swapping Help Needed?
    By riches in forum New To Java
    Replies: 3
    Last Post: 03-11-2013, 01:40 PM
  2. Linked List swapping question
    By FiasseKrystella in forum New To Java
    Replies: 1
    Last Post: 02-19-2012, 11:35 AM
  3. Swapping elements of an array help please
    By ikillu in forum New To Java
    Replies: 11
    Last Post: 01-15-2012, 08:49 PM
  4. Swapping/Sorting elements in an Array
    By kumalh in forum New To Java
    Replies: 2
    Last Post: 08-10-2011, 04:52 AM
  5. Swapping numbers in an array
    By Awt582 in forum New To Java
    Replies: 3
    Last Post: 11-01-2010, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •