Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default Need help with Arrays

    This is what I am given:

    In this exercise we want to simulate the effects of mutations on a DNA string (with at most 1000 characters). For simplicity we will only consider a selected subset of chromosomal mutations. We would simulate the following (simplified) mutations:

    Amplification (amp) : this inserts a duplicate copy of the specified region beside it;
    Deletion (del) : this deletes the selected region;
    Inversion (inv) : this reverses the orientation of the specified region.

    The first line of the input consists of a string (with at most 1000 characters) that denotes the DNA sequence. The second line consists of a positive integer n denoting the number of mutations. The next n lines specify the mutations, each on a single line.

    Each line representing a mutation consists of (1) the mutation command (which can be amp, del, or inv), (2) the start index (inclusive), and (3) the end index (inclusive). The first character in the DNA sequence has index 0. All commands are in lower case.

    For example "amp 2 4" on "AGCTAGATT" results in "AGCTACTAGATT" as CTA is duplicated. The duplicated part is marked in blue for your reference.

    The following is always true about the input and the DNA at every point of time:

    There will always be a command, followed by a space, followed by an integer value (denoting the start index), followed by a space, followed by an integer value (denoting the end index).


    There is always only a single space between the command and the start index.


    There is always only a single space between the start index and the end index.


    All indices are inclusive.


    The start index is always smaller or equal to the end index.


    All DNA strings will consist only of 4 possible characters (in uppercase) which are 'A', 'T', 'C', 'G'.


    The number of commands is always exactly equals to n.


    The length of a DNA string will always be at least 1 and at most 1000 at any point of time.


    The input mutation commands are always in lower case.


    The mutations have been greatly simplified and you should model your mutations only in the descriptions given here.


    The output consists of a single line containing the modified string.

    It is a must to work on character array.


    And the skeleton i'm provided with:

    import java.util.*;

    public class Mutations
    {
    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    String dna = stdIn.nextLine();
    int count = stdIn.nextInt(); // number of mutation commands

    int dnaLength = dna.length();
    char[] dnaArray = new char[1000];
    for (int i = 0; i < dna.length(); i++)
    {
    dnaArray[i] = dna.charAt(i);
    }

    /*
    * Type your code here.
    * Make sure you remember to update the dnaLength variable!
    */

    /*
    * We cannot simply use the dnaArray because it has a length of 1000.
    * Therefore we should only take a substring of its proper length.
    */
    String output = new String(dnaArray);
    output = output.substring(0, dnaLength);
    System.out.println(output);
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Hmm, dietgal provided with a skeleton..That figures! (pun intended).

    To get help here, you'll need to be specific about what you're having a problem understanding. I mean, you didn't really think you could dump your assignment here and someone would do your homework for you, now did you?

    Take a look at How To Ask Questions The Smart Way, it'll help you to rephrase your question to get useful answers.

    db

  4. #4
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Yea, I know I shouldn't.

    But I am really lost in this chapter and although this is supposed to be the easier question, I can't do it, at all.

    Would appreciate any help as long as it can get me started. Please.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    To get you started? OK.
    The Java™ Tutorials

    db

  6. #6
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    I mean, to get me started with this question.

    I've googled about character array but still couldn't compile without errors.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by dietgal View Post
    I mean, to get me started with this question.

    I've googled about character array but still couldn't compile without errors.
    What errors?

    And post the complete code that you have up to this point and use code tags.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, send here to see your error. Seems to me your char type array declaration is fine. So where are you stuck with?

  9. #9
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    String mutation = stdIn.nextLine();
            int num1 = stdIn.nextInt();
            int num2 = stdIn.nextInt();
            
            if (mutation.equals("amp"))
            {
            }
            
            else if (mutation.equals("del"))
            {
            }
            
            else (mutation.equals("inv"))
            {
            }
    This is the changes I made, compliable.

    However I am not sure how to do the effects that amp, del and inv are supposed to have, thus the blanks within the braces.

    And here's the sample run provided:
    Java Code:
    $ javac Mutations.java
    $ java Mutations
    AAAGGCCTTAGCTTAGATTACGATCG
    1
    amp 2 7
    AAAGGCCTAGGCCTTAGCTTAGATTACGATCG

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by dietgal View Post
    However I am not sure how to do the effects that amp, del and inv are supposed to have, thus the blanks within the braces.
    Well, at least give it a try, we are not simply going to do it for you. Carefully read the description for what they are to do again, and then at least try. We will help you correct it, or nudge you in the proper direction, but we are not going to do it for you.

    I will say, however, that you should make three methods to perform the actions and simply call those methods from within the if blocks rather than doing the actual processing there.

  11. #11
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    How about this? Is my definition of the statement correct?

    Java Code:
    if (mutation.equals("amp"))
            {
              System.arraycopy(dnaArray, num1, dnaArray, num1, num2);
            }
    And I've another question, how do I make use of the

    Java Code:
    int count
    given in the skeleton?

    By the way, I think I haven't learnt to call methods...

  12. #12
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Java Code:
    else if (mutation.equals("del"))
            {
              return (s.substring(0,dna.length)+s.substring(num1) && s.substring(0,dna.length)+s.substring(num1));
            }
    And let me know why the above couldn't work. Thanks.

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by dietgal View Post
    How about this? Is my definition of the statement correct?

    Java Code:
    if (mutation.equals("amp"))
            {
              System.arraycopy(dnaArray, num1, dnaArray, num1, num2);
            }
    Okay, lets take a look at this. amp means you are insert a copy of the region beside it and num1 is the start index and num2 the end index.

    arraycopy's arguments are as follows
    1. The source array
    2. The starting index in that source array
    3. The target array
    4. The starting index in that target array
    5. The length of the sequence to copy

    However, arraycopy does not insert, it overwrites. So, designating num1 as the start index in both occurances will affect nothing. And, the length is not num2. Num2 is the end index, the length is the end index - the start index, is it not? Also, keep track of the index of the last used element. The reason for that will soon be made clear.

    What you really need to do is, copy the elements starting from one spot after the "end index to copy" until the last used element leaving a "hole" just large enough to write the sequence that should be copied, then copy the element to copied into that hole.

    And I've another question, how do I make use of the

    Java Code:
    int count
    given in the skeleton?
    You need to wrap the code above in a while loop incrementing that count variable each pass through the loop, as each pass through the loop is a mutation.
    By the way, I think I haven't learnt to call methods...
    Then ignore that for the moment.

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Also, all of this gets a little tricky. Are you sure you are not allowed to redefine the char array. Then I would copy from the beginning of the source array to the end of the copy index, then copy again from the start index to the last used element. The thing with simply pushing the part after the nlock to copy over is, say the block to copy is 4 characters long, and there are six characters after the block to copy. Then by pushing that block over, the first character moved four spaces over would overwrite the character at that position before it has been pushed over.

  15. #15
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Also, all of this gets a little tricky. Are you sure you are not allowed to redefine the char array. Then I would copy from the beginning of the source array to the end of the copy index, then copy again from the start index to the last used element. The thing with simply pushing the part after the nlock to copy over is, say the block to copy is 4 characters long, and there are six characters after the block to copy. Then by pushing that block over, the first character moved four spaces over would overwrite the character at that position before it has been pushed over.
    In that case, I should use a loop? I do remember that we are not supposed to have long codes and the codes duplicate.

    What you really need to do is, copy the elements starting from one spot after the "end index to copy" until the last used element leaving a "hole" just large enough to write the sequence that should be copied, then copy the element to copied into that hole.
    May I ask how? Any specific array to use?

    Below is my new code, I've inserted the while loop and my "del" and "inv" still have errors.
    Java Code:
    import java.util.*;
    
    public class Mutations
    {
        public static void main(String[] args)
        {
            Scanner stdIn = new Scanner(System.in);
            String dna = stdIn.nextLine();
            int count = stdIn.nextInt(); // number of mutation commands
    
            int dnaLength = dna.length();
            char[] dnaArray = new char[1000];
            for (int i = 0; i < dna.length(); i++)
            {
                dnaArray[i] = dna.charAt(i);
            }
    
            /*
             * Type your code here. 
             * Make sure you remember to update the dnaLength variable!
             */
            
            while (count >=1)
              
            {
            
            String mutation = stdIn.nextLine();
            int num1 = stdIn.nextInt();
            int num2 = stdIn.nextInt();
            
            if (mutation.equals("amp"))
            {
              System.arraycopy(dnaArray, num1, dnaArray, num1, (num2-num1));
            }
            
            else if (mutation.equals("del"))
            {
              return (s.substring(0,dna.length)+s.substring(num1) && s.substring(0,dna.length)+s.substring(num1));
            }
            
            else if (mutation.equals("inv"))
            {
              reverse (dnaArray.substring(num1, num2));  
      
              System.out.print(dnaArray.substring(0,1));
    
            }
            
    
            /*
             * We cannot simply use the dnaArray because it has a length of 1000.
             * Therefore we should only take a substring of its proper length.
             */
            String output = new String(dnaArray);
            output = output.substring(0, dnaLength);
            System.out.println(output);
        }
        }
    }

  16. #16
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post further study needed.

    Quote Originally Posted by dietgal View Post
    In that case, I should use a loop?
    Loop logic is tricky to get right, but I would rather use a loop that I understand than something from proven code that I do not understand. Given that we have some very specific and detailed information on how the data comes in; what I see is that any point we stay with Character datatype. That determines:
    Quote Originally Posted by dietgal View Post
    May I ask how? Any specific array to use?
    as always being a character array type. Most, probably all of the Collections will operate as <char> types.
    Quote Originally Posted by dietgal View Post
    I do remember that we are not supposed to have long codes and the codes duplicate.
    Usually, but this is just for design reasons. There is only so much the mind can keep up with.
    Comments inserted:
    Java Code:
    import java.util.*;
    
    public class Mutations
    {
        // with insertions, result can possibly be longer than 1000
        // unless the specification never longer than 1000 holds true 
        // for resultant work. It may be that a temp work area could 
        // exceed specified lenght during program run, depending on 
        // approach used.
        char[] dnaArray = new char[1000];
        
        // Why the copy? though that may be the work area possibly.
        for (int i = 0; i < dna.length(); i++)
        /*
         * Type your code here. <- no you type the code here. 
         */
         //.....
        String mutation = stdIn.nextLine();
        int num1 = stdIn.nextInt();
        int num2 = stdIn.nextInt();
        // As given by masijade: "arraycopy does not insert, it overwrites."
        if (mutation.equals("amp"))
        {
            // Would StringBuffer work here?
            System.arraycopy(dnaArray, num1, dnaArray, num1, (num2-num1));
        }
        // Cannot return a String from main, probably keep working here.
        else if (mutation.equals("del"))
        {
          return (s.substring(0,dna.length)+s.substring(num1) && s.substring(0,dna.length)+s.substring(num1));
        }
        // Where is reverse? All of main should call functions on something.
        // Can be the enclosing class if you declare a variable of type
        // Mutations and write methods to do the work.
        else if (mutation.equals("inv"))
        {
          reverse (dnaArray.substring(num1, num2));  
        }
    }
    Java Code:
        /*
         * We cannot simply use the dnaArray because it has a length of 1000.
         * Therefore we should only take a substring of its proper length.
         */
    Why?
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  17. #17
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Yes, I do note that copyarray overwrites instead of insert. But I am still thinking (and searching) how to insert.

    Sadly, in this assignment, we are not allowed to use StringBuffer.

    Java Code:
    // Cannot return a String from main, probably keep working here.
    Then how do I get back the new string? Do I create another string to contain the one with the changes?

    Java Code:
    // Where is reverse? All of main should call functions on something.
        // Can be the enclosing class if you declare a variable of type
        // Mutations and write methods to do the work.
    So I need to create a method called reverse is it? I am not too sure about this, as I goggled it.

    Java Code:
      /*
         * We cannot simply use the dnaArray because it has a length of 1000.
         * Therefore we should only take a substring of its proper length.
         */
    This is provided in the skeleton provided for us.

  18. #18
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Hi all, I have came up with this code, finally compliable but still couldn't pass the dynamic cases when I submitted my code. Please help me out.

    Java Code:
    import java.util.*;
    
    public class Mutations
    {
        public static void main(String[] args)
        {
            Scanner stdIn = new Scanner(System.in);
            String dna = stdIn.nextLine();
            //String dna = "GGCCTTAGCTTAGATTACGATCGGTTCCGAGCTTAGATTACGATCG";
    
            int count = stdIn.nextInt(); // number of mutation commands
    
            int dnaLength = dna.length();
            char[] dnaArray = new char[1000];
            char[] newArray = new char[1000];
    
            for (int i = 0; i < dna.length(); i++)
            {
                dnaArray[i] = dna.charAt(i);
            }
    
            /*
             * Type your code here.
             * Make sure you remember to update the dnaLength variable!
             */
    
            for(int i=1;i<=count;i++){
    
       String command = stdIn.next();
       int startIndex = stdIn.nextInt();
       int endIndex = stdIn.nextInt();
    
       //System.out.println("Command: "+command);
       //System.out.println("Index[S][E] :["+startIndex+"]["+endIndex+"]");
    
       if(command.equals("amp")){
        int difference = endIndex-startIndex+1;
        dnaLength = dnaLength + difference;
        for (int k = 0; k < dnaLength; k++)
        {
         if(k>endIndex){
          newArray[k] = dnaArray[k-difference];
         }
         else{
          newArray[k] = dnaArray[k];
         }
        }
        //System.out.println("old : "+ new String(dnaArray));
        //System.out.println("new : "+ new String(newArray));
       }
    
       if(command.equals("del")){
        int difference = endIndex-startIndex+1;
        dnaLength = dnaLength - difference;
    
        for (int k = 0; k < dnaLength; k++)
        {
         if(k>=startIndex){
          newArray[k] = dnaArray[k+difference];
         }
         else{
          newArray[k] = dnaArray[k];
         }
        }
        //System.out.println("old : "+ new String(dnaArray));
        //System.out.println("new : "+ new String(newArray));
       }
    
       if(command.equals("inv")){
        int difference = endIndex-startIndex+1;
    
        for (int k = 0; k < dnaLength; k++)
        {
         if(k>=startIndex && k<=endIndex){
          newArray[k] = dnaArray[endIndex-k+startIndex];
         }
         else{
          newArray[k] = dnaArray[k];
         }
        }
        //System.out.println("old : "+ new String(dnaArray));
        //System.out.println("new : "+ new String(newArray));
       }
    
       for (int l = 0; l < dnaLength; l++)
          {
             dnaArray[l] = newArray[l];
             }
      }
            /*
             * We cannot simply use the dnaArray because it has a length of 1000.
             * Therefore we should only take a substring of its proper length.
             */
            String output = new String(dnaArray);
            output = output.substring(0, dnaLength);
            System.out.println("");
            System.out.println(output);
        }
    }

  19. #19
    gravity is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default

    Hi there, i have run through your program and i find that it is quite elegantly done, however i would have used an arraylist... as for the dynamic test i think you are failing it because of addtional things being printed out...

    Take your second last line of code for example.. System.out.println("");

    I would take this line out.

  20. #20
    dietgal is offline Member
    Join Date
    Oct 2008
    Posts
    19
    Rep Power
    0

    Default

    Thanks for the help!!

    I passed all the test cases after I removed that line! (:

    By the way, haven't learnt ArrayList yet but what I've learnt till now is killing me already.

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with Arrays
    By bri1547 in forum New To Java
    Replies: 4
    Last Post: 08-01-2008, 06:12 AM
  2. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 03:38 AM
  3. Help on Arrays...
    By cuellar14 in forum New To Java
    Replies: 4
    Last Post: 07-25-2008, 09:16 PM
  4. need help with arrays
    By Jman in forum New To Java
    Replies: 17
    Last Post: 07-21-2008, 03:34 AM
  5. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM

Posting Permissions

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