Last active
March 28, 2017 19:01
-
-
Save jamesrochabrun/7da56d9fa341df11805c0ee8af9b71e7 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
//: Playground - noun: a place where people can play | |
import UIKit | |
//fizbuzz | |
var oneHundred = [Int]() | |
for i in 1...100 { | |
oneHundred.append(i) | |
} | |
func isFizzBuzz(_ value: Int) { | |
if value % 3 == 0 && value % 5 == 0 { | |
print("FIZZBUZZ") | |
} else if value % 3 == 0 { | |
print("FIZZ") | |
} else if value % 5 == 0 { | |
print("BUZZ") | |
} else { | |
print(value) | |
} | |
} | |
//oneHundred.map{isFizzBuzz($0)} | |
//binary search | |
var oneThousand = [Int]() | |
for i in 1...200 { | |
oneThousand.append(i) | |
} | |
func binarySearchForValue(searchValue: Int, array: [Int]) -> Bool { | |
var leftIndex = 0 | |
var rightindex = array.count - 1 | |
while leftIndex <= rightindex { | |
let middleIndex = (leftIndex + rightindex) / 2 | |
let middleValue = array[middleIndex] | |
print("times") | |
if middleValue == searchValue { | |
return true | |
} | |
if searchValue > middleValue { | |
leftIndex = middleIndex + 1 | |
} | |
if searchValue < middleValue { | |
rightindex = middleIndex - 1 | |
} | |
} | |
return false | |
} | |
//print(binarySearchForValue(searchValue: 1, array: oneThousand)) | |
//factorials and recursion | |
func factorialOn(value: Int) -> Int { | |
if value == 0 { return 1 } | |
var num = 1 | |
for i in 1...value { | |
num *= i | |
} | |
return num | |
//factorial | |
//return value * factorialOn(value: value - 1) | |
} | |
print(factorialOn(value: 5)) | |
//most common name in array | |
let arrayName = ["jose", "james", "joe", "laura", "sasha", "joe", "joe"] | |
func mostCommonNamein(array: [String]) -> String { | |
var nameCountDictionary = [String : Int]() | |
for name in array { | |
if let count = nameCountDictionary[name] { | |
nameCountDictionary[name] = count + 1 | |
} else { | |
nameCountDictionary[name] = 1 | |
} | |
} | |
var mostCommonName = "" | |
for (key, value) in nameCountDictionary { | |
print(key, value) | |
if mostCommonName == "" { | |
mostCommonName = key | |
} else { | |
let count = nameCountDictionary[key] | |
if count! > nameCountDictionary[mostCommonName]! { | |
mostCommonName = key | |
} | |
} | |
} | |
return mostCommonName | |
} | |
mostCommonNamein(array: arrayName) | |
//reverse every other word | |
let sampleSentence = "hello this is my function" | |
func reverseWordsInSentence(sentence: String) -> String { | |
let allWords = sentence.components(separatedBy: " ") | |
var newSentence = "" | |
for index in 0...allWords.count - 1 { | |
if newSentence != "" { | |
newSentence += " " | |
} | |
let word = allWords[index] | |
if index % 2 == 1 { | |
let reversedWord = String(word.characters.reversed()) | |
newSentence += reversedWord.stringByRemovingVowels() | |
} else { | |
newSentence += word.stringByRemovingVowels() | |
} | |
} | |
return newSentence | |
} | |
//remove vowels | |
extension String { | |
func stringByRemovingVowels() -> String { | |
var newWord = self | |
for vowel in ["a", "e", "i", "o", "u"] { | |
newWord = newWord.replacingOccurrences(of: vowel, with: "") | |
} | |
return newWord | |
} | |
} | |
//print(reverseWordsInSentence(sentence: sampleSentence)) | |
//is it palindrome algorithm | |
func isPalindrome(_ word: String) -> Bool { | |
var ispalidrome = false | |
let arrayOfCharacters = word.characters.map({$0}) | |
for index in 0...(arrayOfCharacters.count - 1) { | |
if arrayOfCharacters[index] == arrayOfCharacters[arrayOfCharacters.count - 1 - index] { | |
ispalidrome = true | |
} else { | |
ispalidrome = false | |
break | |
} | |
} | |
return ispalidrome | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment