Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# From 60 lines to 1 line?

Matt Wilde
Ranch Hand
Posts: 34
How to reduce this? I don't want exact answers just a tip. Create an array and scan through it? Use the "?" ternary operator in some way? I'm stumped....

Bear Bibeault
Author and ninkuma
Marshal
Posts: 64858
86
• 1
Map

Koen Aerts
Ranch Hand
Posts: 344
Or depending on what variable "i" stands for, a Collection or even a simple int[] ...

Jeff Verdegan
Bartender
Posts: 6109
6
Matt Wilde wrote:How to reduce this? I don't want exact answers just a tip. Create an array and scan through it? Use the "?" ternary operator in some way? I'm stumped....

Is there some mathematical relationship between the left hand side and the right hand side? If you weren't using Java, is there some equation or set of rules you can use to describe how to get from one to the other without listing them all out?

If so, then you can figure out how to express the equation or rules in Java, and you'll probably end up with something significantly more compact.

If not, then you'll still have to explicitly describe each mapping, but there are ways you can do it that are more compact than that.

If the input side is in the range 0..N with no holes or few holes, you could use an array. If there are a lot of holes, you could use a Map.

Orthogonally to the above, if the mappings will rarely or never change, you can hardcode the values. Otherwise, you can read them from a config file.

Henry Wong
author
Marshal
Posts: 21127
78

I once converted a large group of if/then/else statements to one expression loaded with ternary operators.

I really liked it. I understood it perfectly. Everyone else hated it...... and years later. I liked it a lot less.

Henry

Matthew Brown
Bartender
Posts: 4567
8
There's also a switch statement, which would be an improvement. But my first choice would be an algorithmic approach like Jeff suggests, or an appropriate data structure if that's not possible.

Winston Gutkowski
Bartender
Posts: 10417
63
• 1
Matt Wilde wrote:Create an array and scan through it?

Sounds reasonable; although from your if statement, I don't think you even need to scan.
Use the "?" ternary operator in some way? I'm stumped....

UGH...although Henry says he's tried it.

None of the them, apart from a pre-initialized version of Bear's suggestion, will do it in 1 line though.

But, while we're in the realm of the absurd,
return i < 20 ? i + Integer.parseInt("722150849790...".substring(4*i, 4)) : -1;
would.

Winston

Rob Spoor
Sheriff
Posts: 20533
54
Since the indexes are incremental I'd go with a simple array combined with a ternary operator for the "else" clause, when the index is not valid for the array. Unless there is some logic between these numbers and indexes we don't know about of course, in which case I'd use that logic.

Matt Wilde
Ranch Hand
Posts: 34
@Bear: I keep hearing "Map" but hardly understand the extent of that class in Java. I'll look into it.
@Koen: makes sense to me...I end up doing something like that. Still not sure if it is the best...
@Jeff: As far as I know I cannot see any realtionship between them. As I said...I don't understand the "mapping". I end up using an array. Config file?
@Henry: I asked my professor about the ternary operators, its possible but he said thats not the best way.
@Matthew: Switch statement? Data structure? sounds useful I will look into it.
@Winston: Brilliant! didn't think of that. I will try that. Obviously I need to learn more about Mapping....
@Rob: Ya don't think there is a pattern to any of it...And yes I did an array. I think it works.

I did:

int[] a = {7221,5084,9790,7601,....}
i = (i>=0&&i<=19) ? a[i] : -1;

Good?

Winston Gutkowski
Bartender
Posts: 10417
63
Matt Wilde wrote:Brilliant! didn't think of that. I will try that...

Oh God, please don't. It was meant as a joke.

I did:
int[] a = {7221,5084,9790,7601,....}
i = (i>=0&&i<=19) ? a[i] : -1;
Good?

Two lines as far as I can make out. You could do it that way in one though. My suggestion (if you absolutely feel you have to do this) would be
return i = i < 0 || i > 19 ? -1 : [whatever horrible construct gets you what you want];

Winston

Rob Spoor
Sheriff
Posts: 20533
54
• 1
I'd fix it just a little:
By checking against the length of the array, you can add or remove values without breaking your code.

And I'd make the array a private static final field, so you don't need to recreate it for each method call.

