43
10
Introduction
Turns out, aliens love memes just as much as we do. Every alien race we've encountered so far has their own version of 2spooky4me
(see the following question) and equivalent, though, with some variation. The inhabitants of planet CUTE1f can't handle a lot of spook, so their preferred spook is 1spooky2me
, while skeletor7 memers love them some spook, so they tend to use 9spooky11me
.
Challenge
Translating memes is hard work, so you've been tasked with writing a universal meme translator to help these guys access the memenet correctly. Your program will accept a meme and a transformation to apply to the digit sequences in that meme to make it appropriate for the inhabitants of a different planet.
Input
Your program will receive two string inputs:
- The input meme (e.g.
2spooky4me
). Matches[a-zA-Z0-9]+
. - The transformation to apply to it (e.g.
+1
, to go from2spooky4me
to3spooky5me
). Matches[+\-*/^]\d+
(you must accept+
,-
,*
,/
, and^
as operators, regardless of the native representation in your language).
Output
Your program must return a string output (printed to standard output or equivalent) with the given transformation applied to the digit sequences in the input meme. In a weird turn of events, it also turns out that all races encountered so far prefer integral memes over fractional ones, so these transformations should perform integer arithmetic (e.g. 1spooky1me /2
should result in 0spooky0me
).
Examples
Standard arithmetic operations apply:
Input: 2spooky4me +1
Output: 3spooky5me
Input: 2spooky4me -1
Output: 1spooky3me
Input: 2spooky4me *15
Output: 30spooky60me
Input: 10spooky900me /5
Output: 2spooky180me
Digit sequences are integral; integer truncation should occur in cases like this:
Input: idontunderstandmemes3 /2
Output: idontunderstandmemes1
Your input may not have any digit sequences:
Input: notreallyafunnymeme *100
Output: notreallyafunnymeme
You must support exponentiation, even if it is not a native operation in your language of choice:
Input: 2spooky4me ^3
Output: 8spooky64me
There's no limit on string length of number of digit sequences in the string:
Input: some1meme2sequences3can4be5really6long7 /2
Output: some0meme1sequences1can2be2really3long3
Addendum
If your language supports arbitrary-precision integers as a language feature, you must use those. If it does not, you do not need to support arbitrary-precision integers. For example, you must use Integer
in Haskell instead of Int
because it's available as part of the language; in Java
, you are not required to use BigInteger
because it's a library feature, not a language feature.
Input: 2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000spooky4me /2
Output: 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000spooky2me
This is code-golf, so standard loopholes are forbidden, and the shortest answer in bytes wins!
Leaderboard
The Stack Snippet at the bottom of this post generates the leaderboard 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
<style>body { text-align: left !important} #answer-list { padding: 10px; width: 290px; float: left; } #language-list { padding: 10px; width: 290px; float: left; } table thead { font-weight: bold; } table td { padding: 5px; }</style><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="language-list"> <h2>Shortest Solution 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>{{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><script>var QUESTION_ID = 79809; var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe"; var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk"; var OVERRIDE_USER = 45941; var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page; function answersUrl(index) { return "https://api.stackexchange.com/2.2/questions/" + QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER; } function commentUrl(index, answers) { return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER; } function getAnswers() { jQuery.ajax({ url: answersUrl(answer_page++), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { answers.push.apply(answers, data.items); answers_hash = []; answer_ids = []; data.items.forEach(function(a) { a.comments = []; var id = +a.share_link.match(/\d+/); answer_ids.push(id); answers_hash[id] = a; }); if (!data.has_more) more_answers = false; comment_page = 1; getComments(); } }); } function getComments() { jQuery.ajax({ url: commentUrl(comment_page++, answer_ids), method: "get", dataType: "jsonp", crossDomain: true, success: function (data) { data.items.forEach(function(c) { if (c.owner.user_id === OVERRIDE_USER) answers_hash[c.post_id].comments.push(c); }); if (data.has_more) getComments(); else if (more_answers) getAnswers(); else process(); } }); } getAnswers(); var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/; var OVERRIDE_REG = /^Override\s*header:\s*/i; function getAuthorName(a) { return a.owner.display_name; } function process() { var valid = []; answers.forEach(function(a) { var body = a.body; a.comments.forEach(function(c) { if(OVERRIDE_REG.test(c.body)) body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>'; }); var match = body.match(SCORE_REG); if (match) valid.push({ user: getAuthorName(a), size: +match[2], language: match[1], link: a.share_link, }); else console.log(body); }); valid.sort(function (a, b) { var aB = a.size, bB = b.size; return aB - bB }); var languages = {}; var place = 1; var lastSize = null; var lastPlace = 1; valid.forEach(function (a) { if (a.size != lastSize) lastPlace = place; lastSize = a.size; ++place; var answer = jQuery("#answer-template").html(); answer = answer.replace("{{PLACE}}", lastPlace + ".") .replace("{{NAME}}", a.user) .replace("{{LANGUAGE}}", a.language) .replace("{{SIZE}}", a.size) .replace("{{LINK}}", a.link); answer = jQuery(answer); jQuery("#answers").append(answer); var lang = a.language; lang = jQuery('<a>'+lang+'</a>').text(); languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang.toLowerCase(), user: a.user, size: a.size, link: a.link}; }); var langs = []; for (var lang in languages) if (languages.hasOwnProperty(lang)) langs.push(languages[lang]); langs.sort(function (a, b) { if (a.lang_raw > b.lang_raw) return 1; if (a.lang_raw < b.lang_raw) return -1; return 0; }); for (var i = 0; i < langs.length; ++i) { var language = jQuery("#language-template").html(); var lang = langs[i]; language = language.replace("{{LANGUAGE}}", lang.lang) .replace("{{NAME}}", lang.user) .replace("{{SIZE}}", lang.size) .replace("{{LINK}}", lang.link); language = jQuery(language); jQuery("#languages").append(language); } }</script>
3Your last test case is wrong. You have one too many zeros in the output for it to be /5. – Fund Monica's Lawsuit – 2016-05-12T19:14:29.210
Welcome to PPCG! As [tag:arithmetic] refers solely to Elementary Arithmetic, I suggest you retag this question to [tag:math], which would be more appropriate for a challenge that requires exponents.
– George Gibson – 2016-05-12T19:18:21.3935
First of all this is a reasonably well put together first post, so congrats :) Note we have a Sandbox where you can post your challenge for feedback before it goes live.
– FryAmTheEggman – 2016-05-12T19:21:07.8403Welcome to PPCG (even though you've apparently been here for 2+ years). Nice first challenge. Does the addendum about arbitrary precision integers mandate that, for example, Java must use
BigInteger
for its calculations? – AdmBorkBork – 2016-05-12T19:24:11.883@GeorgeGibson Ah, thank you! I wasn't sure about how to tag it, but thank you for letting me know. – Itai Ferber – 2016-05-12T19:29:26.620
@FryAmTheEggman Thanks! I'll definitely go through the sandbox next time. :) – Itai Ferber – 2016-05-12T19:30:03.900
@TimmyD Clarified in the post — if it's a language feature, then yes. If it's just available as part of the standard library, then no. e.g. yes in Haskell, no in Java – Itai Ferber – 2016-05-12T19:33:24.823
1The exponential one is evil. – Bálint – 2016-05-12T19:55:01.910
@Bálint Why, thank you. – Itai Ferber – 2016-05-12T20:24:00.967
Do we have to use the symbols
+
,-
,*
,/
and^
or can we use the symbols our language uses natively? – Dennis – 2016-05-12T20:41:12.19318Every alien race we've encountered so far... That's totally true! :-) – Luis Mendo – 2016-05-12T20:48:33.660
@Dennis You must use the symbols
+
,-
,*
,/
, and^
... Sorry not sorry. – Itai Ferber – 2016-05-12T20:50:25.3402It's your challenge and ultimately up to you, but that's not really fair towards languages that happen to use a different syntax. – Dennis – 2016-05-12T20:51:33.727
@Dennis True. However, that would then tie specific inputs to specific languages, which I think goes against the spirit of the question, no? Is this a common thing among other code golf questions? – Itai Ferber – 2016-05-12T20:55:48.887
@Dennis If it is common to make exceptions for languages in this way, then I will amend my question. I think you would know way better than I do. – Itai Ferber – 2016-05-12T20:56:41.650
Yes, this is common. – Dennis – 2016-05-12T20:57:15.897
1@Dennis I think the challenge is fine as-is. It's an explicit translation request, so it should fall under the "specific format" exception. – AdmBorkBork – 2016-05-12T20:58:35.017
BTW, I'd expected something like "and because some aliens are scared from long code, you need to write it as short as possible" – Bálint – 2016-05-12T21:19:12.903
@Dennis As a compromise, would it be appropriate to award bonus bytes if your language does not support those arithmetic operators by default but you implement them anyway? (Allowing you to stick to the defaults if necessary.) – Itai Ferber – 2016-05-12T22:27:08.663
Bonuses in code-golf are generally not a good idea. At this point, I wouldn't change the challenge anymore. – Dennis – 2016-05-12T23:13:51.753
If you insist on BigInt functionality, you should include a test case that requires them. My previous revision of the Julia submission was failing
2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000spooky4me, /2
. Fixing that cost 17 bytes... :( – Dennis – 2016-05-12T23:32:13.777@Dennis Yikes, sorry! :( Added an example above. – Itai Ferber – 2016-05-13T02:00:06.187
2I might be overlooking it in the spec but do we need to handle inputs like
2spooky4me -5
? – Martin Ender – 2016-05-13T12:40:05.420What's the answer to
2spooky4me, /0
? – Magic Octopus Urn – 2016-10-13T18:27:31.6509spooky11me
is of a slightly bad taste, and it's not spooky at all (it's just macabre/tragic/sad/reminding<bad>)... maybe you can choose6spooky66me
instead? Satanism is not prohibited, as far as I know :) – Erik the Outgolfer – 2016-10-13T18:55:28.663@carusocomputing That's undefined, as the operation doesn't really make sense. – Itai Ferber – 2016-10-13T19:03:07.993
@ItaiFerber As in your code can error or cannot error on it? – Magic Octopus Urn – 2016-10-13T19:04:40.627
1@carusocomputing Your code is allowed to error on it; it's not really a valid input. – Itai Ferber – 2016-10-13T19:05:18.023