<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[partitioning of an array]]></title><description><![CDATA[<p>Hello,</p>
<p>If have got some Haskell code to convert it in c++. I'm a beginner in c++ and not very common with mathematic algorithms. Here is my problem/code:</p>
<p>import Data.List<br />
import Data.Ord</p>
<p>-- search all partitions having n parts of a list xs<br />
-- e.g. partitions [1,2,3,4] 3 should give<br />
-- &lsqb;&lsqb;[1],[2],[3,4&rsqb;&rsqb;,&lsqb;&lsqb;1],[2,3],[4&rsqb;&rsqb;,&lsqb;&lsqb;1,2],[3],[4&rsqb;&rsqb;]<br />
partitions xs n =<br />
let l = length xs in<br />
let firstParts = map ((flip splitAt) xs) [1.. l - (n - 1)] in<br />
if n == 2<br />
then map (\(l1, l2) -&gt; [l1, l2]) firstParts<br />
else concat $ map (\fp -&gt; map (( : ) (fst fp)) (partitions (snd fp) (n - 1))) firstParts</p>
<p>-- the value of a partition is the maximum of the sums of the parts<br />
partitionValue partition = maximum $ map sum partition</p>
<p>-- the best partition is the partition with minimum value<br />
bestPartition xs n = minimumBy (comparing partitionValue) $ partitions xs n</p>
<p>-- test with 6 stages (11 km -&gt; 16 km -&gt; 5 km -&gt; 5 km -&gt; 12 km -&gt; 10 km) in 3 days<br />
-- result should be &lsqb;&lsqb;11],[16,5,5],[12,10&rsqb;&rsqb;<br />
main = print $ bestPartition [11, 16, 5, 5, 12, 10] 3</p>
<p>It works in HASKELL, but I need to convert it in c++, problem is that it is an exponential algorithm. It works with low stages and days (and will have a stack overflow, when stages/days are a bit higher). I need an algorithm/solution in c++ for higher stages/days. Thanks for your anwer!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/333551/partitioning-of-an-array</link><generator>RSS for Node</generator><lastBuildDate>Sun, 26 Apr 2026 21:17:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/333551.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 15 Jul 2015 00:59:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to partitioning of an array on Wed, 15 Jul 2015 00:59:43 GMT]]></title><description><![CDATA[<p>Hello,</p>
<p>If have got some Haskell code to convert it in c++. I'm a beginner in c++ and not very common with mathematic algorithms. Here is my problem/code:</p>
<p>import Data.List<br />
import Data.Ord</p>
<p>-- search all partitions having n parts of a list xs<br />
-- e.g. partitions [1,2,3,4] 3 should give<br />
-- &lsqb;&lsqb;[1],[2],[3,4&rsqb;&rsqb;,&lsqb;&lsqb;1],[2,3],[4&rsqb;&rsqb;,&lsqb;&lsqb;1,2],[3],[4&rsqb;&rsqb;]<br />
partitions xs n =<br />
let l = length xs in<br />
let firstParts = map ((flip splitAt) xs) [1.. l - (n - 1)] in<br />
if n == 2<br />
then map (\(l1, l2) -&gt; [l1, l2]) firstParts<br />
else concat $ map (\fp -&gt; map (( : ) (fst fp)) (partitions (snd fp) (n - 1))) firstParts</p>
<p>-- the value of a partition is the maximum of the sums of the parts<br />
partitionValue partition = maximum $ map sum partition</p>
<p>-- the best partition is the partition with minimum value<br />
bestPartition xs n = minimumBy (comparing partitionValue) $ partitions xs n</p>
<p>-- test with 6 stages (11 km -&gt; 16 km -&gt; 5 km -&gt; 5 km -&gt; 12 km -&gt; 10 km) in 3 days<br />
-- result should be &lsqb;&lsqb;11],[16,5,5],[12,10&rsqb;&rsqb;<br />
main = print $ bestPartition [11, 16, 5, 5, 12, 10] 3</p>
<p>It works in HASKELL, but I need to convert it in c++, problem is that it is an exponential algorithm. It works with low stages and days (and will have a stack overflow, when stages/days are a bit higher). I need an algorithm/solution in c++ for higher stages/days. Thanks for your anwer!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2459824</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2459824</guid><dc:creator><![CDATA[SeppH]]></dc:creator><pubDate>Wed, 15 Jul 2015 00:59:43 GMT</pubDate></item></channel></rss>