8
\$\begingroup\$

Introduction

Output all the numbers in specific base that would appear in an adapted version of the game of seven.

Challenge

Your task is to output all the numbers in a given base in a given range, skipping the ones matching certain conditions.

You will be given four inputs: base elim start and end. The roles of the four variables are as follows:

  • base specifies what base we are using for the other three variables, and also for output. One way to express it is using a single character that is '8', '9' or a capital letter from 'A' to 'Z', using 'A' for decimal system, and 'G' for hexadecimal system. The bases expected to be supported are 8 to 35 (inclusive). This is just my suggested way to express the base and I will use it in the Example Input and Output section, but it is also fine to use your own way to express it. For example, you may choose to format base as its usual decimal representation.

  • start and end are two strings that specify the range of numbers to be output, expressed in the given base. If base is G and start is 10 and end is 1F, you need to output 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F, but you also need to eliminate some out them based on what elim is, to be explained soon.

  • elim is a single character and must be a valid digit in the given base. All numbers containing the digit elim or are multiples of elim should be eliminated from the list to generate the final output.

In the case above, if elim is 7, then 17 need to be eliminated because it contains the digit 7. 15 and 1C also need to be eliminated because they are multiples of 7. So the final output would be 10 11 12 13 14 16 18 19 1A 1B 1D 1E 1F

Test Cases

Input -> Output

G,7,10,1F -> 10 11 12 13 14 16 18 19 1A 1B 1D 1E 1F
A,3,1,100 -> 1 2 4 5 7 8 10 11 14 16 17 19 20 22 25 26 28 29 40 41 44 46 47 49 50 52 55 56 58 59 61 62 64 65 67 68 70 71 74 76 77 79 80 82 85 86 88 89 91 92 94 95 97 98 100
G,A,96,C0 -> 97 98 99 9B 9C 9D 9E 9F B0 B1 B2 B3 B5 B6 B7 B8 B9 BB BC BD BF C0

The third test case is added after the original post. All the answers posted before the edit has been tested and passed the third test.

Specs

  • The input will always be valid so don't worry about exception handling.

  • This is , the lowest number of bytes wins.

  • You can take input and provide output through any standard form, and you are free to choose the format.

  • As usual, default loopholes apply here.

  • This is based on the (real-life version of) the game of sevens.

  • I require base to be at least 8 purely due to personal taste. I just don't want to see the number of digits in the sequence grow too fast.

Scoreboard

Snippet taken from this question. Run the snippet to get the scoreboard.

var QUESTION_ID=154147,OVERRIDE_USER=77134;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} /* font fix */ body {font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;} /* #language-list x-pos fix */ #answer-list {margin-right: 200px;}
<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>

