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
- (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; | |
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
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 |
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
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 |
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
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 | |
} |
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
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") |
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 |
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
"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
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 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". |
NewerOlder