Last active
March 13, 2021 15:26
-
-
Save gozzoo/fcd42f6f67966e8141e878f9fa5588b7 to your computer and use it in GitHub Desktop.
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 BitSet { | |
func getElements() -> [Int] { | |
var res = [Int]() | |
var sIdx = -1 | |
var mask:UInt64 = 1 | |
for var i in 0...size { | |
var bitPos = i % 64 | |
if bitPos == 0 { | |
sIdx += 1 | |
mask = 1 | |
} else { | |
mask <<= 1 | |
} | |
if mask > s[sIdx] { | |
i += 64 - bitPos | |
} else if mask & s[sIdx] != 0 { | |
res.append(i) | |
} | |
} | |
return 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
let MAX_SIZE = 1000 | |
let BITS_IN_UINT = 64 | |
func sort(_ a: inout [Int]) { | |
var s : [UInt64] = Array(repeating: 0, count: MAX_SIZE / 64 + 1) | |
for j in a { | |
let mask: UInt64 = (1 << (j % BITS_IN_UINT)) | |
s[j / BITS_IN_UINT] |= mask | |
} | |
var i = 0 | |
for n in 0..<MAX_SIZE { | |
let mask: UInt64 = (1 << (n % BITS_IN_UINT)) | |
if (s[n / BITS_IN_UINT] & mask) != 0 { | |
a[i] = n | |
i += 1 | |
} | |
} | |
} | |
var a = [5,45,80,1,17,28] | |
sort(&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
extension Array where Element: Comparable { | |
func minPos() -> Index? { | |
return self.indices.min { self[$0] < self[$1] } | |
} | |
} | |
func largest(_ arr: [Int], _ k: Int) -> [Int] { | |
var ka = Array(arr[0..<k]) | |
var mp: Int! = ka.minPos() | |
for e in arr[k...] { | |
if e > ka[mp] { | |
mp = ka.minPos() | |
ka[mp] = e | |
} | |
} | |
return ka | |
} |
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 matchBrackets(_ s: [Character], _ i: Int) -> Int { | |
if i == s.count || s[i] == ")" { | |
return i | |
} | |
if s[i] == "(" { | |
let closer = matchBrackets(s, i + 1) | |
if closer < s.count && s[closer] == ")" { | |
print(i, closer) | |
return matchBrackets(s, closer + 1) | |
} | |
return i | |
} | |
return matchBrackets(s, i + 1); | |
} | |
func matchBrackets(_ s: String) -> Bool { | |
return matchBrackets(Array(s), 0) == s.count | |
} | |
func testMatch(_ data: String,_ expected: Bool) { | |
print(data) | |
let res = matchBrackets(data) | |
if res != expected { | |
print("Result of matchBrackets: Data: \(data), Expected: \(expected), Result: \(res) ") | |
} | |
} | |
testMatch("(", false) | |
testMatch(")(", false) | |
testMatch("()", true) | |
testMatch("(())", true) | |
testMatch("()()()()()", true) | |
testMatch(")()()()()()(", false) | |
testMatch("(", false) | |
testMatch("((()))", true) | |
testMatch("((())", false) |
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 isEmpty: Bool { | |
get { return (arr.count == 0) } | |
} | |
} | |
func matchBrackets(_ s: String) -> Bool { | |
var bs = Stack() | |
for (i, c) in s.enumerated() { | |
switch c { | |
case "(": | |
bs.push(i) | |
case ")": | |
if let openBracketPos = bs.pop() { | |
print(openBracketPos, i) | |
} else { | |
return false | |
} | |
default: | |
break | |
} | |
} | |
return bs.isEmpty | |
} | |
var mb = matchBrackets("(((())))") | |
print(mb) |
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 BracketsError: Error { | |
case emptyString | |
} | |
func matchBrackets(_ str: String) throws -> Bool { | |
if str.count == 0 { | |
throw BracketsError.emptyString | |
} | |
var count = 0 | |
for s in str { | |
if s == "(" { | |
count += 1 | |
} else if s == ")" { | |
count -= 1 | |
if count < 0 { | |
return false | |
} | |
} | |
} | |
return count == 0 | |
} | |
func test(_ data: String, _ expectedResult: Bool) { | |
do { | |
let res = try matchBrackets(data) | |
if res != expectedResult { | |
print("matchBrackets: expected", expectedResult, "returned", res) | |
} | |
} catch { | |
print("matchBrackets: unexpected error with data \'\(data)\'") | |
} | |
} | |
func testError(_ data: String) { | |
do { | |
let res = try matchBrackets(data) | |
print("matchBrackets: expected error but no error was thrown") | |
} catch { | |
// do nothing | |
} | |
} | |
testError("") | |
test("())", false) | |
test("()", true) | |
test("(())", true) | |
test("()()", true) | |
test("(()()())()", true) | |
test("((()", false) | |
test("()))", false) | |
test(")", false) |
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 Matrix<T> { | |
internal var m: [T] = [] | |
private(set) var rows = 0 | |
private(set) var columns = 0 | |
private(set) var defaultValue : T | |
init(_ rows: Int, _ columns: Int, _ value: () -> T) { | |
self.rows = rows | |
self.columns = columns | |
self.defaultValue = value() | |
m.reserveCapacity(rows * columns) | |
for _ in 0 ..< rows * columns { | |
m.append(value()) | |
} | |
} | |
func show() { | |
for i in 0 ..< rows { | |
for j in 0 ..< columns { | |
print("\(m[i * columns + j])", terminator: " ") | |
} | |
print() | |
} | |
print("-----------") | |
} | |
func isValid(_ a: Set<Int>) -> Bool { | |
if !a.isEmpty { | |
if a.max()! >= self.rows || a.min()! < 0 { | |
return false | |
} | |
} | |
return true | |
} | |
func delete(rows: [Int] = [], columns: [Int] = []) { | |
if rows.isEmpty && columns.isEmpty { | |
return | |
} | |
let sr = Set(rows) | |
let sc = Set(columns) | |
if !isValid(sr) || !isValid(sc) { | |
return | |
} | |
var iTo: Int = { | |
var minRow = sr.min() ?? 0 | |
if minRow == 0 { | |
return 0 | |
} | |
return sc.isEmpty ? minRow * self.columns : sc.min()! | |
} () | |
for var i in iTo ..< m.count { | |
let r = i / self.columns | |
if sr.contains(r) { | |
i += self.columns | |
continue | |
} | |
let c = i % self.columns | |
if !sc.contains(c) { | |
m[iTo] = m[i] | |
iTo += 1 | |
} | |
} | |
self.rows -= rows.count | |
self.columns -= columns.count | |
m.removeLast(m.count - self.rows * self.columns) | |
} | |
} | |
func rand() -> Int { | |
return Int.random(in: 1...50) | |
} | |
var m = Matrix<Int>(4, 4, rand) | |
m.show() | |
m.delete(rows: [1, 2], columns: [3]) | |
m.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment