Hone logo
Hone
Problems

Mastering Variadic Functions in Go: The Flexible Calculator

Go's variadic functions allow you to pass a variable number of arguments to a function, offering incredible flexibility. This challenge will test your understanding of how to define and utilize these powerful features in a practical scenario. You'll implement a flexible calculator that can perform operations on any number of numeric inputs.

Problem Description

Your task is to create a Go program that includes a variadic function capable of performing calculations on a variable list of numbers. Specifically, you need to implement a function that can sum all provided numbers. Later, you will extend this to support other basic arithmetic operations like subtraction and multiplication. The function should be robust enough to handle cases where no arguments are provided.

Key Requirements:

  1. Summation Function: Implement a variadic function, let's call it Calculate, that accepts a variable number of integers (...int) and returns their sum.
  2. Flexible Operation: Extend the Calculate function to accept an additional first argument, an Operation type (which you'll define), to specify the desired calculation.
  3. Support for Operations: Implement support for at least three operations: Add, Subtract, and Multiply.
  4. Handling No Arguments: The function should gracefully handle cases where no numbers are provided.

Expected Behavior:

  • When called with Add and a list of numbers, it should return the sum of those numbers.
  • When called with Subtract and a list of numbers, it should perform sequential subtraction (e.g., a - b - c).
  • When called with Multiply and a list of numbers, it should return the product of those numbers.
  • If no numbers are provided, the Add operation should return 0, Subtract should return 0, and Multiply should return 1 (as 1 is the multiplicative identity).

Edge Cases:

  • Calling Calculate with no numbers.
  • Calling Calculate with only one number.

Examples

Example 1: Summation

Input: Calculate(Add, 1, 2, 3, 4, 5)
Output: 15
Explanation: The function sums all the provided integers: 1 + 2 + 3 + 4 + 5 = 15.

Example 2: Subtraction

Input: Calculate(Subtract, 10, 2, 3)
Output: 5
Explanation: The function performs sequential subtraction: 10 - 2 - 3 = 5.

Example 3: Multiplication

Input: Calculate(Multiply, 2, 3, 4)
Output: 24
Explanation: The function multiplies all the provided integers: 2 * 3 * 4 = 24.

Example 4: No Arguments

Input: Calculate(Add)
Output: 0
Explanation: When no numbers are provided for addition, the sum is the additive identity, 0.

Input: Calculate(Multiply)
Output: 1
Explanation: When no numbers are provided for multiplication, the product is the multiplicative identity, 1.

Example 5: Single Argument

Input: Calculate(Subtract, 7)
Output: 7
Explanation: With only one number, subtraction results in that number itself.

Input: Calculate(Add, 9)
Output: 9
Explanation: With only one number, addition results in that number itself.

Constraints

  • The input numbers will be integers.
  • The Operation type will be a custom type (e.g., an iota based enumeration).
  • The function should be efficient for a reasonable number of arguments (up to 1000).
  • No external libraries are permitted beyond standard Go packages.

Notes

  • Consider how you will define the Operation type. An iota constant is a common and idiomatic way to do this in Go.
  • The variadic parameter will be a slice of integers. You'll need to iterate over this slice to perform the calculations.
  • Pay close attention to the base cases for each operation when no arguments or only one argument is provided. This is crucial for correctness.
  • The Calculate function signature will look something like func Calculate(op Operation, nums ...int) int.
Loading editor...
go