@{
ViewData["Title"] = "Home Page";
}
<script>
// Function to find ranges that intersect with provided bounds
function findMatchingRanges(ranges,
From, To) {
var matchingRanges = [];
// Iterate through each range
for (var i = 0; i < ranges.length;
i++) {
var range = ranges[i];
// Check if the current range intersects with provided bounds
if (From < range.ToKg
&& To > range.FromKg) {
matchingRanges.push(range); // Add the range to matchingRanges array
}
}
return matchingRanges; // Return array of matching ranges
}
// Function to find missing ranges within provided ranges
function findMissingRanges(ranges,
From, To) {
var missingRangesFound = false;
// Check for missing range before the first range
if (ranges[0].FromKg > 1) {
// Check if the lower bound is zero
if (0 == From) {
console.log("Missing range between 0 and", ranges[0].FromKg - 1); //
Log missing range
missingRangesFound = true;
return true; // Indicate missing range found
} else {
console.log("Hi"); // Log message indicating non-zero lower bound
return true; // Indicate missing range found
}
}
// Check for missing ranges between subsequent ranges
for (var i = 0; i < ranges.length
- 1; i++) {
var currentRange = ranges[i];
var nextRange = ranges[i + 1];
// Check for gap between current and next range
if (currentRange.ToKg + 1 !==
nextRange.FromKg) {
console.log("Missing range between", currentRange.ToKg + 1, "and", nextRange.FromKg - 1); //
Log missing range
missingRangesFound = true;
return true; // Indicate missing range found
}
}
// If no missing range is found, indicate it
if (!missingRangesFound) {
console.log("No missing range found."); // Log message indicating no
missing range
}
}
// Define ranges array
var ranges = [
{ FromKg: 6, ToKg:
50 },
{ FromKg: 51, ToKg:
61 },
{ FromKg: 101, ToKg:
120 },
{ FromKg: 201, ToKg:
300 },
{ FromKg: 400, ToKg:
500 }
];
// Define From and To values
var From = 0;
var To = 4;
// Find matching ranges and log them
var matchingRanges =
findMatchingRanges(ranges, From, To);
if (matchingRanges.length ===
0) {
console.log("No matching range found"); // Log message indicating no
matching range found
} else {
console.log("Matching ranges found:", matchingRanges); // Log
matching ranges
}
// Find missing ranges and log them
findMissingRanges(ranges, From, To);
</script>
-------
using System;
using System.Collections.Generic;
class Program
{
// Function to find ranges that intersect with provided bounds
static List<Range>
FindMatchingRanges(List<Range> ranges, int From, int To)
{
List<Range>
matchingRanges = new List<Range>();
// Iterate through each range
foreach (var range in ranges)
{
// Check if the current range intersects with provided bounds
if (From < range.ToKg
&& To > range.FromKg)
{
matchingRanges.Add(range); //
Add the range to matchingRanges list
}
}
return matchingRanges; // Return list of matching ranges
}
// Function to find missing ranges within provided ranges
static bool
FindMissingRanges(List<Range> ranges, int From, int To)
{
bool missingRangesFound = false;
// Check for missing range before the first range
if (ranges[0].FromKg > 1)
{
// Check if the lower bound is zero
if (From == 0)
{
Console.WriteLine("Missing range between
0 and " + (ranges[0].FromKg - 1)); // Log missing range
missingRangesFound = true;
return true; // Indicate missing range found
}
else
{
Console.WriteLine("Hi"); // Log message indicating
non-zero lower bound
return true; // Indicate missing range found
}
}
// Check for missing ranges between subsequent ranges
for (int i = 0; i < ranges.Count -
1; i++)
{
var currentRange = ranges[i];
var nextRange = ranges[i + 1];
// Check for gap between current and next range
if (currentRange.ToKg + 1 !=
nextRange.FromKg)
{
Console.WriteLine("Missing range between
" + (currentRange.ToKg + 1) + " and " +
(nextRange.FromKg - 1)); // Log missing range
missingRangesFound = true;
return true; // Indicate missing range found
}
}
// If no missing range is found, indicate it
if (!missingRangesFound)
{
Console.WriteLine("No missing range
found."); // Log message indicating no missing range
}
return missingRangesFound;
}
// Range class representing a range of values
class Range
{
public int FromKg { get; set; }
public int ToKg { get; set; }
}
static void Main(string[] args)
{
// Define ranges list
var ranges = new List<Range>
{
new Range { FromKg = 6, ToKg =
50 },
new Range { FromKg = 51, ToKg =
61 },
new Range { FromKg = 101, ToKg =
120 },
new Range { FromKg = 201, ToKg =
300 },
new Range { FromKg = 400, ToKg = 500 }
};
// Define From and To values
var From = 0;
var To = 4;
// Find matching ranges and log them
var matchingRanges =
FindMatchingRanges(ranges, From, To);
if (matchingRanges.Count == 0)
{
Console.WriteLine("No matching range
found"); // Log message indicating no matching range found
}
else
{
Console.WriteLine("Matching ranges
found:");
foreach (var range in matchingRanges)
{
Console.WriteLine($"From: {range.FromKg}, To: {range.ToKg}");
}
}
// Find missing ranges and log them
FindMissingRanges(ranges, From, To);
}
}
-- Define
a table to store ranges
CREATE TABLE Ranges (
FromKg INT,
ToKg INT
);
-- Insert
sample data into the table
INSERT INTO Ranges (FromKg, ToKg)
VALUES
(6, 50),
(51, 61),
(101, 120),
(201, 300),
(400, 500);
--
Function to find matching ranges
CREATE FUNCTION FindMatchingRanges
(
@From INT,
@To INT
)
RETURNS TABLE
AS
RETURN
(
SELECT *
FROM Ranges
WHERE @From < ToKg AND @To > FromKg
);
--
Function to find missing ranges
CREATE FUNCTION FindMissingRanges
(
@From INT,
@To INT
)
RETURNS TABLE
AS
RETURN
(
SELECT 'Missing range between ' + CAST((r.ToKg + 1) AS VARCHAR) + ' and ' + CAST((n.FromKg - 1) AS VARCHAR) AS MissingRange
FROM Ranges r
INNER JOIN Ranges n
ON r.ToKg < n.FromKg
WHERE r.ToKg + 1 BETWEEN @From AND @To
);
--
Example usage
DECLARE @From INT = 0;
DECLARE @To INT = 4;
-- Find
matching ranges
SELECT * FROM FindMatchingRanges(@From, @To);
-- Find
missing ranges
SELECT * FROM FindMissingRanges(@From, @To);