https://kotlinlang.org logo
#getting-started
Title
# getting-started
o

oday

07/10/2021, 9:41 PM
I’d like to walk in 2 loops over 0 to half of string and then end of string back to half, this doesn’t look like the way to do it
Copy code
for (i in 0 until halfIndex) {
                for (j in s.length downTo halfIndex) {
                    println("i: ${s[i]} j: ${s[j]}")
                }
            }
halfIndex is
s.length / 2
let’s say 5 for example, so the first loop from
0 to 5
and the second from
11 to 5
s

Sourabh Rawat

07/10/2021, 9:50 PM
for j in s.lastIndex downTo halfIndex
Also, do you want to nest them or run the loops after one another
o

oday

07/10/2021, 9:51 PM
I want to be testing forwards and backwards at the same time with halfIndex being the stopper
yea not nesting
s

Sourabh Rawat

07/10/2021, 9:54 PM
Then you should move the second for loop outside of the first for loop
e

ephemient

07/11/2021, 2:59 AM
well, you immediately have a OOBE on
s[j]
because j = s.length initially
if you want to traverse front-to-back and back-to-front in lockstep, consider
Copy code
for (i in 0 until halfIndex) {
    val j = s.lastIndex - i
    println("${s[i]} ${s[j]}")
}
or maybe
Copy code
val forward = s.listIterator()
val backward = s.listIterator(s.size)
while (forward.nextIndex() < backward.previousIndex()) {
    println("${forward.next()} ${backward.previous()}")
}
listIterator only works on List (as the name implies), so that second one is not useful on String
s

Sourabh Rawat

07/11/2021, 4:01 AM
@ephemient I believe your solution won't work for a list/string with odd number of items.
e

ephemient

07/11/2021, 4:03 AM
@Sourabh Rawat depends on what you want to happen with the middle element. what I wrote will skip it, just as @oday's original
0 until halfIndex
would, but it's a simple tweak to include it
s

Sourabh Rawat

07/11/2021, 4:07 AM
The original code won't skip it. The second loop will take it. Although it's start condition should be
s.lastIndex
e

ephemient

07/11/2021, 5:24 AM
the code "as written" won't take it because it crashes first. the code "as described" ("testing forwards and backwards at the same time") won't take it either, if it's using a
0 until halfIndex
loop.
s

Sourabh Rawat

07/11/2021, 5:28 AM
the code is not what OP wants. That is true and already agreed upon. If you were to fix the
s.length
thing, then the second loop (correct ver. -
s.lastIndex downTo halfIndex
) will take the middle element.
e

ephemient

07/11/2021, 5:57 AM
not if it's also using
0 until halfIndex
and stepping both in parallel:
(0 until halfIndex).zip(lastIndex downTo lastIndex)
will exclude the middle element if odd
o

oday

07/11/2021, 2:51 PM
this is what I wanted
Copy code
var j = final.lastIndex

            for (i in final.indices) {
                if (final[i] != final[j]) {
                    return false
                } else {
                    j--
                }
            }
comparing first and last, second and last - 1, etc. no need for halfIndex turns out, wanted to check for palindrome in a certain way
s

Sourabh Rawat

07/11/2021, 7:18 PM
typical example of https://xyproblem.info/, it seems, for future references.
o

oday

07/11/2021, 7:48 PM
except the solution is in the understanding of the problem
e

ephemient

07/11/2021, 8:11 PM
in that case you don't need to keep track of `j`; that would be equivalent to a tweak to what I wrote with just minor adjustments,
Copy code
for (i in final.indices) {
    if (final[i] != final[final.lastIndex - i]) {
        return false
    }
}
if performance isn't a concern, that could be more succinctly represented with
Copy code
final.zip(final.reversed()) { a, b ->
    if (a != b) {
        return false
    }
}
o

oday

07/11/2021, 9:36 PM
thanks again
k

Klitos Kyriacou

07/13/2021, 9:21 AM
Note that your solution does twice the work that would be necessary to check that a string is a palindrome, because i and j cross each other and then continue to the end, instead of stopping and returning true when they meet half way.
o

oday

07/13/2021, 11:59 AM
true
2 Views