\$\endgroup\$
  • 11
    \$\begingroup\$ Welcome to PPCG! This is a nice first post, but I have few recommendations: Avoid very strict Input / Output formats (base,elim,start,end, 'A' for decimal system, 'G' for hexadecimal system), try to format your test cases in a way that is easy to use and consider using the Sandbox in order to receive feedback prior to posting to the main site. Overall, this is a good first challenge! \$\endgroup\$ – Mr. Xcoder Jan 26 '18 at 17:01
  • 1
    \$\begingroup\$ @WeijunZhou Sure, I’ll try to take a look at it later. Also feel free to reach us in chat if you encounter difficulties while editing and / or want tips. \$\endgroup\$ – Mr. Xcoder Jan 26 '18 at 17:06
  • 1
    \$\begingroup\$ @Mr.Xcoder I have finished the edit. You can also directly edit the question to make it better and I will try to learn from your edit. \$\endgroup\$ – Weijun Zhou Jan 26 '18 at 17:10
  • 1
    \$\begingroup\$ @WeijunZhou I'd also suggest making the input formats for start, end and elim more lenient. \$\endgroup\$ – Erik the Outgolfer Jan 26 '18 at 17:17
  • 5
    \$\begingroup\$ For the "G,7,10,1F" input a fully flexible approach would allow, for example, [16,7,16,31] as input and [[1,0],[1,1],[1,2],[1,3],[1,4],[1,6],[1,8],[1,9],[1,10],[1,11],[1,13],[1,14],[1,15]] as output. (Note that the first answer assumed some of this had been allowed already, since it's pretty standard - but it is totally up to you). \$\endgroup\$ – Jonathan Allan Jan 26 '18 at 17:27
10
\$\begingroup\$

Emojicode, 141 116 bytes

thanks to @NieDzejkob for 25 bytes!

🍇a🚂b🚂c🚂d🚂🔂i⏩c➕1d🍇🍊🎊❎😛0🚮i b☁️🔍🔡i a🔡b a🍇😀🔡i a🍉🍉🍉

Try it online!

🍇				
  a🚂 b🚂 c🚂 d🚂		👴 4 arguments of type 🚂 (integer)
  🔂 i  ⏩ c  ➕ 1 d 🍇		👴 for i in range(c, d+1)
    🍊 🎊 			👴   if cond_a and cond_b
      ❎ 😛 0  🚮 i b		👴     cond_a: i%b != 0
      ☁️ 🔍 🔡 i a  🔡 b a	👴     cond_b: i in base-a doesnt have b in base-a
    🍇				👴   then
      😀 🔡 i a			👴     print i in base-a
    🍉
  🍉
🍉

\$\endgroup\$
  • 3
    \$\begingroup\$ -1 for language choice ಠ_ಠ \$\endgroup\$ – Rod Jan 26 '18 at 18:13
  • 3
    \$\begingroup\$ You can just post a closure, which is the emojicode equivalent of a lambda. This should save you some bytes. \$\endgroup\$ – NieDzejkob Jan 26 '18 at 18:17
5
\$\begingroup\$

Jelly,  12  11 bytes

-1 thanks to Erik the Outgolfer (use of "order" in place of divisible with swapped arguments, ḍ@)

r/ọÐḟ⁵bċÐḟ⁵

A full program taking three inputs as command line arguments, all of which employ decimal notation - a list [start, end], and two numbers, base and elim - which prints the Jelly representation of a list of lists, where each represents the digits of a number in base and each digit is a decimal number (e.g. 1043 in base 20 would be [2,12,3], i.e. 2×202+12×201+3×200).

Try it online!

A small note: this won't work for ranges that include negative values, but the linked challenge starts from 1 and counts up.

How?

r/ọÐḟ⁵bċÐḟ⁵ - Main link: list, [start, end]; number, base
 /          - reduce [start, end] with:
r           - inclusive range => [start, start+1, ..., end]
   Ðḟ       - filter discard if:
     ⁵      -   program's 5th argument (the 3rd input), elim
  ọ         -   order (how many times the element is divisible by elim, or 0 if not)
      b     - convert to base "base"
        Ðḟ  - filter discard if:
          ⁵ -   program's 5th argument (the 3rd input), elim
       ċ    -   count (0 if elim is not a digit)
\$\endgroup\$
  • \$\begingroup\$ ḍ@ can be ;) (btw I'd have posted rọÐḟ⁶b⁵ċÐḟ⁶...) \$\endgroup\$ – Erik the Outgolfer Jan 26 '18 at 17:45
  • \$\begingroup\$ Ah yeah, never used the sixth arg! \$\endgroup\$ – Jonathan Allan Jan 26 '18 at 17:49
  • 1
    \$\begingroup\$ As usual, would you mind expanding it and elaborating how it works? \$\endgroup\$ – Weijun Zhou Jan 26 '18 at 17:51
  • \$\begingroup\$ An alternative for ọÐḟ could be %Ðf \$\endgroup\$ – Mr. Xcoder Jan 26 '18 at 17:59
2
\$\begingroup\$

Julia 0.6, 69 bytes

f(b,e,r,g=filter)=g(z->!contains(z,base(b,e)),base.(b,g(x->x%e>0,r)))

Inputs are base, elim, start:end where start:end is a UnitRange. as base 10 integers. Outputs a list of strings of the numbers in the given base. Filters for divisibility in integer form, uses base. to elementwise convert to strings, then filters on digit containment. Called example: f(10,3,1:100)

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I hadn't thought of using defaults in a signature to alias subroutines, I'll have to remember that for future Perl 6 posts. \$\endgroup\$ – Brad Gilbert b2gills Jan 27 '18 at 17:55
1
\$\begingroup\$

Python 2, 102 94 bytes

lambda b,e,l,u:[i for i in range(l,u+1)if i%e*x(i,b,e)]
x=lambda n,b,e:n<1or(e-n%b)*x(n/b,b,e)

Try it online!

Input and output is in base 10.

\$\endgroup\$
1
\$\begingroup\$

Perl 6,  94  57 bytes

->\a,$b,\c,\d {grep {!/$b/},grep({$_%$b.parse-base(a)},[...] (c,d)».parse-base(a))».base(a)}

Test it (94)

Since the rules have changed:

->\b,$e,\r{grep {!/"$e.base(b)"/},grep(*%$e,r)».base(b)}

Test it ( Int, Int, Range )
Output is a list of strings in the base

Expanded

->
  \b,  # base  (Int)
  $e,  # elim  (Int)
  \r   # range (Int Range)
{
  grep
    {!/"$e.base(b)"/},  # remove the values that contain the character

    grep(
      * % $e,           # remove values that are divisible by elim
      r                 # the input Range

    )».base(b)          # convert each into base 「b」
}
\$\endgroup\$
  • \$\begingroup\$ Thank you for your interest and effort, also for writing a test. Learned a lot. \$\endgroup\$ – Weijun Zhou Jan 27 '18 at 18:55
1
\$\begingroup\$

JavaScript, 82 bytes

A quickie before I hit the boozer! Takes input in base-10 in the order base, elim, start & end and outputs a space delimited string of base-10 numbers with a single trailing space.

(b,l,s,e)=>(g=o=>++s>e?o:g(s%l&&!~s[t=`toString`](b).search(l[t](b))?o+s+` `:o))``

Try it online

\$\endgroup\$
  • \$\begingroup\$ This one has the problem as the Japt one. Would you mind modifying it a bit? \$\endgroup\$ – Weijun Zhou Jan 27 '18 at 9:27
  • \$\begingroup\$ @WeijunZhou: Updated. \$\endgroup\$ – Shaggy Jan 28 '18 at 11:22
  • \$\begingroup\$ Thank you very much. You can update the "explanation" part of the other answer whenever you like to. No hurry. \$\endgroup\$ – Weijun Zhou Jan 28 '18 at 11:25
  • \$\begingroup\$ @WeijunZhou: I assume you mean on my Japt solution, rather than this one? I'll be updating when I get back to my computer tomorrow. \$\endgroup\$ – Shaggy Jan 28 '18 at 11:27
  • \$\begingroup\$ Yes, my bad. I edited the comment right away but you still saw the original one ... Sorry about it. \$\endgroup\$ – Weijun Zhou Jan 28 '18 at 11:28
1
\$\begingroup\$

Japt, 16 bytes

Hastily rewritten after the spec was changed to allow decimal input so can probably be improved upon.

Takes input in the order start, end, base, elim and outputs an array of numbers in the given base.

òV kvX msW køXsW

Try it


Explanation

                   :Implicit input of integers U=start, V=end, W=base & X=elim
òV                 :[U,V]
   k               :Remove
    vX             :  elements divisible by X
       m           :Map
        sW         :  Convert to base-W string
           kø      :Remove elements containing
             XsW   :  X converted to a base-W string
                   :Implicit output of resulting array
\$\endgroup\$
  • \$\begingroup\$ For values of elim that are larger than 10, yet smaller than the base, this will fail to remove elements that contain the digit. For example for 0,35,12,11 the output erroneously contains 23=(1,11)base12 \$\endgroup\$ – gggg Jan 26 '18 at 22:59
  • \$\begingroup\$ @gggg, it looks like the spec was changed since I posted this; originally, elim was specified to be a single digit (i.e., 0-9) rather than a single character in the input base. \$\endgroup\$ – Shaggy Jan 26 '18 at 23:07
  • \$\begingroup\$ Fair enough, I found the spec to be super confusing. \$\endgroup\$ – gggg Jan 26 '18 at 23:17
  • \$\begingroup\$ By "single digit" I mean a digit in the corresponding base. Hence "A" (or if you prefer to express it as a 10) is a digit in a hexadecimal base. Sorry about the confusion. \$\endgroup\$ – Weijun Zhou Jan 27 '18 at 9:21
  • 1
    \$\begingroup\$ @WeijunZhou: Done \$\endgroup\$ – Shaggy Feb 9 '18 at 19:30
0
\$\begingroup\$

Java 8, 92 bytes

(b,k,s,e)->{String r="",t;for(;s<=e;r+=t.contains(k)?"":t+" ")t=b.toString(s++,b);return r;}

Input as Integer (base), String (elem), int (start), int (end).

Explanation:

Try it online.

(b,k,s,e)->{              // Method with the four parameters, and String return-type
  String r="",            //  Result-String, starting empty
         t;               //  Temp-String
  for(;s<=e;              //  Loop from start to end (inclusive)
      r+=                 //    After every iteration: append the result-String with:
         t.contains(k)?   //     If the temp-String contains the elem we exclude
          ""              //      Don't append anything to the result-String
         :                //     Else:
          t+" ")          //      Append the temp-String + a space
    t=b.toString(s++,b);  //   Convert the number to the given base and set the temp-String
  return r;}              //  Return the result-String

The three test cases are inputted like this:

16,"7",16,31
10,"3",1,100
16,"A",151,192
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.