48
7
This challenge is a simple ascii-art one. Given two inputs, describing the height and width of a Lego piece, you have print an ASCII art representation of it.
Here is how the Lego pieces are supposed to look:
(4, 2)
___________
| o o o o |
| o o o o |
-----------
(8, 2)
___________________
| o o o o o o o o |
| o o o o o o o o |
-------------------
(4, 4)
___________
| o o o o |
| o o o o |
| o o o o |
| o o o o |
-----------
(3, 2)
_________
| o o o |
| o o o |
---------
(1, 1)
o
If you can't tell from the test-cases, the top and bottom are width*2+3 underscores and dashes, and each row has pipes for the sides, o's for the little things, and everything is separated by spaces.
The only exception for this is (1, 1), which is just a single o.
You will never get 0 for any of the dimensions.
This is code-golf, so shortest code in bytes wins!
var QUESTION_ID=84050,OVERRIDE_USER=31343;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 commentUrl(e,s){return"http://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>

2Is it possible that the width or the height will be greater than 10? What range should we support? – James – 2016-06-30T02:37:43.823
29The special case is a real bummer. – Conor O'Brien – 2016-06-30T03:03:17.260
@DrGreenEggsandIronMan definitely could be greater than 10. I guess whatever integer range your language supports (bearing in mind the standard loophole about abusing this). – Maltysen – 2016-06-30T03:28:39.820
Do (2, 1) and (1, 2) look similar to the bigger ones? Can we have an extra space at the end of each line? Ending newline required or optional? – aditsu quit because SE is EVIL – 2016-06-30T05:37:12.393
@aditsu yeah, they do,
(1, 1)is the only exception. Trailing whitespace is not allowed, except for a single trailing newline. – Maltysen – 2016-06-30T05:47:05.02747In the next few years, I want to see another "Print a Lego piece" challenge that requires writing the code to tell a 3D printer to produce a Lego. – Kevin – 2016-06-30T08:47:06.873
-will be better as"– None – 2016-06-30T13:30:26.917@Kevin such a thing can already be done - see (e.g.) http://www.thingiverse.com/thing:231651 for an example of exactly that.
– Nathaniel – 2016-06-30T13:39:53.970@Kevin i was hoping for something like this when i clicked on this hot network question – Gigala – 2016-06-30T15:03:50.160
8Wait, "whatever integer range your language supports"? That ain't how LEGO works. The bricks are only available in a handful of very specific dimensions. Even if you add in plates, you only get a couple more. Any script that does not discard input such as (1,7) or (5,3) is complete garbage. – RegDwight – 2016-07-01T14:43:55.657
3Why doesn't the single piece (1,1) have sides? There is a real Lego piece with a single nipple on top of a cube. – tcrosley – 2016-07-02T00:00:58.190
@tcrosley i know, I just wanted a special case, and there also exist (1,1) lego pieces without sides – Maltysen – 2016-07-02T00:03:32.517
Really (re ones with no sides). I don't think the sets our grand-kids play with have any like that, but there's aren't the mega sets with hundreds of pieces. – tcrosley – 2016-07-02T00:10:08.127
1@RegDwight you are only seeing the top of it. It could be bound underneath. – Conor O'Brien – 2016-07-03T16:26:15.960
1
@tcrosley this kinda thing: http://s.ecrater.com/stores/196820/5355aaa76f14f_196820b.jpg
– Maltysen – 2016-07-03T21:07:27.993http://www.thingiverse.com/search/page:1?q=lego – Sparr – 2016-10-08T16:15:11.057
a top down 1x1 lego view – FantaC – 2018-01-12T00:01:12.687