diff --git a/solutions/2701-2800/2703-return-length-of-arguments-passed/README.md b/solutions/2701-2800/2703-return-length-of-arguments-passed/README.md new file mode 100644 index 0000000..4cdfba5 --- /dev/null +++ b/solutions/2701-2800/2703-return-length-of-arguments-passed/README.md @@ -0,0 +1,13 @@ +# 2703. Return Length of Arguments Passed + +[LeetCode problem 2703](https://leetcode.com/problems/return-length-of-arguments-passed/) + +- **Difficulty**: Easy +- **Tags**: javascript + +## Description + +Write a function that accepts any number of arguments of any type and +returns how many arguments it received. The original problem is +JavaScript-specific (it hands back `arguments.length`); here it's modeled as +a function taking a brace-init list of `std::any` values. diff --git a/solutions/2701-2800/2703-return-length-of-arguments-passed/solution.cpp b/solutions/2701-2800/2703-return-length-of-arguments-passed/solution.cpp new file mode 100644 index 0000000..6cb3553 --- /dev/null +++ b/solutions/2701-2800/2703-return-length-of-arguments-passed/solution.cpp @@ -0,0 +1,9 @@ +#include "solution.hpp" + +namespace leetcode::p2703 { + +std::size_t solve(std::initializer_list args) { + return args.size(); +} + +} // namespace leetcode::p2703 diff --git a/solutions/2701-2800/2703-return-length-of-arguments-passed/solution.hpp b/solutions/2701-2800/2703-return-length-of-arguments-passed/solution.hpp new file mode 100644 index 0000000..2a170a6 --- /dev/null +++ b/solutions/2701-2800/2703-return-length-of-arguments-passed/solution.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include + +namespace leetcode::p2703 { + +// optimized: O(1) time, O(1) space — an initializer_list already knows its +// size. +std::size_t solve(std::initializer_list args); + +} // namespace leetcode::p2703 diff --git a/solutions/2701-2800/2703-return-length-of-arguments-passed/test.cpp b/solutions/2701-2800/2703-return-length-of-arguments-passed/test.cpp new file mode 100644 index 0000000..9cfb785 --- /dev/null +++ b/solutions/2701-2800/2703-return-length-of-arguments-passed/test.cpp @@ -0,0 +1,12 @@ +#include +#include + +#include + +#include "solution.hpp" + +TEST_CASE("Problem 2703", "[p2703]") { + CHECK(leetcode::p2703::solve({std::any{}, std::string("hello"), 2}) == 3); + CHECK(leetcode::p2703::solve({}) == 0); + CHECK(leetcode::p2703::solve({1, 2, 3, std::any{}, 5}) == 5); +} diff --git a/swift/2703.swift b/swift/2703.swift deleted file mode 100644 index a19e9ed..0000000 --- a/swift/2703.swift +++ /dev/null @@ -1,15 +0,0 @@ -func argumentsLength(args: [Any?]) -> Int { - return args.count -} - -let tests = [ - argumentsLength(args: [nil, "hello", 2]) == 3, - argumentsLength(args: []) == 0, - argumentsLength(args: [1, 2, 3, nil, 5]) == 5 -] - -if tests.allSatisfy({ $0 == true }) { - print("All tests passed") -} else { - print("Some tests failed") -}