找出与原始数据中没有的数据的算法
2005-09-04 00:01
274 查看
用C# 呵NUnit 做开发呵测试工具
using System;
using System.Collections;
using NUnit.Framework;
namespace cn.lovetyping.UnitTest
{
/// <summary>
/// Sort 的摘要说明。
/// </summary>
///
[TestFixture]
public class Sort
{
/// <summary>
/// the orignal data which is used to compare with the new
/// data.If there is some data exist in the newData but not in orignal data.
/// add it to the result.
/// </summary>
/// Input:
/// <param name="orignal"></param>
/// <param name="newData"></param>
/// Output
/// <returns>arrayList</returns>
public ArrayList sortData(string[] orignal,string[] newData)
{
ArrayList result = new ArrayList();
int oIndex=0,nIndex=0;
//according to the condition
while(oIndex<orignal.Length && nIndex<newData.Length)
{
if(newData[nIndex].CompareTo(orignal[oIndex]) <0)
{
result.Add(newData[nIndex]);
nIndex++;
}
else if(newData[nIndex].CompareTo(orignal[oIndex]) ==0)
{
oIndex++;nIndex++;
}
else if(newData[nIndex].CompareTo(orignal[oIndex]) > 0)
{
oIndex++;
continue;
}
}
//if the
if(nIndex == newData.Length || oIndex< orignal.Length)
{
return result;
}
else if( nIndex < newData.Length)
{
while(nIndex< newData.Length)
{
result.Add(newData[nIndex++]);
}
}
return result;
}
[Test]
public void testSort()
{
string[] code1 = new string[]{"0","4","6","9"};
string[] code2 = new string[]{"1","3","6","7","9","12"};
ArrayList result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 4);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("---------------Another data------------");
code1 = new string[]{"0","4","6","9"};
code2 = new string[]{"1","3","6"};
result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 2);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("---------------Another data------------");
code1 = new string[]{"0","4","6","9"};
code2 = new string[]{"1","3","7"};
result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 3);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("---------------Another data------------");
code1 = new string[]{"0","11","13","3","3","9"};
code2 = new string[]{"1","11","15","16","19","3","7"};
result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 5);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
}
}
}
using System;
using System.Collections;
using NUnit.Framework;
namespace cn.lovetyping.UnitTest
{
/// <summary>
/// Sort 的摘要说明。
/// </summary>
///
[TestFixture]
public class Sort
{
/// <summary>
/// the orignal data which is used to compare with the new
/// data.If there is some data exist in the newData but not in orignal data.
/// add it to the result.
/// </summary>
/// Input:
/// <param name="orignal"></param>
/// <param name="newData"></param>
/// Output
/// <returns>arrayList</returns>
public ArrayList sortData(string[] orignal,string[] newData)
{
ArrayList result = new ArrayList();
int oIndex=0,nIndex=0;
//according to the condition
while(oIndex<orignal.Length && nIndex<newData.Length)
{
if(newData[nIndex].CompareTo(orignal[oIndex]) <0)
{
result.Add(newData[nIndex]);
nIndex++;
}
else if(newData[nIndex].CompareTo(orignal[oIndex]) ==0)
{
oIndex++;nIndex++;
}
else if(newData[nIndex].CompareTo(orignal[oIndex]) > 0)
{
oIndex++;
continue;
}
}
//if the
if(nIndex == newData.Length || oIndex< orignal.Length)
{
return result;
}
else if( nIndex < newData.Length)
{
while(nIndex< newData.Length)
{
result.Add(newData[nIndex++]);
}
}
return result;
}
[Test]
public void testSort()
{
string[] code1 = new string[]{"0","4","6","9"};
string[] code2 = new string[]{"1","3","6","7","9","12"};
ArrayList result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 4);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("---------------Another data------------");
code1 = new string[]{"0","4","6","9"};
code2 = new string[]{"1","3","6"};
result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 2);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("---------------Another data------------");
code1 = new string[]{"0","4","6","9"};
code2 = new string[]{"1","3","7"};
result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 3);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
Console.WriteLine("---------------Another data------------");
code1 = new string[]{"0","11","13","3","3","9"};
code2 = new string[]{"1","11","15","16","19","3","7"};
result = this.sortData(code1,code2);
Assert.IsTrue(result.Count == 5);
for(int i=0;i<result.Count;i++)
{
Console.WriteLine(result[i]);
}
}
}
}
相关文章推荐
- 设定二维整数数组B[0..m-1,0..n-1]的数据在行,列方向上都按从小到大的顺序排序,且整形变量x中的数据在B中存在。设计一个算法,找出一对满足B[i][j]=x的I,j值,要求比较次数不超过m
- 算法系列-大数据面试题-两个大文件中找出共同记录
- 解决android greenDAO没有默认值default value和没有原始数据类型的问题
- 找出数据集合中的最小值和最大值的两种算法比较
- 数据结构——算法之(001)(在一个数组中找出重复数值)
- 问题描述如下: 有2.5亿个整数(这2.5亿个整数存储在一个数组里面,至于数组是放在外存还是内存,没有进一步具体说明); 要求找出这2.5亿个数字里面,不重复的数字的个数; 另外,可用的内存限定为600M; 要求算法尽量高效,最优;
- 解决android greenDAO没有默认值default value和没有原始数据类型的问题
- 大数据DDos检测——DDos攻击本质上是时间序列数据,t+1时刻的数据特点和t时刻强相关,因此用HMM或者CRF来做检测是必然! 和一个句子的分词算法CRF没有区别!
- 算法(根据提供的某数值,找出与其最接近的一组数据
- 算法(根据提供的某数值,找出与其最接近的一组数据)
- 算法(根据提供的某数值,找出与其最接近的一组数据)
- 找出文件1中有而文件2中没有的数据
- 数据结构与算法学习之路:迷宫问题——回溯思想找出所有路径
- 算法 2.3节 有重复数据和没有的快速排序
- mysql 一组数组中,找出数据里没有的值。
- 查找算法(3)、经典算法(8):从N个乱序数据中找出第K小的数
- 算法(根据提供的某数值,找出与其最接近的一组数据)
- Python 数据结构与算法——从某个列表中找出两个彼此最接近但不相等的数
- 数据结构第5章例题 若矩阵Am×n中存在某个元素aij满足:aij是第i行中的最小值且是第j列中的最大值,则称该元素为矩阵A的一个鞍点。试编写一个算法,找出A中的所有鞍点。
- 算法系列-大数据面试题-在超大文件中找出访问百度次数最多的IP