How to reset variable of seed on loop using stride with Swift? - ios

I want to reset the variable (seed) on loop using stride with Swift.
I have this code perfectly working on C#
for (int i = 0; i <= 10; i++)
{
//something
i = 0; //restart this value when necessary
}
And I'm trying this with swift
for var i in stride(from: 0, to: 10, by: 1){
//something
i = 0; //I need to reset this value when necessary, but not working
}
The variable "i" change for a second, but then returns to the original value and the behavior is different from C#.
Thanks.

As Alexander responded, the exact thing you're actually asking to do is villainous. I'm inclined to believe that a labeled do statement isn't going to be your best option, either, but it's the simplest solution without seeing any more code.
The following will print 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
var condition = true
loopReset: do {
for i in 0..<10 {
if condition, i > 5 {
condition = false
continue loopReset
}
print(i)
}
}

May use
var i = 0
while i <= 10 {
i += 1
// reset if necessary
}

Related

Pairing duplicate elements in an array

I am trying to pair the duplicate elements of an array and count the pairs.
When given array is : [10, 20, 20, 10, 10, 30, 50, 10, 20], I'm expecting numberOfPairs to be 3. Because there are 2 pairs of 10s and 1 pair of 20.
My "if condition" is checking if current element's index is first index or not. If it is not the last index, it means that there is a duplicate of the current element. So I'am adding 1 to numberOfPairs.
For input [10, 20, 20, 10, 10, 30, 50, 10, 20], my numberOfPairs is 2 but it should be 3.
For input [1 1 3 1 2 1 3 3 3 3], myNumberOfPairs is not printing at all? But instead it should be 4.
My What am I missing here?
func sockMerchant(n: Int, ar: [Int]) -> Int {
// Write your code here
var array = ar
var numberOfPairs = 0
for i in 0..<array.count {
var element = array[i]
let indexOfLastElement = array.lastIndex(of: element)
let indexOfFirstElement = array.firstIndex(of: element)
print("indexOfLastElement is \(indexOfLastElement)")
print("indexOfFirstElement is \(indexOfFirstElement)")
if indexOfFirstElement != indexOfLastElement {
numberOfPairs += 1
array.remove(at: indexOfFirstElement!)
array.remove(at: indexOfLastElement!)
continue
}
return numberOfPairs
}
return numberOfPairs
}
You're mutating your array by calling remove(at:) at the same time as you're accessing it which is why you're having these weird side effects.
I assume you're trying to solve a Leetcode task (or something similar), so I won't provide a solution upfront. My suggestion for you is to think of an algorithm that doesn't involve changing the contents of the List while you're reading these contents of that same List.
I'm agree with #MartinR that in such cases you should place breakpoints and go throught your code line by line, glad you've found your mistake by yourself.
But also in terms of performance, lastIndex and firstIndex are very heavy operations, because they may go thought all items and find nothing, which makes Big O notation of your algorithm around O(log n). In such cases dictionary is widely used(if you're not much limited with the space).
You can use value as a key and count as a value for a dictionary and count all items, then just sum like this:
func sockMerchant(ar: [Int]) -> Int {
ar.reduce(into: [Int:Int]()) { map, value in
map[value, default: 0] += 1
}.reduce(0) { sum, count in
sum + count.value / 2
}
}
So, I've solved the problem as below, thanks to #Vym and #Martin R.
func sockMerchant(n: Int, ar: [Int]) -> Int {
// Write your code here
var array = ar
var numberOfPairs = 0
var newArray = [Int]()
var done = false
for i in 0..<array.count {
let element = array[i]
let indexOfLastElement = array.lastIndex(of: element)
let indexOfFirstElement = array.firstIndex(of: element)
if indexOfFirstElement != indexOfLastElement {
newArray.append(element)
numberOfPairs = newArray.count/2
done = true
}
}
if done == true {
return numberOfPairs
}
return numberOfPairs
}

Compare array to reference array and remove duplicates [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I can't understand how to rewrite this expression in more "swift" and efficient way:
for result in results {
var isExists = false
for ref in referenceArray {
if result.id == ref.id {
isExists = true
break
}
}
if isExists == false {
filteredResults.append(result)
}
}
I tried this:
filteredResults = results.filter { result in
referenceArray.contains { $0.id != result.id }
}
But it gives me empty array.
Thanks.
Correct me if I'm wrong but it sounds like you want to do something like this:
Given a set of items A and set of items B, create a set of items C with only the items that are in B and not in A.
To paraphrase, I think you're looking for the "new" things in B that don't already exist in A.
If this is what you're trying to do you can use a Set. This is a trivial example with Ints, but hopefully it'll help:
let setA = Set([1,2,3,4,5,6,7,8,9,10])
let setB = Set([2,4,6,8,10,13])
// Only the values that overlap both sets
let evens = setA.intersection(setB) // {6, 10, 2, 4, 8}
// Only the values that do not overlap both sets
let odds = setA.symmetricDifference(setB) // {9, 5, 7, 3, 1, 13}
// All unique elements in both sets
let uniqueToBoth = setA.union(setB) // {13, 10, 2, 4, 9, 5, 6, 7, 3, 1, 8}
// Only elements unique to B
let uniqueToB = setB.subtracting(setA) // {13}

How to remove one duplicate value in an array?

I have two arrays for which I am comparing [Int]
let filter = strongAgainstArray.filter{weakAgainstArray.contains($0)}
This returns an array of common values in the 2 arrays. I then want to go through and remove those values from each array, which I'm doing like so
for item in filter {
for var i = 0; i < strongAgainstArray.count; i += 1 {
if item == strongAgainstArray[i] {
strongAgainstArray.removeAtIndex(i)
print("Removing a Strong against index \(item)")
}
}
for var i = 0; i < weakAgainstArray.count; i += 1 {
if item == weakAgainstArray[i] {
weakAgainstArray.removeAtIndex(i)
print("Removing a Weak against index \(item)")
}
}
}
This works fine, but let's say one of my arrays contains two entries for 12 as an example. How do I only remove one of them? As it stands, all entries of 12 are being removed entirely.
EDIT
I'm now comparing my two arrays using
let commonValues = Array(Set(strongAgainstArray).intersect(weakAgainstArray))
and then those commonValues from each array with
cleanStrongAgainstArray = Array(Set(strongAgainstArray).subtract(Set(commonValues)).sort())
cleanWeakAgainstArray = Array(Set(weakAgainstArray).subtract(Set(commonValues)).sort())
This is a much better overall solution, but I'm still eventually running into the same issue, albeit slightly different than before.
In the playground, for example...
let array = [7,7,9]
let test = Array(Set(array))
test comes back containing [7, 9], and I need to keep that extra 7. How do I do that?
If the order of the arrays aren't important then you can easily achieve the whole solution using Sets:
let dirtyArray = [1,4,6,1,56,4,4,66,23,3,3,12]
let dirtyArray1 = [3,1,6,99,54]
let cleanArray = Array(Set(dirtyArray).union(Set(dirtyArray1)))
print (cleanArray)
[12, 54, 23, 4, 6, 66, 99, 56, 1, 3]
If order is important, use NSOrderedSet:
let strongAgainstArray = [1, 2, 3, 4]
let weakAgainstArray = [3, 4, 5, 6]
let result = NSOrderedSet(array: (strongAgainstArray + weakAgainstArray)).array

Sum progression in an array Swift

I have an basic Int array
Array = [8, 9, 8]
How do i sum all of its values progressively so that the end result would look like this
EndResult = [8, 17, 25]
Tried using for and while loops, but to no avail.
NB: Basic array[0] + array[1] advices will not work. I'm looking for something automatic like a loop solution.
Looking forward to your advices.
Thanks!
May be this:
var arr = [8, 9, 8]
for i in 1..<arr.count {
arr[i] += arr[i-1]
}
print(arr)
Probably there are better ways than this one, but it works
var array = [8, 9, 8]
var result = [Int]()
for i in 0..<array.count{
var temp = 0;
for j in 0...i{
temp+=array[j]
}
result.append(temp)
}
print(result) //[8, 17, 25]
You could use a reduce function in Swift to accomplish this. Note that you can't really do it with map, because you would need to know what the previous call to the map function returned, keep state in a variable outside the map function (which seems dirty), or loop over your array for every map function call.
let array = [8, 9, 8]
let results = array.reduce((0, []), combine: { (reduction: (lastValue: Int, values: Array<Int>), value: Int) in
let newValue = reduction.lastValue + value
return (newValue, reduction.values + [newValue])
}).1

No output in Swift Playground

I am trying to put fibonacci number in an array and wanted to see the array output in playground console but for some reason I do not see any ouput. Can someone plz help in making me understand the mistake that I am cdoing in my program ?
import UIKit
class FibonacciSequence {
let includesZero: Bool
let values: [Int]
init(maxNumber: Int, includesZero: Bool) {
self.includesZero = includesZero
values = [0]
var counter: Int
if (includesZero == true) { counter = 0 }
else { counter = 1 }
for counter <= maxNumber; {
if ( counter == 0 ) {
values.append(0)
counter = 1
}
else {
counter = counter + counter
values.append(counter)
}
}
println(values)
}
println(values)
return values
}
let fibanocciSequence = FibonacciSequence(maxNumber:123, includesZero: true)
#ABakerSmith has given you a good rundown of the problems in the code as-is, but you also might want to consider, instead of a class that initializes an array member variable, writing a SequenceType that returns fibonacci numbers:
struct FibonacciSequence: SequenceType {
let maxNumber: Int
let includesZero: Bool
func generate() -> GeneratorOf<Int> {
var (i, j) = includesZero ? (0,1) : (1,1)
return GeneratorOf {
(i, j) = (j, i+j)
return (i < self.maxNumber) ? i : nil
}
}
}
let seq = FibonacciSequence(maxNumber: 20, includesZero: false)
// no arrays were harmed in the generation of this for loop
for num in seq {
println(num)
}
// if you want it in array form:
let array = Array(seq)
You could of course memoize the sequence if you want to improve performance on multiple generations.
Your problem is your code has errors in it; if there are errors in your code Playgrounds won't run it and you won't get any output.
On the line for counter <= maxNumber; you've got a semi-colon, but also, I'm pretty sure you can't declare a for loop like that, unless I'm missing something? You could use a while loop though.
Why are you trying to return values from your init method?
You've declared values as a constant but are then trying to change it using append.
Using this code and fixing the errors stated does not produce the Fibonacci sequence, instead it produces: [0, 0, 2, 4, 8, 16, 32, 64, 128]
Try this code:
class FibonacciSequence {
let values: [Int]
init(maxNumber: Int, includesZero: Bool) {
var tempValues = includesZero ? [0] : [1]
var current = 1
do {
tempValues.append(current)
let nMinus2 = tempValues[tempValues.count - 2]
let nMinus1 = tempValues[tempValues.count - 1]
current = nMinus2 + nMinus1
} while current <= maxNumber
self.values = tempValues
}
}
Then create an instance:
let fibanocciSequence = FibonacciSequence(maxNumber:123, includesZero: true)
println(fibanocciSequence.values) // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
Hope that helps!

Resources