Trying to access some elements in an IndexSet - swift

I'm using IndexSet and I'm trying to access some indexes which at times are consecutive and at other times are not.
For example, my set may contain [1, 2, 3, 5, 6, 7, 13, 31]
I want to pull out of the set a range of 3...13, but am having difficulty with the syntax. I've learned how to use the function commands from Apple documentation, by using myIndexSet.sorted(). However, the Apple Documentation does not give an example of how to access a range of elements in the set. The Apple Documentation for accessing elements in the index set are the following:
subscript(Range<IndexSet.Index>)
I've tried a number of ways to write this but can't figure out how to do it right. Can someone show me how to access a range of elements in the set to create a new set? I've tried things such as:
let subset = subscript(Range: myLargerSet.3...13)
but it doesn't seem to work.
Thanks

What you're looking for is the intersection of your IndexSet ([1, 2, 3, 5, 6, 7, 13, 31]) with another IndexSet ([3, 4, ..., 12, 13]):
let yourIndexSet: IndexSet = [1, 2, 3, 5, 6, 7, 13, 31]
let desiredIndexRange = IndexSet(3...13)
let indicesOfInterest = yourIndexSet.intersection(desiredIndexRange)
print(indicesOfInterest.sorted()) // => [3, 5, 6, 7, 13]

One possible solution is to use a filter to create a new IndexSet.
let set = IndexSet(arrayLiteral: 1,2,3,5,6,7,13,31)
let subset = set.filteredIndexSet { (index) -> Bool in
index >= 3 && index <= 13
}

You can access a slice of your original set as follows:
let slice = indexSet[indexSet.indexRange(in: 3...13)]
slice accesses the existing elements in place, so creation of the slice is O(1)

Related

Swift - Declaring a Set using named variable

I am trying to understand Sets in Swift and how to declare them correctly but I have found the following a little confusing.
I understand that this works:
let elements = [ 1, 2, 3, 4, 5, 1, 2, 6, 7]
let setFromElements = Set(elements)
But I don't understand why the following doesn't:
let setFromElements : Set = elements
Or even:
let setFromElements : Set<Int> = elements
When the following is valid:
let setFromArray : Set = [ 1, 2, 4, 5]
Can someone please help me understand why this is the case?
let setFromArray: Set = [ 1, 2, 4, 5] works because Set conforms to ExpressibleByArrayLiteral and hence has an initializer that takes an ArrayLiteral. See Set.init(arrayLiteral:). This conformance gives syntactic sugar for not having to explicitly call the init.
On the other hand, once you save the array literal into a variable using let elements = [ 1, 2, 3, 4, 5, 1, 2, 6, 7], elements becomes an Array, not an ArrayLiteral and hence another initializer of Set has to be called that takes an Array. This init does not provide syntactic sugar like ExpressibleByArrayLiteral does, so you explicitly have to call the init by doing Set(array).
Set has an initializer that takes an array, and that makes a set, by taking the unique items in the array. But a set is not an array, two different types, so you can't just use = to assign one to the other.

Very Basic Misunderstanding of for loops in Swift

I'm new to swift. can someone please explain what I'm doing wrong here.
1.
var numbers = [1, 5, 7, 6, 6, 6, 6, 6, 2]
for i in numbers{
print(numbers[i],terminator: "")
}
why doesn't this just print the numbers in the array?
2.
Here I want to set the elements in the array to a random number from 0 to 2, and then print them.
for j in numbers{
numbers[j] = Int.random(in: 0...2)
print(numbers[j],terminator: "")
}
this seems to work, but then if, outside of the for loop, I print them again:
for k in numbers{
print(numbers[k],terminator: "")
}
It outputs different numbers, from 0 to 2
3.
OK so I try a different syntax:
for m in numbers{
print(m,terminator: "")
}
now I get the same numbers every time and they are not from 0 to 2... I'm sure my mistakes are trivial but an explanation would help me out. Thanks.
to achieve what you expect, you need to loop over the indices of the array, like this:
var numbers = [1, 5, 7, 6, 6, 6, 6, 6, 2]
for i in numbers.indices { // <-- here
print(numbers[i])
}
And as mentioned, read the basics of Swift.

How to replace slice of array in Swift?

