3

I've been trying to loop these if but I don't get it, where do I have to put the loop?

public static void Run(int n) {
    int l;
    int c;
    for (l = 1; l <= 2 * n - 1; l++) {
    System.out.println("\n");
    for (c = 1; c <= 2 * n - 1; c++) {

        if ((l == 1) || (l == 2 * n - 1) || (c == 1) || (c == 2 * n - 1)) {
        System.out.print('a');
        } else if ((l == 2) || (l == 2 * n - 2) || (c == 2) || (c == 2 * n - 2)) {
        System.out.print(defLettre(n, 1));
        } else if ((l == 3) || (l == 2 * n - 3) || (c == 3) || (c == 2 * n - 3)) {
        System.out.print(defLettre(n, 2));
        } else if ((l == 4) || (l == 2 * n - 4) || (c == 4) || (c == 2 * n - 4)) {
        System.out.print(defLettre(n, 3));
        } else if ((l == 5) || (l == 2 * n - 5) || (c == 5) || (c == 2 * n - 5)) {
        System.out.print(defLettre(n, 4));

        } else {
        System.out.print(" ");
        }

    }

    }

}

}

Thing is, the more n becomes the more if I have to input and i don't get how you reunite them.

EDIT:

Thanks for your answer. The program I wanted to do was this:

http://pastebin.com/dKBGjVqj (couldn't paste it correctly here).

I was able to do it, only it was confusing because if n was like 10, I would've to input 10 if..else.

BTW, my program needs to be run under 1s on a 1ghz computer and has to be under 8000 Ko. How can I see for the running under 1s part? I guess the .java size is for the size.

halfer
  • 19,824
  • 17
  • 99
  • 186
aydos
  • 31
  • 3
  • With respect to your question on how to measure execution time, you should look at one of the many other SO answers to that question. Such as, http://stackoverflow.com/questions/180158/how-do-i-time-a-methods-execution-in-java – isomarcte Dec 26 '14 at 00:16

4 Answers4

2

With Java 8, I would do something like that :

public static void Run(int n) {
    int l;
    int c;
    for (l = 1; l <= 2 * n - 1; l++) {
        System.out.println("\n");
        for (c = 1; c <= 2 * n - 1; c++) {
            final int lf = l, cf = c;
            IntPredicate pred = x -> lf == x || lf == 2*n - x || cf == x || cf == 2*n - x;
            IntStream.range(1,2*n - 1).filter(pred).findFirst()
               .ifPresent(x -> System.out.println(x == 1 ? "a" : defLettre(n,x)));
        }
    }
}
Dici
  • 25,226
  • 7
  • 41
  • 82
  • You are missing the `\n` from the OP question. – isomarcte Dec 26 '14 at 00:37
  • I think you want `IntStream.of(2*n)`. Otherwise you won't call `defLettre` with the right arguments, i.e. `defLettre(n, 4)`. Also your predicate needs to operate over [1, 2*n] for values of `x`. – isomarcte Dec 26 '14 at 00:41
  • @isomarcte Right, thanks. However, the biggest problem of this code is that it "brute forces" (like other solutions) all possible tests to find a match, whereas I think it is possible to avoid it. More specifically to my answer, if `n` is huge, it will create a lot of `Predicate` objects, which can be costly – Dici Dec 26 '14 at 00:42
  • @isomarcte `IntStream.of(1).filter(pred).findFirst()` this expression will find the first match, which necessarily occur between 1 and 2n because `lf` and `cf` are between 1 and 2n - 1. Actually, I should remove the second `else` statement – Dici Dec 26 '14 at 00:43
  • How are you accounting for conditions like `l == 2 * n - 2` for all `l <= 2 * n - 1` and `c <= 2 * n - 1`? I don't see it. Or for that matter, how are you accounting for `l == 4`, it seems you only check `l == 1`, since IntStream.of(1) is always just a stream of `1`. – isomarcte Dec 26 '14 at 00:49
  • @isomarcte Are you familiar with Java 8 ? I create an `IntPredicate` that takes an integer as a parameter and returns true if it matches the condition, else otherwise. Then, I create an `IntStream` that will be filtered by the predicate. Calling `findFirst` at the end causes the stream to be evaluated until a value is found, ie until an integer matches the condition – Dici Dec 26 '14 at 00:52
  • @isomarcte I just saw a mistake, I think my stream only contains 1, I shoould be using a range. Thank for letting me see this with your questions – Dici Dec 26 '14 at 00:53
  • 1
    BTW, I like your use of the Java functional tools. It is nice to see someone using them, they make for much nicer code in Java 8. I see what you are saying about creating many predicate objects. It is too bad you can't create lazy evaluating Java or first class functions, then you could avoid the cost issue. – isomarcte Dec 26 '14 at 00:55
  • Oh yes, I forgot the lower bound in my comment. I guess I was just perpetuating the confusion. I am glad you figured out what I was saying regardless. :) – isomarcte Dec 26 '14 at 00:56
