15
\$\begingroup\$

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:

**********
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  **
*  *  *  *
**  *  * *
* *  *  ** 
\$\endgroup\$

closed as unclear what you're asking by Peter Taylor, Sriotchilism O'Zaic, 0 ', Erik the Outgolfer, Toto Oct 7 '17 at 9:31

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 25
    \$\begingroup\$ Don't redirect him to SO; he'll get eaten alive. \$\endgroup\$ – Leaky Nun Aug 22 '16 at 15:55
  • 3
    \$\begingroup\$ Can you clarify the output and specs of the program? i.e. example input/output etc \$\endgroup\$ – TheLethalCoder Aug 22 '16 at 15:56
  • 25
    \$\begingroup\$ I'm not entirely sure why this being close voted. This is most definitely not a general programming question. It's pretty much a standard 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? \$\endgroup\$ – Martin Ender Aug 22 '16 at 15:58
  • 3
    \$\begingroup\$ @NathanMerrill Advice for specific golfing problems is most definitely not off topic. \$\endgroup\$ – Martin Ender Aug 22 '16 at 15:59
  • 3
    \$\begingroup\$ @LeakyNun we don't eat people alive, we kill them first ;) \$\endgroup\$ – user51896 Aug 23 '16 at 0:28
1
\$\begingroup\$

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();
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Yytsi Sep 14 '16 at 6:30
  • 1
    \$\begingroup\$ this is just an example to logic @TuukkaX. \$\endgroup\$ – Abbas Kararawala Sep 19 '16 at 7:55
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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 " "

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.