Nun gehe ich diese Aufgabe nach einiger Zeit noch einmal an, aber verstehe trotzdem immer noch nicht,
warum die for-Schleifen (der Lösung) für i und j so funktionieren können.
Mein Kopf meint, dass [i] und [j] durch das j=i+1 immer Nachbarn und daher in dieser Liste meist nicht von derselben Art (Zahl und Zahl, Wort und Wort)
sind. Demnach also auch nicht gemäß ihrer Werte mit "Artgenossen" verglichen werden können.
Bedeutet das, dass [j] automatisch immer schon einen Schritt weiter ist als [i], und daher ein +1 zur übernächsten Stelle führt?
(Aber dann wären ja wiederum die beiden aufeinanderfolgenden Zahlen in dieser Liste ungünstig betroffen.)
Kann bitte, bitte jemand die Rechthaberei meines Kopfes "geraderücken"?🙃
Wieso treffen die beiden for-Schleifen auf den passenden Folge-String?
Gelöst
Kommentare (2)
- Beliebt
- Neu
- Alt
Du musst angemeldet sein, um einen Kommentar schreiben zu können
Guadalupe Gagnon
18 April, 13:36Lösung
One loop is inside the other loop. That means the inner loop runs completely before the outer loop moves to the next step. That also means a brand new inner loop is created each new step of the outer loop. Logically you want to set the inner loop to the index the outer loop is on plus 1 because if you don't then you are comparing the values at the same index.
For example, with this code, if you had a list of 5 items the outer loop would start at 0. Then the inner loop would start at 0 + 1. Here is how that example would run:
outer i = 0
new inner loop created with j = 0 + 1
inner = 1 (compares values at index 0 and 1)
inner = 2 (compares values at index 0 and 2)
inner = 3 (compares values at index 0 and 3)
inner = 4 (compares values at index 0 and 4)
outer i = 1
new inner loop created with j = 1 + 1
inner = 2 (compares values at index 1 and 2)
inner = 3 (compares values at index 1 and 3)
inner = 4 (compares values at index 1 and 4)
outer i = 2
new inner loop created with j = 2 + 1
inner = 3 (compares values at index 2 and 3)
inner = 4 (compares values at index 2 and 4)
outer i = 3
new inner loop created with j = 3 + 1
inner = 4 (compares values at index 3 and 3)
+4
claudia
19 April, 10:11
Thank you very, very much, Guadalupe Gagnon! Now i've got it.🤩
+1