#Code Equivalence

52 messages · Page 1 of 1 (latest)

prime violet
agile solsticeBOT
#

This post has been reserved for your question.

Hey @prime violet! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

prime violet
#

how would this look

#

I tried something like this

#

but it doesn't give the same result

austere kayak
#

Im not at my computer right now, but check your syntax
Your. : is in the wrong spot

Everything after the : is the else statement

regal remnant
#

not exactly

#

the entire ? : is still in the Math.min

#

ternaries do not match exactly to if/else, because if/else uses statements while ternaries use expressions

prime violet
#

so for this instance if else wouldn't work?

regal remnant
#

it would, but it's not straightforward

#

look at where the ternary is, what it's affecting

#

try just first extracting the ternary out

#

which part do you think is actually the ternary's operands

prime violet
#

this part?

regal remnant
#

yep

#

so everything else will stay the same

#

only that portion will change

#

both in the ternary and in the if/else

prime violet
#

how does the comma work after the operand

#

its the first time i've seen it

regal remnant
#

it doesn't, that's just separating arguments

#

copy the entire ternary code into both branches of the if/else, extract the condition out to use in the if, then replace the ternary with its true branch in the if and its false branch in the else
this is how you go from ternary to if/else

prime violet
#

I see

regal remnant
#

there isn't a solid way to go from if/else to ternary, since statements can do more than expressions
but if between the if branch and the else branch, just one value changes, then you can put a ternary there, reversing the stuff you did to turn a ternary into an if/else

#

i assume your question is just for practice, but in case it isn't; there isn't generally a need to remove ternaries or remove if/elses, the ternary works well there
once you expand the ternary into an if/else, you'll see that a lot of the code is repeated just with 1 value changed

prime violet
#

yeah I just wanted to see if I could change between the two

regal remnant
#

you can always go from ternary to if/else, but not always the reverse

#

in languages like java where not all expressions are statements, then a ternary would have already been part of a statement before the transformation, and you'll end up with a statement afterwards

in languages like js where expressions are valid statements technically, then a ternary could be like above or it could be just a plain expression and it'll again be a plain expression after the transformation

#

<sidenote>
also in languages like js or python where "truthiness" is a thing, or in bash where there's only exit codes, &&/and and ||/or can act as plain ifs due to short circuiting. this only works with boolean expressions in java
</sidenote>

prime violet
#

regarding the change from ternary to if/else, my logic is that if the position at the 2d array is 0 then it chooses the minimum of the two and sets it to that point

#

but i'm not sure where to fit the separated argument

regal remnant
#

no, look at what Math.min encloses

#

(what's the type of td btw)

prime violet
#

int

regal remnant
#

you mean int[][]?

prime violet
#

ye

regal remnant
#

Integer.MAX_VALUE < td[i][j] is always false

prime violet
#

then we can just remove it right?

#

since it doesn't seem to be adding anything

regal remnant
#

i mean.. why did you add it in the first place

prime violet
#

I was just following code i saw from somewhere else

#

seeing if i could change it

#

but if I change it to this then it also gives a different answer

regal remnant
#

yeah because you aren't following what the ternary says

regal remnant
prime violet
regal remnant
#

no

#

the ternary is inside the Math.min. the Math.min will not change in this first step.