-
Code extension Int { var toBinary: Int { return (self < 0 ? -1 : 1) * (Int(String(abs(self), radix: 2)) ?? 0) } }
Test Cases 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) ])
Output:
-
Code 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)
- }
- }
Test Cases - 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)
- ])
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}
Please sign in or sign up to leave a comment.