Skip to content

Instantly share code, notes, and snippets.

func sumOfTwoSmallestIntegersIn(_ array: [Int]) -> Int {
let smallArray = array.sorted()
var firstNum = smallArray[0]
var secondNum = smallArray[1]
return firstNum + secondNum
}
sumOfTwoSmallestIntegersIn([19, 5, 42, 2, 77])
@shakyra
shakyra / The Big Nerd Ranch Guide - Swift Programming - Chapter 10 Silver challenge
Created May 22, 2019 13:46
Creating a dictionary with counties + zip codes in Ohio
var counties = ["Franklin": [43215, 43213, 43219, 43227, 43228], "Delaware": [43015, 43035, 43003, 43061, 43065], "Pickaway": [43113, 43103, 43116, 43117, 43156]]
for zipCode in counties.values {
print("Ohio has the following zip codes: \(zipCode)")
}
var toDoList = ["Take out garbage", "Pay bills", "Cross off finished items"]
for _ in toDoList {
toDoList.reverse()
print(toDoList)
}
@shakyra
shakyra / Grasshopper - Personalized Message 845 of 6,617
Last active January 16, 2023 22:11
Personalized greeting Create a function that gives a personalized greeting. This function takes two parameters: name and owner. Use conditionals to return the proper message: ======= case | return --- | --- name equals owner | 'Hello boss' otherwise | 'Hello guest'
func great(_ name: String, _ owner: String) -> String {
// complete this function
if name == owner {
return("Hello boss")
}
else {
return("Hello guest")
}
}
@shakyra
shakyra / fizzBuzz
Created May 21, 2019 18:59
My nooby attempt at fizzBuzz using for where loops
var i: Int = 3
var fizz: String = "Fizz"
var buzz: String = "Buzz"
var fizzBuzz: String = "FizzBuzz"
for i in 1...100 where i % 3 == 0 {
print(fizz)
}
for i in 1...100 where i % 5 == 0 {
@shakyra
shakyra / Switch it Up!
Created May 21, 2019 03:25
When provided with a number between 0-9, return it in words. Input :: 1 Output :: "One". Try using "Switch" statements.
func switchItUp (_ number: Int) -> String {
switch number {
case 0:
return "Zero"
case 1:
return "One"
case 2:
return "Two"
case 3:
@shakyra
shakyra / Transportation on vacation #783
Created May 19, 2019 14:02
After a hard quarter in the office you decide to get some rest on a vacation. So you will book a flight for you and your girlfriend and try to leave all the mess behind you. You will need a rental car in order for you to get around in your vacation. The manager of the car rental makes you some good offers. Every day you rent the car costs $40. I…
func RentalCarCost(_ days: Int) -> Int {
let costPerDay = 40
var totalCost = costPerDay * days
if (days >= 7) {
totalCost -= 50
}
else if (days >= 3) {
totalCost -= 20
@shakyra
shakyra / Calculate BMI #504
Created May 19, 2019 13:39
Write function bmi that calculates body mass index (bmi = weight / height ^ 2). if bmi <= 18.5 return "Underweight" if bmi <= 25.0 return "Normal" if bmi <= 30.0 return "Overweight" if bmi > 30 return "Obese"
func bmi(_ weight: Int, _ height: Double) -> String {
let bmi = Double(weight) / (height * height)
if (bmi <= 18.5) {
return "Underweight"
}
else if (bmi <= 25.0) {