Sets and Membership
Lesson 22 of 48 in Coddy's Swift for C Developers course.
C can model a small fixed universe of permissions with flags or a bit mask; a general set requires an explicit container implementation. Swift Set stores unique Hashable values and supports insertion, removal and membership. A count is the number of distinct present members, not a history of insert calls.
The source flags and target set both revoke write permission while retaining read. Swift Set is a value type, so a copied set can evolve independently. The Boolean C representation is specific to two known permissions and is not a general replacement for arbitrary string membership.
C: complete program
main.c
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(void) {
bool read_permission = true;
bool write_permission = true;
write_permission = false;
printf("%s %d\n", write_permission ? "true" : "false", (int)read_permission + (int)write_permission);
return 0;
}
Swift: complete program
main.swift
import Foundation
var permissions: Set<String> = ["read", "write"]
permissions.remove("write")
print("\(permissions.contains("write")) \(permissions.count)")Neither membership nor uniqueness implies sorted display order. Sort a suitable sequence explicitly when deterministic presentation matters.
Challenge
EasyImplement overlap(_:_:): return distinct shared integers sorted ascending. Input is two lists separated by one |; each list contains 0 through 30 whitespace-separated integers -100 through 100. Output space-separated shared values or a blank line.
Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.
Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.
Try it yourself
import Foundation
func apiChecks(_ input: String) -> Bool {
return overlap([4,4,2],[2,4,4]) == [2,4] && overlap([], [1]) == []
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for C Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry Rules5Collection Tools
Dictionaries and Missing KeysSets and MembershipUnicode and Character IndicesClosures and TransformationsRecap: Catalog MergePractice on your own: Swift playground