Could golang implement this interview question of getting array summary without for/while if/else?

huangapple go评论87阅读模式
英文:

Could golang implement this interview question of getting array summary without for/while if/else?

问题

我正在研究一个有趣的面试问题,并尝试用Go语言实现它。

(1) 输入一个整数,比如i

(2) 计算1+2+...+i的总和,并输出结果

(3) 要求:不使用乘法,不使用循环(for/while),也不使用if/else语句

在C++或Java中,这个问题很容易解决。我们可以使用静态变量初始化一个对象数组,而对象的构造函数计算这个总和。像这样:

#include <iostream>
struct s {
    static int count;
    static int sum;
    s() {++count; sum += count;}
};
int s::count = 0;
int s::sum = 0;
 
int main(int argc, char *argv[]) {
   s obj[10];
   std::cout << s::sum << std::endl; // 55, ok
   return 0;
}

在C++中,我们还可以使用模板类型推导来实现。互联网上有很多示例。

但是,我们能用Go语言实现这个吗?

我知道Go语言既没有构造函数,也不支持静态变量。它也没有任何模板语法。此外,使用递归函数仍然需要在代码中使用if分支。

所以,在Go语言中能否做到同样的事情呢?(不使用for循环,不使用if else)。谢谢。

英文:

I'm looking into an interesting interview question, and try to implement it with go.

> (1) Input an integer number, say i

> (2) Calculate the summary of 1+2+...+i, output the summary

> (3) Requirement: don't use multiply, don't use loop(for/while), and don't use if/else

Well, in c++ or java this is pretty easy. We can use static variable to initialize an object array, while the objects's constructor function calculate this summary. Like this:

#include &lt;iostream&gt;
struct s {
    static int count;
    static int sum;
    s() {++count; sum += count;}
};
int s::count = 0;
int s::sum = 0;
 
int main(int argc, char *argv[]) {
   s obj[10];
   std::cout &lt;&lt; s::sum &lt;&lt; std::endl; // 55, ok
   return 0;
}

(2) in c++ we could also use template type deduction to do this. Plenty samples on the internet.

But, can we achieve this with go language?

I know that golang has neither constructor function, nor it supports static variable. And it doesn't have any template syntax. Plus, using recursive function still requires if branches in the code.

So is this possible to do same thing in go? (no for loop, no if else). Thanks.

答案1

得分: 1

这是一个语言中立的解决方案,不使用乘法、for循环和if语句。

这种解决方案有点像递归解决方案,其中if语句被替换为一个函数映射,该映射具有true和false分支的函数:

fs := map[bool]func(int) int{}
fs[false] = func(int) int { return 0 }
fs[true] = func(i int) int { return i + fs[i > 1](i-1) }

var i int
fmt.Scanln(&i)

fmt.Println("sum:", fs[i > 0](i))

输入10,输出结果为(在Go Playground上尝试):

sum: 55
英文:

Here's a language-neutral solution that uses no multiplication, no for and no if.

It's kind of like a recursive solution, where the if is substituted with a function map, having functions for the true and false branches:

fs := map[bool]func(int) int{}
fs[false] = func(int) int { return 0 }
fs[true] = func(i int) int { return i + fs[i &gt; 1](i-1) }

var i int
fmt.Scanln(&amp;i)

fmt.Println(&quot;sum:&quot;, fs[i &gt; 0](i))

Inputting 10, the output is (try it on the Go Playground):

sum: 55

huangapple
  • 本文由 发表于 2022年8月19日 15:53:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/73413518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定