-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d585006
commit b51ce58
Showing
25 changed files
with
510 additions
and
293 deletions.
There are no files selected for viewing
44 changes: 21 additions & 23 deletions
44
exercises/practice/grains/.meta/Sources/Grains/GrainsExample.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
import Foundation | ||
|
||
enum GrainsError: Error { | ||
case inputTooLow | ||
case inputTooHigh | ||
} | ||
|
||
struct Grains { | ||
|
||
enum GrainsError: Error { | ||
case inputTooLow(String) | ||
case inputTooHigh(String) | ||
static func square(_ num: Int) throws -> UInt64 { | ||
guard num >= 1 else { | ||
throw GrainsError.inputTooLow | ||
} | ||
|
||
static func square(_ num: Int) throws -> UInt64 { | ||
guard num >= 1 else { | ||
let message = "Input[\(num)] invalid. Input should be between 1 and 64 (inclusive)" | ||
throw GrainsError.inputTooLow(message) | ||
} | ||
|
||
guard num <= 64 else { | ||
let message = "Input[\(num)] invalid. Input should be between 1 and 64 (inclusive)" | ||
throw GrainsError.inputTooHigh(message) | ||
} | ||
|
||
let one: UInt64 = 1 | ||
let shift = UInt64(num - 1) | ||
return one << shift | ||
guard num <= 64 else { | ||
throw GrainsError.inputTooHigh | ||
} | ||
|
||
static var total: UInt64 { | ||
let numbers = (1...64).map { $0 } | ||
let one: UInt64 = 1 | ||
let shift = UInt64(num - 1) | ||
return one << shift | ||
} | ||
|
||
static var total: UInt64 { | ||
let numbers = (1...64).map { $0 } | ||
|
||
return numbers.reduce(UInt64(0)) { | ||
guard let squared = try? square($1) else { return $0 } | ||
return $0 + squared | ||
} | ||
return numbers.reduce(UInt64(0)) { | ||
guard let squared = try? square($1) else { return $0 } | ||
return $0 + squared | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import XCTest | ||
@testable import {{exercise|camelCase}} | ||
class {{exercise|camelCase}}Tests: XCTestCase { | ||
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
{% outer: for case in cases %} | ||
{% ifnot case.expected %} | ||
{%- for subCases in case.cases %} | ||
{%- if forloop.outer.first and forloop.first %} | ||
func test{{subCases.description |camelCase }}() { | ||
{%- else %} | ||
func test{{subCases.description |camelCase }}() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
{%- endif %} | ||
{%- ifnot subCases.expected.error %} | ||
XCTAssertEqual(try! Grains.square({{subCases.input.square}}), {{subCases.expected}}) | ||
{%- else %} | ||
XCTAssertThrowsError(try Grains.square({{subCases.input.square}})) { error in | ||
{%- if subCases.input.square < 1 %} | ||
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooLow) | ||
{%- elif subCases.input.square > 64 %} | ||
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooHigh) | ||
{%- endif %} | ||
} | ||
{%- endif -%} | ||
|
||
} | ||
{% endfor -%} | ||
{%- else %} | ||
func test{{subCases.description |camelCase }}{{ forloop.outer.counter }}() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.total, {{case.expected}}) | ||
} | ||
{%- endif %} | ||
{% endfor -%} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
//Solution goes in Sources | ||
struct Grains { | ||
static func square(_ num: Int) throws -> UInt64 { | ||
// Write your code for the 'Grains' exercise in this file. | ||
} | ||
} |
96 changes: 48 additions & 48 deletions
96
exercises/practice/grains/Tests/GrainsTests/GrainsTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
import XCTest | ||
|
||
@testable import Grains | ||
|
||
class GrainsTests: XCTestCase { | ||
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
func testInvalidInput1() { | ||
XCTAssertThrowsError(try Grains.square(65)) { error in | ||
if case let Grains.GrainsError.inputTooHigh(message) = error { | ||
XCTAssertTrue(message == "Input[65] invalid. Input should be between 1 and 64 (inclusive)") | ||
} else { | ||
XCTFail("Expected error not thrown") | ||
} | ||
} | ||
} | ||
func testGrainsOnSquare1() { | ||
XCTAssertEqual(try! Grains.square(1), 1) | ||
} | ||
|
||
func testInvalidInput2() { | ||
XCTAssertThrowsError(try Grains.square(0)) { error in | ||
if case let Grains.GrainsError.inputTooLow(message) = error { | ||
XCTAssertTrue(message == "Input[0] invalid. Input should be between 1 and 64 (inclusive)") | ||
} else { | ||
XCTFail("Expected error not thrown") | ||
} | ||
} | ||
} | ||
func testGrainsOnSquare2() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.square(2), 2) | ||
} | ||
|
||
func testInvalidInput3() { | ||
XCTAssertThrowsError(try Grains.square(-1)) { error in | ||
if case let Grains.GrainsError.inputTooLow(message) = error { | ||
XCTAssertTrue(message == "Input[-1] invalid. Input should be between 1 and 64 (inclusive)") | ||
} else { | ||
XCTFail("Expected error not thrown") | ||
} | ||
} | ||
} | ||
func testGrainsOnSquare3() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.square(3), 4) | ||
} | ||
|
||
func testSquare1() { | ||
XCTAssertEqual(try! Grains.square(1), 1) | ||
} | ||
func testGrainsOnSquare4() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.square(4), 8) | ||
} | ||
|
||
func testSquare2() { | ||
XCTAssertEqual(try! Grains.square(2), 2) | ||
} | ||
func testGrainsOnSquare16() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.square(16), 32768) | ||
} | ||
|
||
func testSquare3() { | ||
XCTAssertEqual(try! Grains.square(3), 4) | ||
} | ||
func testGrainsOnSquare32() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.square(32), 2_147_483_648) | ||
} | ||
|
||
func testSquare4() { | ||
XCTAssertEqual(try! Grains.square(4), 8) | ||
} | ||
func testGrainsOnSquare64() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.square(64), 9_223_372_036_854_775_808) | ||
} | ||
|
||
func testSquare16() { | ||
XCTAssertEqual(try! Grains.square(16), 32_768) | ||
func testSquare0RaisesAnException() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertThrowsError(try Grains.square(0)) { error in | ||
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooLow) | ||
} | ||
} | ||
|
||
func testSquare32() { | ||
XCTAssertEqual(try! Grains.square(32), 2_147_483_648) | ||
func testNegativeSquareRaisesAnException() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertThrowsError(try Grains.square(-1)) { error in | ||
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooLow) | ||
} | ||
} | ||
|
||
func testSquare64() { | ||
XCTAssertEqual(try! Grains.square(64), 9_223_372_036_854_775_808) | ||
func testSquareGreaterThan64RaisesAnException() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertThrowsError(try Grains.square(65)) { error in | ||
XCTAssertEqual(error as? GrainsError, GrainsError.inputTooHigh) | ||
} | ||
} | ||
|
||
func testTotalGrains() { | ||
XCTAssertEqual(Grains.total, 18_446_744_073_709_551_615) | ||
} | ||
func test2() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
XCTAssertEqual(try! Grains.total, 18_446_744_073_709_551_615) | ||
} | ||
} |
10 changes: 7 additions & 3 deletions
10
exercises/practice/hamming/.meta/Sources/Hamming/HammingExample.swift
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import XCTest | ||
@testable import {{exercise|camelCase}} | ||
class {{exercise|camelCase}}Tests: XCTestCase { | ||
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
{% for case in cases %} | ||
{% if forloop.first -%} | ||
func test{{case.description |camelCase }}() { | ||
{% else -%} | ||
func test{{case.description |camelCase }}() throws { | ||
try XCTSkipIf(true && !runAll) // change true to false to run this test | ||
{% endif -%} | ||
{%- ifnot case.expected.error -%} | ||
let result = try! Hamming.compute("{{case.input.strand1}}", against:"{{case.input.strand2}}")! | ||
let expected = {{case.expected}} | ||
XCTAssertEqual(expected, result) | ||
{%- else -%} | ||
XCTAssertThrowsError(try Hamming.compute("{{case.input.strand1}}", against:"{{case.input.strand2}}")) | ||
{%- endif -%} | ||
} | ||
{% endfor -%} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
//Solution goes in Sources | ||
func compute(_ dnaSequence: String, against: String) throws -> Int? { | ||
// Write your code for the 'Hamming' exercise in this file. | ||
} |
Oops, something went wrong.