从数字列表创建范围? [英] Create Ranges from a list of numbers?

查看:64
本文介绍了从数字列表创建范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含我的信息的DataRow数组.

该表基于我包含的10000个数字.我想要做的就是将这些数字取值并将其压缩到数值范围内.

所以我可能会有1,2,3,4,5,6,7,8,9,22,23,25,26,27,28 ,22-23,25-28.

我没有的代码没有提供我想要的结果.

I have a DataRow Array that contains my info.

The table this is based on my contain 10000 numbers. What I want to do is take these numbers and condense them into numerical ranges.

so I might have 1,2,3,4,5,6,7,8,9,22,23,25,26,27,28 I would want to condense this down into ranges so that it would be 1-9,22-23,25-28.

The code I have not is not providing the results I want.

DataRow[] drOrdZips = dtZI.Select("Zone IS NOT NULL and Zone <> 'Zone'", "Zone,SCAC,ZIP");
 
int intZipStart = int.Parse(string.Format("{0:00000}", drOrdZips[0]["Zip"].ToString()));
int intPrevZip = intZipStart;
int intZipEnd;
string strZone = drOrdZips[0]["Zone"].ToString();
string strSCAC = drOrdZips[0]["SCAC"].ToString();
 
for (int x = 0; x < drOrdZips.Length; x++)
{
    intZipEnd = int.Parse(string.Format("{0:00000}", drOrdZips[x]["Zip"].ToString()));

    if ((intZipEnd - intPrevZip == 1 || intZipEnd - intPrevZip == 0) && strZone == drOrdZips[x]["Zone"].ToString() && drOrdZips[x]["importID"].ToString() == _intImportID.ToString() && strSCAC == drOrdZips[x]["SCAC"].ToString())
    {
        intPrevZip = intZipEnd;
    }
    else
    {
        RateInserter.ZoneImportRangeRow drZIR = dtZIR.NewZoneImportRangeRow();
        drZIR.importID = _intImportID;
        drZIR.ZipStart = string.Format("{0:00000}", intZipStart);
        drZIR.ZipEnd = string.Format("{0:00000}", intZipEnd);
        drZIR.Zone = drOrdZips[x]["Zone"].ToString();
        if (!string.IsNullOrEmpty(drOrdZips[x]["SCAC"].ToString()))
        {
            drZIR.SCAC = drOrdZips[x]["SCAC"].ToString();
            drZIR.Master_Is_CC = true;
        }

        dtZIR.AddZoneImportRangeRow(drZIR);

        intZipStart = x + 1 < drOrdZips.Length ? int.Parse(drOrdZips[x + 1]["Zip"].ToString()) : int.Parse(drOrdZips[x]["Zip"].ToString());
        intPrevZip = intZipStart;

        strZone = drOrdZips[x]["Zone"].ToString();
        strSCAC = drOrdZips[x]["SCAC"].ToString();
    }
}



任何帮助将不胜感激.



Any help would be greatly appreciated.

推荐答案

我不知道为什么您的代码看起来如此复杂.如果对输入序列进行排序,则该算法非常简单.您需要定义"Range"类并使用范围列表.

I don''t know why your code looks so complicated. The algorithm is extremely simple if the input sequence is sorted. You need to define the class "Range" and use the list of ranges.

using RangeList = System.Collections.Generic.List<Range>;

internal class Range {
   internal Range(int start) { fStart = start; fLength = 1; } 
   internal Range(int start, int length) { fStart = start; fLength = length; } 
   internal void Add() { Length++; }
   internal int Start { return fStart; }
   internal int Range { return fLength; }
   internal int Last { return fStart + fLength; }
   int fStart, fLength;
}



现在,一些伪代码:



Now, some pseudo-code:

RangeList rangeList = new RangeList();
int previous = (first in inputSequence) - 1;
currentRange = new Range(first in inputSequence);
rangeList.Add(currentRange);
foreach (int current in inputSequence) {
    currentRange = last in rangeList;
    if ((current - previous) == 1) //consecutive
       currentRange.Add();
    else {
       currentRange = new Range(current);
       rangeList.Add(currentRange);
    }
    previous = current;
}



这个想法很简单:您支持currentRange作为rangeList中的最后一个范围,并在每次检测到输入序列中的非连续值时添加新范围.为了检测连续值,请记住先前的值.如果该值是连续的,请增加currentRange,否则创建一个新的Range并将其添加到列表中.

最后,您将整个输入序列重新整理到(可能较短)的范围列表中.有些范围的长度为1,有些范围更长,因此您在rangeList中获得了一些压缩序列.

—SA



The idea is simple: you support currentRange as the last range in rangeList and add new range every time you detect non-consecutive value in input sequence. For detection of consecutive value remember previous value. If the value is consecutive, increment the currentRange, otherwise create a new Range and add it to the list.

At the end, you will re-work the whole input sequence into the (potentially shorter) list of ranges. Some ranges will be of length 1, some are longer, hence you achieved some compacted sequence in the rangeList.

—SA


这篇关于从数字列表创建范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