2

我正在尝试解决这个 CodingBat 问题:

我们想做一包目标公斤的巧克力。我们有小条(每条 1 公斤)和大条(每条 5 公斤)。返回要使用的小柱的数量,假设我们总是在小柱之前使用大柱。如果无法完成,则返回 -1。

我理解问题的逻辑,但每当我尝试运行它时,都会出现超时异常。那么谁能告诉我我做错了什么?

def make_chocolate(small, big, goal):

  total = 0

  if goal < 5:
    big = 0
  for i in xrange(big):
    total += 5
    if total == goal:
      return 0
    elif total+5>goal:
      break
  for k in xrange(small):
    total +=1
    if total == goal:
      return (k+1)

  return -1
4

14 回答 14

3

失败是由执行此测试所需的时间引起的:

makeChocolate(1000, 1000000, 5000006)

即使其他测试可以通过,一旦一个测试超时,报告会将所有测试显示为超时。要看到这是真的,请更改xrange(big)xrange(big if big < 101 else 0),您将看到除上述测试之外的所有测试通过。

出于性能原因,基于 Web 的评估器需要像这样的超时。一定是 Python 超时允许的循环少于 Java 超时。

这是通过的非循环解决方案:

def make_chocolate(small, big, goal):
    big *= 5
    if big + small < goal or small < goal%5:
        return -1
    small = goal - big
    return small%5 if small < 0 else small 

Java 需要一个稍微不同的解决方案,因为它如何处理负数的模数。

public int makeChocolate(int small, int big, int goal) {
    big *= 5;
    if (big + small < goal || small < goal%5)
        return -1;
    small = goal - big;
    return small < 0 ? (big+small)%5 : small;
}
于 2013-12-20T01:21:36.473 回答
0

你可以简单地这样做:

def make_chocolate(small, big, goal):
    noOfBigs = big if(5 * big <= goal) else goal / 5
    return  goal - (noOfBigs * 5)  if small >= (goal - (noOfBigs * 5)) else -1
于 2014-08-28T05:46:37.530 回答
0

如果您首先将 Big value 减少到您实际使用的数字,这似乎是更清晰的逻辑:

def make_chocolate(small, big, goal):
  while big * 5 > goal:
    big -= 1
  if (goal - (big * 5)) <= small:
    return goal - (big * 5)
  else:
    return -1
于 2014-12-03T03:42:20.103 回答
0

This is clunky, but it works:

def make_chocolate(small, big, goal):
    bigbars=goal//5
    if bigbars<=big:
        smallgoal=goal-(bigbars*5)
        if smallgoal>=0 and smallgoal<=small:
            return smallgoal
        if smallgoal>small:
            return -1
    if bigbars>big:
        smallgoal=goal-(big*5)
        if smallgoal<=small:
            return smallgoal
        if smallgoal>small:
            return -1
于 2014-03-12T16:49:11.730 回答
0
def make_chocolate(small, big, goal):  
    big = big*5  
    if (goal >= big) and (small >= goal - big):  
        return goal - big  
    if (goal < big) and (small >= goal % 5):  
        return goal % 5  
    return -1  
于 2014-10-15T15:52:35.090 回答
0
def make_chocolate(small,big,goal):
    if goal > small + big * 5:
        return -1
    else:
        if goal % 5 <= small:
            if big * 5 <=goal:
                return goal - (big * 5)
            else:
                return goal % 5
        else:
            return -1
于 2019-02-16T15:04:50.250 回答
0
def make_chocolate(small, big, goal):
  i = goal // 5
  b_big = big*5
  x = i*5
  y = goal - x

  if big == 0:
    if small >= goal:
      return small - (goal - small)
    else:
      return -1
  elif b_big == goal:
    return 0
  elif big >= i:
    if x + small < goal:
      return -1 
    else:
      return (goal - (x))
  else:
    if b_big + small < goal:
      return -1
    else:
      return(goal - b_big)
于 2018-12-24T12:19:39.000 回答
0
def make_chocolate(small, big, goal):
  if(goal>=big*5) and(small+big*5>=goal) and (goal%5 <=small):
     rem_cho=abs(goal-big*5)
     return(rem_cho)
  elif(goal%5>small):
     return(-1)
  elif(goal<big*5):
     return(goal%5)
  else:
     return(-1)
于 2022-02-23T02:03:11.680 回答
0

以下代码适用于所有测试数据。对不起,由于它是自我解释的,我没有提供任何解释。

def make_chocolate(small, big, goal):
    s = small*1
    b = big*5
    mod = goal%5
    if b < goal:
        if s+b == goal:
            return s
        elif s+b > goal:
            return goal-b
        elif s+b < goal:
            return -1
    elif b > goal:
        if s < mod:
            return -1
        return mod
    elif b == goal:
        return 0
