Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitespace Cleanup #150

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 0 additions & 2 deletions exercises/accumulate/AccumulateExample.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

// Foundation not needed


extension Array {
func accumulate<S>(yield: (Element) -> S) -> [S] {
var result: [S] = [S]()
Expand Down
52 changes: 19 additions & 33 deletions exercises/accumulate/AccumulateTest.swift
Original file line number Diff line number Diff line change
@@ -1,87 +1,74 @@

import XCTest



private extension String {

var length: Int {return self.characters.count}

var length: Int { return self.characters.count }

func reverse() -> String {
var result:String = ""
for char in self.characters {
result = "\(char)\(result)"
}
return result
}

}

class AccumulateTest: XCTestCase {

func testEmptyAccumulation() {

let input = [Int]()
func square(input:Int) -> Int {
func testEmptyAccumulation() {
let input = [Int]()
func square(input:Int) -> Int {
return input * input
}
let result = input.accumulate(square)
}
let result = input.accumulate(square)

XCTAssertTrue(result.isEmpty)
}

func testAccumulateSquares() {

let input = [1,2,3,4]
let expected = [1,4,9,16]
func square(input:Int) -> Int {
return input * input
}

let result = input.accumulate(square)

XCTAssertEqual(expected, result)
}


func testAccumulateUpcases() {

let input = ["hello","world"]
let expected = ["HELLO","WORLD"]
func toUpper(input:String) -> String {
return input.uppercaseString
}

let result = input.accumulate(toUpper)

XCTAssertEqual(expected, result)
}

}


func testAccumulateReversedStrings() {

let input = ["the","quick","brown","fox","etc"]
let expected = ["eht","kciuq","nworb","xof","cte"]
func reverse(input:String) -> String {
return input.reverse()
}

let result = input.accumulate(reverse)

XCTAssertEqual(expected, result)
}

func testAccumulateRecursively() {

let input = ["a","b","c"]

let expected = [
["a1","a2","a3"],
["b1","b2","b3"],
["c1","c2","c3"]
]

func recurse(input:String) -> [String] {
func appendTo(innerInput:String) -> String {
return input+innerInput
Expand All @@ -90,9 +77,8 @@ class AccumulateTest: XCTestCase {
return result
}


let result = input.accumulate(recurse)

XCTAssertEqual(expected, result)
}
}
40 changes: 16 additions & 24 deletions exercises/acronym/AcronymExample.swift
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
// Foundation not needed



private extension String{

func substringWithRangeInt(intRange:Range<Int>)->String{
private extension String {
func substringWithRangeInt(intRange:Range<Int>) -> String {
let start = self.startIndex.advancedBy(intRange.startIndex)
let end = self.startIndex.advancedBy(intRange.endIndex)
return self.substringWithRange(start..<end)
}
func substringWithRangeInt(start start:Int, end:Int) -> String{

func substringWithRangeInt(start start:Int, end:Int) -> String {
let range = start..<end
return self.substringWithRangeInt(range)
}

var isUppercase:Bool {
return self == self.uppercaseString
}

var isLowercase:Bool {
return self == self.lowercaseString
}
}

struct Acronym{

struct Acronym {
static func abbreviate(inString:String) -> String {

var previousLetter:String = ""

func splitCamelcaseAt(currentLetter: String, inout withString previousLetter: String ) -> Bool {

defer { previousLetter = currentLetter }

if currentLetter == " " { return false
} else if currentLetter.isEmpty { return false
} else if (previousLetter.isLowercase && currentLetter.isUppercase){
Expand All @@ -43,11 +38,11 @@ struct Acronym{
//previousLetter = currentLetter // see defer block
return false
}

func insertSpaceAtCamelcase(inString:String)->String{
var accumulate = ""
var lastIndexAdded = 0

for (index , each) in inString.characters.map({String($0)}).enumerate() {
if splitCamelcaseAt(each, withString: &previousLetter){
accumulate += inString.substringWithRangeInt(start: lastIndexAdded, end: index)+" " // inserts a space
Expand All @@ -57,7 +52,7 @@ struct Acronym{
let lastStringSection = inString.substringWithRangeInt(start: lastIndexAdded, end: inString.characters.count)
return accumulate + lastStringSection
}

func splitAt(characterToCompare:Character, charToSplitAt:String = " ,-:")-> Bool{
for each in charToSplitAt.characters{
if each == characterToCompare{
Expand All @@ -66,15 +61,12 @@ struct Acronym{
}
return false
}

func splitStringToArray(inString:String) -> [String]{

return inString.characters.split(isSeparator: { splitAt($0) }).map{String($0)}
}

return splitStringToArray(insertSpaceAtCamelcase(inString)).map({$0.uppercaseString.substringWithRangeInt(start: 0, end: 1)}).joinWithSeparator("")
}

}


17 changes: 6 additions & 11 deletions exercises/acronym/AcronymTest.swift
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@

import XCTest



class AcronymTest: XCTestCase {

func testAcronymAbbreviateTest1(){
XCTAssertEqual("PNG", Acronym.abbreviate("Portable Network Graphics"))
}

func testAcronymAbbreviateTest2(){
XCTAssertEqual("ROR", Acronym.abbreviate("Ruby on Rails"))
}

func testAcronymAbbreviateTest3(){
XCTAssertEqual("HTML", Acronym.abbreviate("HyperText Markup Language"))
}

func testAcronymAbbreviateTest4(){
XCTAssertEqual("FIFO", Acronym.abbreviate("First In, First Out"))
}

func testAcronymAbbreviateTest5(){
XCTAssertEqual("PHP", Acronym.abbreviate("PHP: Hypertext Preprocessor"))
}

func testAcronymAbbreviateTest6(){
XCTAssertEqual("CMOS", Acronym.abbreviate("Complementary metal-oxide semiconductor"))
}

}
}
8 changes: 3 additions & 5 deletions exercises/allergies/AllergiesExample.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Foundation not needed



enum Allergen: UInt {
case Eggs = 1
case Peanuts = 2
Expand All @@ -15,12 +13,12 @@ enum Allergen: UInt {

struct Allergies {
let score: UInt

init(_ score: UInt) {
self.score = UInt(score)
}

func hasAllergy(allergen: Allergen) -> Bool {
return allergen.rawValue & score == allergen.rawValue ? true : false
}
}
}
22 changes: 6 additions & 16 deletions exercises/allergies/AllergiesTest.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@

import XCTest



class AllergiesTest: XCTestCase {

func testBob() {

let allergies = Allergies(34)

XCTAssertTrue(allergies.hasAllergy(.Peanuts), "Bob is allergic to peanuts")
XCTAssertTrue(allergies.hasAllergy(.Chocolate), "Bob is allergic to chocolate")
XCTAssertFalse(allergies.hasAllergy(.Cats), "Bob is not allergic to cats")
}

func testEggsNcats() {

let allergies = Allergies(129)

XCTAssertTrue(allergies.hasAllergy(.Eggs))
XCTAssertTrue(allergies.hasAllergy(.Cats))
XCTAssertFalse(allergies.hasAllergy(.Chocolate))

}

func testNone() {
let allergies = Allergies(0)

XCTAssertFalse(allergies.hasAllergy(.Pollen))
}

func testAll() {

let allInt = UInt(Array(0...7).reduce(0){ return ($0 | (1 << $1)) })
let allergies = Allergies(allInt)

XCTAssertTrue(allergies.hasAllergy(.Eggs))
XCTAssertTrue(allergies.hasAllergy(.Peanuts))
XCTAssertTrue(allergies.hasAllergy(.Shellfish))
Expand All @@ -43,6 +34,5 @@ class AllergiesTest: XCTestCase {
XCTAssertTrue(allergies.hasAllergy(.Chocolate))
XCTAssertTrue(allergies.hasAllergy(.Pollen))
XCTAssertTrue(allergies.hasAllergy(.Cats))

}
}
13 changes: 4 additions & 9 deletions exercises/anagram/AnagramExample.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Foundation not needed



struct Anagram {
var baseWord = ""
init (word: String) {
baseWord = word
}

func sortLetters(wordToSort: String) -> String {
var characters: [String] = []
for char in wordToSort.characters {
Expand All @@ -16,20 +14,17 @@ struct Anagram {
characters = characters.sort(< )
return characters.reduce("", combine: +)
}

func match(words: [String]) -> [String] {
var matches: [String] = []

for candidateWord in words {
if sortLetters(baseWord.lowercaseString) == sortLetters(candidateWord.lowercaseString)
&& baseWord.lowercaseString != candidateWord.lowercaseString {
matches.append(candidateWord)
}
}

return matches
}

}


Loading