To replace a slice of array in Swift, we can use subscript assignment or replaceSubrange method on array. But, I wonder how can I change the elements of named subrange so that original array change as well?
var array = Array(0..<10)
var slice = array[0..<3]
slice[...] = [-1,-1,-1]
print(slice) // [-1, -1, -1]
print(array) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] does not change

How to add integer to array (with explicite int index) in swift?

I read swift handbook and was trying to do some exercises. But I run into a problem and I do not know if I do something wrong or if xCode 6 beta is just buggy.
// Playground - noun: a place where people can play
import Cocoa
let interestingNumbers = [
"Prime": [2, 3, 5, 7, 11, 13],
"Fibonacci": [1, 1, 2, 3, 5, 8],
"Square": [1, 4, 9, 16, 25],
]
var largest = 0
var lastLargest = Integer[]()
var index = 0
for (kind, numbers) in interestingNumbers {
for number in numbers {
if number > largest {
//lastLargest[index] = number
index++
largest = number
}
}
}
index
lastLargest
largest
As soon as I uncomment lastLargest[index] = number I do not get any results on right side in playground. Nor I get any infos about index, lastLargest or largest.
Following example does not work either:
var index2 = 0
var lastLargest2 = Integer[]()
lastLargest2[index2] = 1
index2++
lastLargest2[index2] = 2
You are appending using an out of bound array-index. Don't do that. Instead, use append:
lastLargest.append(number)
From Apple's documentation:
You can’t use subscript syntax to append a new item to the end of an array. If you try to use subscript syntax to retrieve or set a value for an index that is outside of an array’s existing bounds, you will trigger a runtime error.
When you're using explicit indexes (subscript notation) to set values in a mutable array, some value must already exist in that array at that index. When you use subscript notation, you're essentially using a 'set', rather than a 'set and add if necessary'.
As a result, you should be using:
lastLargest.insert(number, atIndex: index)
If you want to insert a new item. This will let you insert an item at the specified index, assuming your collection's size is already greater than or equal to the index you're trying to replace.

Matlab -- Finding missing number in a list

I have a relatively large data set, and I'm looking for the missing number via MatLab.
For example, I have a list of numbers that might look like:
1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 6, 6, 7, 7, 7, 7, 9, 10, 10.....
You can see the 8 is missing here. The list is in the thousands, and there are maybe just a couple missing numbers. How can I find out which ones are missing? My search only turned up useful results without randomly repeating numbers. Seems simple but I can't figure it out.
Thanks for help!
Use unique, like this:
B=unique(A); % A is your data
C=setdiff(1:max(A),B)
and C is your desired missing numbers.
EDIT (afetr seeing claj's answer):
If your data starts from another value (not "1"), the second line should be:
C=setdiff(min(A):max(A),B)
EDIT2: (according to Eitan's comment)
C=setdiff(min(A):max(A),A);
This line replaces the two lines from the original answer.
You could do something like this:
% Your data:
data = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 6, 6, 7, 7, 7, 7, 9, 10, 10];
for i = 1:data(end)
if (isempty(find(data==i)))
disp(['i = ',num2str(i)]);
end
end
Which will print out the values of the missing elements.
Or even simpler you could just use the ismember() function to construct
the set difference in just a single line below.
% First enter your data and construct 'set':
data = [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5, 5, 6, 6, 7, 7, 7, 7, 9, 10, 10];
set = data(1):data(end);
Then to determine which elements of 'set' are also in 'data':
ismember(set, data)
The output then shows the locations in 'set' where the data is missing:
ans =
1 1 1 1 1 1 1 0 1 1
Use the ismember() function to check if a number is member of the data array
% set your data array
maximum = max(data);
minimum = min(data);
for i= minimum:maximum
if ~ismember(i,data);
disp([num2str(i) , ' is missed']);
end
end
Create a unique list of values in the array.
Find the min and max numbers in this unique set (these should be the same numbers as in the array, but quicker to find).
Create a range from min to max like [min:max].
Make a set difference of the uniqued array and the range-set.
This gives you the missing numbers in decently quick way.
this is similar to a few of the above but the simplest i've found is
find(~ismember(set,data))
which will return the indices of the members of set that are not in data