Skip to content

Instantly share code, notes, and snippets.

View trilliwon's full-sized avatar
๐ŸŽฏ
Focusing

WonJo trilliwon

๐ŸŽฏ
Focusing
View GitHub Profile
@trilliwon
trilliwon / uiimage-data.swift
Last active January 12, 2024 14:45
Swift UIImage to Data, Data to UIImage
// Swift4
let image = UIImage(named: "sample")
let data = image?.pngData()
let data = image?.jpegData(compressionQuality: 0.9)
let uiImage: UIImage = UIImage(data: imageData)
// deprecated
// var imageData: Data = UIImagePNGRepresentation(image)
import Foundation
import UIKit
// Usage Examples
let shadowColor = Color.shadow.value
let shadowColorWithAlpha = Color.shadow.withAlpha(0.5)
let customColorWithAlpha = Color.custom(hexString: "#123edd", alpha: 0.25).value
enum Color {
import Foundation
import UIKit
// Usage Examples
let system12 = Font(.system, size: .standard(.h5)).instance
let robotoThin20 = Font(.installed(.RobotoThin), size: .standard(.h1)).instance
let robotoBlack14 = Font(.installed(.RobotoBlack), size: .standard(.h4)).instance
let helveticaLight13 = Font(.custom("Helvetica-Light"), size: .custom(13.0)).instance
struct Font {
@trilliwon
trilliwon / InsertionSort.swift
Created January 4, 2017 03:11
InsertionSort in swift
import Foundation
func insertionSort(array: [Int]) -> [Int] {
var array = array
for index in 1..<array.count {
let nextValue = array[index]
var aux = index - 1
while aux >= 0 && array[aux] > nextValue {
@trilliwon
trilliwon / swift-screen-name.codesnippet
Created December 26, 2016 06:23
custom code snippets
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>trilliwon-screen-name</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>ClassImplementation</string>
</array>
@trilliwon
trilliwon / heapsort.cpp
Created December 19, 2016 02:55
heapsort alghorithm cpp
/* #Heap Sort
###Input :
- ์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์—๋Š” ์ž…๋ ฅํ•  ํ‚ค์˜ ๊ฐฏ์ˆ˜(1 โ‰ค N โ‰ค 100,000)์™€ ์ •์ˆ˜ k๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.(1 โ‰ค k โ‰ค 30)
- ๋‹ค์Œ ๋ผ์ธ ๋ถ€ํ„ฐ N๊ฐœ์˜ ํ‚ค๋ฅผ ์ž…๋ ฅ ํ•ฉ๋‹ˆ๋‹ค.
###Output :
- ์ฒซ ๋ฒˆ์งธ ๋ผ์ธ์€ ํž™์—์„œ k๊ฐœ์˜ elements ๊ฐ์†Œํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅ ํ•ฉ๋‹ˆ๋‹ค.
- ๋‘ ๋ฒˆ์งธ ๋ผ์ธ์€ ํž™์—์„œ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ k ๊ฐœ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
@trilliwon
trilliwon / QuicksortTests.swift
Created December 16, 2016 01:43
QuicksortTests in swift
import XCTest
@testable import Quicksort
class QuicksortTests: XCTestCase {
// RED
// / \
// REFACTOR GREEN
@trilliwon
trilliwon / quicksort.swift
Last active December 15, 2016 07:20
Quicksort in Swift
import Foundation
func quicksort<T: Comparable>(array: [T]) -> [T] {
if array.count <= 1 { return array }
let pivot: T = array[array.count/2]
return quicksort(array: array.filter ({ $0 < pivot })) + array.filter ({ $0 == pivot }) + quicksort(array: array.filter ({ $0 > pivot }))
}
// http://www.slideshare.net/ssuserae280e/quicksort-in-swift
@trilliwon
trilliwon / operators.swift
Created December 13, 2016 07:13
custom operators in swift
infix operator ???
infix operator ?->
// user.name ??? sendMessage()
// is euqual to
// if let name = user.name {
// sendMessage()
// }
@trilliwon
trilliwon / HashTable.swift
Created November 29, 2016 01:54
money class with TDD
public struct HashTable<Key: Hashable, Value> {
fileprivate typealias Element = (key: Key, value: Value)
fileprivate typealias Bucket = [Element]
fileprivate var buckets: [Bucket]
fileprivate(set) var count = 0
public init(capacity: Int) {
assert(capacity > 0)
buckets = .init(repeating: [], count: capacity)