-1

I have a Price list as

10
21
30
90
100
150
400

I want to add prices in 10 increments below 100. Above 100 to display in increments of 100

So my final price list would be like

10
20
30
40
50
60
70
80
90
100
200
300
400

Has anyone has done something similar to this. Any help or suggestions would be appreciated.

Thanks in advance

Owais Ahmed
  • 1,364
  • 1
  • 30
  • 62

3 Answers3

0

There is a bit of lack of clarity on whether you want to generate 'n' elements or whether you want to generate till the max value in the list.

Case 1 : If Generate till Max Value in List

public IEnumerable<int> Generate(int maxValue)
{
    var currentValue = 0;
    while(currentValue<maxValue)
    {
        currentValue += currentValue < 100 ? 10:100;
        yield return currentValue;
    }
}

You can now use the method as

var myList = new List<int>(new int[] { 12, 21, 30, 90, 100, 150, 404 });
var result = Generate(myList.Max());

Case 2 : When you want to generate N Elements.

var initialValue = 0;
var list = Enumerable.Range(1,totalNumberOfExpectedItems)
                     .Select(x=> 
                       { 
                          initialValue = initialValue < 100 ? initialValue + 10 : initialValue + 100; 
                          return initialValue;
                       })

Enumerable.Range aids in generating sequence in a specified range

For totalNumberOfExpectedItems = 13,

Output

10 
20 
30 
40 
50 
60 
70 
80 
90 
100 
200 
300 
400 
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
0

I believe that the following program works as you desire:

using System;
using System.Linq;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var prices = new int[]{10, 21, 30, 90, 100, 150, 400}.ToList();
            var finalPriceList =
                Enumerable
                .Range(0, 10)
                .Select(i => prices.Min() + 10 * i)
                .TakeWhile(i => i < Math.Min(100, prices.Max()))
                .Concat(
                    Enumerable
                    .Range(1, 1000)
                    .Select(i => 100 * i)
                    .TakeWhile(i => i <= prices.Max())
                ).ToList();
            Console.WriteLine(string.Join(", ", finalPriceList));
        }
    }
}

It prints:

10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400
Daniel Jonsson
  • 3,261
  • 5
  • 45
  • 66
0

Try this...

I am assuming you are getting all values between the lowest and highest values in the list.

List<int> myList = new List<int>(new int[] { 12, 21, 30, 90, 100, 150, 404 });
int max = myList.Max() - myList.Max() % 100;
int min = myList.Min() - myList.Min() % 10;

int tmp = (int)Math.Round(Math.Log10(min), MidpointRounding.ToZero);
List<int> newList = new List<int>();
for (int a = tmp, b = min / (int)Math.Pow(10, tmp); (Math.Pow(10, a) * b <= max); )
{
     newList.Add((int)(Math.Pow(10, a) * b));
     if (b < 9) b++;
     else
     {
         b = 1;
         a++;
     }
}

//newList : [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200, 300, 400]
preciousbetine
  • 2,959
  • 3
  • 13
  • 29