9 Replies - 1059 Views - Last Post: 05 August 2011 - 12:00 PM Rate Topic: -----

#1 bk5115545  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-August 11

SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 07:43 AM

Hi everyone I am a year-one Java student who is having a friendly competition with 2 other friends. We are trying to see who can make the shortest code to draw a diamond based on user input.
//user inputs 5
     *
    ***
   *****
    ***
     *


The other two are tied for the lead at 176 characters, including spaces but not including invisible(\n), so they decided to use the forum of their choice to see who can get the shorter code. Of course, I am still in this thing (sort-of) with 194 characters, so I complained and was able to use the forum of my choice. I have never posted on this forum but since you guys always explain in detail, I learn what I need to know.
My code is below; I have broken it so others cannot copy-paste-turnin, but the break is pretty obvious to long-time Java programmers.

class
d{
public
static
void
main(String[]a)
int
n=new
Byte(a[0]),
i=1,f=1;
for(String
l="*";i>0;i+=f,f=n<=i*2?-1:f){
System.out.printf("%"+n+i+"s%n",l);
if(f<0)l=l.replaceFirst("\\*\\*","");
else
l+="**";
}}}



Is This A Good Question/Topic? 0
  • +

Replies To: SHORT CODE CONTEST:i need help making my program shorter

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 08:32 AM

Start by properly indenting your code so we can read it.

You could use a ternary operator for this:
if(f<0)l=l.replaceFirst("\\*\\*","");
15	else
16	l+="**";



Shortening it to this:
l = f < 0 ? l.replaceFirst("\\*\\*","") : l + "**";


Was This Post Helpful? 2
  • +
  • -

#3 bk5115545  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-August 11

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 10:31 AM

class d{
public static void main(String[]a){
int n=new Byte(a[0]),i=1,f=1;
for(String l="*";i>0;i+=f,f=n<=i*2?-1:f){
System.out.printf("%"+n+i+"s%n",l);
l=f<0?l.replaceFirst("\\*\\*",""):l+"**";
}}}



class d{
public static void main(String[]a)
int n=new Byte(a[0]),i=1,f=1;
for(String l="*";  i>0;  i+=f,  f=n<=i*2?-1:f){
     System.out.printf("%"+n+i+"s%n",l);
     l=f<0?l.replaceFirst("\\*\\*",""):l+"**";
}}}


the actual code has no spaces but as per request, here is the easy to follow concept; broken in the same manner as before
Was This Post Helpful? 0
  • +
  • -

#4 bk5115545  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-August 11

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 11:09 AM

well it looks like this method of doing this can't get any shorter. i going to try to mirror the diamond horizontally instead of vertically. thanks for showing me how to use the ternary operator; learn something new everyday i guess
Was This Post Helpful? 0
  • +
  • -

#5 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 11:51 AM

I don't do Java, so don't know if this will work, but you could save 2 characters (possibly) by replacing i>0; in your for loop by doing simply i?

Logically it would evaluate to false when it hits 0 anyway, so should still yield the same results.

Sorry if Java's typing system isn't like this, probably isn't :P

This post has been edited by RudiVisser: 05 August 2011 - 11:52 AM

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 11:52 AM

It doesn't work the same way in Java. Unlike in PHP, false, 0, null, "", etc., are very different. :)
Was This Post Helpful? 1
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 11:55 AM

Yeah, didn't think it would.

I did always think of Java as a rubbish language on the same wavelength as PHP though - So my bad :D
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 11:57 AM

For certain things, yes. Java is far more structured, though. :P
Was This Post Helpful? 0
  • +
  • -

#9 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 11:58 AM

View Postmacosxnerd101, on 05 August 2011 - 07:57 PM, said:

For certain things, yes. Java is far more structured, though. :P

Haha! There's nothing structured about PHP so I don't doubt that for a second (even Brainfuck makes more sense!). I still want to get at least a bit of Java experience, just don't like the whole JVM thing. That's me though.

Anyhoo I'm out - Sorry for the miniature hijack, bk5115545

This post has been edited by RudiVisser: 05 August 2011 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10647
  • View blog
  • Posts: 39,542
  • Joined: 27-December 08

Re: SHORT CODE CONTEST:i need help making my program shorter

Posted 05 August 2011 - 12:00 PM

You can use PHP on the JVM.

Link: http://en.wikipedia....f_JVM_languages

</offtopic>

Back on topic now. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1