1

I've been set a challenge that I'm trying to get my head around, but am struggling with the best (or 'correct') way to implement it. The challenge is to create a simple console app written in Go that calculates the sum of squares of n numbers. However, I must do so without using a for loop and only the standard library.

Having never touched Go before this point, I assumed it would just be a case of using a while loop, yet after digging through the docs I know that functionality in Go is folded into the for statement. No luck there then.

I also know that Go implements goto statements which seem would be an option but in my ~2 years of programming, I've been taught to avoid them like the plague as they're 'terrible form' and only hang around for a few specific cases, thus I cannot imagine that this challenge is looking for me to come up with a goto statement.

While I know that Go has some more complex flow control functionality that I haven't mentioned, I haven't come across anything obvious that would replicate the functionality that I'd get from a simple for loop.

What conceptually can replicate a for loop in go?

  • 1
    Are you familiar with the concept of Recursion? gobyexample.com/recursion – KChaloux Mar 29 '16 at 13:01
  • Thanks for the reply, I'm familiar with the theory but have never used it so wouldn't have thought of it off the top of my head. Much appreciated! Not sure why the down votes on this? I was of the opinion that Programmers was for asking theoretical questions? – Hexodus Mar 29 '16 at 13:14
  • 2
    "Theoretical" questions often are on-topic here. My guess is the downvotes are either because 1) it sounds kind of like a code-writing question which is off-topic here 2) when a problem has artificial constraints like "I can't use for because I don't want to" it's usually a very uninteresting problem 3) the alternative solution is a trivial "use recursion" and nothing else. But personally I don't see the need to downvote this, just post the (admittedly trivial and uninteresting) answer then move on. – Ixrec Mar 29 '16 at 13:22
  • 1
    I just made a short edit to the question to hopefully make it better scoped. I think this is on topic and well written. It might help to edit your question to include some examples however as it would be a bit more clear what specifically you have tried and are interested in doing. – enderland Mar 29 '16 at 13:25
  • 1
    One alternative in Go is to use channels and goroutines, although that's not something that I'd expect from someone that has "never touched Go before this point." Was this an interview question? – kdgregory Mar 29 '16 at 14:56
4

Usually challenges like this are intended to either test or teach you the unique idioms of a language or its standard library. Usually the answer would be fairly short, elegant, or at least clever if done "correctly" but tedious and ugly otherwise. For example, this problem solved in idiomatic Scala is:

input.map(x => x*x).sum

Therefore, I would stay away from the solutions, like "just use recursion," that are applicable to any language, and look for things that are unique to Go, like channels and goroutines, and keep refining your solution until it is as easy to understand as a for loop, provided you are familiar with the language. Then you will have learned quite a bit about how to use Go to its own best advantage.

  • 1
    This is a fair approach, which is more practical than mine. I brought up the concept of recursion because I believe it's an important one to understand, and to have in your toolbox. I do agree however that in a practical scenario that it's important to know what methods of flow control are idiomatic to your specific language (for example, Ruby has a for loop, but you almost always want to use .each {|x| ...} instead) – KChaloux Mar 29 '16 at 20:40
2

I'm not familiar with Go as a language, but I briefly checked it out, and it does support Recursion. If your language supports recursion (and most do), you can implement looping behavior without ever explicitly using a loop.

Recursion is performed when a function makes a call to itself, usually operating on some subset of the original data passed in. Using pseudocode, you could calculate the sums of the squares of a list of numbers this way:

def calculateSquareSums (values : [Int]):
  if values is empty: // This is our 'base case' - it stops the function from recursing
    return 0
  else:
    square : Int = values[0] * values[0]       // Get the square of the first value
    rest : [Int] = values[1..]                 // Get the remaining values
    return square + calculateSquareSums(rest)  // Recursively call this function to process the remaining values

If you were to pass this function a list of values, say [7, 5, 8, 2] it would act as though it had expanded to ((7*7) + ((5*5) + ((8*8) + ((2*2) + 0)))).

Recursion is an extremely powerful concept that has a lot of applications, especially in divide-and-conquer type algorithms. However, it has some downsides and stipulations depending on the language you're using.

  • In many languages, each recursive call pushes a new Stack Frame, meaning that recursive functions that have a lot of calls may cause a Stack Overflow Exception. Some languages can optimize this away for certain cases.
  • Recursive functions can be harder to wrap your head around than simple loops, as you mentally have to keep track of the state going into each call.
1

I do not know the Go language.

If there is or if you can create a function in Go that is the equivalent of JavaScript’s setInterval and clearInterval then it is possible to find the sum of the squares of n numbers using them instead of using any type of for-loop construct. I do not know if this is the best solution. However it is a solution I would accept.

If you have access to a user-interface library within the Go language, it is also possible to find the sum of the squares of n number using events triggered by the user via mouse or keyboard clicks. For every click, the square of a number is added to a running total.

Recursion would be another way of computing the sum of squares of n numbers. However if every function call includes a dynamic allocation of computer memory then recursion would be a poor choice when n is large.

As a demonstration, I created an form-based app instead of a console-based app. The app uses JavaScript's setInterval and clearInterval functions. An input text field is used to enter the array of numbers to process. Another textfield is used to display the sum. If you are interested, the app is a dynamic, interactive diagram in the following PDF document: http://www.aespen.ca/AEnswers/VYJLj1459603849.pdf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.