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)

他のテストは合格できますが、1つのテストがタイムアウトすると、レポートにはすべてのテストがタイムアウトとして表示されます。これが正しいことを確認するには、に変更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):
    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):
    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
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

最初にBig値を実際に使用する数に減らすと、よりクリーンなロジックのように見えます。

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

以下のコードは、すべてのテストデータに対して完全に機能します。申し訳ありませんが、自明なので説明はしませんでした。

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):
  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):
  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 > 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):
    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 に答える
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):
  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 に答える
-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 に答える