Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm Minimize playlist without altering the playout

Im looking for an algorithm to reduce a list (playlist) of ordered but not unique items. Searched for set theory but havent found anything suitable yet

Examples

[a, b, b, c] -> [a, b, b, c] Cannot be reduced. 
[a, b, b, a, b, b] -> [a, b, b]. 
[b, b, b, b, b] -> [b]. 
[b, b, b, b, a] -> [b, b, b, b, a] Cannot be reduced. 

Thinking of getting all existing sublists and count each instance. If there is such a sublist where the count times the sublist length is equal to the orginal list, take the shortest sublist matching this criteria.

This seems a bit brute force, there must be a simpler/faster solution available.

like image 556
kiteloop Avatar asked Nov 28 '10 21:11

kiteloop


1 Answers

For starters, you don't need to check all sublists -- just those with lengths that are factors of the length of the full list.

If your main concern is coding simplicity rather than raw speed, just let a regex engine solve the problem:

/^(.+?)\1+$/

Which is a variant on Abigail's awesome Perl regex to find prime numbers.

like image 199
FMc Avatar answered Sep 22 '22 13:09

FMc