From 5b33b014a0e62e7eebb9fcedb8f6576c046c4c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Borba?= Date: Sat, 19 Sep 2026 09:29:49 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9F=A2=20Port=20problem=202703=20(Return?= =?UTF-8?q?=20Length=20of=20Arguments=20Passed)=20from=20Swift=20to=20C++2?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replaces swift/2703.swift with solutions/2701-2800/2703-return-length-of-arguments-passed/. The original LeetCode problem is JavaScript-specific (arguments.length); modeled here as a function over an initializer_list. --- .../README.md | 13 +++++++++++++ .../solution.cpp | 9 +++++++++ .../solution.hpp | 13 +++++++++++++ .../test.cpp | 12 ++++++++++++ swift/2703.swift | 15 --------------- 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 solutions/2701-2800/2703-return-length-of-arguments-passed/README.md create mode 100644 solutions/2701-2800/2703-return-length-of-arguments-passed/solution.cpp create mode 100644 solutions/2701-2800/2703-return-length-of-arguments-passed/solution.hpp create mode 100644 solutions/2701-2800/2703-return-length-of-arguments-passed/test.cpp delete mode 100644 swift/2703.swift 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") -}