36
1
Write a program or function that takes an array of non-negative integers as input and outputs a set of vectors/arrays with the elements of the input array in order, split so that each vector sums up to 15. If the sum of the first N elements doesn't "hit 15", then the number that made it pass 15 must be cut off, and the remainder will be the first element of the next vector. This goes on until you reach the end of the input array. If the sum of the final vector is less than 15, then a number must be added at the end to make the sum go up.
I think the rules are more easily understood by looking at the examples:
Input: 3 7 5 10
Output:
3 7 5 <- Sum is 15
10 5 <- 5 is added to make the sum 15
Input: 2 4 5 9 2 3 5 0 2 4 5 0 3
Output:
2 4 5 4 <- Sum 15. 9 is split in two.
5 2 3 5 <- The first 5 is the remainder of 9
0 2 4 5 0 3 1 <- The last number is added to make the sum 15
Input: 1 1 1
Output:
1 1 1 12 <- The number 12 is added to make the sum 15
Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Output:
1 2 3 4 5
6 7 2 <- 2 is the first part of 8
6 9 <- 6 is the remainder of 8
10 5 <- 5 is first part of 11
6 9 <- 6 is remainder of 11. 9 is first part of 12
3 12 <- 3 is remainder of 12. 12 is first part of 13
1 14 <- 1 is remainder of 13. 14 is 14
15
15 <- 15 is first part of 16
1 14 <- 1 is remainder of 16. 14 is first part of 17
3 12 <- 3 is remainder of 17. 12 is added to make the sum 15
Input: 20 20
Output:
15
5 10 <- 5 is remainder from the first 20
10 5 <- 10 is remainder from second 20. 5 is added to make the sum = 15.
Both the input and the output format is optional. Whatever is best in your language.
The shortest code in bytes wins.
Leaderboard
The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
## Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
## Ruby, <s>104</s> <s>101</s> 96 bytes
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:
## Perl, 43 + 2 (-p flag) = 45 bytes
You can also make the language name a link which will then show up in the snippet:
## [><>](http://esolangs.org/wiki/Fish), 121 bytes
var QUESTION_ID=65577,OVERRIDE_USER=44713;function answersUrl(e){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"https://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
'Output format is optional'. Does that mean
[[3, 7, 5], [10, 5]]
would be a valid output for the first test case? – Morgan Thrapp – 2015-12-02T19:17:45.467@MorganThrapp, yes. that's valid. – Stewie Griffin – 2015-12-02T19:19:45.757
1@FlagAsSpam, I've added more explanations in the example you're asking about. – Stewie Griffin – 2015-12-02T19:20:23.857
3Good test case:
Input: 100 Output: 15; 15; 15; 15; 15; 15; 10 5
– randomra – 2015-12-03T11:23:22.923when i hit exactly 15 my programm puts a 0 like Input
15
Output15 0
is that ok? – Eumel – 2015-12-03T16:10:48.313@Eumel, it has to be only 15. Sorry... – Stewie Griffin – 2015-12-03T16:13:31.740
is kicking out 0s from input allowed? – Eumel – 2015-12-03T16:16:02.053
No, the zeros must be included. See the second example. – Stewie Griffin – 2015-12-03T16:28:15.647
3This should definitely replace the FizzBuzz test – CSᵠ – 2015-12-04T00:44:47.050
What should we output for
15 0
?15 0
or15; 0 15
? Also for input15 0 15
which one is correct,15 0;15
or15; 0 15
? (In retrospect question would have be cleaner with only positive integers.) (You should state that only non-negative integers are in the input, if that is the case.) – randomra – 2015-12-04T10:47:48.513The correct output for
15 0
is15; 0 15
. This is according to example 2, where the zero is carried on to the next line. But this wasn't very clear from the question, so existing answers outputting15 0
will not be invalidated. Outputting only15
however, is not valid. – Stewie Griffin – 2015-12-04T11:19:14.177