Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the max rating in a list

Status
Not open for further replies.

KohlyKohl

Programmer
Mar 2, 2009
3
US
I am not looking for the answer, only a direction to take this assignment. I need to find the max rating of a list of songs that will fit on a given cd size.

example:
song rating filesize
Song1 5 600
Song2 4 200
Song3 2 400

If I had 400 space on the CD, Song2 and Song3 would go on because they have a max rating of 4+2=6, vs only 5 for song1.

So far I have:

% Song data

song( song1, 1, 200000 ).
song( song2, 2, 300000 ).
song( song3, 5, 400000 ).

% Rules

% Trivial Case
burnCD( SizeOfCD, [], CDBurnList ).

% Recursive algorithm
burnCD( SizeOfCD, [Item|Rest], L ) :-
song( Title, Rating, Size ),
burnCD( SizeOfCD - Size, Rest, L ),
burnCD( SizeOfCD - Size, Rest, L ).


burnCD( SizeOfCD, [Item|Rest], L ) :-
song( SongTitle, SongRating, SongSize ),
getTotalRating( R, TotalRating ) >
getTotalRating( L, TotalRating ),
NewSizeOfCD is SizeOfCD - SongSize.

getTotalRating( [Item|Rest], Result ) :-
song( _, Rating, _),
Result is Result + Rating.

First question I have is this: When I run trace, it is only going through the first songs data. Why is this?
 
Ok I fixed my first issue and it now correctly iterates through each song and also passes in the correct file sizes.

Here is the Updated Method.

% Recursive algorithm
burnCD( SizeOfCD, [Item|Rest], [Item|NewList] ) :-
song( Item, _, Size ),
burnCD( SizeOfCD - Size, Rest, NewList ),
burnCD( SizeOfCD - Size, Rest, NewList ).

My next question is: Once I have gone through the left side and then the right, Where do I calculate which side has the highest Rating?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top