24
3
The minmod function is a variant of the familiar min, which appears in slope-limiting high-resolution schemes for partial differential equations. Given a number of slopes, it picks out the flattest slope, while taking care of relative signs between the slopes.
The function takes an arbitrary number of parameters. Then minmod(x1, x2, ..., xn) is defined as:
- min(x1, x2, ..., xn), if all xi are strictly positive
- max(x1, x2, ..., xn), if all xi are strictly negative
- 0, otherwise.
We'll only consider integer inputs, because that does not really affect the implementation and should be more inclusive for some (esoteric) languages.
Write a program or function, which takes n signed integers (for n > 0) via STDIN, ARGV or function argument (you can use an array if that's more convenient than a variadic function), and returns or prints (to STDOUT) the result of minmod(a,b).
You must not use built-in min or max functions (and obviously, no built-in minmod either, if you can actually find that). In addition, you must not use any built-in sorting functions, except to sort a fixed small number of items (less than 5).
If your language doesn't have signed types, you may use an unsigned type and interpret it as two's complement. E.g. if your language only uses unsigned bytes, you can use 255
to stand in for -1
and 128
to stand in for -128
, etc.
This is code golf, so the shortest answer (in bytes) wins.
Test Cases
Input Output
2 2
-3 -3
0 0
3 -5 0
2 4 1 1
0 1 2 0
-1 1 2 0
-4 -2 -3 -2 -2
-5 0 -1 0
1 0 -1 0
Leaderboards
The following Stack Snippet generates both a regular leaderboard and an overview of winners by language. So even if your language of choice doesn't let you win the entire challenge, why not try to snatch a spot on the second list?
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
function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function getAnswers(){$.ajax({url:answersUrl(page++),method:"get",dataType:"jsonp",crossDomain:true,success:function(e){answers.push.apply(answers,e.items);if(e.has_more)getAnswers();else process()}})}function shouldHaveHeading(e){var t=false;var n=e.body_markdown.split("\n");try{t|=/^#/.test(e.body_markdown);t|=["-","="].indexOf(n[1][0])>-1;t&=LANGUAGE_REG.test(e.body_markdown)}catch(r){}return t}function shouldHaveScore(e){var t=false;try{t|=SIZE_REG.test(e.body_markdown.split("\n")[0])}catch(n){}return t}function getAuthorName(e){return e.owner.display_name}function process(){answers=answers.filter(shouldHaveScore).filter(shouldHaveHeading);answers.sort(function(e,t){var n=+(e.body_markdown.split("\n")[0].match(SIZE_REG)||[Infinity])[0],r=+(t.body_markdown.split("\n")[0].match(SIZE_REG)||[Infinity])[0];return n-r});var e={};var t=1;answers.forEach(function(n){var r=n.body_markdown.split("\n")[0];var i=$("#answer-template").html();var s=r.match(NUMBER_REG)[0];var o=(r.match(SIZE_REG)||[0])[0];var u=r.match(LANGUAGE_REG)[1];var a=getAuthorName(n);i=i.replace("{{PLACE}}",t++ +".").replace("{{NAME}}",a).replace("{{LANGUAGE}}",u).replace("{{SIZE}}",o).replace("{{LINK}}",n.share_link);i=$(i);$("#answers").append(i);e[u]=e[u]||{lang:u,user:a,size:o,link:n.share_link}});var n=[];for(var r in e)if(e.hasOwnProperty(r))n.push(e[r]);n.sort(function(e,t){if(e.lang>t.lang)return 1;if(e.lang<t.lang)return-1;return 0});for(var i=0;i<n.length;++i){var s=$("#language-template").html();var r=n[i];s=s.replace("{{LANGUAGE}}",r.lang).replace("{{NAME}}",r.user).replace("{{SIZE}}",r.size).replace("{{LINK}}",r.link);s=$(s);$("#languages").append(s)}}var QUESTION_ID=42079;var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";var answers=[],page=1;getAnswers();var SIZE_REG=/\d+(?=[^\d&]*(?:<(?:s>[^&]*<\/s>|[^&]+>)[^\d&]*)*$)/;var NUMBER_REG=/\d+/;var LANGUAGE_REG=/^#*\s*([^,]+)/
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>Language<td>Size<tbody id=answers></table></div><div id=language-list><h2>Winners by Language</h2><table class=language-list><thead><tr><td>Language<td>User<td>Score<tbody id=languages></table></div><table style=display:none><tbody id=answer-template><tr><td>{{PLACE}}</td><td>{{NAME}}<td>{{LANGUAGE}}<td>{{SIZE}}<td><a href={{LINK}}>Link</a></table><table style=display:none><tbody id=language-template><tr><td>{{LANGUAGE}}<td>{{NAME}}<td>{{SIZE}}<td><a href={{LINK}}>Link</a></table>
Reading the input will not be in O(n) if there isn't a limit. – jimmy23013 – 2014-12-01T15:42:14.873
@user23013 Good point. I removed this altogether, because I only added it to rule out sorting functions in the first place. So instead I'm now simply doing exactly that. – Martin Ender – 2014-12-01T15:45:27.643
1Maybe add a column for how many answers are there in each language – proud haskeller – 2014-12-01T21:45:49.557
1@proudhaskeller Hmm, I like that the two tables currently fit next to each other without having go open the snippet full screen - I think it would be a bit too crammed if I added another column. If your comment gets significantly more upvotes than mine, I'll see what I can do. ;) – Martin Ender – 2014-12-01T21:48:04.387
Both +1'ed.. ;) – Timtech – 2014-12-01T22:54:44.063
I've now allowed sorting of small fixed arrays (as per popular demand). – Martin Ender – 2014-12-01T23:19:51.553
as per the chat* – Timtech – 2014-12-02T01:20:24.843
@MartinBüttner I think the allowing sorting at this stage is destructive and changes the whole thinking angle towards the question, rendering all existing (answers added before this rule addition) kind of useless and obsolete. – Optimizer – 2014-12-02T06:28:14.800
@Timtech and a couple of answers. – Martin Ender – 2014-12-02T10:07:32.680
1@Optimizer I decided that the previous version of the rules was doing more harm to the creativity of people's answers than I had intended. In addition, I was going to award a bounty to the answer that was leading before the rule change anyway so I don't think any harm in terms of rep is done either. (Yes, I agree that rule changes aren't such a good idea, but I figured it would be worth it in this case.) – Martin Ender – 2014-12-02T10:09:15.880
1@MartinBüttner - I don't see any creativity in the newer answers now. It has all come down to reducing to a pairwise minmod. Creativity was in xnor's answers or Mig's approach on which many other answers are influenced on. – Optimizer – 2014-12-02T11:12:18.863
True, xnor's solution was brilliant, and I think the newer answers are a bit cheaty. – Timtech – 2014-12-02T12:19:38.820
2@Optimizer, it's not always possible to tell whether a new answer is exactly as creative as an older one which it's similar to or whether it's an unimaginative port. – Peter Taylor – 2014-12-02T12:39:23.657