我正在努力完成编码挑战,以提高我的编程技能,或者说是缺乏编程技能。挑战的细节在这里。
房间里有N根绳子和N个重物每根绳子都连接到
只有一个重量(只有一端),每根绳子都有一个
耐久性:它可以悬挂的最大重量。还有一个
挂钩,挂在天花板上。绳子可以系在钩子上
在没有重量的情况下绑住末端。绳子也可以系在
其他重物;也就是说,绳子和重物可以连在一起
另一个在链子里。如果重量总和相连,绳子就会断
对它来说,直接或间接的,都大于它的耐久性。
我们知道我们要系N根绳子的顺序更准确地说,
我们知道绳子的参数(耐久性和重量)和
每个附件的位置。持续时间、重量和位置
在三个长度为n的零索引数组a、b、c中给出,每个i(0
连接到第i根绳子,c[i](这样c[i]如果C[I]等于-1
钩子,否则我们会把重物系在C[I]-第根绳子上。
我们的目标是找到可以连接的绳索的最大数量。
按规定的顺序,不折断任何绳索。写一个
函数:def solution(a,b,c),给定三个零索引数组
N整数的A,B,C,返回最大的绳索数。
以给定的顺序附加的。例如,给定以下数组:
A= [4,3,1]
B = [2,2,1]
C = [-1,0,1]
函数应该返回2,就好像我们附加了第三根绳子,那么一根绳子就会断,因为重量之和大于
耐久性(2+2+1=5且5>4)。
下面是我尝试的解决方案。我有一个名为add_weights的助手函数,如果添加最新的rope不会导致任何其他rope断开,则返回true,否则返回false。
def add_weights(A, ancestors, weights, weight, rope):
#add weight(int) to rope and ancestors
if (rope == -1):
return (True)
else:
weights[rope] += weight
if (A[rope] < weights[rope]):
print "Rope that breaks", rope
return False
ancestor = ancestors[rope]
print ancestor
add_weights(A, ancestors, weights, weight, ancestor)
def solution(A, B, C):
# write your code in Python 2.7
weights = {}
ancestors = {}
for i in range(len(B)):
weights[i] = B[i]
for i in range(len(C)):
#attaching rope i to rope x
x = C[i]
ancestors[i] = x
broke = add_weights(A, ancestors, weights, B[i], x)
if (not broke):
return i
return len(C)
问题是在函数解决方案中for循环的第二次迭代期间(当我尝试add rope 1时),变量break以某种方式计算为none,这时我可以清楚地看到add_weights返回true。我也用调试器测试过它,所以我不完全确定发生了什么。欢迎任何帮助。
最佳答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Exercise5_DurablityofRopes
{
class Program
{
static int[] A_Durability = new int[] { 15, 6, 2,3,1 };
static int[] B_Weight = new int[] { 2, 1, 2,3,1 };
static int[] C_Position = new int[] { -1, 0, 1 ,2,3};
static int no_of_ropes_attached = 0;
static int maxropeattached = 0;
static void Main(string[] args)
{
// first position cannot necessarily be -1 hence Checking for each position How many maximum ropes can be attached
for (int i = 0; i <= C_Position.Length - 1; i++)
{
int[] Copy_Durability = new int[A_Durability.Length];
for (int l = 0; l <= C_Position.Length - 1; l++)
{
Copy_Durability[l] = A_Durability[l];
}
AddNextRope(i, B_Weight[i], Copy_Durability);
Console.WriteLine("Total Number of ropes can be attached to " + C_Position[i] + " ropes are" + no_of_ropes_attached);
if (no_of_ropes_attached>=maxropeattached)
{
maxropeattached = no_of_ropes_attached;
}
no_of_ropes_attached = 0;
}
Console.WriteLine("Total Number of ropes can be attached is " + maxropeattached);
Console.ReadKey();
}
private static void AddNextRope(int currentRopePosition,int newWeight,int[] Durability)
{
if (currentRopePosition == C_Position.Length - 1) return;
// decrease same amount of weight from all ansestors from their durability and check if any of them breaks (durability <0) with new weight added
for (int k = currentRopePosition; k != 0; k--)
{
Durability[k] = Durability[k] - newWeight;
if(Durability[k]<0)
{
return;
}
}
no_of_ropes_attached = no_of_ropes_attached + 1;
for (int i = 0; i <= C_Position.Length - 1; i++)
{
if (C_Position[i] == C_Position[currentRopePosition] + 1)
{
if (A_Durability[i] > B_Weight[i])
{
AddNextRope(i, B_Weight[i], Durability);
}
else
{
return;
}
}
}
}
}
}