diff --git a/swiftNinjaChallenge1.playground/Contents.swift b/swiftNinjaChallenge1.playground/Contents.swift new file mode 100644 index 0000000..dde689a --- /dev/null +++ b/swiftNinjaChallenge1.playground/Contents.swift @@ -0,0 +1,19 @@ +//: Playground - noun: a place where people can play + +import UIKit + +// Your first challenge is to write a function that takes two variables (of any type) as +// parameters and swaps their values. +// Need to use inout keyword so that the values can be changed outside of the function scope. +func swapper(inout a: Type, inout _ b: Type) +{ + (a, b) = (b, a) +} + +var a = "first" +var b = "second" +// Call the function +swapper(&a, &b) + +print(a) +print(b) diff --git a/swiftNinjaChallenge1.playground/contents.xcplayground b/swiftNinjaChallenge1.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/swiftNinjaChallenge1.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/swiftNinjaChallenge1.playground/playground.xcworkspace/contents.xcworkspacedata b/swiftNinjaChallenge1.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/swiftNinjaChallenge1.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/swiftNinjaChallenge1.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate b/swiftNinjaChallenge1.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..771163c Binary files /dev/null and b/swiftNinjaChallenge1.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/swiftNinjaChallenge3.playground/Contents.swift b/swiftNinjaChallenge3.playground/Contents.swift new file mode 100644 index 0000000..81ddc5d --- /dev/null +++ b/swiftNinjaChallenge3.playground/Contents.swift @@ -0,0 +1,24 @@ +//: Playground - noun: a place where people can play + +import UIKit + +// Single return statement. +func sumAny(anys: Any...) -> String { + return String((anys.map({item in + switch item { + case "" as String, 0 as Int: + return -10 + case let s as String where Int(s) > 0: + return Int(s)! + case is Int: + return item as! Int + default: + return 0 + } + }) as [Int]).reduce(0) { + // Reformat to String + $0 + $1 + }) +} + +sumAny(5, "5", "dad?", "5.0") diff --git a/swiftNinjaChallenge3.playground/contents.xcplayground b/swiftNinjaChallenge3.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/swiftNinjaChallenge3.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/swiftNinjaChallenge3.playground/playground.xcworkspace/contents.xcworkspacedata b/swiftNinjaChallenge3.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/swiftNinjaChallenge3.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/swiftNinjaChallenge3.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate b/swiftNinjaChallenge3.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..f3e8467 Binary files /dev/null and b/swiftNinjaChallenge3.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/swiftNinjaChallenge4.playground/Contents.swift b/swiftNinjaChallenge4.playground/Contents.swift new file mode 100644 index 0000000..39711b6 --- /dev/null +++ b/swiftNinjaChallenge4.playground/Contents.swift @@ -0,0 +1,15 @@ +//: Playground - noun: a place where people can play + +import UIKit + +// Resursvely count from from to to +func countFrom(from: Int, to: Int) +{ + print(from, terminator: "") + + if (from < to) { + countFrom(from + 1, to: to) + } +} + +countFrom(1, to: 5) diff --git a/swiftNinjaChallenge4.playground/contents.xcplayground b/swiftNinjaChallenge4.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/swiftNinjaChallenge4.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/swiftNinjaChallenge4.playground/playground.xcworkspace/contents.xcworkspacedata b/swiftNinjaChallenge4.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/swiftNinjaChallenge4.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/swiftNinjaChallenge4.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate b/swiftNinjaChallenge4.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..2eccc40 Binary files /dev/null and b/swiftNinjaChallenge4.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/swiftNinjaProgress b/swiftNinjaProgress new file mode 100644 index 0000000..0e6b830 --- /dev/null +++ b/swiftNinjaProgress @@ -0,0 +1,9 @@ +challenge 1: + 2 stars +challenge 2: + 3 stars +challenge 3: + 1 star +challenge 4: + 2 stars + \ No newline at end of file diff --git a/swiftNinjachallenge2.playground/Contents.swift b/swiftNinjachallenge2.playground/Contents.swift new file mode 100644 index 0000000..30dc2b2 --- /dev/null +++ b/swiftNinjachallenge2.playground/Contents.swift @@ -0,0 +1,27 @@ +//: Playground - noun: a place where people can play + +import UIKit + +// Concetenate the arguments if they exist. +func flexStrings(s1: String?=nil, _ s2: String?=nil) -> String +{ + if s1 == nil { + return "none" + } + else if s2 == nil{ + return s1! + } + else { + return s1! + s2! + } +} + +// Their faster implementation +func betterFlexStrings(s1: String = "", _ s2: String = "") -> String { + return s1 + s2 == "" ? "none": s1 + s2 +} + +var s1 = "One", s2 = "Two" + +print(flexStrings(s1, s2)) + diff --git a/swiftNinjachallenge2.playground/contents.xcplayground b/swiftNinjachallenge2.playground/contents.xcplayground new file mode 100644 index 0000000..5da2641 --- /dev/null +++ b/swiftNinjachallenge2.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/swiftNinjachallenge2.playground/playground.xcworkspace/contents.xcworkspacedata b/swiftNinjachallenge2.playground/playground.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/swiftNinjachallenge2.playground/playground.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/swiftNinjachallenge2.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate b/swiftNinjachallenge2.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate new file mode 100644 index 0000000..f51f43d Binary files /dev/null and b/swiftNinjachallenge2.playground/playground.xcworkspace/xcuserdata/lukepetruzzi.xcuserdatad/UserInterfaceState.xcuserstate differ