23
Adapted from exercise 4 of 100 little Keg exercises
The ability to repeat chunks of code within a program is an important part of any programming language. Also just as important is the ability to make decisions based on different values.
The Challenge
I want you to write a program that:
- Starts at the number 0, and:
- For each number from 0 to 100 (inclusive):
- If the number is even, print the letter
E
(uppercase or lowercase) - Otherwise: print the number
Keeping in the spirit of this challenge, your source code must fit the pattern of having characters with an odd ordinal value followed by characters with an even ordinal value. Note that ordinal value is defined here as if I called python's ord()
function on the character using your language's prefered code page. In other words, the code points of your program should be as such:
Odd Even Odd Even Odd Even ...
or
Even Odd Even Odd Even Odd ...
More concisely, the code points of your program must alternate between being odd and even.
Example Output
E 1 E 3 E 5 E 7 E 9 E 11 E 13 E 15 E 17 E 19 E 21 E 23 E 25 E 27 E 29 E 31 E 33 E 35 E 37 E 39 E 41 E 43 E 45 E 47 E 49 E 51 E 53 E 55 E 57 E 59 E 61 E 63 E 65 E 67 E 69 E 71 E 73 E 75 E 77 E 79 E 81 E 83 E 85 E 87 E 89 E 91 E 93 E 95 E 97 E 99 E
Output can be in any other convenient format with any other separator. Leading and trailing whitespace is acceptable.
Script Validation (UTF-8 only)
Here is a Keg script to see if your program fits the parity pattern requirement
Note that this can only be used for languages that use utf-8 as their codepage.
Scoring
This is code-golf so the answer with the fewest amount of bytes wins.
Let it be known that flags don't count towards being in the pattern. They also don't count towards byte count in this challenge.
Leaderboards
Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.
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 leaderboard snippet:
# [><>](http://esolangs.org/wiki/Fish), 121 bytes
var QUESTION_ID=197210;
var OVERRIDE_USER=78850;
var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;function answersUrl(d){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+d+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(d,e){return"https://api.stackexchange.com/2.2/answers/"+e.join(";")+"/comments?page="+d+"&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(d){answers.push.apply(answers,d.items),answers_hash=[],answer_ids=[],d.items.forEach(function(e){e.comments=[];var f=+e.share_link.match(/\d+/);answer_ids.push(f),answers_hash[f]=e}),d.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(d){d.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),d.has_more?getComments():more_answers?getAnswers():process()}})}getAnswers();var SCORE_REG=function(){var d=String.raw`h\d`,e=String.raw`\-?\d+\.?\d*`,f=String.raw`[^\n<>]*`,g=String.raw`<s>${f}</s>|<strike>${f}</strike>|<del>${f}</del>`,h=String.raw`[^\n\d<>]*`,j=String.raw`<[^\n<>]+>`;return new RegExp(String.raw`<${d}>`+String.raw`\s*([^\n,]*[^\s,]),.*?`+String.raw`(${e})`+String.raw`(?=`+String.raw`${h}`+String.raw`(?:(?:${g}|${j})${h})*`+String.raw`</${d}>`+String.raw`)`)}(),OVERRIDE_REG=/^Override\s*header:\s*/i;function getAuthorName(d){return d.owner.display_name}function process(){var d=[];answers.forEach(function(n){var o=n.body;n.comments.forEach(function(q){OVERRIDE_REG.test(q.body)&&(o="<h1>"+q.body.replace(OVERRIDE_REG,"")+"</h1>")});var p=o.match(SCORE_REG);p&&d.push({user:getAuthorName(n),size:+p[2],language:p[1],link:n.share_link})}),d.sort(function(n,o){var p=n.size,q=o.size;return p-q});var e={},f=1,g=null,h=1;d.forEach(function(n){n.size!=g&&(h=f),g=n.size,++f;var o=jQuery("#answer-template").html();o=o.replace("{{PLACE}}",h+".").replace("{{NAME}}",n.user).replace("{{LANGUAGE}}",n.language).replace("{{SIZE}}",n.size).replace("{{LINK}}",n.link),o=jQuery(o),jQuery("#answers").append(o);var p=n.language;p=jQuery("<i>"+n.language+"</i>").text().toLowerCase(),e[p]=e[p]||{lang:n.language,user:n.user,size:n.size,link:n.link,uniq:p}});var j=[];for(var k in e)e.hasOwnProperty(k)&&j.push(e[k]);j.sort(function(n,o){return n.uniq>o.uniq?1:n.uniq<o.uniq?-1:0});for(var l=0;l<j.length;++l){var m=jQuery("#language-template").html(),k=j[l];m=m.replace("{{LANGUAGE}}",k.lang).replace("{{NAME}}",k.user).replace("{{SIZE}}",k.size).replace("{{LINK}}",k.link),m=jQuery(m),jQuery("#languages").append(m)}}
body{text-align:left!important}#answer-list{padding:10px;float:left}#language-list{padding:10px;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="https://cdn.sstatic.net/Sites/codegolf/primary.css?v=f52df912b654"> <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><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><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr></tbody> </table>
You say "100 (inclusive)", but the sample output only goes up to 99 – caird coinheringaahing – 2019-12-19T21:09:40.627
@cairdcoinheringaahing Fixed. – Lyxal – 2019-12-19T21:11:11.160
4Why is this a decision problem? – xnor – 2019-12-20T06:25:20.143
@xnor because you have to decide if integers are odd or even. – Lyxal – 2019-12-20T06:30:19.013
4... That's not what a [tag:decision-problem] is. Decision problems check whether the input meets a certain criteria and outputs based on that – Jo King – 2019-12-20T10:49:16.750
1Are we allowed to output a lowercase
e
instead of uppercase one? Also, are we allowed to output as a list (instead of a string so I could save on the additional join)? – Kevin Cruijssen – 2019-12-20T16:46:15.9772It should be noted that the Keg script in the question only checks the first line if you input more than one. – Post Rock Garf Hunter – 2019-12-20T19:52:19.250
2Here is a version that handles bad lines and also highlights the wrong characters – Post Rock Garf Hunter – 2019-12-20T20:02:11.893
I don't fully understand what the source restriction on this challenge is. Is it possible to explain it more clearly? – Skoddie – 2019-12-20T21:53:52.787
2@Skoddie
the code points of your program must alternate between being odd and even.
I don't really see how that is hard to understand. Codepoints are the ordinal value of a character in an encoding (usually UTF-8). For example a space has the codepoint 32, which is even. Your code must alternate between odd and even, starting with either. This means that a lot of commonly used function names, likeprint
,range
or any of the same character in a row (&&
or||
) cannot be used in your program, since they violate that rule – Jo King – 2019-12-21T07:44:00.320@JoKing That makes sense. That does disqualify languages that don't have alternatives to certain keywords. Oh well. – Skoddie – 2019-12-22T22:16:51.867