Skip to content

Commit

Permalink
Update batch 5 (#654)
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed Aug 2, 2023
1 parent d585006 commit b51ce58
Show file tree
Hide file tree
Showing 25 changed files with 510 additions and 293 deletions.
44 changes: 21 additions & 23 deletions exercises/practice/grains/.meta/Sources/Grains/GrainsExample.swift
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
}
}
}
36 changes: 36 additions & 0 deletions exercises/practice/grains/.meta/template.swift
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 -%}
}
30 changes: 15 additions & 15 deletions exercises/practice/grains/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import PackageDescription

let package = Package(
name: "Grains",
products: [
.library(
name: "Grains",
targets: ["Grains"]),
],
dependencies: [],
targets: [
.target(
name: "Grains",
dependencies: []),
.testTarget(
name: "GrainsTests",
dependencies: ["Grains"]),
]
name: "Grains",
products: [
.library(
name: "Grains",
targets: ["Grains"])
],
dependencies: [],
targets: [
.target(
name: "Grains",
dependencies: []),
.testTarget(
name: "GrainsTests",
dependencies: ["Grains"]),
]
)
6 changes: 5 additions & 1 deletion exercises/practice/grains/Sources/Grains/Grains.swift
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 exercises/practice/grains/Tests/GrainsTests/GrainsTests.swift
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)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
func compute(_ dnaSequence: String, against: String) -> Int? {
if dnaSequence.count != against.count {
return nil
enum HammingError: Error {
case differentLength
}

func compute(_ dnaSequence: String, against: String) throws -> Int? {
guard dnaSequence.count == against.count else {
throw HammingError.differentLength
}
let distance: Int = zip(dnaSequence, against).filter({ $0 != $1 }).count
return distance
Expand Down
22 changes: 22 additions & 0 deletions exercises/practice/hamming/.meta/template.swift
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 -%}
}
6 changes: 6 additions & 0 deletions exercises/practice/hamming/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,27 @@ description = "long different strands"

[919f8ef0-b767-4d1b-8516-6379d07fcb28]
description = "disallow first strand longer"
include = false

[b9228bb1-465f-4141-b40f-1f99812de5a8]
description = "disallow first strand longer"
reimplements = "919f8ef0-b767-4d1b-8516-6379d07fcb28"

[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e]
description = "disallow second strand longer"
include = false

[dab38838-26bb-4fff-acbe-3b0a9bfeba2d]
description = "disallow second strand longer"
reimplements = "8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e"

[5dce058b-28d4-4ca7-aa64-adfe4e17784c]
description = "disallow left empty strand"
include = false

[db92e77e-7c72-499d-8fe6-9354d2bfd504]
description = "disallow left empty strand"
include = false
reimplements = "5dce058b-28d4-4ca7-aa64-adfe4e17784c"

[b764d47c-83ff-4de2-ab10-6cfe4b15c0f3]
Expand All @@ -51,9 +55,11 @@ reimplements = "db92e77e-7c72-499d-8fe6-9354d2bfd504"

[38826d4b-16fb-4639-ac3e-ba027dec8b5f]
description = "disallow right empty strand"
include = false

[920cd6e3-18f4-4143-b6b8-74270bb8f8a3]
description = "disallow right empty strand"
include = false
reimplements = "38826d4b-16fb-4639-ac3e-ba027dec8b5f"

[9ab9262f-3521-4191-81f5-0ed184a5aa89]
Expand Down
30 changes: 15 additions & 15 deletions exercises/practice/hamming/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import PackageDescription

let package = Package(
name: "Hamming",
products: [
.library(
name: "Hamming",
targets: ["Hamming"]),
],
dependencies: [],
targets: [
.target(
name: "Hamming",
dependencies: []),
.testTarget(
name: "HammingTests",
dependencies: ["Hamming"]),
]
name: "Hamming",
products: [
.library(
name: "Hamming",
targets: ["Hamming"])
],
dependencies: [],
targets: [
.target(
name: "Hamming",
dependencies: []),
.testTarget(
name: "HammingTests",
dependencies: ["Hamming"]),
]
)
4 changes: 3 additions & 1 deletion exercises/practice/hamming/Sources/Hamming/Hamming.swift
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.
}

0 comments on commit b51ce58

Please sign in to comment.