Skip to content

Instantly share code, notes, and snippets.

View bradleyyin's full-sized avatar

BRADLEY YIN bradleyyin

View GitHub Profile
@bradleyyin
bradleyyin / iOSMorningCodeChallenge-Sprint01-Challenge1.swift
Last active July 10, 2019 15:25
ios8 week 1 day 3 code challenge
//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 {
@bradleyyin
bradleyyin / iOSCodeChallenge-Sprint02-02.swift
Created July 17, 2019 15:44
iOSCodeChallenge-Sprint02-02
/*
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
@bradleyyin
bradleyyin / ios week3 day 3 code challenge
Created July 24, 2019 15:43
ios week 3 day 3 code challenge
// 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()
/* 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] = [:]
@bradleyyin
bradleyyin / codeChallengeWeek6.swift
Created August 14, 2019 15:39
iOS code challenge week 6
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
@bradleyyin
bradleyyin / challenge7.swift
Created August 20, 2019 04:57
code challenge AH
"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".
@bradleyyin
bradleyyin / codeChallengeWeek7.swift
Created August 21, 2019 15:51
morning code challenge 8/21
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
@bradleyyin
bradleyyin / challenge8.swift
Created August 21, 2019 17:04
code challenge AH 8
"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.
@bradleyyin
bradleyyin / challenge9.swift
Created August 22, 2019 23:34
code challenge AH 9
//"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.
@bradleyyin
bradleyyin / codeChallengeWeek9.swift
Created September 4, 2019 15:53
morning code challenge 8/21
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