- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我的意思是:
给定输入的数字集:
1,2,3,4,5 变成“1-5”。
1,2,3,5,7,9,10,11,12,14 变成“1-3, 5, 7, 9-12, 14”
这是我想出的最好的:[C#]
我觉得这有点草率,所以问题是,是否有更易读和/或更优雅的解决方案?
public static string[] FormatInts(int[] ints)
{
if (ints == null)
throw new ArgumentNullException("ints"); // hey what are you doing?
if (ints.Length == 0)
return new string[] { "" }; // nothing to process
if (ints.Length == 1)
return new string[] { ints[0].ToString() }; // nothing to process
Array.Sort<int>(ints); // need to sort these lil' babies
List<string> values = new List<string>();
int lastNumber = ints[0]; // start with the first number
int firstNumber = ints[0]; // same as above
for (int i = 1; i < ints.Length; i++)
{
int current = ints[i];
int difference = (lastNumber - current ); // compute difference between last number and current number
if (difference == -1) // the numbers are adjacent
{
if (firstNumber == 0) // this is the first of the adjacent numbers
{
firstNumber = lastNumber;
}
else // we're somehow in the middle or at the end of the adjacent number set
{
lastNumber = current;
continue;
}
}
else
{
if (firstNumber > 0 && firstNumber != lastNumber) // get ready to print a set of numbers
{
values.Add(string.Format("{0}-{1}", firstNumber, lastNumber));
firstNumber = 0; // reset
}
else // print a single value
{
values.Add(string.Format("{0}", lastNumber));
}
}
lastNumber = current;
}
if (firstNumber > 0) // if theres anything left, print it out
{
values.Add(string.Format("{0}-{1}", firstNumber, lastNumber));
}
return values.ToArray();
}
最佳答案
我已经像这样重写了你的代码:
public static string[] FormatInts(int[] ints)
{
Array.Sort<int>(ints);
List<string> values = new List<string>();
for (int i = 0; i < ints.Length; i++)
{
int groupStart = ints[i];
int groupEnd = groupStart;
while (i < ints.Length - 1 && ints[i] - ints[i + 1] == -1)
{
groupEnd = ints[i + 1];
i++;
}
values.Add(string.Format(groupEnd == groupStart ? "{0}":"{0} - {1}", groupStart, groupEnd));
}
return values.ToArray();
}
然后:
/////////////////
int[] myInts = { 1,2,3,5,7,9,10,11,12,14 };
string[] result = FormatInts(myInts); // now result haves "1-3", "5", "7", "9-12", "14"
关于algorithm - 相邻数算法 grouper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/245735/
我的意思是: 给定输入的数字集: 1,2,3,4,5 变成“1-5”。 1,2,3,5,7,9,10,11,12,14 变成“1-3, 5, 7, 9-12, 14” 这是我想出的最好的:[C#] 我
考虑一个具有 MultiIndex 的系列,该系列在级别 0 上提供自然分组值,在级别 1 上提供时间序列: s = pd.Series(range(12), index=pd.MultiIndex.
从这里开始: https://docs.python.org/3/library/itertools.html#itertools-recipes def grouper(iterable, n, f
我有每月的时间序列数据,这些数据既缺少一些条目,又由于其他原因分散了 NaN 值。我需要将数据汇总到季度和年度系列中,但我不想报告缺少数据的季度/年度数据。例如,在下面的数据中,我不想报告 2014
难道 pandas.Grouper 只被认为是用于日期?或者它也可以用于整数吗? 我想将 pandas.Grouper 与 pandas.pivot_table 结合使用。以下是有关如何将 panda
我有一个像这样的示例数据框: import pandas as pd df = pd.DataFrame({"id": [0]*5 + [1]*5, "time": ['20
我有一个时间序列,我正在将其重新采样为 5s 窗口,如下所示: INDEX size price 2018-05-07 21:53:13.731
我有一个数据集,我想按列和数据集中每个月的数据进行分组。我使用 pd.Grouper() 作为每月分组日期部分。 df.groupby(['A',pd.Grouper(key='date', freq
我正在尝试将 x 天的组放在另一列的组中。出于某种原因,当我添加另一级别的分组时,分组行为发生了变化。 参见下面的玩具示例: 创建一个包含 40 个连续日期、一个 ID 列和随机值的随机数据框: im
我搜索了 stackoverflow 以了解如何按月对 DateTime 进行分组,但出于某种原因我一直收到此错误,即使在我通过 pd.to.datetime 传递数据帧之后也是如此 TypeErro
我是Python新手。在阅读 python 标准库引用时,我对 itertools 食谱部分中的 grouper() 示例感到困惑。 我尝试将示例代码放入一个小程序中,如下所示: from itert
我正在尝试学习如何在 Python 中使用 itertools.groupby,我想找到每组字符的大小。起初我试图看看我是否可以找到单个组的长度: from itertools import grou
我有这个 Pandas 数据框 datetime machineID errorID 0 2015-01-06 03:00:00 1 error3 1
以下数据以5分钟为间隔 数据框名称为 df: 脚本 ID 约会时间 打开 高的 低的 关闭 体积 0 201 2019-02-04 14:55:00 1408.05 1408.05 1407 1408
我在我的数据集上编写了这两个 groupby 函数,第一个函数对我的数据进行分组,并将数据的日期时间分隔为开始日期时间、结束日期时间。 这是数据集: Blast HoleEast Coordinate
我有一个包含 38 列的数据框,其中一列是时间。我建立了一个bin框架空间 timeframe=['4-6','7-9','10-12','13-15','16-18','19-21','22-24'
我正在尝试获取每家医院每天的平均记录数。 |Hospital|Date|Number of Records 0|Hospital B|2018-02-12 16:07:54.183|5 1|Hospi
我有一个带有每小时时间戳的 DataFrame: 2019-01-01 0:00:00 1 2019-01-01 1:00:00 2 2019-01-1
我有这样的代码: y1 = data.groupby(['name', pd.Grouper(key='datetime', freq='15d')])['ext price'].mean() y=p
你好,我正在使用 Python 的 itertools 中的 Grouper 函数来削减大块的 select where in(idlist) 查询以提高 sqlite 性能。问题是石斑鱼填满了 ch
我是一名优秀的程序员,十分优秀!