Lesson 1
Array Hopping Adventure with Gloria in C#
Introduction

Welcome to a delightful lesson on list traversal! Today, we invite you to join an endearing bunny named Gloria on an intricate quest. Gloria has a soft spot for number games, especially when they involve hopping between lists. Our goal on this exciting journey is to assist Gloria with her escapade and identify the maximum value she encounters along the way. Are you ready to embark on this adventure?

Task Statement

Gloria's quest unravels with two lists, both brimming with non-negative integers. Starting at the first element of listA, she leaps to listB based on the index she discovers in listA. She then bounces back to listA according to the index she stumbles upon in listB. Gloria repeats these hops until she returns to where she started in listA. What an adventure!

Your challenge is to craft a C# method that aids Gloria on her trip. The method will take two lists of integers as inputs, representing listA and listB. The objective is to find the highest value from listB that Gloria jumps to during her voyage.

It is guaranteed that at some point Gloria returns to the starting position.

Example

If listA = {2, 4, 3, 1, 2} and listB = {4, 0, 3, 2, 0}, the output should be 3.

In this scenario, Gloria starts from the first element of listA, which is 2. Then, she jumps to listB at index 2, where she discovers 3. She then bounces back to listA at index 3, where she arrives at 1. From there, she leaps back to listB at index 1, stumbling upon a 0. Finally, she bounces back to listA at index 0, the location where she started her adventure. Hence, she stops here, and during this journey, she came across the highest value, 3, from listB.

Solution Building: Step 1 - Initialization

Before we make headway with our code, let's kickstart with the initialization of variables in C#. Let indexA and indexB denote the last positions of Gloria in listA and listB, respectively. We will also use maxValue for tracking the highest value encountered in listB. Her quest starts from listA, so we also maintain a Boolean flag inListA.

C#
1int indexA = 0; 2int? indexB = null; 3bool inListA = true; 4double maxValue = double.MinValue;
Solution Building: Step 2 - List Hopping

Our assistant for Gloria’s hopping challenge will be a while loop! This keeps iterating until Gloria returns to her starting position in listA.

If Gloria is in listA, we check if the value in listB where she is going to land is greater than maxValue, and update maxValue if it is. We also switch Gloria's position to the other list in each iteration.

C#
1while (true) 2{ 3 if (inListA) 4 { 5 indexB = listA[indexA]; 6 if (listB[indexB.Value] > maxValue) 7 { 8 maxValue = listB[indexB.Value]; 9 } 10 } 11 else 12 { 13 indexA = listB[indexB.Value]; 14 if (indexA == 0) 15 { 16 return maxValue; 17 } 18 } 19 inListA = !inListA; 20}
Final Function

Collecting all the pieces together, here's our ultimate method in C#:

C#
1public static double FindMaxValue(List<int> listA, List<int> listB) 2{ 3 int indexA = 0; 4 int? indexB = null; 5 bool inListA = true; 6 double maxValue = double.MinValue; 7 8 while (true) 9 { 10 if (inListA) 11 { 12 indexB = listA[indexA]; 13 if (listB[indexB.Value] > maxValue) 14 { 15 maxValue = listB[indexB.Value]; 16 } 17 } 18 else 19 { 20 indexA = listB[indexB.Value]; 21 if (indexA == 0) 22 { 23 return maxValue; 24 } 25 } 26 inListA = !inListA; 27 } 28}
Main Method and Example Usage

Let's extend our solution further by incorporating a Main method that serves as the entry point of our program. We'll also provide an example usage of the FindMaxValue method and display the output.

C#
1using System; 2using System.Collections.Generic; 3 4class Program 5{ 6 public static void Main(string[] args) 7 { 8 List<int> listA = new List<int> { 2, 4, 3, 1, 2 }; 9 List<int> listB = new List<int> { 4, 0, 3, 2, 0 }; 10 11 double maxValue = FindMaxValue(listA, listB); 12 13 Console.WriteLine("Maximum value encountered in listB during Gloria's adventure: " + maxValue); 14 } 15 16 public static double FindMaxValue(List<int> listA, List<int> listB) 17 { 18 int indexA = 0; 19 int? indexB = null; 20 bool inListA = true; 21 double maxValue = double.MinValue; 22 23 while (true) 24 { 25 if (inListA) 26 { 27 indexB = listA[indexA]; 28 if (listB[indexB.Value] > maxValue) 29 { 30 maxValue = listB[indexB.Value]; 31 } 32 } 33 else 34 { 35 indexA = listB[indexB.Value]; 36 if (indexA == 0) 37 { 38 return maxValue; 39 } 40 } 41 inListA = !inListA; 42 } 43 } 44}

Example Output

1Maximum value encountered in listB during Gloria's adventure: 3
Lesson Summary

Heartiest congratulations on guiding Gloria through her list hopping adventure. Not only have you heightened Gloria's joy, but you've also skillfully solved a complex task. You've deftly handled lists, tracked indices, and made careful use of conditional statements.

This experience should empower you to take on more complex coding challenges. Keep practicing, keep exploring, and keep growing. Happy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.