Skip to content

Instantly share code, notes, and snippets.

View AdrianFerreyra's full-sized avatar

Adrián Ferreyra AdrianFerreyra

  • Sports Illustrated Play
  • Buenos Aires, Argentina
View GitHub Profile
@AdrianFerreyra
AdrianFerreyra / linkedListSwapElements.swift
Last active October 31, 2017 18:54
Return the head node of the singly linked list with each pair of nodes swapped. If there is a last odd node leave it in place. Example: Input: 1 -> 2 -> 3 -> 4 -> 5 Output: 2 -> 1 -> 4 -> 3 -> 5
enum LinkedList {
case empty
indirect case node(T, LinkedList)
func swapTwoFirst() -> LinkedList {
switch self {
case .node(let value, .node(let nextValue, let next)):
return .node(nextValue, .node(value, next.swapTwoFirst()))
@sketchytech
sketchytech / Calculating a count for UTF16 string with unicode scalar characters - use utf16Count
Last active September 17, 2015 20:58
Working with bytes and strings in Swift (Unicode Scalar, UTF8 and UTF16)
newStr = "hello 😂😒😡"
// the number of elements:
let count = newStr.utf16Count
// create array of appropriate length:
var array = [UInt16](count: count, repeatedValue: 0)
for a in enumerate(newStr.utf16) {