Exercise: Screening Messages in Arrays and Loops - Develop in Swift Explorations

Hi there! I'm stuck. aliceMessages array is just a really long conversation. Here is the exercise (17th page of Arrays and Loops lesson):

Create a for…in loop to process the aliceMessages array by iterating through its legal indices. In the body of the loop, use the index to access a message from the array and check whether it contains the string "Caterpillar". If the message refers to the Caterpillar, print it to the console, along with its index. This process is called a linear search. Linear search is a common algorithm for processing arrays by examining its contents in order and evaluating each item to determine whether it meets certain criteria

Thanks for your help!

Answered by Claude31 in 721513022

You were nearly there:

for messageNumber in 0 ... aliceMessages.count - 1 {
    if aliceMessages.contains("Caterpillar") {
        print("\(messageNumber): \(aliceMessages[messageNumber])")
    }
}

But:

  • aliceMessages.contains tests for exact match:
  • I created this test:
let aliceMessages = ["A Caterpillar", "other machine"]
if aliceMessages.contains("Caterpillar") { print("OK") } else { print("aliceMessages Does not contain Caterpillar")}
if aliceMessages.contains("A Caterpillar") { print("OK A") }

You get

aliceMessages Does not contain Caterpillar
OK A
  • Reason is when you test aliceMessages.contains("Caterpillar"), you test for exact match in the array, for for item containing the word.

Just change forfor messageNumber in 0 ... aliceMessages.count - 1 {

    if aliceMessages[messageNumber].contains("Caterpillar") {
        print("\(messageNumber): \(aliceMessages[messageNumber])")
    }
}

And you get

0: A Caterpillar

Forum is not here to do exercise for you…

So please, show what you tried, explain what does not work or what you don't understand.

And provide more context than just a reference to the exercise.

Some hint, without seeing the context.

Loop could be like:

for (index, message) in aliceMessages.enumerated() {

}

Of course. Sorry, but I'm new here. This is what i tried:

for messageNumber in 0 ... aliceMessages.count - 1 {

    if aliceMessages.contains("Caterpillar") {

        print("\(messageNumber): \(aliceMessages[messageNumber])")

    }

}

But nothing is showing up. If I do this, without if statement, I get very line of array, numbered:

for messageNumber in 0 ... aliceMessages.count - 1 {

        print("\(messageNumber): \(aliceMessages[messageNumber])")
}

and if I do this I get lines that contain "Caterpillar" string, but i don't know how to number them in correct order:

for messages in aliceMessages {

    if messages.contains("Caterpillar") {

        print("\(messages)")

    }

}

and this is the context:

Exercise: Screening Messages

You've somehow come to possess a huge list of messages about a series of characters: Caterpillar, Dormouse, Cheshire Cat, and others. The list is contained in the aliceMessages constant below.

Try to print out the aliceMessages array to see the whole list, but beware: It's large enough that it may cause your playground to run slowly.

The Caterpillar has asked you to go through the messages and to relay any that contain the Caterpillar's name, along with the message number. Instead of reading all the text yourself, you decide to write more code to help.

You'll use another kind of for...in loop on this page. You're accustomed to the loop variable containing the items from the array, but in this case, you want to print the message number. So your loop will iterate over the indices of the array instead of its contents. Here's an example of iterating over the indices of an array.

let colors = ["Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"]

for i in 0 ... colors.count - 1 {

    print("\(i): \(colors[i])")
}

The loop variable is i. Its values are defined by the range expression to the right. It uses the ... operator to define a series of values starting with the one on the left, going up through the one on the right. To generate the indices of an array, the start of the range is 0, and the end is the last index, which is one less than the count of the array.

exercise: Create a for…in loop to process the aliceMessages array by iterating through its legal indices.

In the body of the loop, use the index to access a message from the array and check whether it contains the string "Caterpillar".

If the message refers to the Caterpillar, print it to the console, along with its index.

This process is called a linear search. Linear search is a common algorithm for processing arrays by examining its contents in order and evaluating each item to determine whether it meets certain criteria.

// Write the `for…in` loop here:
Accepted Answer

You were nearly there:

for messageNumber in 0 ... aliceMessages.count - 1 {
    if aliceMessages.contains("Caterpillar") {
        print("\(messageNumber): \(aliceMessages[messageNumber])")
    }
}

But:

  • aliceMessages.contains tests for exact match:
  • I created this test:
let aliceMessages = ["A Caterpillar", "other machine"]
if aliceMessages.contains("Caterpillar") { print("OK") } else { print("aliceMessages Does not contain Caterpillar")}
if aliceMessages.contains("A Caterpillar") { print("OK A") }

You get

aliceMessages Does not contain Caterpillar
OK A
  • Reason is when you test aliceMessages.contains("Caterpillar"), you test for exact match in the array, for for item containing the word.

Just change forfor messageNumber in 0 ... aliceMessages.count - 1 {

    if aliceMessages[messageNumber].contains("Caterpillar") {
        print("\(messageNumber): \(aliceMessages[messageNumber])")
    }
}

And you get

0: A Caterpillar

It worked and now I understand why it wasn't printing anything. Thanks a lot for your help! :)

Exercise: Screening Messages in Arrays and Loops - Develop in Swift Explorations
 
 
Q