Skip to content

Instantly share code, notes, and snippets.

@mkeyno
Last active November 11, 2021 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkeyno/f8546421df69ea5af4ea29bd24bdb080 to your computer and use it in GitHub Desktop.
Save mkeyno/f8546421df69ea5af4ea29bd24bdb080 to your computer and use it in GitHub Desktop.
# name, id , w, v
sample_string="(ba,100,1),(eng,320,2),(di,199,3),(kos,ss,1.2),(ri,188,.6),(fork,23,.8),(pis,210,11),(ert,137,.9),\
(omaa,186,1.8),(babel,156,1),(eng2,320,2),(diy,164.5,.48),(kos,196,1.2),(alfe,193,.6),(me,123,.8),(you,210,11),\
(thy,137,.9),(jer,192.7,1.8),(ba2,100,1),(eng2,320,2),(di2,199,3),(kos2,155,1.2),(ri2,188,.6),(fork2,23,.8),\
(pis2,210,11),(ert2,137,.9),(omaa2,186,1.8),(babel2,156,1),(eng21,320,2),(diy3,164.5,.48),(kos3,196,1.2),\
(alfe3,193,.6),(me3,123,.8),(you3,210,11),(thy3,137,.9),(jer3,192.7,1.8)"
#Max single cargo item weight 200kg
max_i_w=200
#Max single cargo item size is 2m^3
max_i_v=2
#Max cargo trolley weight is 2000kg
max_T_w=2000
def check_number(s):
"""
Check whether we get the true number
"""
try:
return int(s),True
except ValueError:
try:
return float(s),True
except ValueError:
#raise ValueError('argument is not a string of number')
return -1,False
def acceptable_item(name, w,v):
"""
is Item in the acceptable range
"""
if w<= max_i_w and v<= max_i_v :
print(f' item {name } \t\taccepted')
return True
print(f' item {name } \tnot\taccepted')
return False
#print(i_string)
def check_format(raw_s):
"""
only include the item with following format
(string,number,number)
"""
ps_list=raw_s.split('(')
corect_list=[]
for i in ps_list:
loc=i.find(')')
if loc >=0 :
i=i[:loc]
li=i.split(',')
if len(li) == 3:
ID=li[0]
W,rezW=check_number(li[1])
V,rezV=check_number(li[2])
if rezW and rezV :
if acceptable_item(ID,W,V):
corect_list.append((ID,W,V))
return corect_list
class Trolley ():
def __init__(self):
self.available_weight=0
self.total_weight=0
self.total_volum=0
self.list_of_items=[]
def __str__(self):
s= f'=========\nTotal wieght={self.total_weight}\nTotal Volume={self.total_volum}\nList={ self.list_of_items}\n========='
return s
def dispense(input_string):
# extract all acceptable items from input string
all_items=check_format(input_string)
#create Trolley object with list of Trolleies
p=Trolley()
list_of_packs=[p]
current_pack_num=0
current_pack=list_of_packs[current_pack_num]
avilabale_weight=0
# start to put the item in the Trolley
for item in all_items:
print(item)
avilabale_weight = max_T_w-current_pack.available_weight
print(f'avilabale_weight={avilabale_weight}')
if item[1] <=avilabale_weight:
current_pack.available_weight += item[1]
current_pack.total_volum += item[2]
current_pack.list_of_items.append(item[0])
print(f'item {item[0]} add to package {current_pack_num}')
else :
print(list_of_packs[current_pack_num])
print("create new Trolley")
p=Trolley()
list_of_packs.append(p)
current_pack_num+=1
current_pack=list_of_packs[current_pack_num]
current_pack.available_weight += item[1]
current_pack.total_volum += item[2]
current_pack.list_of_items.append(item[0])
print(f'item {item[0]} add to package {current_pack_num}')
#print last Trolley
print(list_of_packs[current_pack_num])
def main():
while True:
try:
x = input()
if x == "" :
print("dispense sample string")
dispense(sample_string)
break
else:
#lines.append(x );
dispense(x)
except EOFError as e:
print(e)
if __name__ == '__main__':
main()
import unittest
import sorting
test_string="(ba,100,1),(eng,320,2),(di,199,3),(kos,ss,1.2),(ri,188,.6),(fork,23,.8),(pis,210,11),(ert,137,.9)"
class TestSort(unittest.TestCase):
def test_check_number(self):
rez, _ =sorting.check_number("5.5");self.assertEqual(rez,5.5)
rez, _ =sorting.check_number("-10");self.assertEqual(rez,-10)
rez, _ =sorting.check_number("text");self.assertEqual(rez,-1)
def test_acceptable_item(self):
self.assertTrue(sorting.acceptable_item("ball",100,1.8))
def test_check_format(self):
obj=sorting.check_format(test_string)
self.assertIsInstance(obj, list)
def setUp(self):
print("Testing..")
def tearDown(self):
print(". End of Test")
if __name__ == '__main__':
unittest.main(exit=False)
@mkeyno
Copy link
Author

mkeyno commented Nov 11, 2021

Considering each item information comprise the weight, volume, and ID or name, which is come as a string with this format
(string,number,number)
the input string can capture by keyboard or shell command such as
python < input.txt sorting.py
in windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment