This file contains 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
//create a vowels string to compare if a character is a vowel | |
//then loop through the entire string and check if vowels contains each character | |
//if vowels has that character, then add 1 to the count. | |
//in the end, return the total count | |
// | |
func numberOfVowels(in string: String) -> Int { | |
var count = 0 | |
let vowels = "aeiouAEIOU" | |
for character in string { |
This file contains 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
/* | |
takes 2 int, sum and product -> smallest 2 positive Int x and y. x+y == sum, x*y == product | |
return x and y in array with smallest in front | |
return empty if no solution found | |
pseudo code: | |
make an empty array for storing answer | |
loop through 1 to sum /2 if sum is even, loop through 1 to sum/2 + 1 if sum is odd and find each complimentary number and store them in dictionary. | |
ex: sum = 6, go through 1~5 and dict = {1:5, 2:4, 3:3} | |
then go through the dict to find the key value pair that produce the product, add that into the array and break out the loop |
This file contains 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
// loop through string with index, if found the first character, check subsequent characters | |
// assuming passing empty string in will return false | |
extension String { | |
func anotherContains (_ string: String) -> Bool{ | |
if string.count > self.count || string.count == 0 { | |
return false | |
} | |
let lowercaseInput = string.lowercased() | |
let lowercaseSelf = self.lowercased() |
This file contains 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
/* for a word to be rearrange to palindrome it will need to have even number of letters on all letters or except one letter | |
aabbcc or aabbc can both be rearrange to palindrome. | |
make a dictionary storing all the letters, if number of letters are all even or all even except one then return true, else return false. | |
*/ | |
func isArrangeToPalindrome(string: String) -> Bool{ | |
guard !string.isEmpty else { | |
return false | |
} | |
var dictionary : [Character : Int] = [:] |
This file contains 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
func expandNumber(_ num: Int) -> [Int] { | |
var number = num | |
var numbers: [Int] = [] | |
let numString = String(num) | |
let numberOfDigit = num >= 0 ? numString.count : numString.count - 1 | |
for i in 1...numberOfDigit { | |
let iDigit = number % Int(pow(10, Double(i))) | |
number -= iDigit | |
This file contains 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
"Challenge 7: Condense whitespace | |
Difficulty: Easy | |
Write a function that returns a string with any consecutive spaces replaced with a single space. | |
Sample input and output | |
I've marked spaces using "[space]" below for visual purposes: | |
The string "a[space][space][space]b[space][space][space]c" should return "a[space]b[space]c". | |
The string "[space][space][space][space]a" should return "[space]a". |
This file contains 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
func isTwinPrime (_ num: Int) -> Bool { | |
guard checkPrime(num) else { | |
return false | |
} | |
let numMinusTwo = num - 2 | |
let numPlusTwo = num + 2 | |
if checkPrime(numMinusTwo) || checkPrime(numPlusTwo) { | |
return true | |
} else { | |
return false |
This file contains 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
"Challenge 8: String is rotated | |
Difficulty: Tricky | |
Write a function that accepts two strings, and returns true if one string is rotation of the other, taking letter case into account. | |
Tip: A string rotation is when you take a string, remove some letters from its end, then append them to the front. For example, "swift" rotated by two characters would be "ftswi". | |
Sample input and output | |
The string "abcde" and "eabcd" should return true. | |
The string "abcde" and "cdeab" should return true. |
This file contains 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
//"Challenge 9: Find pangrams | |
//Difficulty: Tricky | |
// | |
//Write a function that returns true if it is given a string that is an English pangram, ignoring letter case. | |
// | |
//Tip: A pangram is a string that contains every letter of the alphabet at least once. | |
// | |
//Sample input and output | |
//The string "The quick brown fox jumps over the lazy dog" should return true. | |
//The string "The quick brown fox jumped over the lazy dog" should return false, because it's missing the S. |
This file contains 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
func bubbleSort(_ numbers: [Int]) -> [Int] { | |
var numbers = numbers | |
var clean = false | |
while !clean { | |
clean = true | |
for i in 0...(numbers.count - 2) { | |
if numbers[i] > numbers[i + 1] { | |
let tempNum = numbers[i] | |
numbers[i] = numbers[i + 1] | |
numbers[i + 1] = tempNum |
OlderNewer