8
\$\begingroup\$

This is my first code golf exercise for you: Your input is two integers n and k. You can assume n >= 1 and k >= 1 You have to output all numbers from 1 to n (inclusively) and replace every element that is a multiple of k or that contains k with "Shaggy".

you can take the input in any convenient form and output it in a human readable form (seperators can be whitespaces, commas or whatever)

Standard loop holes are forbidden. Functions or programs are acceptable. Shortest code in byte wins!

This is different from the "Bzzt"-game, as this takes a variable "Bzzt" parameter, which, for values>=10, may increase the difficulty in some languages.

Examples:

Input: 20 6  
Output: 1 2 3 4 5 Shaggy 7 8 9 10 11 Shaggy 13 14 15 Shaggy 17 Shaggy 19 20  

Input: 21 4  
Output: 1 2 3 Shaggy 5 6 7 Shaggy 9 10 11 Shaggy 13 Shaggy 15 Shaggy 17 18 19 Shaggy 21

Input: 212 11  
Output: 1 2 3 4 5 6 7 8 9 10 Shaggy 12 13 14 15 16 17 18 19 20 21 Shaggy 23 24 25 26 27 28 29 30 31 32 Shaggy 34 35 36 37 38 39 40 41 42 43 Shaggy 45 46 47 48 49 50 51 52 53 54 Shaggy 56 57 58 59 60 61 62 63 64 65 Shaggy 67 68 69 70 71 72 73 74 75 76 Shaggy 78 79 80 81 82 83 84 85 86 87 Shaggy 89 90 91 92 93 94 95 96 97 98 Shaggy 100 101 102 103 104 105 106 107 108 109 Shaggy Shaggy Shaggy Shaggy Shaggy Shaggy Shaggy Shaggy Shaggy Shaggy 120 Shaggy 122 123 124 125 126 127 128 129 130 131 Shaggy 133 134 135 136 137 138 139 140 141 142 Shaggy 144 145 146 147 148 149 150 151 152 153 Shaggy 155 156 157 158 159 160 161 162 163 164 Shaggy 166 167 168 169 170 171 172 173 174 175 Shaggy 177 178 179 180 181 182 183 184 185 186 Shaggy 188 189 190 191 192 193 194 195 196 197 Shaggy 199 200 201 202 203 204 205 206 207 208 Shaggy 210 Shaggy 212
\$\endgroup\$
  • \$\begingroup\$ must it be a full program or may it be a function? and does "1 to n" include both endpoints? \$\endgroup\$ – FlipTack Nov 3 '16 at 22:08
  • \$\begingroup\$ full program or function. 1 to n is inclusively. \$\endgroup\$ – infinitezero Nov 3 '16 at 22:11
  • \$\begingroup\$ Clear spec, but a bit simple (which is where the downvotes are coming from). Regardless, welcome to the site! \$\endgroup\$ – Nathan Merrill Nov 3 '16 at 22:12
  • \$\begingroup\$ Thanks. Too me, it was not so simple to determine if a number like 1621 contains the forbidden number "6". At least not very golfy. \$\endgroup\$ – infinitezero Nov 3 '16 at 22:13
  • 2
    \$\begingroup\$ Hi @infinitezero and welcome. You may want to try posting in the sandbox first next time to get some feedback on your challenge. \$\endgroup\$ – ElPedro Nov 3 '16 at 22:15
8
\$\begingroup\$

Python 2, 64 63 bytes

Anonymous function with arguments n and k, returns the list to output.

lambda n,k:[[i,'Shaggy'][i%k<1or`k`in`i`]for i in range(1,n+1)]

Alternatively, a function which prints to STDOUT for the same bytecount:

def f(n,k,i=1):exec"print[i,'Shaggy'][i%k<1or`k`in`i`];i+=1;"*n
\$\endgroup\$
  • 2
    \$\begingroup\$ Just spent 15 minutes fighting with this one in Python but I think I was missing the obvious. Nice answer. \$\endgroup\$ – ElPedro Nov 3 '16 at 22:48
  • 1
    \$\begingroup\$ sometimes, the shortest solutions are the simplest :) but I liked your recursive solution too @ElPedro \$\endgroup\$ – FlipTack Nov 4 '16 at 16:23
  • \$\begingroup\$ Thanks. Shame the input was specified as two integers otherwise I could have used def f(a,b,c) and called it using f(a,b,1) and only been 8 bytes behind you ;-) \$\endgroup\$ – ElPedro Nov 4 '16 at 16:42
3
\$\begingroup\$

Pyth, 25 23 bytes

m?|!%dQ}`Q`d"Shaggy"dSE

A program that takes input of two newline-separated integers k, n and prints a list.

Try it online!

How it works

m?|!%dQ}`Q`d"Shaggy"dSE  Program. Inputs: Q, E
m                    SE  Map over [1, 2, 3, 4, ..., E] with varaible d:
 ?                         If
    %dQ                    d % Q
   !                       negated is truthy (divisible by H)
  |                        or
        `Q                 str(Q)
       }                   is in 
          `d               str(d):
            "Shaggy"         Yield "Shaggy"
                           Else:
                    d        Yield d
                           Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Lua, 71 70 Bytes