于 2017-03-07T07:41:23.383 回答
0
def make_chocolate(small, big, goal):
    can_do = small + 5*big
    if can_do<goal:
        return -1
    elif can_do==goal:
        return small
    else:
        b_max = goal/5
        b_max = b_max if b_max<=big else big
        s_req = goal - (b_max)*5
        if s_req <= small:
            return s_req
        else:
            return -1
于 2020-06-06T07:18:11.577 回答
0
  def make_chocolate(small, big, goal):
  s_cnt = 0;
  t = goal;
  for i in range(**550**):
    if(goal >= 5 and big > 0):
        goal = goal-5;
        big = big-1;
    elif(goal > 0 and small > 0):
        goal = goal-1;
        small = small-1;
        s_cnt = s_cnt+1;

  if (goal == 0):
    return s_cnt;
  else:
    return -1;

// 在大写字母范围(550)中,您可以看到我已经对其进行了硬编码,因为对于较大的值,它将使循环运行时间过长,因此您会收到超时错误。

于 2018-08-09T10:00:55.533 回答
0
def make_chocolate(small, big, goal):
    if goal - 5 * big == 0:
        return 0
    elif (goal - 5 * big) < 0:
        return -1 if (goal % 5) > small else goal % 5
    elif (goal - 5 * big) > small:
        return -1
    else:
        return (goal - 5 * big)
于 2020-05-28T18:17:19.613 回答
-1
def make_chocolate(small, big, goal):        
  while big*5>goal:
    if goal%5==0:
      return 0
    else:
      if goal%5<=small:
        return goal%5
      else:
        return -1
    else:
      if goal-(big*5)<=small:
        return goal-(big*5)
      else:
        return -1   
于 2015-07-27T06:52:28.117 回答
-1
def permutations(amount, size, threshold):


if amount == 1:
      return size
  combinations = []
  for i in range(1, amount + 1):
    combinations.append(size * i)
  if combinations[-1] == threshold:
      return combinations[-1]
  elif combinations[-1] > threshold:
      if size == 1:
        return combinations[-1] - threshold
      elif size == 5:
        return combinations[-2]
  else:
      return combinations

def make_chocolate(small_amount, big_amount, goal):
    "Adjusts values for small and big chocolate bars until we find out whether there is a solution"
    if big_amount == 0 and small_amount == goal:
        return goal
    elif big_amount == 0 and small_amount != goal:
        return -1
    else:
        big_size = 5
        small_size = 1
        big_sums_list = permutations(big_amount, big_size, goal)
        big_sum = big_sums_list
        if type(big_sum) == list: #If none of the sums reached our goal
            new_threshold_value = goal - big_sums_list[-1] # We adjust the new threshold value for the permutations function
            small_sums_list = permutations(small_amount, small_size, goal) #Get all possible sums with our given small values
            small_sum = small_sums_list # If the sum of our small values has indeed equaled or exceeded our goal
            if type(small_sums_list) == list:  # If the sum of our small values has not equaled nor exceeded our goal
                small_sums_list = permutations(small_amount, small_size, new_threshold_value)
                small_sum = small_sums_list
                if type(small_sums_list) == list: # If the sum of small bars has not exceeded the threshold value
                    return -1
                elif small_sum == new_threshold_value:
                    return small_sum # Whichever value we choose will be correct
                elif small_sum > new_threshold_value:
                    return new_threshold_value # It is the number of small bars we have left before our goal that matters
            elif small_sum == goal:
                return small_sum # If the sum has equaled our goal, it is indiferent whether we choose one or the other variable
            elif small_sum > goal: # If the sum has exceeded our goal
                return goal #Since the size is one, we can return goal as the result
        elif big_sum >= goal:
            return make_chocolate(small_amount, big_amount - 1, goal) # We adjust the amounts by - 1, to check for different combinations
        elif big_sum == 5:
            new_threshold_value = goal - big_sum # We adjust the new threshold value for the permutations function
            small_sums_list = permutations(small_amount, small_size, new_threshold_value) #Get all possible sums with our given small values
            small_sum = small_sums_list # If the sum of our small values has indeed equaled or exceeded our goal
            if type(small_sum) == list:  # If the sum of our small values has not equaled nor exceeded our goal
                return -1
            elif small_sum == new_threshold_value:
                return small_sum # If the sum has equaled our goal, it is indiferent whether we choose one or the other variable
            elif small_sum > new_threshold_value: # If the sum has exceeded our goal
                return goal #Since the size is one, we can return goal as the result

print(make_chocolate(4, 1, 10))
于 2017-01-27T14:46:29.963 回答