I’d like to walk in 2 loops over 0 to half of stri...
# getting-started
o
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
for j in s.lastIndex downTo halfIndex
Also, do you want to nest them or run the loops after one another
o
I want to be testing forwards and backwards at the same time with halfIndex being the stopper
yea not nesting
s
Then you should move the second for loop outside of the first for loop
e
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
@ephemient I believe your solution won't work for a list/string with odd number of items.
e
@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
The original code won't skip it. The second loop will take it. Although it's start condition should be
s.lastIndex
e
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
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
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
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
typical example of https://xyproblem.info/, it seems, for future references.
o
except the solution is in the understanding of the problem
e
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
thanks again
k
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
true