# -*- coding: utf-8 -*- """ Created on Tue Sep 7 15:17:31 2021 @author: Marcus Galinski """ # 1 Stone which can be cut into 4 # Total weight is 40 kg # weight can go from 1 to 40 kg # To calculate the weight you have to sides which needs to be equal # i + j + k + l = 40 import time # Returns all Possible solutions how a 40kg rock can be split into 4 pieces and can # measure the weight of an object up to 40kg def getWeigths(): solution = [] combinations = [] # Assume 1 Piece is always 1kg -> reduce the code about 1 loop and increases # execution speed i = 1 # Since max weight of all Stones is 40 kg and we can have up to 4 Stones, # we have to consider this in the loops # 39kg - 1kg of first Piece, second Piece can be up to 37kg, since the loops # stop if the variable equals the comparation number, in this case 38 - 1 is the last # Number checked vor the second piece for j in range(1, 39 - i): # For the third piece its the same as for the second piece only that we # must consider the previous stone pieces for k in range(1, 39 - i - j): # fourth pieces is a subtraction of all previous pieces, with this calculation # we avoid an additional loop l = 40 - i - j - k # if somehow the combination of all 4 pieces is unequal 40 kg throw an error if(i + j + k + l != 40): print("error", i, j, k, l) exit(); else: # Assume the combination is a solution and begin the # iteration from 1kg to 40kg and check if one of the following # situations reach an equal if none is true set the solution to false and break possible_solution = True for weigth in range(1, 41): # Combinations which can be used to equal both sides of the scales # The number of combinations increases with the amount of possible pieces if(i + j + k + l == weigth): continue if(i + j + k == weigth): continue if(i + j == weigth): continue if(i + k == weigth): continue if(i == weigth): continue if(i + j + l == weigth): continue if(i + l == weigth): continue if(l == weigth): continue if(i + k + l == weigth): continue if(k + l == weigth): continue if(k == weigth): continue if(j + k + l == weigth): continue if(j + l == weigth): continue if(j + k == weigth): continue if(j == weigth): continue if(weigth + i == j): continue if(weigth + k == j): continue if(weigth + l == j): continue if(weigth + i + k == j): continue if(weigth + i + l == j): continue if(weigth + l + k == j): continue if(weigth + i + k + l == j): continue if(weigth + i == k): continue if(weigth + j == k): continue if(weigth + l == k): continue if(weigth + i + j == k): continue if(weigth + i + l == k): continue if(weigth + j + l == k): continue if(weigth + i + j + l == k): continue if(weigth + i == l): continue if(weigth + j == l): continue if(weigth + k == l): continue if(weigth + i + j == l): continue if(weigth + i + k == l): continue if(weigth + j + k == l): continue if(weigth + i + j + k == l): continue if(weigth + k == i + j): continue if(weigth + l == i + j): continue if(weigth + k + l == i + j): continue if(weigth + j == i + k): continue if(weigth + l == i + k): continue if(weigth + j + l == i + k): continue if(weigth + j == i + l): continue if(weigth + k == i + l): continue if(weigth + j + k == i + l): continue if(weigth + i == j + k): continue if(weigth + l == j + k): continue if(weigth + i + l == j + k): continue if(weigth + i == j + l): continue if(weigth + k == j + l): continue if(weigth + i + k == j + l): continue if(weigth + i == k + l): continue if(weigth + j == k + l): continue if(weigth + i + j == k + l): continue if(weigth + i == j + k + l): continue if(weigth + j == i + k + l): continue if(weigth + k == i + j + l): continue if(weigth + l == i + j + k): continue possible_solution = False break tmpSol = [i, j, k, l] tmpSol.sort() #print(tmpSol) if(possible_solution and tmpSol not in solution): solution.append(tmpSol) return solution startTime = time.time_ns() solution = getWeigths() stopTime = time.time_ns() print("finished") print("solution:", solution) print("duration:", str((stopTime - startTime) / 1000 / 1000)[0:6] + "ms")
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question