Loop Review: Coding Challenge On Swift

The directions for the loop review challenge were simple: Loop through a string and count how many characters are in it, and I have pasted my code below. It works, however, I was wondering if there is a more efficient way to write this code?

var countString: String = "this string should return 39 characters"
var aCharacter = 0
for _ in countString {
  aCharacter += 1
}
print("'\(countString)' is \(aCharacter) characters long.")

And indeed…

'this string should return 39 characters' is 39 characters long.

Maybe it’s just me, but I feel like I’ve included an extra step or two, though I cannot quite place my finger on where…

Anyway, thanks for reading!

PS: for reference, this exercise comes from the loop review on the Swift course ^U^

Using loops, I think this is pretty much the most efficient way you can count the characters in a string. It has O(N) runtime as it iterates through each character in the input string. Another option using loops would be to use a while loop to go through each of the characters in the string until you’ve reached the end, counting along the way.

Of course, there are more efficient ways to count the number of characters in a string that don’t use loops (though your code is great since this exercise does want you to practice the concept). The most concise way would be to use the .count property of the string like so countString.count. That gives you the number of characters in the string as an integer. You will learn more about properties, methods, etc. later in the course.

Thank you soooo much!! I really appreciate your input :blush:, and as you said, there are many more things to learn as I progress further into the course… though it does make me more confident to go on when I can get validation (or get corrected haha) on something I’m attempting to learn by myself. So thank you again!

1 Like

No problem! Keep sharing your code and progress through the course, it’s a great way to get feedback and collaborate!

1 Like

I was also wondering… challenge 4 asks us to Recreate the following pattern using loops

*
**
***
****

It took me a while, but playing around with the code eventually got me to this:

var pattern = ""
for _ in 1...4 {
  pattern += "*"
  print(pattern)
}

The code above works fine, however, I also noticed that if I replace the numbers such that for _ in 1...4 is for _ in 0...3 it has the same result. How come that is so?

Thanks again!

What you are doing when you use 1...4 or 0...3 is creating a range that is inclusive of both the first and last number. For 1...4, the first iteration, _ = 1, the second iteration _ = 2, the third iteration _ = 3, and the fourth iteration _ = 4. For 0...3, it’s the same thing but with the numbers 0-3 rather than 1-4. As you can see, both of the ranges contain 4 numbers in total, and therefore both will result in your loop iterating 4 times in total, meaning you’ll get the same result.

Wow… thank you for putting it that way! That does make it a lot clearer >U<

If I may bother you with just one more question :point_right: :point_left:

I want to print out if a number is prime or not given a range of integers from 1 to 10. The code I wrote works to an extent…

for num in 1...10 {
  if num % 2 == 0 || num % 3 == 0{
    print ("\(num) is not prime")
  } else {
    print("\(num) is prime")
  }
}

…but the problem is that it outputs 2 and 3 are not prime.

// prints
1 is prime
2 is not prime
3 is not prime
// and so on...

So I am wondering, would I also have to explicitly write a condition checking if the number is not equal to 2 or 3, or is there another way using loops?

Reference: loop review exercise 2

Yes, you could just write a condition checking if the number is not 2 or 3, or is greater than 3, but there is another way you can accomplish this using loops. However, this method wouldn’t only be able to check numbers from one to 10, it would be able to check if any number was prime. Can you figure it out? Hint: this will require nested loops (one loop inside the other).

Aside: do you mind deleting your other post just so that we can keep the forums nice and tidy, thanks!

1 Like

Of course! It is done, sorry about that! I had made the other post earlier than when I followed up here, and so didn’t think to delete it… :sweat_smile:

Aha, thank you for the hint! The thing is that I haven’t fully learned how to code nested loops yet, so I will definitely keep this in mind. When I do though, I want to try and approach this problem again—and if I can solve it then, I will definitely let you know!

1 Like

Thanks for deleting the other post! Unfortunately, I don’t think the Swift course teaches nested loops (courses in other languages do), so you’ll have to learn that elsewhere. Let me know when you solve it, or if you need help doing so! :slight_smile:

1 Like

Yeah I was wondering about that, thank you for letting me know, and of course, for all your help!! I am actually also very into learning more JavaScript, so I think I may be able to pick it up there :sweat_smile:

I will definitely keep you posted, much thanks again!

1 Like