Posts: 1,950
Threads: 8
Joined: Jun 2018
(Apr-10-2019, 09:33 PM)Truman Wrote: but it makes a tuple of it which is not good. It seems that dictionaries can't be sorted by value.
Up to Python 3.6 sorting dictionaries has no point as dictionaries were unordered. Starting from 3.6 they became insertion ordered and from 3.7 dictionaries are guaranteed to be insertion ordered. If relying on dictionaries order one must be sure that version >3.6 is used.
Tuples are good. You just wrap into dict() and there you are:
>>> d = {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2,
... 'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9}
>>> n = dict(sorted(d.items(), key=lambda x: x[1]))
>>> n
{'Milkshake': 2, 'Lola': 2, 'Florence': 2, 'Maggie': 3, 'Moo Moo': 3, 'Millie': 5, 'Oreo': 6, 'Herman': 7, 'Betsy': 9, 'Henrietta': 9}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Apr-10-2019, 09:33 PM)Truman Wrote: How to fill the spaceship with cows if one cow should be skipped. For example, let's say that the weight of the first cow is 7, of the second is 5 and of the third is 2. It should take the first one, skip the second because limit is 10 and take the third one which gives value 9 in total.
itertools.combinations() may seem overkill for three cows  , but still this is the tool for this kind of tasks.
>>> from itertools import combinations
>>> cows = [7, 5, 2]
>>> list(combinations(cows, 2))
[(7, 5), (7, 2), (5, 2)] # all combinations of pairs
>>> max(pair for pair in combinations(cows, 2) if sum(pair) < 10)
(7, 2) # maximum from pairs which weight is less that 10
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 404
Threads: 94
Joined: Dec 2017
Apr-11-2019, 11:06 PM
(This post was last modified: Apr-11-2019, 11:06 PM by Truman.)
perfringo, this is what I get in my code
Error: {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2,
'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9}
Traceback (most recent call last):
File "C:\Python36\kodovi\6.00.2x\ps1.py", line 84, in <module>
print(greedy_cow_transport(cows, limit))
File "C:\Python36\kodovi\6.00.2x\ps1.py", line 26, in greedy_cow_transport
totalWeight += value
TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'
the problem is in this line
totalWeight += value Regarding your second comment, I like it but the problem is if three cows can stand into one spaceship. :F But it's quite possible that this thing is OOS of problem given in this course. I'll try with your solution. I'll have to figure out what to do when I have a dictionary like in my example, rather than just a list of numbers.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Apr-12-2019, 04:40 AM
(This post was last modified: Apr-12-2019, 04:40 AM by perfringo.)
It's hard to tell without code  but if I have to guess then along the road you sorted cows dictionary (row #12 in original code) and got list of tuples and then used for-loop with enumerate (row #14 where name 'key' means indice and 'value' means tuple in list) and then tried to add to said tuple:
>>> d = {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2,
... 'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9}
>>> sorted(d.items())
[('Betsy', 9), ('Florence', 2), ('Henrietta', 9), ('Herman', 7), ('Lola', 2), ('Maggie', 3), ('Milkshake', 2), ('Millie', 5), ('Moo Moo', 3), ('Oreo', 6)]
>>> for key, value in enumerate(sorted(d.items())):
... print(key, value)
...
0 ('Betsy', 9)
1 ('Florence', 2)
2 ('Henrietta', 9)
3 ('Herman', 7)
4 ('Lola', 2)
5 ('Maggie', 3)
6 ('Milkshake', 2)
7 ('Millie', 5)
8 ('Moo Moo', 3)
9 ('Oreo', 6) Regarding combinations with dictionaries - it's can be achieved quite easily. Below there is step-by-step approach (which can be condensed if required):
>>> d = {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2,
... 'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9}
>>> combs = ({j: d[j] for j in i} for i in combinations(d, 2)) # generator of dictionaries to yield all pair combinations
>>> qualifying = (row for row in combs if sum(row.values()) < 10) # generator to yield those pairs which weight is less than 10
>>> sorted(qualifying, key=lambda x: sum(x.values()), reverse=True) # consuming piped generators and getting sorted list of dictionaries
[{'Maggie': 3, 'Oreo': 6},
{'Herman': 7, 'Milkshake': 2},
{'Herman': 7, 'Lola': 2},
{'Herman': 7, 'Florence': 2},
{'Oreo': 6, 'Moo Moo': 3},
{'Maggie': 3, 'Millie': 5},
{'Oreo': 6, 'Milkshake': 2},
{'Oreo': 6, 'Lola': 2},
{'Oreo': 6, 'Florence': 2},
{'Moo Moo': 3, 'Millie': 5},
{'Milkshake': 2, 'Millie': 5},
{'Millie': 5, 'Lola': 2},
{'Millie': 5, 'Florence': 2},
{'Maggie': 3, 'Moo Moo': 3},
{'Maggie': 3, 'Milkshake': 2},
{'Maggie': 3, 'Lola': 2},
{'Maggie': 3, 'Florence': 2},
{'Moo Moo': 3, 'Milkshake': 2},
{'Moo Moo': 3, 'Lola': 2},
{'Moo Moo': 3, 'Florence': 2},
{'Milkshake': 2, 'Lola': 2},
{'Milkshake': 2, 'Florence': 2},
{'Lola': 2, 'Florence': 2}] If you look closely you can observe, that there are missing cows  in dictionary as Henriette and Betsy are too heavy to make into pair. Of course there are repetitions as well.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 404
Threads: 94
Joined: Dec 2017
'combinations' is not defined in my end.
Posts: 1,950
Threads: 8
Joined: Jun 2018
Look at my previous posts in this thread.
You should import it from built-in module itertools:
>>> from itertools import combinations
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 404
Threads: 94
Joined: Dec 2017
Thanks
is j value and i key:value pair in your code?
Posts: 1,950
Threads: 8
Joined: Jun 2018
(Apr-15-2019, 08:42 PM)Truman Wrote: is j value and i key:value pair in your code?
You can easily test what is what
In [1]: from itertools import combinations
In [2]: d = {'Maggie': 3, 'Herman': 7, 'Betsy': 9, 'Oreo': 6, 'Moo Moo': 3, 'Milkshake': 2,
...: 'Millie': 5, 'Lola': 2, 'Florence': 2, 'Henrietta': 9}
In [3]: list(combinations(d, 2))[:5] # first five combinations to see what we get
Out[3]:
[('Maggie', 'Herman'),
('Maggie', 'Betsy'),
('Maggie', 'Oreo'),
('Maggie', 'Moo Moo'),
('Maggie', 'Milkshake')] We get key pair combinations, but they don't have values. With this part we 'put values back and convert to dict': {j: d[j] for j in i} - from each pair we take individual keys and construct new dictionary by getting key and its corresponding value from original dictionary.
For first row in pair combinations ('Maggie', 'Herman') :
{j: d[j] for j in i} --> {'Maggie': d['Maggie'], 'Herman': d['Herman']} --> {'Maggie': 3, 'Herman': 7} For sake of readabilty the d[j] could be replaced with d.get(j) - this gives better understanding that we will 'get' value of j.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 404
Threads: 94
Joined: Dec 2017
yeah...that's what I meant. :D
|