Skip to content

Instantly share code, notes, and snippets.

@Otumian-empire
Created January 1, 2020 15:29
Show Gist options
  • Save Otumian-empire/a8fc091935f236498416221a5e5ca06c to your computer and use it in GitHub Desktop.
Save Otumian-empire/a8fc091935f236498416221a5e5ca06c to your computer and use it in GitHub Desktop.
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n. Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting…
# Problem definition and analysis
# Determine the maximum value obtainable by cutting
# up a rod and selling the pieces
# given given that the rod is of size, n
# and an array with the prices of pieces of the rod
# less than n
# For example, if length of the rod is 8 and the values of
# different pieces are given as following, then the maximum
# obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
# length | 1 2 3 4 5 6 7 8
# --------------------------------------------
# price | 1 5 8 9 10 17 17 20
# my solution
# I assume that the smallest rod is of size 1
# also that the max rod size can be n
# I assume that the size of the rod is basically the size of the array
# *- my algo
# 1- find the individual pieces that sum up to n
# 2- sum the prices
# 3- return the max from the prices
# find the individual pieces that sum up to n
# def get_num_combo(size_of_rod = 8):
# a = 0
# b = size_of_rod
# sum_combos = []
# while a <= b and b >= 0:
# if a + b == size_of_rod:
# # sort a and b and remove duplicates
# sorted_result = sorted((a, b))
# if sorted_result not in sum_combos:
# sum_combos.append(sorted_result)
# a += 1
# b -= 1
# else:
# return sum_combos
def get_num_combo(size_of_rod = 8):
pass
print(get_num_combo())
# sum the prices
def get_sum_of_combo_prices(combo_obj, array_prices):
# print(combo_obj)
# print(array_prices)
# sum_of_combo = []
# for combo in combo_obj:
# combo_1, combo_2 = combo
# # if either combo_1 or combo_2 is 0 or size of the list
# # then I consider only the last element
# list_size = len(array_prices)
# if combo_1 == 0 or combo_2 == list_size:
# sum_of_combo.append(array_prices[list_size - 1])
# else:
# sum_of_combo.append(sum((array_prices[combo_1 - 1], array_prices[combo_2 - 1])))
# return sum_of_combo
pass
print(get_sum_of_combo_prices(get_num_combo(), [3, 5, 8, 9, 10, 17, 17, 20]))
# return the max from the prices
def max_val_obtainable(sum_obj_list):
return max(sum_obj_list)
# this function pulls all the functions together
def main(prices):
rod_piece_prices = prices
rod_size = len(rod_piece_prices)
print('rod size:', rod_size)
print('prices of rods:', rod_piece_prices)
num_combo = get_num_combo(rod_size)
print('The size combination:', num_combo)
sum_combo_prices = get_sum_of_combo_prices(num_combo, rod_piece_prices)
print("The price of the combinations:", sum_combo_prices)
max_price = max_val_obtainable(sum_combo_prices)
print("The max price:", max_price)
# main([1, 5, 8, 9, 10, 17, 17, 20])
# main([3, 5, 8, 9, 10, 17, 17, 20])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment