Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions CloptonCodeChallenge.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//: Playground - noun: a place where people can play

import UIKit

// Larissa Clopton - Code Challenge

// Challenge #1
func swap<T>(inout x: T, inout with y: T)
{
// swap two values
(x, y) = (y, x)
}

var x = "Larissa", y = "Clopton"
swap(&x,&y)


// Challenge #2
func flexStrings(str1: String = "", str2: String = "") -> String
{
// concatenate 0, 1, or 2 strings
return (str1 + str2 == "") ? "none": (str1 + str2)
}

flexStrings()
flexStrings("One")
flexStrings("One", str2: "Two")


// Challenge #3
func sumAny(items: Any...) -> String
{
// add a variable number of items of any type, return as string
return String((items.map({value in
switch value {
case "" as String, 0 as Int:
return -10
case let str as String where Int(str) > 0:
return Int(str)!
case is Int:
return value as! Int
default:
return 0
}
}) as [Int]).reduce(0) {
$0 + $1
})
}

let sum = sumAny("Marin Todorov", 2, 22, "-3", "10", "", 0, 33, -5)


// Challenge #4
func countFrom(from: Int, to: Int)
{
// count from one number to another
if from > to {
return
}
else {
print(from)
countFrom(from + 1, to: to)
}
}

countFrom(1, to: 5)

4 changes: 4 additions & 0 deletions CloptonCodeChallenge.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.