Saved a byte thanks to Flp.Tkc

Getting in before Jelly or Pyth or similar can swoop in and destroy my byte count.

a,b=...for i=1,a do print(((i..""):find(b)or i%b<1)and"Shaggy"or i)end

Ungolfed with comments

local a,b = ...                         -- Command Line Input
for i=1, a do                           -- For all numbers between 1 and a
    print(                              -- Print with newlines...
            ((i..""):find(b) or i%b<1)  -- if the String version of i contains b, or if the remainder when i is divided by b == 0
            and "Shaggy"                -- then Shaggy
            or i                        -- Else i
        )
end
\$\endgroup\$
  • \$\begingroup\$ could you use i%b<1 to save a byte? \$\endgroup\$ – FlipTack Nov 3 '16 at 22:39
  • \$\begingroup\$ I could use i%b<1, which I will do. Thanks. (<0 will always return false) \$\endgroup\$ – ATaco Nov 3 '16 at 22:39
  • \$\begingroup\$ haha sorry i meant to put a 1 there \$\endgroup\$ – FlipTack Nov 3 '16 at 22:40
2
\$\begingroup\$

SQL (PostgreSQL flavour), 130 136 bytes

Done as a prepared statement:

prepare p(int,int)as select CASE WHEN i%$2=0or i::char like'%'||$2||'%'THEN'Shaggy'else i::char end from generate_series(1,$1)a(i)

This is executed in the following manner

execute p(20,6)

and the result is output as rows.

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

Python 2, 74 72 bytes

Having already been soundly beaten by @Flp.Tkc I'll just post my 74 72 byte recursive solution because it's different and it works and not because it will win (and also because I like recursive functions).

def f(a,b,c=1):
 print(c,"Shaggy")[c%b<1or`b`in`c`]
 if c<a:f(a,b,c+1)

Edit

-2 by removing the unneeded `` around the c in the print statement

From comments:

Call using

 f(a,b)

c will default to 1 due to the

 c=1

in the function definition and can then be incremented for the recursive function calls.

\$\endgroup\$
  • \$\begingroup\$ Am I confused, or do you never need to assign to c? \$\endgroup\$ – nedla2004 Nov 3 '16 at 23:27
  • \$\begingroup\$ The function call is f(a,b). c defaults to 1 because of the c=1 in the function definition. That means I can use c in the first run without having to add it to the function call (Python functions accept optional keyword args). I can then increment it on the recursive function calls and its value on the recall overrides the default. Hope that makes it clear. Took me a while to get to grips with it :) \$\endgroup\$ – ElPedro Nov 3 '16 at 23:32
  • \$\begingroup\$ Basically it means that c starts with a default value of 1 and I just add 1 to it every time I call the function while c<a \$\endgroup\$ – ElPedro Nov 3 '16 at 23:36
  • \$\begingroup\$ My pleasure. Always happy to (try to) explain :) \$\endgroup\$ – ElPedro Nov 3 '16 at 23:54
  • \$\begingroup\$ Explanation added to my answer for info. \$\endgroup\$ – ElPedro Nov 4 '16 at 0:35
0
\$\begingroup\$

C, 122 bytes

f(n,k){int m,i;for(i=1;i<=n;++i){if(i%k){for(m=i;m>0;m/=10)if(m%10==k)goto l;printf("%d ",i);continue;}l:puts("Shaggy");}}

Newlines and spaces both act as separators.

\$\endgroup\$
0
\$\begingroup\$

C#, 97 bytes

n=>k=>{var r="";for(int i=1;i<=n;i++)r+=i%k>0&&(i+"").IndexOf(k+"")<0?i+" ":"Shaggy ";return r;};

Full program with ungolfed method and test cases:

using System;

namespace Shaggy
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int,Func<int,string>>f= n=>k=>
            {
                var r = "";
                for(int i = 1; i <=n ; i++)
                    r += i % k > 0 && (i + "").IndexOf(k + "") < 0 ? i + " " : "Shaggy ";
                return r;
            };

            Console.WriteLine(f(20)(6));
            Console.WriteLine(f(21)(4));
            Console.WriteLine(f(212)(11));
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ Hi, you could golf it to this: n=>k=>{var r="";for(int i=0;++i<=n;)r+=i%k<1|(i+"").Contains(k+"")?"Shaggy ":i+" ";return r;}; (94 bytes) \$\endgroup\$ – Kevin Cruijssen Nov 4 '16 at 8:42
0
\$\begingroup\$

C, 98 96 bytes

n,h[],t[];s(i,j){for(;n<=i;n&&puts(!strstr(t,h)&&n%j?t:"Shaggy"),n++)sprintf(n?t:h,"%d",n?n:j);}

Wandbox

\$\endgroup\$

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