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
/* 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
// 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
/* | |
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
//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 { |
NewerOlder