18
A parity bit, is one of the simplest forms of a checksum. First, you have to pick the parity, even or odd. Let's say we pick even. Now, we need a message to transmit. Let's say our message is "Foo". This is written in binary as:
01000110 01101111 01101111
Now, we count the total number of 1
's in there, which is 15. Since 15 is an odd number, we must add one extra bit to the end of our message, and we will now have an even number of 'on' bits. This last added bit is known as the "parity bit". If we had picked an odd parity for our checksum, we would have to add an extra '0' so that the number of on bits remains odd.
The challenge:
You must write a program or function that determines what the correct parity bit for a string is. Your program must take two inputs:
A string,
s
. This is the message that the checksum will be calculated on. This will be restricted to the 95 printable ASCII characters.A character or single character string
p
, that will either bee
for even parity, oro
for odd parity.
and produce a truthy-falsey value representing the correct parity bit. Truthy if it's a 1
, and falsey if it's a 0
.
Builtins that count the number of "on" bits in a string or character are not allowed. For example, a function f
that does this: f('a') == 3
or f('foo') == 16
is banned. Anything else, such as base conversion, is fair game.
Test IO:
(without the quotes)
s: "0"
p: 'e'
output: 0
s: "Foo"
p: 'e'
output: 1
s: "Hello World!"
p: 'o'
output: 0
s: "Alex is right"
p: 'e'
output: 1
s: "Programming Puzzles and Code-Golf"
p: 'e'
output: 0
s: "Programming Puzzles and Code-Golf"
p: 'o'
output: 1
This is codegolf, so standard loopholes apply, and the shortest answer in bytes wins.
Leaderboard
var QUESTION_ID=78569,OVERRIDE_USER=31716;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>
10Rather annoyingly,
o
has even parity. – Neil – 2016-04-25T23:36:05.223Can you clarify "Builtins that count the number of "on" bits in a string or character are not allowed." a bit better? What about built-in base conversion? Etc.. – orlp – 2016-04-25T23:46:12.443
@DrGreenEggsandHamDJ Comments on Stack sites are volatile and subject to disappear without warning for no reason, at any capable user's whim. As a result, it's highly encouraged that specs integral to the challenge be in the post itself, not the comment section. – cat – 2016-04-26T02:00:07.313
@DrGreenEggsandHamDJ To be clear, if I compose two builtin functions
g
andx
into a functionf
such thatf('a') == 3
andf('foo') == 16
, does that count as a builtin? – cat – 2016-04-26T02:01:41.0271@cat For example, in python:
str(int(s, 2)).count('1')
? No, I wouldn't consider that to be a single builtin function that violates that rule. Does my edit make it more clear? – James – 2016-04-26T02:08:21.370@DrGreenEggsandHamDJ Yes, it does, thank you! – cat – 2016-04-26T02:20:13.703
Do I have to accept the
p
in char form if my language has a character type that's disparate from the string type? Or can I ask for it in string form (that is,"e"
rather than101
)? – cat – 2016-04-26T19:09:01.2971@cat As far as I'm concerned
char == single_char_string
. I also edited that into the post. – James – 2016-04-26T19:10:46.540@DJMcMayhem Actually it's not the same thing (at least in C),
single_char_string
is an array consisting of onechar
element and'\0'
. Also, do I really need to gete
oro
as strings or chars, or can I get0
or1
too? – Erik the Outgolfer – 2016-10-13T12:26:19.397Does
sum
count as counting ON bits? For example, doessum([0,0,1,1,1,1,0,0,1,1,0])
in Python count as a cheat? Because it doesn't count ON bits, it sums up all the numbers, and it just happens to function like this, in this case. – Erik the Outgolfer – 2016-10-13T12:38:37.973@EriktheGolfer I'm pretty sure I answered both of those points in the post. I know that
char
andstring with only one char
are different. I don't care which one your program takes, which is why I specified they're both OK. I also wroteBuiltins that count the number of "on" bits in a string or character are not allowed. For example, a function f that does this: f('a') == 3 or f('foo') == 16 is banned. Anything else, such as base conversion, is fair game.
So sincesum('a') != 3
it doesn't count as a builtin. – James – 2016-10-13T19:18:44.253