Results 1 to 16 of 16
  1. #1
    csisdifficult is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default Help me re-write pseudo code?

    Here it is:


    public static StringList power (String s){
    StringList answer = new StringList();
    // Base case
    if (s.length() == 0){
    prepend("", answer);
    }else{
    StringList partial = power(butFirst(s));
    String firstChar = first(s);
    //pseudocode
    StringList prependedP = new StringList();
    For each String x in P
    insert firstChar + x into prependedP
    //end of pseudocode
    answer = p + prependedP;
    }

    return answer;
    }
    Last edited by csisdifficult; 04-22-2011 at 02:49 PM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    OK

    You write the first letter. Then I will write the second letter. Then we take it in turns.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    11

    Default

    h


    damn limits, am i doing it right? :)

    @ op: on this forum you will be doing most of the work, we will simply be helping you out.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    28

    Default

    Original poster, please make a meaningful contribution to this thread before I have to lock it has a homework dump.

  5. #5
    csisdifficult is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    I apologize, I thought that was what I was implying. I'm not sure how to go about re-writing the pseudo code, so what steps should I take to begin re-writing it?

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    11

    Default

    Is the original post pseudo code? It looks closer to real code than pseudo code. Also, please wrap pseudo code, and regular code in code tags.

    [code ] //delete space
    YOUR CODE HERE
    [/code]

    Also, ask specific questions, and say what exactly is giving you trouble.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    Sunde if you look close the for each loop near the bottom is the only pseudocode.

  8. #8
    csisdifficult is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Okay, here's the code with code tags:

    Java Code:
    public static StringList power (String s){
    StringList answer = new StringList();
    // Base case
    if (s.length() == 0){
    prepend("", answer);
    }else{
    StringList partial = power(butFirst(s));
    String firstChar = first(s);
    //pseudocode
    StringList prependedP = new StringList();
    For each String x in P
    insert firstChar + x into prependedP
    //end of pseudocode 
    answer = p + prependedP;
    }
    
    return answer;
    }
    Last edited by csisdifficult; 04-22-2011 at 02:50 PM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    No you use a for each loop. If you haven't heard of them do some research.

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    11

    Default

    Ah, thanks Junky, I'm lazy lol, I only briefly scanned his code.

    @op: First you need to understand what a for each loop is. It's basically, for each item x in list of item x.

    So I assume you know that arrays must be declared with some type(same with any other collection.) Say you have an array of strings, you can iterate through the loop with a for each loop. I understand a regular for loop does the same thing, but at times a for each loop is nice.

    In an array of integers you would do something like this
    Java Code:
    int[] ints = {1, 2, 3, 4,5, 6, 7, 8};
    for(Integer x : ints){
      //do something
    }
    this loop does something for each item in the loop. Each pass through the loop the current item being worked on can be referenced by x. As a more concreter example, lets multiply each value in the array by 5.

    Java Code:
    public class By5{
      public static void main(String[] args){
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        for(Integer x : nums){
          x *= 5;
        }
        for(Integer x : nums){
          System.out.println(x);
        }
      }
    }

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,807
    Rep Power
    10

    Default

    You're lazy? You supplied a for each example whereas I told them to look it up themselves.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    11

    Default

    lol, Im lazy when it comes to reading unformatted code.

  13. #13
    csisdifficult is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    "For each" loop? I wrote that code with the intention of using just a "while" loop. I've only been taught "while" loops.
    Last edited by csisdifficult; 04-22-2011 at 02:50 PM.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    11

    Default

    Do you understand how to use a regular for loop?

  15. #15
    csisdifficult is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Yes, but my pseudocode is making it more difficult for me to understand what to write...

    What am I initializing, testing, and updating?

    So confused...

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    11

Similar Threads

  1. What are you using to write your code?
    By CaptainMorgan in forum New To Java
    Replies: 1035
    Last Post: 12-21-2016, 10:53 PM
  2. Starting Pseudo-Code For My Asignment?
    By EpyonCustom in forum New To Java
    Replies: 26
    Last Post: 04-07-2011, 11:07 AM
  3. pseudo code
    By jamiem in forum New To Java
    Replies: 4
    Last Post: 12-20-2010, 06:25 PM
  4. Replies: 57
    Last Post: 07-15-2010, 04:10 AM
  5. how do i write this code?
    By Libertyman in forum New To Java
    Replies: 23
    Last Post: 06-22-2010, 12:43 PM

Tags for this Thread

Posting Permissions

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