Last active
December 17, 2020 10:09
-
-
Save gozzoo/9c2d40c3beb814ce9ec9bd1ab4d1176d to your computer and use it in GitHub Desktop.
swift problems
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func find(_ arr: [Int],_ num: Int) -> Bool { | |
var leftS = 0 , rightS = arr.count - 1 | |
while leftS <= rightS { | |
var mid = (leftS + rightS) / 2 | |
if arr[mid] == num { | |
return true | |
} else if arr[mid] < num { | |
leftS = mid + 1 | |
} else { | |
rightS = mid - 1 | |
} | |
} | |
return false | |
} | |
let a = [1,2,4,6,8,10] | |
let search = 1 | |
let start = find(a, search) | |
print(start) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Brick { | |
var x, y, z: Int | |
} | |
struct Hole { | |
var x, y: Int | |
func fit(_ brick: Brick) -> Bool { | |
return fitSide(brick.x, brick.y) || | |
fitSide(brick.x, brick.z) || | |
fitSide(brick.z, brick.y) | |
} | |
func fitSide(_ x: Int, _ y: Int) -> Bool { | |
return self.x > x && self.y > y || self.x > y && self.y > x | |
} | |
} | |
var brick = Brick(x: 1, y: 2, z: 3) | |
var hole = Hole(x: 1, y: 2) | |
print(hole.fit(brick)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let defaultBufSize = 20 | |
class CircularBuffer { | |
var a: [Int] | |
private var widx = 0, ridx = 0 | |
init(size: Int = defaultBufSize) { | |
self.a = Array(repeating: 0, count: size) | |
} | |
func write(data: [Int]) { | |
for d in data { | |
a[idx] = d | |
// idx = (idx + 1) % a.count | |
idx = (idx == a.count) ? 0 : idx + 1 | |
} | |
} | |
func write(_ data: [Int]) { | |
for d in data { | |
a[widx] = d | |
// idx = (idx + 1) % a.count | |
widx = (widx == a.count - 1) ? 0 : widx + 1 | |
} | |
} | |
func read() -> [Int] { | |
var res: [Int] = [] | |
var i = ridx | |
while i < widx { | |
res.append(a[i]) | |
i = (i == a.count - 1) ? 0 : i + 1 | |
} | |
ridx = widx | |
return res | |
} | |
} | |
var cb = CircularBuffer() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func parseCSV(_ line: String) -> [String] { | |
var result = [String](); | |
var value = ""; | |
var singleQuote = false, doubleQuote = false | |
for c in line { | |
switch c { | |
case ",": | |
if singleQuote || doubleQuote { | |
value += String(c) | |
} else { | |
result.append(value) | |
value = "" | |
} | |
case "'": | |
if doubleQuote { | |
value += String(c) | |
} else { | |
singleQuote = !singleQuote | |
} | |
case "\"": | |
if singleQuote { | |
value += String(c) | |
} else { | |
doubleQuote = !doubleQuote | |
} | |
case " ": | |
if singleQuote || doubleQuote { | |
value += String(c) | |
} | |
default: | |
value += String(c) | |
} | |
} | |
result.append(value) | |
return result | |
} | |
var line = """ | |
12, 'aa, aa' , "bb bb", ala bala | |
""" | |
let result = parseCSV(line); | |
print(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum ParserState { case inQuote, noQuote } | |
func parseCSV(_ line: String) -> [String] { | |
var result = [String](); | |
var value = ""; | |
var parserState: ParserState = .noQuote | |
var quote: Character = " " | |
for c in line { | |
switch c { | |
case ",": | |
if parserState != .noQuote { | |
value += String(c) | |
} else { | |
result.append(value) | |
value = "" | |
} | |
case "'", "\"": | |
if parserState == .inQuote { | |
if c != quote { | |
value += String(c) | |
} else { | |
parserState = .noQuote | |
} | |
} else { | |
parserState = .inQuote | |
quote = c | |
} | |
case " ": | |
if parserState != .noQuote { | |
value += String(c) | |
} | |
default: | |
value += String(c) | |
} | |
} | |
result.append(value) | |
return result | |
} | |
var line = """ | |
12, 'aa, aa' , "bb bb", ala bala | |
""" | |
let result = parseCSV(line); | |
print(result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] | |
func days(_ date: Int) -> Int { | |
var d = date / 100 | |
var m = date % 100 - 1 | |
var result = 0 | |
m -= 1 | |
while m >= 0 { | |
result += daysInMonth[m] | |
m -= 1 | |
} | |
result += d | |
return result | |
} | |
func datesDiff(_ date1: Int, _ date2: Int) -> Int { | |
var d1 = days(date1) | |
var d2 = days(date2) | |
return d1 < d2 ? -1 : (d1 - d2) | |
} | |
var d = datesDiff(0112, 3011) | |
print(d) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isLeapYear (_ year : Int ) -> Bool { | |
if year % 4 == 0 { | |
if year % 100 == 0 && year % 400 != 0 { | |
return false | |
} | |
return true | |
} | |
return f | |
} | |
func daysSince2000(_ y: Int, _ m: Int, _ d: Int) -> Int { | |
var result = 0 | |
//for i in stride(from: 2000, to: y, by: 4) { | |
// | |
//} | |
//result += 15 * 365 + | |
for yy in 2000..<y { | |
reulst += 365 | |
if isLeapYear(yy) { | |
result += 1 | |
} | |
} | |
return result | |
} | |
let day01012000 = 1 // tuesday | |
let dayNames = ["monday", "tuesday"] | |
func dayOfWeek(_ date: Int) -> String { | |
var y = date % 10000 | |
var date = date / 10000 | |
var d = date / 100 | |
var m = date % 100 - 1 | |
var days = daysSince2000(y, m, d) | |
// 7 + 1 - days since the monday of the week of start day: day01012000 | |
// 8.01.2000 - > (7 + 1) % 7 = 1 | |
var dayOfWeek = (days + day01012000) % 7 | |
return dayNames[dayOfWeek] | |
} | |
var day = dayOfWeek(12122020) | |
print("day: ", day) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findMissingNum(_ s: String) -> Int { | |
var maxNumLength = s.count / 2 | |
for numLength in 1 ... maxNumLength { | |
if var seq = genSeqence(s, numLength: numLength) { | |
return seq | |
} | |
} | |
return -1 | |
} | |
func genSeqence(_ s: String, numLength: Int) -> Int? { | |
var start = s.prefix(numLength) | |
guard let num = Int(start) else { | |
return nil | |
} | |
var missing = num + 1 | |
while true { | |
var res = start | |
var n = num + 1 | |
while res.count < s.count { | |
if n != missing { | |
res += String(n) | |
} | |
n += 1 | |
} | |
if res == s { | |
return missing | |
} | |
missing += 1 | |
if n == missing { | |
return nil | |
} | |
} | |
} | |
var s = "2526282930" | |
var num = findMissingNum(s) | |
print(num) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sortByFrequency(_ a: [Int]) -> [Int] { | |
var result: [Int] = [] | |
var frequency = Array(repeating: 0, count: 10) | |
for i in a { | |
frequency[i] += 1 | |
} | |
while true { | |
var (count, x) = max(frequency) | |
if count == 0 { | |
break | |
} | |
for i in 1 ... count { | |
result.append(x) | |
} | |
frequency[x] = 0 | |
} | |
return result | |
} | |
func max(_ a: [Int]) -> (Int, Int) { | |
var max = 0 | |
var pos = 0, maxPos = 0 | |
while pos < a.count { | |
var i = a[pos] | |
if i > max { | |
max = i | |
maxPos = pos | |
} | |
pos += 1 | |
} | |
return (max, maxPos) | |
} | |
var a = [2,3,5,3,7,9,5,3,7] | |
var b = sortByFrequency(a) | |
print(b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func generateSeqencue(_ n: Int) -> [Int] { | |
var i = 0 | |
var result = [i] | |
while result.count < n { | |
var j = 1 | |
while true { | |
if !hasSameDigits(i, j) && !result.contains(j) { | |
result.append(j) | |
i = j | |
break | |
} | |
j += 1 | |
} | |
} | |
return result | |
} | |
func hasSameDigits(_ i: Int, _ j: Int) -> Bool { | |
var digits = Array(repeating: false, count: 10) | |
var i = i, j = j | |
while i > 0 { | |
digits[i % 10] = true | |
i /= 10 | |
} | |
while j > 0 { | |
if digits[j % 10] { | |
return true | |
} | |
j /= 10 | |
} | |
return false | |
} | |
// 0,1,2,3,4,5,6,7,8,9,10,22,11,20,13,24... | |
var seq = generateSeqencue(150) | |
print(seq) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func printByCity(_ people: [Person]) { | |
var dict: [String:[String]] = [:] | |
for p in people { | |
if var peopleFromCity: [String] = dict[p.city] { | |
peopleFromCity[p.city].append(p.name) | |
dict[p.city] = peopleFromCity | |
} else { | |
var a: [String] = [] | |
a.append(p.name) | |
dict[p.city] = a | |
} | |
} | |
for (city, peopleFromCity) in dict { | |
print(city) | |
for p in peopleFromCity { | |
print(" ", p) | |
} | |
} | |
} | |
struct Person { | |
var name: String | |
var city: String | |
} | |
var people: [Person] = [] | |
func addPerson(_ n: String, _ c: String) { | |
var p = Person(name: n, city: c) | |
people.append(p) | |
} | |
addPerson("Ivan", "Sofia") | |
addPerson("Stoyan", "Varna") | |
addPerson("Diana", "Sofia") | |
addPerson("Kaloyan", "Plovdiv") | |
addPerson("Maria", "Burgas") | |
printByCity(people) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func maxNumber(_ y: Int) -> Int { | |
var x = y | |
var digits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] | |
while x > 0 { | |
var d = x % 10 | |
digits[d] += 1 | |
x /= 10 | |
} | |
var result = 0 | |
var i = 9 | |
while i >= 0 { | |
var count = digits[i] | |
while count > 0 { | |
result *= 10 | |
result += i | |
count -= 1 | |
} | |
i -= 1 | |
} | |
return result | |
} | |
let x = maxNumber(26001) | |
print(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func merge(_ a1: [Int], _ a2: [Int]) -> [Int] { | |
var i1 = 0, i2 = 0 | |
var result: [Int] = [] | |
while i1 < a1.count && i2 < a2.count { | |
if a1[i1] < a2[i2] { | |
result.append(a1[i1]) | |
i1 += 1 | |
} else { | |
result.append(a2[i2]) | |
i2 += 1 | |
} | |
} | |
var (b, i) = (i1 == a1.count) ? (a2, i2) : (a1, i1) | |
result += b[i...] | |
return result | |
} | |
let a = [1, 3, 5, 7] | |
let b = [2, 4, 6] | |
let c = merge(a, b) | |
print(c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isPrime(_ num: Int) -> Bool{ | |
if num <= 2 { | |
return true | |
} | |
for i in 2 ..< num { | |
if (num % i) == 0 { | |
return false | |
} | |
} | |
return true; | |
} | |
func primes(_ end: Int) -> [Int]{ | |
var result = [Int]() | |
for i in 1 ... end { | |
if isPrime(i) { | |
result.append(i) | |
} | |
} | |
return result | |
} | |
let res = primes(100) | |
print(res) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func isPrime(_ num: Int, _ primes: [Int]) -> Bool { | |
if num <= 2 { | |
return true | |
} | |
for p in primes { | |
if num % p == 0 { | |
return false | |
} | |
} | |
return true; | |
} | |
func primes(_ end: Int) -> [Int] { | |
var result = [Int]() | |
for i in 2 ... end { | |
if isPrime(i, result) { | |
result.append(i) | |
} | |
} | |
return result | |
} | |
let res = primes(100) | |
print(res) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func primes(_ end: Int) -> [Int]{ | |
var result = [Int]() | |
var arr = Array(repeating: false, count: end + 1) | |
for i in 2 ... end { | |
if arr[i] { | |
continue | |
} | |
result.append(i) | |
for j in stride(from: i * i, through: end, by: i) { | |
arr[j] = true | |
} | |
} | |
return result | |
} | |
let res = primes(25) | |
print(res) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Rect { | |
var x1, y1, x2, y2: Int | |
} | |
protocol DrawOjbect { | |
func contains(x: Int, y: Int) -> Bool | |
} | |
class Circle : DrawOjbect { | |
func contains(x: Int, y: Int) -> Bool { | |
return false | |
} | |
} | |
class Rectangle : DrawOjbect | |
{ | |
var x1, y1, x2, y2: Int | |
var color: String | |
init(_ x1: Int, _ y1: Int, _ x2: Int, _ y2: Int, _ color: String) { | |
self.x1 = x1 | |
self.y1 = y1 | |
self.x2 = x2 | |
self.y2 = y2 | |
self.color = color | |
} | |
func contains(x: Int, y: Int) -> Bool { | |
return x >= x1 && x <= x2 && y >= y1 && y <= y2 | |
} | |
} | |
class Canvas { | |
var objects: [DrawObject] = [] | |
func cirle(c: Cirle) { | |
objects.append(r) | |
} | |
func rect(_ x1: Int, _ y1: Int, _ x2: Int, _ y2: Int, _ color: String) { | |
var r = Rectangle(x1, y1, x2, y2, color) | |
objects.append(r) | |
} | |
func color(_ x: Int, _ y: Int) -> String { | |
for r in objects { | |
if r.contains(x, y) { | |
return r.color | |
} | |
} | |
return "undefined" | |
} | |
} | |
var c = Canvas() | |
c.rect(0, 0, 5, 5, "red") | |
c.rect(1, 1, 6, 6, "green") | |
var color = c.color(2, 2) | |
print(color) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let windowSize = 5 | |
var a:[Float] = Array(repeating: 0.0, count: windowSize) | |
var idx = 0 | |
var sum: Float = 0.0 | |
func average(_ x: Float) -> Float { | |
idx = (idx == windowSize - 1) ? 0 : idx + 1 | |
sum -= a[idx] | |
a[idx] = x | |
sum += x | |
return sum / Float(a.count) | |
} | |
var x = average(12.0) | |
x = average(12.0) | |
x = average(12.0) | |
x = average(12.0) | |
x = average(12.0) | |
x = average(12.0) | |
x = average(12.0) | |
x = average(12.0) | |
print(x) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rotate(_ a:inout [Int], _ steps: Int) { | |
if a.count <= 1 { | |
return | |
} | |
var steps = steps % a.count | |
if steps == 0 { | |
return | |
} | |
for _ in 1 ... steps { | |
var e = a.removeFirst() | |
a.append(e) | |
} | |
} | |
var a = [1, 2, 3, 4, 5] | |
rotate(&a, 1) | |
print(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rotate(_ a:inout [Int], _ steps: Int) { | |
if a.count <= 1 { | |
return | |
} | |
var steps = steps % a.count | |
if steps == 0 { | |
return | |
} | |
for _ in 1 ... steps { | |
for i in 0 ..< a.count - 1 { | |
a.swapAt(i, i + 1) | |
} | |
} | |
} | |
var a = [1, 2, 3, 4, 5] | |
rotate(&a, 1) | |
print(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sort(_ arr:inout [Int]) { | |
for i in 0..<arr.count { | |
var curMin = min(arr,i) | |
var temp = arr[i] | |
arr[i] = arr[curMin] | |
arr[curMin] = temp | |
} | |
} | |
func min(_ arr: [Int], _ n: Int) -> Int { | |
var min : Int = n | |
for i in n+1 ..< arr.count { | |
if arr[i] < arr[min] { | |
min = i | |
} | |
} | |
return min | |
} | |
var a = [2,3,5,3,7,9,5] | |
sort(&a) // & е pointer | |
print(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func sort(_ arr:inout [Int]) { | |
var i = 0, j = arr.count - 1 | |
while i < j { | |
var (min,max) = minMax(arr, i, j) | |
if i == max && j == min { | |
arr.swapAt(i, j) | |
} else if max == i { | |
arr.swapAt(i, min) | |
arr.swapAt(j, min) | |
} else { | |
arr.swapAt(i, min) | |
arr.swapAt(j, max) | |
} | |
i += 1 | |
j -= 1 | |
} | |
} | |
func minMax(_ arr: [Int], _ n: Int, _ m: Int) -> (Int,Int) { | |
var min = n, max = m | |
for i in n ... m { | |
if arr[i] < arr[min] { | |
min = i | |
} | |
if arr[i] > arr[max] { | |
max = i | |
} | |
} | |
return (min,max) | |
} | |
// [12, 5, 1, 6, 9, 0] | |
var a = [10, 5, 1] | |
sort(&a) | |
print(a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Seqence { | |
var start: Int, end = 0 | |
var ns = Set<Int>() | |
var len: Int { | |
get { end - start } | |
} | |
init(start: Int, num: Int) { | |
ns.insert(num) | |
self.start = start | |
} | |
func checkFull(_ num: Int) -> Bool { | |
ns.insert(num) | |
return ns.count == 3 | |
} | |
} | |
func shortestSequence(_ a: [Int], _ nums: [Int]) -> [Int] { | |
var ns = Set(nums) | |
var sa: [Seqence] = [] | |
var min: Seqence? | |
for i in 0 ..< a.count { | |
var v = a[i] | |
if ns.contains(v) { | |
for (j, seq) in sa.enumerated() { | |
if seq.checkFull(v) { | |
sa.remove(at: j) | |
seq.end = i | |
min = min ?? seq | |
if seq.len < min!.len { | |
min = seq | |
} | |
} | |
} | |
var seq = Seqence(start: i, num: v) | |
sa.append(seq) | |
} | |
} | |
if let m = min { | |
return Array(a[m.start...m.end]) | |
} | |
return [] | |
} | |
var arr = [1, 3, 8, 4, 2, 8, 2, 11, 1, 5, 13] | |
var nums = [1, 4, 8, 66] | |
var sh = shortestSequence(arr, nums) | |
print(sh) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func shortestSequence(_ a: [Int], _ nums: [Int]) -> [Int] { | |
var ns = Set(nums) | |
var nd: [Int:Int] = [:] | |
var cs = (start: -1, end: -1) | |
var ms = (start: -1, end: -1) | |
for (i, v) in a.enumerated() { | |
if ns.contains(v) { | |
nd[v] = i | |
if nd.count == 1 { | |
cs.start = i | |
} else if nd.count == nums.count { | |
cs.end = i | |
if ms.start == -1 || cs.end - cs.start < ms.end - ms.start { | |
ms = cs | |
} | |
var firstInSeq = a[cs.start] | |
nd[firstInSeq] = nil | |
for (num, pos) in nd where num != v { | |
nd[v] = i | |
cs.start = pos | |
} | |
} | |
} | |
} | |
return ms.start < 0 ? [] : Array(a[ms.start ... ms.end]) | |
} | |
var arr = [1, 4, 1, 3, 8, 4, 2, 8, 2, 11, 1, 5, 13] | |
var nums = [1, 4, 8, 66] | |
var sh = shortestSequence(arr, nums) | |
print(sh) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stack { | |
var arr: [Int] = [] | |
func push(_ num : Int) { | |
arr.append(num) | |
} | |
func pop() -> Int? { | |
if arr.count == 0 { | |
return nil | |
} | |
return arr.removeLast() | |
} | |
} | |
var s = Stack() | |
s.push(1) | |
s.push(2) | |
s.push(3) | |
if let num = s.pop() { | |
print(num) | |
} | |
if let num = s.pop() { | |
print(num) | |
} | |
if let num = s.pop() { | |
print(num) | |
} | |
if let num = s.pop() { | |
print(num) | |
} | |
if let num = s.pop() { | |
print(num) | |
} else { | |
print("Empty Stack!") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func str_intersect(_ s1:String, _ s2: String) -> String { | |
var cs1 = Set(s1), cs2 = Set(s2) | |
var cs = cs1.intersection(cs2) | |
return String(cs) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let alphabetSize = 26 | |
func str_intersection(_ s1: String, _ s2: String) -> String { | |
var ca = Array(repeating: 0, count: alphabetSize) | |
var asciiValueA = Character("a").asciiValue! | |
for c in s1 { | |
if "a" ... "z" ~= c { | |
var i = Int(c.asciiValue! - asciiValueA) | |
ca[i] = 1 | |
} | |
} | |
for c in s2 { | |
if "a" ... "z" ~= c { | |
var i = Int(c.asciiValue! - asciiValueA) | |
if ca[i] == 1 { | |
ca[i] = 2 | |
} | |
} | |
} | |
var res = "" | |
for i in 0 ..< ca.count { | |
if ca[i] > 1 { | |
var ascii = UInt8(i) + asciiValueA | |
res.append(Character(UnicodeScalar(ascii))) | |
} | |
} | |
return res | |
} | |
var s1 = "abc" | |
var s2 = "cdeeeec" | |
var s = str_intersection(s1, s2) | |
print(s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func str_intersect(_ s1:String, _ s2: String) -> String { | |
var res = "" | |
var cs: Set<Character> = [] | |
for c in s1 { | |
cs.insert(c) | |
} | |
for c in s2 { | |
if cs.contains(c) { | |
res.append(c) | |
cs.remove(c) | |
} | |
} | |
return String(res) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func unique(_ a: [Int]) -> [Int] { | |
var uniqueNum: Set<Int> = [] | |
var result: [Int] = [] | |
for x in a { | |
if !uniqueNum.contains(x) { | |
uniqueNum.insert(x) | |
result.append(x) | |
} | |
} | |
return result | |
} | |
var a = unique([3, 12, 5, 12, 8, 5]) | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment