Algorithm 通过交替元素混合两个阵列(拉链式)
在两个数组(可能大小不同)中混合元素,以便从每个数组交替绘制项目,并将剩余添加到末尾的优雅算法是什么 例如 数组1-A、B、C、D、E、F、G 数组2-1,2,3,4 混合数组-A、1、B、2、C、3、D、4、E、F、G 我更喜欢C#中的解决方案,但我应该能够阅读和转换任何语言(甚至某种形式的伪代码)中的解决方案Algorithm 通过交替元素混合两个阵列(拉链式),algorithm,Algorithm,在两个数组(可能大小不同)中混合元素,以便从每个数组交替绘制项目,并将剩余添加到末尾的优雅算法是什么 例如 数组1-A、B、C、D、E、F、G 数组2-1,2,3,4 混合数组-A、1、B、2、C、3、D、4、E、F、G 我更喜欢C#中的解决方案,但我应该能够阅读和转换任何语言(甚至某种形式的伪代码)中的解决方案 不要担心空值检查或任何其他边缘情况,我会处理这些问题。你是说类似的问题吗 // naive/boring approach int i = 0; int m = 0; while (i
不要担心空值检查或任何其他边缘情况,我会处理这些问题。你是说类似的问题吗
// naive/boring approach
int i = 0;
int m = 0;
while (i < a1.size() || i < a2.size()) {
if (i < a1.size())
mixed[m++] = a1[i];
if (i < a2.size())
mixed[m++] = a2[i];
i++;
}
//天真/无聊的方法
int i=0;
int m=0;
而(i
如果使用此方法,您可能希望将数组长度存储在变量中,这样就不必一直调用size()方法(或使用任何语言的任何方法)。我看到了一个O(N),N=较大集合的大小的算法,因为您必须迭代所有条目 中有一个函数正是这样做的,它与n个数组一起工作 我想出了另一个方法,如果短数组更快用完,它将在短数组中不断重复:
from itertools import *
def repeatrobin(*iterables):
cycles = cycle(map(cycle, iterables))
stop = iter(xrange(max(map(len, iterables)) * len(iterables) - 1))
for c in cycles:
yield c.next()
stop.next()
>>> list(repeatrobin(('A', 'B', 'C', 'D', 'E', 'F', 'G'), (1, 2, 3, 4)))
['A', 1, 'B', 2, 'C', 3, 'D', 4, 'E', 1, 'F', 2, 'G', 3]
>>> list(repeatrobin(('A', 'B', 'C', 'D', 'E'), (1, 2, 3), ('*',)))
['A', 1, '*', 'B', 2, '*', 'C', 3, '*', 'D', 1, '*', 'E', 2, '*']
我只是涉猎了C#,当我正在学习IEnumerable时,我想我应该尝试用迭代器来解决这个问题
TwoListMerge将两个列表作为参数。虽然两个列表中都有一些值要处理,但它会在每个列表之间交替返回一个值。当一个或另一个列表耗尽时,迭代器不会交替,有效地完成剩余列表的值
public static IEnumerable TwoListMerger( List<object> List1, List<object> List2 )
{
// Intialise two indices for the two lists
int ListIndex1 = 0;
int ListIndex2 = 0;
// Begin zipper - List1 will provide the first value, then List2, etc.
bool YieldFromList1 = true;
// While values in either list remain...
while ( ( ListIndex1 < List1.Count ) ||
( ListIndex2 < List2.Count ) )
{
// If next value comes from List1...
if ( YieldFromList1 )
{
// Yield from List1 if List2 exhausted( otherwise from List2 )
YieldFromList1 = ( ListIndex2 >= List2.Count );
yield return List1[ ListIndex1++ ];
}
// Next value comes from List2...
else
{
// Yield from List1 if List1 not exhausted (otherwise from List2)
YieldFromList1 = ( ListIndex1 < List1.Count );
yield return List2[ ListIndex2++ ];
}
}
// End iterator
yield break;
}
// Example usage (List1 and List2 are lists of integers)
List<object> MergedList = new List<object>( );
foreach ( object o in TwoListMerger( List1, List2 ) )
{
MergedList.Add( o );
}
foreach ( object o in MergedList )
{
Console.Write( "{0} ", o.ToString() );
}
Console.WriteLine( "}" );
公共静态IEnumerable TwoListMerge(列表1、列表2)
{
//为两个列表初始化两个索引
int ListIndex1=0;
int ListIndex2=0;
//Begin zippers-列表1将提供第一个值,然后列表2,以此类推。
bool YieldFromList1=真;
//当任一列表中的值保持不变时。。。
而((ListIndex1=List2.Count);
收益率返回列表1[ListIndex1++];
}
//下一个值来自列表2。。。
其他的
{
//如果列表1未用尽,则从列表1中获得收益(否则从列表2中获得)
YieldFromList1=(ListIndex1
我现在真的很喜欢IEnumerator
public static IEnumerable TwoListMerger( List<object> List1, List<object> List2 )
{
IEnumerator e1 = List1.GetEnumerator( );
IEnumerator e2 = List2.GetEnumerator( );
// Declare here (scope of while test)
bool b1 = true;
bool b2 = true;
// While values remain in either list
while ( b1 || b2 )
{
// NB assignments in "if"s return bool
// If we have a value remaining in List1, yield return it
if ( b1 && ( b1 = e1.MoveNext( ) ) )
yield return e1.Current;
// If we have a value remaining List2, yield return it
if ( b2 && ( b2 = e2.MoveNext( ) ) )
yield return e2.Current; }
// Done
yield break;
}
公共静态IEnumerable TwoListMerge(列表1、列表2)
{
IEnumerator e1=List1.GetEnumerator();
IEnumerator e2=List2.GetEnumerator();
//在此声明(while测试的范围)
bool b1=真;
布尔b2=真;
//而值保留在任一列表中
而(b1 | | b2)
{
//“如果”返回bool中的NB分配
//如果列表1中还有一个值,则返回它
if(b1&(b1=e1.MoveNext())
收益率返回e1.电流;
//如果我们有一个剩余值List2,则返回它
if(b2&(b2=e2.MoveNext())
返回e2.Current;}
//完成
屈服断裂;
}
PHP函数(仅适用于索引数组):
函数数组\合并\交替(&$array1,&$array2)
{
$result=array();
$count1=计数($array1);
$count2=计数($array2);
$i=0;
而($i<$count1)| |($i<$count2))
{
如果($i<$count1)
array_push($result,$array1[$i]);
如果($i<$count2)
array_push($result,$array2[$i]);
$i++;
}
返回$result;
}
感谢瑞安·格雷厄姆 哇,我会记住这一点,但在本例中我需要的远不止这些:)python的酷之处在于,在这么少的代码行中编写这样的东西是多么容易。“天真/无聊”的方法正是我需要的起点。我为我的具体实现填充了其他细节。我喜欢它。很好地使用了屈服操作。
public static IEnumerable TwoListMerger( List<object> List1, List<object> List2 )
{
IEnumerator e1 = List1.GetEnumerator( );
IEnumerator e2 = List2.GetEnumerator( );
// Declare here (scope of while test)
bool b1 = true;
bool b2 = true;
// While values remain in either list
while ( b1 || b2 )
{
// NB assignments in "if"s return bool
// If we have a value remaining in List1, yield return it
if ( b1 && ( b1 = e1.MoveNext( ) ) )
yield return e1.Current;
// If we have a value remaining List2, yield return it
if ( b2 && ( b2 = e2.MoveNext( ) ) )
yield return e2.Current; }
// Done
yield break;
}
function array_merge_alternating(&$array1, &$array2)
{
$result = array();
$count1 = count($array1);
$count2 = count($array2);
$i = 0;
while (($i < $count1) || ($i < $count2))
{
if($i < $count1)
array_push($result, $array1[$i]);
if($i < $count2)
array_push($result, $array2[$i]);
$i++;
}
return $result;
}