Java Asterisk Rectangle

15

1

For my CS class, my instructor has given us the assignment of creating a rectangle made of asterisks with diagonal lines drawn through it in Java.

He also told us to write it in as few bytes as possible. I've gotten it down to 190 bytes, but I need to find a few to simplify this code even more to decrease bytes. Can anyone help me with this?

This code is functional:

interface d{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]);z<h*w;){int y=z/w,x=z++%w;System.out.print((x>w-2)?"*\n":(y%(h-1)*x*((y-x)%3)==0)?"*":" ");}}}

Input is 10 10.

Output:

**********
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  ** 

Noah Sherry

Posted 2016-08-22T15:52:44.693

Reputation: 179

Question was closed 2017-10-07T09:31:44.207

25Don't redirect him to SO; he'll get eaten alive. – Leaky Nun – 2016-08-22T15:55:57.480

3Can you clarify the output and specs of the program? i.e. example input/output etc – TheLethalCoder – 2016-08-22T15:56:52.337

2What are the dimensions of the rectangle? Any other specifications? – Theo – 2016-08-22T15:56:53.390

Hello and welcome to PPCG. The way this question is worded is somewhat off-topic for this site. If asking for golfing advice, it is better to specify a particular aspect of your code. Take a look at the [tag:tips] tag for more info. You could also check out our tips for golfing in Java post.

– FryAmTheEggman – 2016-08-22T15:58:06.260

25I'm not entirely sure why this being close voted. This is most definitely not a general programming question. It's pretty much a standard [tag:tips] question, which is very much on topic. I'm not sure whether we have a policy on homework, although as far as I can see the OP is even showing their own effort, so I don't think there's really anything wrong with this? – Martin Ender – 2016-08-22T15:58:31.750

3

@NathanMerrill Advice for specific golfing problems is most definitely not off topic.

– Martin Ender – 2016-08-22T15:59:24.577

2@MartinEnder I'm not sure if I worded this badly in my other comment, but I think that post implies asking about a specific change ("how can I shorten these nested loops?") would be on-topic, but just "golf my code" seems like it's too broad. I haven't voted yet, but am I missing something? – FryAmTheEggman – 2016-08-22T16:01:06.857

1@FryAmTheEggman You're right that a slightly more general question would probably be a bit more useful for future visitors, but I don't think asking about a specific piece of code is actually off topic. I'd imagine answers could still be very helpful content for beginning Java golfers in the future. – Martin Ender – 2016-08-22T16:08:11.667

@MartinEnder sorry for not making the question specific enough, At this point I've exhausted my knowledge of Java, so I'm reaching out to codegolf for help here. as FryAmTheEggman said, I pretty much am just asking if you guys know anyway to help lower the character count. I'll update the code with the intended output. – Noah Sherry – 2016-08-22T17:11:18.783

3@LeakyNun we don't eat people alive, we kill them first ;) – None – 2016-08-23T00:28:00.670

Is the last line of the grip supposed to have holes? I would think it's supposed to be a rectangle, filled with diagonal lines, instead of filled with diagonal lines. – Kevin Cruijssen – 2016-09-13T10:00:09.397

When I solely look at your code for golfing opportunities, I see a few: You can set the y and x inside the print itself, that way you can remove the brackets of the for-loop. You can change the ?"*":" " to chars instead of strings, so you can change it to ?42:' ' to save another byte. You can remove a few unnecessary parenthesis. Result (177 bytes) interface D{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]),y,x;z<h*w;)System.out.print((x=z++%w)>w-2?"*\n":(y=z/w)%(h-1)*x*((y-x)%3)==0?42:' ');}} – Kevin Cruijssen – 2016-09-13T10:23:26.040

I really recommend getting into golfing, especially with practical languages like Java or C. It really helps when trying to solve (very trivial) difficult challenges that you'll never learn how to solve in class, or being crucified on StackOverflow – Stan Strum – 2017-09-23T18:36:32.070

Answers

1

The code you provided could be reduced by doing this:

  • replace "*" by 42 and " " by ' ' (taking advantage of the charcode for *)
  • move ((y - x) % 3) to the begining of the things to be multiplied and remove the surrounding parenthesis
  • remove surrounding parenthesis from (x > w - 2) and from ((y - x) % 3 * y % (h - 1) * x == 0)

The resulting code would be:

interface r{static void main(String[]a){for(int z=0,w=new Byte(a[0]),h=new Byte(a[1]);z<h*w;){int y=z/w,x=z++%w;System.out.print(x>w-2?"*\n":(y-x)%3*y%(h-1)*x==0?42:' ');}}}

Note: the last line is missing in the example output of the question! The output of the sample code is different.

Reinis Mazeiks

Posted 2016-08-22T15:52:44.693

Reputation: 388

1

logically, there should be Asterik ("*") every time i == j & i+j==w-1(for diagonals), i == 0 & j == 0 (for top line and left side) and j == w-1& i==h-1 (for right side and bottom line).

class d {
    public static void main(String[] a) {
        for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++) {
            for(int j=0;j<w;j++) {
                System.out.print(i==0 || j==0 || i==h-1 || i+j==w-1 || j==w-1 || i==j ? "*":" ");
            }
            System.out.println();
        }
    }
}

Abbas Kararawala

Posted 2016-08-22T15:52:44.693

Reputation: 111

I don't know if this is an example of the logic that can be used or a fully golfed code. If it's the second case, there's a lot to golf here. Such as, the useless System.out.println() and all the useless whitespaces between the OR pipes (||) and the ternary operator. – Yytsi – 2016-09-14T06:30:06.360

1this is just an example to logic @TuukkaX. – Abbas Kararawala – 2016-09-19T07:55:09.120

0

I don't actually have Java on my computer so I can't test this but I think it should work for 174 bytes and almost definitely could be golfed more

class d{static void main(String[]a){for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++)for(int j=0;j<w;j++)System.out.print(j==w-1?"*\n":i%h-1==0||!j||i+j%2==0?"*":" ");}}

white space put in for clarity:

class d {
    static void string main(String[] a) {
        for(int i=0,w=new Byte(a[0]),h=new Byte(a[1]);i<h;i++)
            for(int j=0;j<w;j++)
                System.out.print(j==w-1 ? "*\n" : i % h-1 == 0 || !j || i+j % 2 == 0 ? "*":" ");
    }
}

print "*\n" for the last char in each line, "*" for all of the first and last lines and the first column, and "*" for any time that the sum of the row and column is even, otherwise prints " "

theLambGoat

Posted 2016-08-22T15:52:44.693

Reputation: 119