Skip to content

Instantly share code, notes, and snippets.

View frakingFrank's full-sized avatar

FrakingFrank frakingFrank

View GitHub Profile
@frakingFrank
frakingFrank / Occurrences of characters.swift
Created December 4, 2017 08:45
A Function that calculates which characters occur in a string, as well as how often each of these characters occur
func occurrencesOfCharacters(in text: String) -> [Character: Int] {
var dict: [Character: Int] = [:]
for key in text {
if dict[key] == nil {
dict[key] = 1
}else{
dict[key]! += 1
}
@frakingFrank
frakingFrank / Are there duplicates in Dictionary.swift
Created December 4, 2017 08:39
Swift Apprentice by Ray Wenderlich Chapter 9 A function that returns true if all of the values of a dictionary[String:Int] are unique.
var dict = ["Eins" : 1, "Zwei": 2]
func isInvertible(_ dictionary: [String: Int]) -> Bool {
var isItTrue = true
var array : [Int] = []
for numbers in dict.values {
if array.contains(numbers) == false {
@frakingFrank
frakingFrank / CodeExample: nested Function.swift
Created November 13, 2017 09:27
Here is an example of nested Functions. The first function solves if two numbers are divisible, if not it returns nil. The second function makes an optional binding and return the answer. In this function there is the return of the first function as parameter.
func divideIfWhole(_ value: Int, by divisor: Int) -> Int? {
var answer: Int? = 0
let divisible = value % divisor
switch divisible {
case 0:
answer = value / divisor
default:
answer = nil
}
return answer
@frakingFrank
frakingFrank / Swift-isItAPrimeNumber.txt
Last active March 22, 2018 13:16
Is it a prime number? Swift
func isNumberDivisible(_ number: Int, by divisor: Int) -> Bool {
if number % divisor == 0 {
return true
} else {
return false
}
}