开发者

I need to add ranges and if a range is missed while adding i should display a message

开发者 https://www.devze.com 2022-12-28 10:34 出处:网络
EXAMPLE :if i add a ran开发者_JS百科ge {1,10} and another range{15,20} i should get a message saying the ranges from 11 to 14 are missing.How are you adding the ranges?

EXAMPLE :if i add a ran开发者_JS百科ge {1,10} and another range{15,20} i should get a message saying the ranges from 11 to 14 are missing.


How are you adding the ranges?

If the ranges are added in order, you can just keep the end of the last range and compare to the start of the range that you add.

If you can add the ranges in any order, you have to do the check when all ranges are added. Sort the ranges on the range start, then loop through the ranges and compare the end of a range with the start of the next range.

update

Example of the second case. First a class to hold ranges:

public class Range {

  public int From { get; private set; }
  public int To { get; private set; }

  public Range(int from, int to) {
    From = from;
    To = to;
  }

}

Create a list of ranges:

List<Range> ranges = new List<Range>();
ranges.Add(new Range(15, 20));
ranges.Add(new Range(1, 10));

Test the ranges:

ranges = ranges.OrderBy(r => r.From).ToList();
for (int i = 1; i < ranges.Count; i++) {
  int to = ranges[i - 1].To;
  int from = ranges[i].From;
  int diff = to.CompareTo(from - 1);
  if (diff < 0) {
    Response.Write("Range from " + (to + 1).ToString() + " to " + (from - 1).ToString() + " is missing<br/>");
  } else if (diff > 0) {
    Response.Write("Range ending at " + to.ToString() + " overlaps range starting at " + from.ToString() + "<br/>");
  }
}

Note: The code checks for both gaps in the ranges and overlapping ranges. If overlapping ranges is not a problem, just remove the part that checks for diff > 0.


It isn't clear what your code is trying to do, but here's the basic approach you should probably take:

  1. Create a Range type that has a Start and End
  2. Order the collection by the Start property (possibly using Enumerable.OrderBy).
  3. Traverse the pairs of ranges in the ordered collection (possibly using Enumerable.Zip to zip the collection with itself offset by one) and check whether they are adjacent. If not, yield the missing range.
  4. Format an error messages using the missing ranges collected from (3).

e.g.

var ordered = ranges.OrderBy(range => range.Start);
var pairs = ordered.Zip(ordered.Skip(1), (a, b) => new { a, b });
var missing = from pair in pairs
              where pair.a.End + 1 < pair.b.Start
              select new Range(pair.a.End + 1, pair.b.Start - 1);


An input tolerant solution and the programming Kata used to build it.

public class Range
{
    public int End;
    public int Start;

    public Range(int start, int end)
    {
        Start = start;
        End = end;
    }
}

public class RangeGapFinder
{
    public Range FindGap(Range range1, Range range2)
    {
        if (range1 == null)
        {
            throw new ArgumentNullException("range1", "range1 cannot be null");
        }

        if (range2 == null)
        {
            throw new ArgumentNullException("range2", "range2 cannot be null");
        }

        if (range2.Start < range1.Start)
        {
            return FindGap(range2, range1);
        }

        if (range1.End < range1.Start)
        {
            range1 = new Range(range1.End, range1.Start);
        }

        if (range2.End < range2.Start)
        {
            range2 = new Range(range2.End, range2.Start);
        }

        if (range1.End + 1 >= range2.Start)
        {
            return null; // no gap
        }

        return new Range(range1.End + 1, range2.Start - 1);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号