Skip to content

Instantly share code, notes, and snippets.

View bradleyyin's full-sized avatar

BRADLEY YIN bradleyyin

View GitHub Profile
@bradleyyin
bradleyyin / codeChallengeWeek14.m
Last active November 16, 2019 07:21
morning code challenge week 14
- (NSString *)ageOnDifferentPlanet:(double)seconds {
double earthYears = seconds / 31557600.0;
double mercuryYears = earthYears / 0.2408467;
double venusYears = earthYears / 0.61519726;
double marsYears = earthYears / 1.8808158;
double jupiterYears = earthYears / 11.862615;
double saturnYears = earthYears / 29.447498;
double uranusYears = earthYears / 84.016846;
double neptuneYears = earthYears / 164.79132;
@bradleyyin
bradleyyin / codeChallengeWeek13.swift
Created October 2, 2019 15:37
morning code challenge week 13
import Foundation
func makeChangeAsString(fromAmount: Double, withCost: Double) -> String {
guard fromAmount >= withCost else { return "Invalid change"}
let change = fromAmount - withCost
var changeInCent = Int(change * 100)
let dollar = changeInCent / 100
changeInCent -= dollar * 100
let quarter = changeInCent / 25
changeInCent -= quarter * 25
@bradleyyin
bradleyyin / codeChallengeWeek13.swift
Created October 2, 2019 15:37
morning code challenge week 13
import Foundation
func makeChangeAsString(fromAmount: Double, withCost: Double) -> String {
guard fromAmount >= withCost else { return "Invalid change"}
let change = fromAmount - withCost
var changeInCent = Int(change * 100)
let dollar = changeInCent / 100
changeInCent -= dollar * 100
let quarter = changeInCent / 25
changeInCent -= quarter * 25
@bradleyyin
bradleyyin / codeChallengeWeek11
Last active September 18, 2019 23:39
morning code challenge week 11
import Foundation
func sameNumberOfBinaryOnes(_ number: Int) -> (Int, Int?) {
let stringNumInBinary = "\(number, radix: .binary)"
var numOfOneInNum: Int = 0
for element in stringNumInBinary {
if element == "1" {
numOfOneInNum += 1
}
@bradleyyin
bradleyyin / codeChallengeWeek10.swift
Created September 11, 2019 16:14
morning code challenge week 10
import Foundation
func doAsTheRomansDoAndNumeralize(_ number: Int) -> String {
guard number > 0 else { return "" }
var romanNumeral = ""
let mRemainder = romanize(number: number, romanNumeral: &romanNumeral, value: 1000, romanSymbol: "M")
let cmRemainder = romanize(number: mRemainder, romanNumeral: &romanNumeral, value: 900, romanSymbol: "CM")
let dRemainder = romanize(number: cmRemainder, romanNumeral: &romanNumeral, value: 500, romanSymbol: "D")
let cdRemainder = romanize(number: dRemainder, romanNumeral: &romanNumeral, value: 400, romanSymbol: "CD")
@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
@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 / 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 / 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 / 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".