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:
**********
** * * *
* * * **
* * * *
** * * *
* * * **
* * * *
** * * *
* * * **
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.26025I'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.5772@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.397When I solely look at your code for golfing opportunities, I see a few: You can set the
y
andx
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.040I 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