if this is code being rewritten in a different way but with the same result
https://cdn.discordapp.com/attachments/1032818977107357706/1093970803508391976/image.png
#Code Equivalence
52 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @prime violet! Please use
/closeor theClose Postbutton 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.
how would this look
I tried something like this
but it doesn't give the same result
Im not at my computer right now, but check your syntax
Your. : is in the wrong spot
Everything after the : is the else statement
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
so for this instance if else wouldn't work?
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
yep
so everything else will stay the same
only that portion will change
both in the ternary and in the if/else
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
I see
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
yeah I just wanted to see if I could change between the two
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>
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
int
you mean int[][]?
ye
Integer.MAX_VALUE < td[i][j] is always false
i mean.. why did you add it in the first place
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
yeah because you aren't following what the ternary says
ive told you how to turn the ternary into an if/else here already