-3

Does anyone know how I might achieve something like this through Lambda expression?

public static string getString(char c, int lenght)
{
    char[] temp = new char[lenght];

    for (int i = 0; lenght > 0; lenght--, i++)
    {
        temp[i] = c;
    }

    return new string(temp);
}

It's a small method that I loop through:

for (int i = 0, j = width; j > 0; j--, i++)
{
    item[i] = getString((char)(value + 48), j);
    Console.WriteLine(item[i]);
}

To output a triangle of a number between 1 - 9.

Example:

666666
66666
6666
666
66
6

I've never used Lambda, but would like to learn it to achieve small loops like this.

If anyone knows a place full of examples, please point me in the direction.

CDspace
  • 2,639
  • 18
  • 30
  • 36
  • 3
    Lambda expressions are cheap anonymous methods; they do not replace loops. – BradleyDotNET Sep 21 '17 at 20:07
  • You could use : Enumerable.Repeat("6", 6); – jdweng Sep 21 '17 at 20:20
  • `new string('6', 6)` https://msdn.microsoft.com/en-us/library/xsa4321w(v=vs.110).aspx – Matthew Whited Sep 21 '17 at 20:44
  • the string constructor was good info, thanks Mat, couldnt sleep so I was playing around with some ideas. tried combining the string constructor with the enumerable.repeat, didn't get the results I was expecting, but honestly that is because I don't use Enumerable methods, so i am unsure how they do their job. here's what i tried int i = 5; int j = i;char c = '6'; string[] str = Enumerable.Repeat((new string(c, j--)), i).ToArray(); foreach(string s in str) { Console.WriteLine(s); } i know why it didnt work, dont know why I thought it might haha, late nights i guess – Darren Bell Sep 23 '17 at 02:29

3 Answers3

0

A LINQ solution for your specific example would be the following:

private string GeneratePyramid(int number, int pyramidHeight)
{
    return string.Concat(Enumerable.Repeat(true, pyramidHeight).Select((n, i) => {
        return new string(Convert.ToChar(number.ToString()), pyramidHeight - i) +
            Environment.NewLine;
    }));
}

(Adopted some nice tricks from @Matthew Whited's answer regarding new string(char, count) and not having to reverse my result)

We take advantage of Enumerable.Repeat to create an array which s length is equal to pyramidHeight, and after that we use Enumerable.Repeat again to create a string with the number repeated i (index) times.

The select method creates a IEnumerable but as mentioned we want to flatten the resulting array of strings to a single string, so we once again use string.Concat to do that for us.

When invoked as such:

Console.WriteLine(GeneratePyramid(9, 12));

It will print the following pyramid:

enter image description here

nbokmans
  • 5,492
  • 4
  • 35
  • 59
0
var lines = Enumerable.Range(0, 6)
                      .Select(i => new string('6', 6 - i));
foreach (var line in lines)
    Console.WriteLine(line);

Output

666666
66666
6666
666
66
6
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
0
char c = (char)(size + 48);

IEnumerable<string> pyramid = Enumerable.Range(1, size)
  .Reverse()
  .Select(i => new String(c, i);

foreach(string line in pyramid)
{
  Console.WriteLine(line);
}

check out this answer. https://stackoverflow.com/a/471592/8155

Amy B
  • 108,202
  • 21
  • 135
  • 185