Skip to content

Instantly share code, notes, and snippets.

View ashalva's full-sized avatar

Shalva Avanashvili ashalva

View GitHub Profile
@ashalva
ashalva / isBST.swift
Last active May 9, 2018 06:40
Checking if the tree is binary or not in swift.
class Node {
var key : Int
var left : Node?
var right: Node?
init(key: Int, left: Node?, right: Node?) {
self.key = key
self.left = left
self.right = right
@ashalva
ashalva / allPrimeFactors.swift
Last active May 9, 2018 06:40
Finding all prime factors for the number in swift.
import Foundation
func allPrimeFactors(number: Int) {
var n = number
while n % 2 == 0 {
print("2")
n /= 2
}
var i = 3
@ashalva
ashalva / ToLowerCase.swift
Last active May 9, 2018 06:40
To lower case implementation in swift.
func toLowerCase(string: String) -> String {
var str = string as NSString
for i in 0...(str.length - 1) {
if str.character(at: i) >= 65 && str.character(at: i) <= 90 {
let changeValue = str.character(at: i) + 32
str = str.replacingCharacters(in: NSMakeRange(i, 1), with: String(Character(UnicodeScalar(changeValue)!))) as NSString
}
}
return str as String
@ashalva
ashalva / Duplicates.swift
Last active May 9, 2018 06:40
Returning all duplicates from the array in swift.
//Duplicate
func returnDuplicates(arr: [Int]) -> [Int] {
var dict: [Int: Bool] = [:]
var duplicates: [Int] = []
for i in arr {
if dict.keys.contains(i) {
duplicates.append(i)
} else {
dict[i] = true
}
@ashalva
ashalva / ReverseDLinkedList.swift
Last active May 9, 2018 06:39
Reversing doubly linked list in swift.
class Node<T> {
var value:T
var next: Node?
var previous: Node?
init(value: T) {
self.value = value
self.next = nil
self.previous = nil
}
@ashalva
ashalva / LCA.swift
Last active May 9, 2018 06:39
Lowest common ancestor in swift.
class Tree {
let value: Int
var left: Tree?
var right: Tree?
init(_ v: Int) {
self.value = v
}
}
@ashalva
ashalva / Traverse.swift
Last active May 9, 2018 06:39
Traversing the tree in swift.
class Node<T> {
var value:T
var next: Node?
var previous: Node?
init(value: T) {
self.value = value
self.next = nil
self.previous = nil
}
@ashalva
ashalva / getRequestSwift4.swift
Last active July 26, 2018 06:07
Sending get request in swift 4
import Foundation
enum Result<T> {
case success(T)
case failure(Error)
}
class Service: ServiceProtocol {
func get(callback: @escaping ((Result<[MyObject]>) -> ())) {
let url = URL(string: Links.baseUrl)!
@ashalva
ashalva / zsh-iterm-custom-commands.markdown
Last active October 22, 2024 07:57
Add custom bash commands to ZSH

Add custom commands for iTerm + ZSH.

  1. Create new file and name .my_custom_commands.sh
  2. Add some functions e.g
function ios() {
  open -a "Xcode" ~/Documents/iOS/MyiOSProject.xcodeproj
}
@ashalva
ashalva / Generate random contacts Swift
Last active March 16, 2022 08:42
Generates random contacts Swift
private func saveRandomContact() {
let store = CNContactStore()
let contact = CNMutableContact()
// Name
contact.givenName = UUID().uuidString
// Phone
contact.phoneNumbers.append(
CNLabeledValue(