function mapOrder(array, order, key) { if (Array.isArray(array) != true) { return [] } return [ {id: 1, name: "April"}, {id: 2, name: "Julian"}, {id: 3, name: "Anne"}, {id: 4, name: "John"}, {id: 5, name: "Hugo"} ]; }
- function mapOrder(array, order, key) {
if (typeof array !== 'object' || typeof order !== 'object' || typeof key !== 'string') return [];array.sort((a, b) => {let A = a[key], B = b[key];if (order.indexOf(A) > order.indexOf(B)) {return 1;} else return -1;});return array;- if (Array.isArray(array) != true) {
- return []
- }
- return [
- {id: 1, name: "April"},
- {id: 2, name: "Julian"},
- {id: 3, name: "Anne"},
- {id: 4, name: "John"},
- {id: 5, name: "Hugo"}
- ];
- }
func abbrevName(_ name: String) -> String { let components = name.components(separatedBy: " ") let left: String = String(components[0].first!) return left + "." + components[1].first!.uppercased() }
public class AbbreviateTwoWords {public static String abbrevName(String name) {String[] names = name.split(" ");return (names[0].charAt(0) + "." + names[1].charAt(0)).toUpperCase();}- func abbrevName(_ name: String) -> String {
- let components = name.components(separatedBy: " ")
- let left: String = String(components[0].first!)
- return left + "." + components[1].first!.uppercased()
- }
import XCTest class Tests: XCTestCase { static var allTests = [ ("", testBasic), ] func testBasic() { XCTAssertEqual("D.D", abbrevName("Donald Duck")) } } XCTMain([ testCase(Tests.allTests) ])
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.runners.JUnit4; public class SolutionTest { @Test public void testFixed() { assertEquals("S.H", AbbreviateTwoWords.abbrevName("Sam Harris")); assertEquals("P.F", AbbreviateTwoWords.abbrevName("Patrick Feenan")); assertEquals("E.C", AbbreviateTwoWords.abbrevName("Evan Cole")); assertEquals("P.F", AbbreviateTwoWords.abbrevName("P Favuzzi")); assertEquals("D.M", AbbreviateTwoWords.abbrevName("David Mendieta")); } }- import XCTest
- class Tests: XCTestCase {
- static var allTests = [
- ("", testBasic),
- ]
- func testBasic() {
- XCTAssertEqual("D.D", abbrevName("Donald Duck"))
- }
- }
- XCTMain([
- testCase(Tests.allTests)
- ])
Given two strings, the task is to check whether these strings are meta strings or not. Meta strings are the strings which can be made equal by exactly one swap in any of the strings. Equal string are considered here as Meta strings.
func areMetaStrings(_ str1: String, _ str2: String) -> Bool {
return str1.lowercased().sorted() == str2.lowercased().sorted()
}
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() {
XCTAssertTrue(areMetaStrings("geeks", "keegs"))
XCTAssertTrue(areMetaStrings("rsting", "string"))
XCTAssertFalse(areMetaStrings("geeks", "peegs"))
}
}
XCTMain([
testCase(SolutionTest.allTests)
])
func gradeCalc(_ score: Int) -> String { if (90...100).contains(score) { return "A" } if (80..<90).contains(score) { return "B" } if (70..<80).contains(score) { return "C" } if (60..<70).contains(score) { return "D" } if (0..<60).contains(score) { return "F" } return "Not a grade" }
- func gradeCalc(_ score: Int) -> String {
switch score {case 90...100: return "A"case 80..<90: return "B"case 70..<80: return "C"case 60..<70: return "D"case 0..<60: return "F"default: return "Not a grade"}- if (90...100).contains(score) { return "A" }
- if (80..<90).contains(score) { return "B" }
- if (70..<80).contains(score) { return "C" }
- if (60..<70).contains(score) { return "D" }
- if (0..<60).contains(score) { return "F" }
- return "Not a grade"
- }
Add a given separator to a string at every n characters. When n is negative or equal to zero then return the orginal string.
func insert(seperator: String, afterEveryXChars: Int, intoString: String) -> String {
guard afterEveryXChars > 0 else { return intoString }
var output = ""
intoString.enumerated().forEach { index, c in
if index % afterEveryXChars == 0 && index > 0 {
output += seperator
}
output.append(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(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")
}
}
XCTMain([
testCase(SolutionTest.allTests)
])
For a given string return a substring without first and last elements of the string.
If the given string has less than three characters then return an empty string.
func substringWithoutFirstAndLastElement(_ string: String) -> String {
guard string.count > 2 else { return "" }
return String(String(string.dropFirst()).dropLast())
}
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 Empty", testEmpty),
("Test Single Character", testSingleCharacter),
("Test Two Characters", testTwoCharacters),
("Test Three Characters", testThreeCharacters),
("Test More Than Three Characters", testMoreThanThreeCharacters),
]
func testEmpty() {
XCTAssertEqual(substringWithoutFirstAndLastElement(""), "")
}
func testSingleCharacter() {
XCTAssertEqual(substringWithoutFirstAndLastElement("a"), "")
}
func testTwoCharacters() {
XCTAssertEqual(substringWithoutFirstAndLastElement("aa"), "")
}
func testThreeCharacters() {
XCTAssertEqual(substringWithoutFirstAndLastElement("aba"), "b")
}
func testMoreThanThreeCharacters() {
XCTAssertEqual(substringWithoutFirstAndLastElement("dzlaednzed"), "zlaednze")
}
}
XCTMain([
testCase(SolutionTest.allTests)
])
Check if a given Int number have a perfect square.
func isPerfectSquare(_ input: Int) -> Bool {
let inputAsDouble = Double(input)
return inputAsDouble.squareRoot().rounded() == inputAsDouble.squareRoot()
}
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 Numbers From 0 To 10", testNumbersFrom0To10),
]
func testNumbersFrom0To10() {
XCTAssertEqual(isPerfectSquare(0), true, "0")
XCTAssertEqual(isPerfectSquare(1), true, "1")
XCTAssertEqual(isPerfectSquare(2), false, "2")
XCTAssertEqual(isPerfectSquare(3), false, "3")
XCTAssertEqual(isPerfectSquare(4), true, "4")
XCTAssertEqual(isPerfectSquare(5), false, "5")
XCTAssertEqual(isPerfectSquare(6), false, "6")
XCTAssertEqual(isPerfectSquare(7), false, "7")
XCTAssertEqual(isPerfectSquare(8), false, "8")
XCTAssertEqual(isPerfectSquare(9), true, "9")
XCTAssertEqual(isPerfectSquare(10), false, "10")
}
}
XCTMain([
testCase(SolutionTest.allTests)
])