Jeff Verdegan
Bartender
Posts: 6109
6
Matt Wilde wrote:
@Jeff: As far as I know I cannot see any realtionship between them. As I said...I don't understand the "mapping". I end up using an array. Config file?

If the mappings change more frequently than you're willing to recompile, you could put them in a text file, one mapping pair per line, and then your app would read the file at startup, or however often it needed to.

Matt Wilde
Ranch Hand
Posts: 34
Jeff Verdegan wrote:
If the mappings change more frequently than you're willing to recompile, you could put them in a text file, one mapping pair per line, and then your app would read the file at startup, or however often it needed to.

So put in a Scanner/Formatter type thing and create a new .txt file that holds all those values and read through the values checking/returning ints?

Winston Gutkowski
Bartender
Posts: 10417
63
Matt Wilde wrote:So put in a Scanner/Formatter type thing and create a new .txt file that holds all those values and read through the values checking/returning ints?

My suggestion would be to turn that file into the array we've all been talking about and store it somewhere; unless you expect it to change while your program is running.

I have to ask: Is this a homework question, or just something you thought up?

Winston

Matt Wilde
Ranch Hand
Posts: 34
@ Winston: Ya that makes more sense now! And yes it is. Started at 250 lines we need it under 80 for an A which I already have it under that. I just want to get it smaller. Thus, I am not asking for explicit answers only suggestions. Thank you!

Winston Gutkowski
Bartender
Posts: 10417
63
Matt Wilde wrote:Thank you!

You're welcome. I hope you noted Rob's last post (which I gave a point).

Winston

Matt Wilde
Ranch Hand
Posts: 34
@Rob: thanks didn't see that.
@Winston: Yes I did and I changed it. I also noticed the sarcasm for the parseInt method ha thanks though!

Thanks for the help guys!

Jeff Verdegan
Bartender
Posts: 6109
6
Rob Spoor wrote:I'd fix it just a little:
By checking against the length of the array, you can add or remove values without breaking your code.

And I'd make the array a private static final field, so you don't need to recreate it for each method call.

Hmmm....

If we make it a String literal, then we it will be created once, at class load time.
7221 is U+1C35, LEPCHA CONSONANT SIGN KANG, ᰵ
5084 is U+13DC, CHEKROKEE LETTER DLA, Ꮬ
...

@OP: It's a joke. Please do not try this at home.

Matt Wilde
Ranch Hand
Posts: 34
well this is what my whole class looks like in the end. From 250 lines to 17. Tied for first in the class. However Unorthodox my method is...it works! haha

Jeff Verdegan
Bartender
Posts: 6109
6
Matt Wilde wrote:well this is what my whole class looks like in the end. From 250 lines to 17. Tied for first in the class. However Unorthodox my method is...it works! haha

I thought you were going to go the array route. That would definitely be less code, and easier to read.

Also, if you're really going to use the ternary operator (And why would you?) then there's no need for the i < 0 || i > 20 test. Just make that the final "default/else" value.

But seriously, ick.

Matt Wilde
Ranch Hand
Posts: 34
Jeff Verdegan wrote:

I thought you were going to go the array route. That would definitely be less code, and easier to read.

how would i do that in one line? the rules are we cannot have Statements on the same line after an opening brace such as if (i == 0){ int j = 4;

Henry Wong
author
Marshal
Posts: 21127
78
Matt Wilde wrote:
Jeff Verdegan wrote:

I thought you were going to go the array route. That would definitely be less code, and easier to read.

how would i do that in one line? the rules are we cannot have Statements on the same line after an opening brace such as if (i == 0){ int j = 4;

Is that the whole goal? It has to be one line? Over actual code size and readability? Well... okay....

Henry

Matt Wilde
Ranch Hand
Posts: 34
Duh... didn't think of that.
That's why I'm in beginner programming

Jeff Verdegan
Bartender
Posts: 6109
6
Matt Wilde wrote:
Jeff Verdegan wrote:

I thought you were going to go the array route. That would definitely be less code, and easier to read.

how would i do that in one line? the rules are we cannot have Statements on the same line after an opening brace such as if (i == 0){ int j = 4;

Are you really required to do it in one line?

Will you really get a better grade if you do it in one horribly unreadable line than in 2 or 3 lines that are easier to read and understand and maintain? If so, go for it.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?