0

I'm interested in doing something like this (C#):

int sum = 0;
for (int i = 0; i < 5; i++)
     sum += i;

But I would like to do it on one line. Something like:

int sum = (embedded loop)

Is there any way to do this on one line? I'm looking to apply this to a more complex string manipulation algorithm so I can't simply replace the loop with an arithmatic formula.

Daniel G
  • 29
  • 1
  • 1
  • 9
  • 1
    Is there a reason? – Amit Sep 22 '17 at 20:21
  • 8
    Sum = n* (n+1) / 2 – TNguyen Sep 22 '17 at 20:21
  • @TPN1994 has the formula for it, which is the *right solution*, but you could also do `var sum = Enumerable.Range(0,5).Sum();` – Matt Burland Sep 22 '17 at 20:24
  • I'm looking to apply this to a more complex algorithm that returns a manipulated string so I can't replace the loop with a formula; it actually needs to have an embedded loop. And I am trying to do this as part of a one-line coding challenge. Thanks! – Daniel G Sep 22 '17 at 20:24
  • 1
    @DanielG: Then give a better example of what you are trying to do. – Matt Burland Sep 22 '17 at 20:25
  • _And I am trying to do this as part of a one-line coding challenge_ - then @TPN1994 have provided most efficient answer for your challenge - it is one line, and do not need loop – Fabio Sep 22 '17 at 20:46
  • @MattBurland I'm trying to loop through a list of words and make each one title case, then return the string[].join(" ") of it – Daniel G Sep 22 '17 at 20:46

3 Answers3

5

So you want to loop and do something in one line?

First off, writing it in one line isn't going to make it run faster. You are still going to loop, you are still going to be O(n). For the particular problem you have in your question you can do something like:

var sum = Enumerable.Range(0,5).Sum();

This will basically make a collection of the numbers 0,1,2...4 and sum them.

More generally, since you want to do something, you might use the Aggregate function. For example:

var str = Enumerable.Range(0,5).Aggregate("",(p,c) => p + c); // Note: not efficient at all

Will take the values 0,1,...4 and do string concatenation with them (using a string builder would be better) to give you the string 01234.

Edit:

I'm trying to loop through a list of words and make each one title case, then return the string[].join(" ") of it

In that case, something like:

var result = string.Join(" ", myListOfWords.Select(w => char.ToUpper(w[0]) + w.Substring(1)));

Should work fine.

Matt Burland
  • 44,552
  • 18
  • 99
  • 171
  • Thank you, this is what I was looking for. I know it sounds stupid but it's for a challenge and it's not meant to be efficient. – Daniel G Sep 22 '17 at 20:32
1

If you don't mind that 'sum' only is in scope of the for loop you could do this:

for (int i = 0, sum = 0; i < 5; sum += i, ++i) { // do something }
oRole
  • 1,316
  • 1
  • 8
  • 24
0
for(int i=0,sum = 0; i<5; sum+=i,++i){}
Community
  • 1
  • 1
n4feng
  • 2,226
  • 3
  • 11
  • 17
  • BTW, it is a duplicated question since it is already indicated here: https://stackoverflow.com/questions/1232176/how-do-i-put-two-increment-statements-in-a-c-for-loop – n4feng Sep 22 '17 at 20:37
  • 1
    That's not what I was asking though. This is two lines. – Daniel G Sep 22 '17 at 20:47
  • Fair well, if you only want sum to be see in the scope then you can just put them in 1 line. And I don't think C# can declare a loop scope and a variable outside that scope in one single line though. – n4feng Sep 24 '17 at 01:14