2

You should explain the logic of what you are trying to do but nevertheless judging from the code, put the following snippet inside the second loop ( pseudocode )

    if(l==c||l+c==2*n)
    {
        int value=min(l,c);
        if(value==1)print("a");
        else print(defLettre(n,value-1));
    }
advocateofnone
  • 2,527
  • 3
  • 17
  • 39
  • Your condition is incorrect – Dici Dec 25 '14 at 23:52
  • 1
    @Dici You should elaborate on that point. – Carcigenicate Dec 26 '14 at 00:59
  • 1
    @Carcigenicate Fine. `l` is not necessarily equal to `c` in the OP's condition because it is a disjunction, so `l == x` and `c == x` might not be true at the same time while the condition is true. Moreover, if we had `l == c` then `l + c` would be either `2*x` or `4*n - 2` and not `2*n` – Dici Dec 26 '14 at 01:46
0

You can try this code :

public static void Run(int n) {
        int l;
        int c;
        for (l = 1; l <= 2 * n - 1; l++) {
            System.out.println("\n");
            for (c = 1; c <= 2 * n - 1; c++) {
                for(int k=1; k<=n; k++){

                if ((l == k) || (l == 2 * n - k) || (c == k) || (c == 2 * n - k)) {
                    System.out.print('a');
                }else {
                    System.out.print(" ");
                }
                }

            }

        }

    }
Rakesh Chouhan
  • 1,196
  • 12
  • 28
0

As others have stated, I am not entirely sure what you are attempting to do. But given your requirements I believe this solution will work for you.

The Problem

To restate your issue, you wish to execute the defLettre function whenever one of four conditions are met.

  • l == j for some j
  • c == j for some j
  • l == 2 * n - j for some j
  • c == 2 * n - j for some j

With special conditions around the cases where l == 1 or c == 1.

The Solution

In order to solve this problem you need to use an inner loop that iterates over the j variable that I have introduced in the discussion.

    public static void Run(int n) {
        final int bound = 2 * n;
        for (int l = 1; l <= bound - 1; l++) {
            System.out.println("\n");
            for (int c = 1; c <= bound - 1; c++) {
                boolean match = false;
                for (int j = 1; j < 2 * n; j++){
                    if ((l == 1) || (l == bound - j) || (c == 1) || (c == bound - j)) {
                        if (j == 1) {
                            System.out.print('a');
                        } else {
                            System.out.print(defLettre(n, j - 1));
                        }
                        match = true;
                        break;
                    }
                }
                if (!match){
                    System.out.println(" ");
                }

            }

        }
    }

Discussion

Note, I have defined bound as 2 * n. Further the inner most loop over j must be between j == 1 and j == 2 * n because on either side of the bounds the conditions will always evaluate false.

Caveats

The logic you are describing integrates over something, which I have defined as j. But the failure condition does side-effect by printing a character. There may be better, tighter, bounds, on j that are different from what I described that you want, but I don't know what they are without a better description of your algorithm.

Community
  • 1
  • 1
isomarcte
  • 1,981
  • 12
  • 16
  • You need to exit the most inner loop when you find a match. Otherwise, I think your solution does the same thing as mine with longer code – Dici Dec 26 '14 at 00:13
  • I thought you just wanted to simplify your `if` `else` blocks. You have not mentioned any exiting conditions in your question. Under what condition do you want the program to exit? – isomarcte Dec 26 '14 at 00:15
  • It is not my question, I just posted an answer. What I meant is that your code may print several times for a single value of the couple `(l,c)`, which is not an expected behaviour basing on the OP's code. Thus, you need to add a break in your most inner loop – Dici Dec 26 '14 at 00:18
  • Oh I'm sorry, I thought you were the poster. I see what you mean... I will make a correction. – isomarcte Dec 26 '14 at 00:22
  • Oh, and you need to print `" "` only at the end of the loop, if no match was found – Dici Dec 26 '14 at 00:25
  • Ah, you are correct again. – isomarcte Dec 26 '14 at 00:26