Allows for things like:
import UIKit
let label = UILabel().configure {
$0.text = "Hello World"
$0.numberOfLines = 4
$0.font = UIFont.systemFont(ofSize: 16)
}
import Foundation
public extension NSObjectProtocol {
@discardableResult
func configure(_ closure: (Self) -> Void) -> Self {
closure(self)
return self
}
}
import XCTest
// XCTest Spec Example:
// TODO: replace with your own tests (TDD), these are just how-to examples to get you started
class SolutionTest: XCTestCase {
static var allTests = [
("Test Example", testExample),
]
func testExample() {
let actual = 1
XCTAssertEqual(actual, 1)
}
}
XCTMain([
testCase(SolutionTest.allTests)
])
class Box
attr_accessor :box, :lines, :cols
def initialize cols, lines
@lines = lines
@cols = cols
@box = []
for line in 0...@lines do
row = []
for col in 0...@cols do
isEdge = col == 0 || line == 0 || col == @cols - 1 || line == @lines - 1
row << 'x' if isEdge
row << ' ' unless isEdge
end
@box << row
end
end
def printBox
for line in 0...@lines do
str = ''
for col in 0...@cols do
str << @box[line][col]
end
puts str
end
end
def placeText text, x, y
textLength = text.length - 1 + x
letters = text.split ""
for col in x..textLength do
@box[y][col] = letters[col-x]
end
end
end
box = Box.new 30, 30
box.placeText "Hello World", 10, 10
box.printBox
# TODO: Replace examples and use TDD development by writing your own tests
# These are some of the methods available:
# Test.expect(boolean, [optional] message)
# Test.assert_equals(actual, expected, [optional] message)
# Test.assert_not_equals(actual, expected, [optional] message)
describe "Solution" do
it "should test for something" do
actual = 0
Test.assert_equals(actual, 0, "This is just an example of how you can write your own TDD tests")
end
end
extension String { func insert(_ value: String, n: Int) -> String { guard n > 0 else { return self } var ndx = 0 return self.reduce("") { (string, c) in guard ndx < n else { ndx = 1 return "\(string)\(value)\(c)" } ndx += 1 return "\(string)\(c)" } } }
func insert(seperator: String, afterEveryXChars: Int, intoString: String) -> String {guard afterEveryXChars > 0 else { return intoString }var output = ""intoString.enumerated().forEach { index, c inif index % afterEveryXChars == 0 && index > 0 {output += seperator- extension String {
- func insert(_ value: String, n: Int) -> String {
- guard n > 0 else {
- return self
- }
- var ndx = 0
- return self.reduce("") { (string, c) in
- guard ndx < n else {
- ndx = 1
- return "\(string)\(value)\(c)"
- }
output.append(c)- ndx += 1
- return "\(string)\(c)"
- }
return output}- }
- }
import XCTest // XCTest Spec Example: // TODO: replace with your own tests (TDD), these are just how-to examples to get you started class SolutionTest: XCTestCase { static var allTests = [ ("Test Example", testExample), ] func testExample() { XCTAssertEqual("603555121".insert("-", n: 0), "603555121") XCTAssertEqual("603555121".insert("-", n: 1), "6-0-3-5-5-5-1-2-1") XCTAssertEqual("603555121".insert("-", n: 3), "603-555-121") XCTAssertEqual("603555121".insert("-", n: 4), "6035-5512-1") XCTAssertEqual("603555121".insert("-", n: 5), "60355-5121") XCTAssertEqual("603555121".insert("-", n: 10), "603555121") // Extra Tests XCTAssertEqual("12345678901234567890".insert("-", n: 0), "12345678901234567890") XCTAssertEqual("12345678901234567890".insert("-", n: 1), "1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0") XCTAssertEqual("12345678901234567890".insert("-", n: 2), "12-34-56-78-90-12-34-56-78-90") XCTAssertEqual("12345678901234567890".insert("-", n: 3), "123-456-789-012-345-678-90") XCTAssertEqual("12345678901234567890".insert("-", n: 5), "12345-67890-12345-67890") XCTAssertEqual("12345678901234567890".insert("-", n: 10), "1234567890-1234567890") } } XCTMain([ testCase(SolutionTest.allTests) ])
- import XCTest
- // XCTest Spec Example:
- // TODO: replace with your own tests (TDD), these are just how-to examples to get you started
- class SolutionTest: XCTestCase {
- static var allTests = [
- ("Test Example", testExample),
- ]
- func testExample() {
XCTAssertEqual(insert(seperator: "-", afterEveryXChars: 0, intoString: "603555121"), "603555121")XCTAssertEqual(insert(seperator: "-", afterEveryXChars: 1, intoString: "603555121"), "6-0-3-5-5-5-1-2-1")XCTAssertEqual(insert(seperator: "-", afterEveryXChars: 3, intoString: "603555121"), "603-555-121")XCTAssertEqual(insert(seperator: "-", afterEveryXChars: 4, intoString: "603555121"), "6035-5512-1")XCTAssertEqual(insert(seperator: "-", afterEveryXChars: 5, intoString: "603555121"), "60355-5121")XCTAssertEqual(insert(seperator: "-", afterEveryXChars: 10, intoString: "603555121"), "603555121")- XCTAssertEqual("603555121".insert("-", n: 0), "603555121")
- XCTAssertEqual("603555121".insert("-", n: 1), "6-0-3-5-5-5-1-2-1")
- XCTAssertEqual("603555121".insert("-", n: 3), "603-555-121")
- XCTAssertEqual("603555121".insert("-", n: 4), "6035-5512-1")
- XCTAssertEqual("603555121".insert("-", n: 5), "60355-5121")
- XCTAssertEqual("603555121".insert("-", n: 10), "603555121")
- // Extra Tests
- XCTAssertEqual("12345678901234567890".insert("-", n: 0), "12345678901234567890")
- XCTAssertEqual("12345678901234567890".insert("-", n: 1), "1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0")
- XCTAssertEqual("12345678901234567890".insert("-", n: 2), "12-34-56-78-90-12-34-56-78-90")
- XCTAssertEqual("12345678901234567890".insert("-", n: 3), "123-456-789-012-345-678-90")
- XCTAssertEqual("12345678901234567890".insert("-", n: 5), "12345-67890-12345-67890")
- XCTAssertEqual("12345678901234567890".insert("-", n: 10), "1234567890-1234567890")
- }
- }
- XCTMain([
- testCase(SolutionTest.allTests)
- ])
extension Int { var toBinary: Int { return (self < 0 ? -1 : 1) * (Int(String(abs(self), radix: 2)) ?? 0) } }
func binaryConverter(_ input: Int) -> Int {//Your Code Herereturn input- extension Int {
- var toBinary: Int {
- return (self < 0 ? -1 : 1) * (Int(String(abs(self), radix: 2)) ?? 0)
- }
- }
import XCTest // XCTest Spec Example: // TODO: replace with your own tests (TDD), these are just how-to examples to get you started class SolutionTest: XCTestCase { static var allTests = [ ("Test Example", testExample), ] func testExample() { XCTAssertEqual(0.toBinary, 0) XCTAssertEqual(1.toBinary, 1) XCTAssertEqual(5.toBinary, 101) XCTAssertEqual(10.toBinary, 1010) XCTAssertEqual(15.toBinary, 1111) XCTAssertEqual(20.toBinary, 10100) XCTAssertEqual(25.toBinary, 11001) XCTAssertEqual(50.toBinary, 110010) XCTAssertEqual(-50.toBinary, -110010) XCTAssertEqual(12345.toBinary, 11000000111001) } } XCTMain([ testCase(SolutionTest.allTests) ])
- import XCTest
- // XCTest Spec Example:
- // TODO: replace with your own tests (TDD), these are just how-to examples to get you started
- class SolutionTest: XCTestCase {
- static var allTests = [
- ("Test Example", testExample),
- ]
- func testExample() {
let actual = 1XCTAssertEqual(actual, 1)- XCTAssertEqual(0.toBinary, 0)
- XCTAssertEqual(1.toBinary, 1)
- XCTAssertEqual(5.toBinary, 101)
- XCTAssertEqual(10.toBinary, 1010)
- XCTAssertEqual(15.toBinary, 1111)
- XCTAssertEqual(20.toBinary, 10100)
- XCTAssertEqual(25.toBinary, 11001)
- XCTAssertEqual(50.toBinary, 110010)
- XCTAssertEqual(-50.toBinary, -110010)
- XCTAssertEqual(12345.toBinary, 11000000111001)
- }
- }
- XCTMain([
- testCase(SolutionTest.allTests)
- ])
func abbrevName(_ name: String) -> String { return name .split(separator: " ") .compactMap { $0.first?.uppercased() } .joined(separator: ".") }
- func abbrevName(_ name: String) -> String {
let components = name.components(separatedBy: " ")let left: String = String(components[0].first!)return left + "." + components[1].first!.uppercased()- return name
- .split(separator: " ")
- .compactMap { $0.first?.uppercased() }
- .joined(separator: ".")
- }
import XCTest class Tests: XCTestCase { static var allTests = [ ("", testBasic), ] func testBasic() { XCTAssertEqual("D.D", abbrevName("Donald Duck")) XCTAssertEqual("L.O.E", abbrevName("Leif O E")) } } XCTMain([ testCase(Tests.allTests) ])
- import XCTest
- class Tests: XCTestCase {
- static var allTests = [
- ("", testBasic),
- ]
- func testBasic() {
- XCTAssertEqual("D.D", abbrevName("Donald Duck"))
- XCTAssertEqual("L.O.E", abbrevName("Leif O E"))
- }
- }
- XCTMain([
- testCase(Tests.allTests)
- ])
Tips & Tricks
func printRandomNumber(in range: ClosedRange<Int>) { print(Int.random(in: range)) }
import Glibcfor count in 1...20 {print(random() % 100)- func printRandomNumber(in range: ClosedRange<Int>) {
- print(Int.random(in: range))
- }
import XCTest // XCTest Spec Example: // TODO: replace with your own tests (TDD), these are just how-to examples to get you started class SolutionTest: XCTestCase { static var allTests = [ ("Test Example", testExample), ] func testExample() { printRandomNumber(in: 0 ... 100) } } XCTMain([ testCase(SolutionTest.allTests) ])
- import XCTest
- // XCTest Spec Example:
- // TODO: replace with your own tests (TDD), these are just how-to examples to get you started
- class SolutionTest: XCTestCase {
- static var allTests = [
- ("Test Example", testExample),
- ]
- func testExample() {
- printRandomNumber(in: 0 ... 100)
- }
- }
- XCTMain([
- testCase(SolutionTest.allTests)
- ])