Swift Array Programming Exercises: Test if a given array of integers contains two 5's next to each other
Swift Array Programming: Exercise-32 with Solution
Write a Swift program to test if a given array of integers contains two 5's next to each other, or there are two 5's separated by one element.
Example: {5, 5, 1}, {5, 1, 5}
Pictorial Presentation:
Sample Solution:
Swift Code:
func num55(array_nums: [Int]) -> Bool {
for x in 0..<array_nums.count-1
{
if (array_nums[x] == 5 && array_nums[x+1] == 5) || (array_nums[x] == 5 && array_nums[x+2] == 5)
{
return true
}
}
return false
}
print(num55(array_nums: [1, 5, 5]))
print(num55(array_nums: [1, 5, 1, 5]))
print(num55(array_nums: [1, 5, 1, 1, 5]))
Sample Output:
true true false
Swift Programming Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a Swift program to check if a given array of integers contains a 3 next to a 3 or a 5 next to a 5, but not both.
Next:Write a Swift program to test if there is a 1 in the array with a 3 somewhere later in a given array of integers.
What is the difficulty level of this exercise?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/swift-programming-exercises/array/swift-array-exercise-32.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics