10
\$\begingroup\$

Challenge

We take three positive integers a, b, and c as input. Using these integers, first create a sequence in the range [0, c] (inclusive on both ends), in steps of b. For example, for a=4, b=2, c=100, the sequence would be [0,2,4,...,96,98,100].

For every number in this sequence which is divisible by a, replace it with the next letter in the lowercase alphabet, starting with the letter 'a' and wrapping back around to 'a' after you reach 'z'.

Example:

Input: a=4, b=2, c=100
Output: a2b6c10d14e18f22g26h30i34j38k42l46m50n54o58p62q66r70s74t78u82v86w90x94y98z

Challenge rules:

  • You can assume that a, b, and c are positive integers only, where b ≤ a ≤ c.
  • You can assume a is a multiple of b.
  • You can assume c is divisible by b.
  • The preferred output is a single concatenated string as above, but a list/array is acceptable as well.

Test cases:

Input: a=4, b=2, c=100
Output:
a2b6c10d14e18f22g26h30i34j38k42l46m50n54o58p62q66r70s74t78u82v86w90x94y98z

Input: a=9, b=3, c=174
Output:
a36b1215c2124d3033e3942f4851g5760h6669i7578j8487k9396l102105m111114n120123o129132p138141q147150r156159s165168t174

Input: a=10, b=2, c=50
Output:
a2468b12141618c22242628d32343638e42444648f

Input: a=25, b=1, c=25
Output:
a123456789101112131415161718192021222324b

Input: a=6, b=6, c=48
Output:
abcdefghi

Input: a=6, b=3, c=48
Output: a3b9c15d21e27f33g39h45i

Input: a=2, b=1, c=100
Output: a1b3c5d7e9f11g13h15i17j19k21l23m25n27o29p31q33r35s37t39u41v43w45x47y49z51a53b55c57d59e61f63g65h67i69j71k73l75m77n79o81p83q85r87s89t91u93v95w97x99y

I'd really like to see an answer in PHP, but this challenge is open to any language. This is , so the answer should be as short as possible. Standard rules apply for functions/programs and default loopholes are forbidden.

\$\endgroup\$
  • 1
    \$\begingroup\$ Also consider using The Sandbox to get advice and feedback on questions before posting it to main. \$\endgroup\$ – Jo King Aug 8 '18 at 9:33
  • 3
    \$\begingroup\$ Hi welcome to PPCG! Although I like the challenge itself, the description is lacking a lot of things. As mentioned by @JoKing A primary winning tag is mandatory, where [codegolf] is the most common one I would recommend. Also mentioned by JoKing, making it language specific is not recommended. It's best to open it to all languages instead. As for the challenge itself, please specify a bit more and add a few more test cases. Based on the example I can see the range is [0,c], but this should be clear without looking at the example. \$\endgroup\$ – Kevin Cruijssen Aug 8 '18 at 9:43
  • 1
    \$\begingroup\$ I think all you need to do is change the title to something more descriptive and this is good to go \$\endgroup\$ – Jo King Aug 8 '18 at 10:34
  • 1
    \$\begingroup\$ I took the liberty of fixing your challenge so it can be re-opened. Next time please use the Sandbox as advised to perfect a challenge before posting it in the main. Please take a look at what I've edited so you know this for future challenges. If anything is incorrect or I misinterpret something, feel free to edit it again. \$\endgroup\$ – Kevin Cruijssen Aug 8 '18 at 12:04
  • 5
    \$\begingroup\$ It seems none of your test-cases wrap around from 'z' to 'a'. Could you please include one that does? \$\endgroup\$ – O.O.Balance Aug 8 '18 at 13:17

26 Answers 26

8
\$\begingroup\$

PHP, 67 bytes

First of all, thanks for posting such a cool challenge! I really enjoyed solving it :) Now, here's my 67 byte solution:

<?for(;$i<=($p=$argv)[3];$i+=$p[2])echo$i%$p[1]?$i:chr($l++%26+97);

To run it:

php -n <filename> {a} {b} {c}

Or Try it online!


Here's the same solution ungolfed and with explanation comments:

$a      = $argv[1];
$b      = $argv[2];
$c      = $argv[3];
$letter = 'a';
for ($i = 0; $i <= $c; $i += $b) {
    if ($i % $a) { // If $i is divisible by $a, the modulo (%) operator will return a remainder of 0, which PHP sees as a falsey value.
        echo $i;
    } else {
        $letter++;
        $letter %= 26; // Wrap $letter around to `a` after it gets to `z`.
        echo chr($letter + 97); // Use PHP's chr function to get the character. 97 is the index of `a`. http://php.net/manual/en/function.chr.php
    }
}

I did a 60 byte solution, but it doesn't wrap around :(

<?for($l=a;$i<=($p=$argv)[3];$i+=$p[2])echo$i%$p[1]?$i:$l++;

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Japt, 15 bytes

;0ôWV £CgX/U ªX

Test it online!

Explanation

;0ôWV £CgX/U ªX    Implicit: U, V, W = inputs
;                  Reset C to the lowercase alphabet (among other variable resets).
 0ôWV              Create the inclusive range [0...W], using a step of V.
      £            Map each item X by this function:
       CgX/U         Get the character at index (X / U) in the lowercase alphabet. If X is
                     not divisible by U, this is a non-integer and the result is undefined.
             ªX      In this case (literally, if falsy), replace it with X.
                   Implicit: output result of last expression
\$\endgroup\$
  • 1
    \$\begingroup\$ 14 bytes taking input in the order c,a,b. \$\endgroup\$ – Shaggy Jan 16 at 18:00
5
\$\begingroup\$

R, 65 63 bytes

function(a,b,c,z=seq(0,c,b)){z[x]=rep(letters,sum(x<-!z%%a));z}

Try it online!

2 bytes saved thanks to JayCe!

Returns a list of strings, as R will coerce numbers to strings. To print, simply replace the trailing z with cat(z,sep="").

\$\endgroup\$
  • \$\begingroup\$ It looks like the l= is not required. TIO \$\endgroup\$ – JayCe Aug 10 '18 at 13:54
  • \$\begingroup\$ @JayCe ah of course. I put it there to prevent it from having to generate too many values, but it is un-golfy to do that! \$\endgroup\$ – Giuseppe Aug 10 '18 at 13:57
5
\$\begingroup\$

Clojure, 84 79 77 bytes

#(for[i(range 0(inc %3)%2)](if(=(mod i %1)0)(char(+(mod(/ i %1)26)(int \a)))i))

Givin' Lisp a little love

Try it online!

(-7 bytes thanks to @NikoNyrh!)

\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! Do you know what needs to be added to call the function and print the result on Try it online? \$\endgroup\$ – Laikoni Aug 8 '18 at 18:14
  • 1
    \$\begingroup\$ @Laikoni I added a "Try it online" to my post. I also corrected an error in my code, so thanks for reminding me! \$\endgroup\$ – Lispy Louie Aug 8 '18 at 19:06
  • \$\begingroup\$ Good job :) you can save 3 bytes if you make the outer function via #(...) and use fn to express the inner one. While you are at it you can replace map with for construct and save further 4 bytes. \$\endgroup\$ – NikoNyrh Aug 9 '18 at 21:59
  • \$\begingroup\$ @NikoNyrh good observation! \$\endgroup\$ – Lispy Louie Aug 9 '18 at 22:18
  • \$\begingroup\$ And %1 can be replaced by % ;) \$\endgroup\$ – NikoNyrh Aug 9 '18 at 22:47
3
\$\begingroup\$

Java 10, 93 83 bytes

(a,b,c)->{var r="";for(int i=0;i<=c;i+=b)r+=i%a<1?(char)(i/a%26+97):i+"";return r;}

Try it online here. Thanks to Scrooble for golfing 10 bytes.

Ungolfed:

(a, b, c) -> { // lambda taking 3 integer arguments and returning a String
    var r = ""; // we build the result String in steps
    for(int i = 0; i <= c; i+=b) // loop over the range [0,c] in steps of b
        r += i % a < 1 ? (char) (i / a % 26 + 97) : "" + i; // if i is a multiple of a, append the next letter to r as a char, else append i
    return r; // return the result
}
\$\endgroup\$
3
\$\begingroup\$

Perl 6,  60  50 bytes

->\a,\b,\c{[~] (0,b...c).map:{$_%a??$_!!('a'..'z')[$++%26]}}

Test it

{$^a;(0,$^b...$^c).map:{$_%$a??$_!!chr 97+$++%26}}

Test it

{  # bare block lambda with parameters $a,$b,$c

  $^a; # declare param $a, but don't use it
       # (so $a can be used in the inner block)

  (
    0, $^b ... $^c  # the base sequence

  ).map:            # for each of those values
  {
    $_ % $a         # is it not divisible by `$a` ( shorter than `%%` )

    ??  $_          # if it isn't divisible just return it

    !!              # otherwise return the following

        chr         # a character from the following number

          97 +      # 'a'.ord +
          $++       # self incrementing value (starts at 0)
          % 26      # wrap around back to 'a'
  }
}
\$\endgroup\$
  • \$\begingroup\$ You're losing a byte by using ->\a,\b,\c: use just $^a etc. And chr 97+$++%26 is quite a bit shorter than ("a".."z")[$++%26]. Finally, you can omit the concatenation because the challenge does not require it. That would save 10 bytes in total, leaving 50 \$\endgroup\$ – Ramillies Aug 8 '18 at 15:10
3
\$\begingroup\$

JavaScript (Node.js), 62 bytes

(a,b,c)=>(g=k=>k>c?'':(k%a?k:Buffer([97+x++%26]))+g(k+b))(x=0)

Try it online!

\$\endgroup\$
3
\$\begingroup\$

05AB1E, 17 15 bytes

/ݹ*εD³ÖiA¾è¼}?

-2 bytes thanks to @MagicOctopusUrn.

Takes the input in the order bca.

Try it online or verify all test cases.

Explanation:

/               # Divide the second (implicit) input `c` by the first (implicit) input `b`
                #  i.e. 2 and 100 → 50
 Ý              # Take the range [0, `divided_c`]
                #  i.e. 50 → [0,1,2,...,48,49,50]
  ¹*            # Multiply it with the first input `b`
                #  [0,1,2,...,48,49,50] → [0,2,4,...,96,98,100]
    εD          # For-each in this list:
      ³Öi       #  If the current number is divisible by the third input `a`:
                #   i.e. 8 and `a=4` → 1 (truthy)
                #   i.e. 10 and `a=4` → 0 (falsey)
         A      #   Push the lowercase alphabet
          ¾     #   Push the counter_variable (which starts at 0 by default)
           è    #   Index the alphabet with this counter_variable (wraps around by default)
         ¼      #   Increase the counter_variable by 1
            }   #  Close the if
             ?  #  Output without new-line
                #  (If the if was not entered, the input is implicitly output as else-case)
\$\endgroup\$
  • 1
    \$\begingroup\$ This seems to be working, but what language is this please. And can I break it into a php function and how? \$\endgroup\$ – Mochesane Aug 8 '18 at 10:18
  • 1
    \$\begingroup\$ The language is 05AB1E. A link to the github and is in the title of my post. And I'm afraid this language is completely different than PHP.. 05AB1E is designed for codegolfing, whereas PHP is a hypertext pre-processor for web development. I would advice to edit the challenge description and make it available for all languages, but state you prefer a PHP answer. Once it's re-opened I'm sure someone who is skilled with PHP is able to make a code-golf answer for it. If you need any help editing/fixing your challenge let me know. \$\endgroup\$ – Kevin Cruijssen Aug 8 '18 at 11:26
  • 1
    \$\begingroup\$ /ݹ*vyD³ÖiA¾è¼}? is 16, just duplicates the y instead of using ë, which ? will ignore if it pushes an alphabet char. Actually, /ݹ*εD³ÖiA¾è¼}? works for 15 even, because we are over-writing the output array then you can tie the current winner >:). \$\endgroup\$ – Magic Octopus Urn Aug 9 '18 at 16:10
  • \$\begingroup\$ @MagicOctopusUrn Thanks! :) Forgot about the implicit ë. \$\endgroup\$ – Kevin Cruijssen Aug 9 '18 at 16:52
2
\$\begingroup\$

C (gcc), 74  72 bytes

i,l;f(a,b,c){for(i=l=0;i<=c;i+=b)i%a?printf("%d",i):putchar(l++%26+97);}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 2, 80 70 bytes

Returns a list, since the OP said that is acceptable.

lambda a,b,c:map(lambda x:x if x%a else chr(x/a%26+97),range(0,c+1,b))

Very different approach from the other Python 2 answer.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

CJam, 23 bytes

q~),@/f%{0W):W26%'a+t}%

Try it online!

q~                              a b c
  ),                            a b [0…c]
    @/                          b [[0…a-1] [a…2a-1] ... […c]]
      f%                        [[0 b…a-b] [a a+b…2a-b] ... […c]]

        {0          t}%         Replace each first with:
          W):W26%'a+            ++W % 26 + 'a'

Reading input as b a c and dropping the @ would be 22 bytes (not sure if that's valid).

\$\endgroup\$
2
\$\begingroup\$

Python 2, 65 63 bytes

Returns a list, since the OP said that is acceptable.

lambda a,b,c:[[chr(n/a%26+97),n][n%a>0]for n in range(0,c+1,b)]

Try it online!


Previous versions used during the creative process (working backwards):

84 bytes

Returns a generator expression.

def f(a,b,c,x=0):
    for n in range(0,c+1,b):yield n%a and n or chr(x%26+97);x+=n%a==0

111 bytes

Uses a generator for the alphabet and returns a list.

def g(x=0):
    while 1:yield chr(x%26+97);x+=1
A=g()
f=lambda a,b,c:[n%a and n or next(A)for n in range(0,c+1,b)]
\$\endgroup\$
  • \$\begingroup\$ Ah, right. That wasn't an option when I was using next. Nice. \$\endgroup\$ – mbomb007 Aug 8 '18 at 19:03
1
\$\begingroup\$

PHP, 79 bytes

Note: Not too good at PHP

<?for(@$l=a;$i<=($p=$argv)[3];$i+=$p[2])$o.=$i%$p[1]?$i:substr($l++,-1);echo$o;

Try it online!


PHP, 84 bytes

This is just the same above code just in function format (since you wanted a PHP answer maybe this is better for you)

function f($a,$b,$c,$l=a){for(;$i<=$c;$i+=$b)$o.=$i%$a?$i:substr($l++,-1);return$o;}

Try it online!

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

Excel VBA, 76 bytes

Takes input, a,b, and c from [A1], [B1] and [C1], respectively then outputs to the VBE immediate window.

For i=0To[C1]Step[B1]:j=i Mod[A1]:?IIf(j,i,Chr(97+k)):k=(k-(j=0))Mod 26:Next
\$\endgroup\$
1
\$\begingroup\$

Python 2, 83 bytes

f=lambda a,b,c,s=0,p=0:c>=s and[chr(p%26+97),`s`][s%a>0]+f(a,b,c,s+b,p+(s%a<1))or''

Try it online!

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

Kotlin, 80 79 bytes

-1 thanks to O.O.Balance!

{a,b,c->var s="";for(x in 0..c step b)s+=if(x%a>0)x else(x/a%26+97).toChar();s}

Try it online!

My first time golfing (or doing anything else) in Kotlin! Probably can be improved.

Very similar to this Java answer, I realized after writing it. Save on the return and lose on the ternary for almost the same score.

\$\endgroup\$
  • \$\begingroup\$ It's not much, but -1 byte by omitting the space after the else. \$\endgroup\$ – O.O.Balance Aug 8 '18 at 14:45
1
\$\begingroup\$

Python3 - 111, 101, 98, 94 bytes

q=iter(map(chr,range(97,123)))
[next(q)if x%a==0else x for x in[y*b for y in range((c+b)//b)]]

Probably not the shortest, but there's room for improvement

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove a few spaces next(q)if x%a==0else x for x in[y*b \$\endgroup\$ – mbomb007 Aug 8 '18 at 15:04
  • \$\begingroup\$ @mbomb007 Good call, didn't realize you could do that. Thanks! \$\endgroup\$ – Braeden Smith Aug 8 '18 at 15:11
  • 1
    \$\begingroup\$ You may like to view Tips for golfing in Python \$\endgroup\$ – mbomb007 Aug 8 '18 at 15:13
1
\$\begingroup\$

Charcoal, 22 bytes

NθNη⭆Φ⊕N¬﹪ιη⎇﹪ιθι§β÷ιθ

Try it online! Link is to verbose version of code. Explanation:

Nθ                      Input `a`
  Nη                    Input `b`
       N                Third input (`c`)
      ⊕                 Increment
     Φ                  Filter over implicit range
           η            `b`
          ι             Current value
         ﹪              Modulo
        ¬               Not (i.e. divisible)
    ⭆                   Map over result and join
              ι         Current value
               θ        `a`
             ﹪          Modulo
            ⎇           Ternary
                ι       Current value
                    ι   Current value
                     θ  `a`
                   ÷    Divide
                  β     Predefined lowercase alphabet
                 §      Cyclically index
\$\endgroup\$
1
\$\begingroup\$

Haskell, 71 69 bytes

(a#b)c=do n<-[0,b..c];[show n,[['a'..]!!mod(div n a)26]]!!(0^mod n a)

Try it online!


Previous 71 bytes:

(a#b)c=do n<-[0,b..c];last$show n:[[['a'..]!!mod(div n a)26]|mod n a<1]

Try it online!

Explanation:

(a#b)c=                         -- given the inputs a, b and c
  do n<-[0,b..c];               -- take n from the range from 0 to c with increments of b
  last$   :[   |mod n a<1]      -- if n is not divisible by a
       show n                   -- then use n converted to a string
            [   mod(div n a)26] -- otherwise divide n by a 
             ['a'..]!!          -- and use the character at this position in the alphabet
\$\endgroup\$
1
\$\begingroup\$

CJam, 64 63 bytes (ouch)

97:Y;riri:Bri\/),[{B*}*]{X\_@\%0={Yc\Y):Y'{i={97:Y;}&}{X\}?}fX;

Try it online!

Explanation

97:Y;                                                                 Set a variable "Y" to the ASCII value of 'a' and pop the variable from the stack
     ri                                                               Read one chunk of the input (Space-delimited)
       ri:B                                                           Set a variable "B" to the next input chunk
           ri\/                                                       Divide the next input chunk by the top value in the stack (B)
               1+                                                     Add one to make the values inclusive
                 ,                                                    Turn it into an array
                   {B*}*                                              Multiply all the array values by B
                  [     ]                                             Capture the results in an array
                         {                                   }fX      Massive for loop
                          X\_@\%0=                                    If X modulo (A value) equals zero
                                  {                   }               If condition true
                                   Yc\                                Push the character with an ASCII value of Y
                                      Y):Y                            Increase the value of Y
                                          '{i=                        If Y's value is that same as that of "{" (the character after z in ASCII)
                                              {97:Y;}                 Set Y's value back to ASCII 'a'
                                                     &                If-only flag
                                                       {  }           If condition false (from X\_@\%0=)
                                                        X\            Push X onto the stack
                                                           ?          If-else flag
                                                               ;      Pop A value from the stack

This could definitely be made better so feel free to join in!


Changes

Helen cut off a byte!

Old: 97:Y;riri:Bri\/1+,[{B*}*]{X\_@\%0={Yc\Y):Y'{i={97:Y;}&}{X\}?}fX;
New: 97:Y;riri:Bri\/),[{B*}*]{X\_@\%0={Yc\Y):Y'{i={97:Y;}&}{X\}?}fX;

By changing 1+ to ) we can cut off a byte.

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

Jelly, 16 bytes

Żmọ⁵œpƊżJṖØaṁƲ$F

A full program taking arguments c b a which prints the result.

Try it online!

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

Clojure, 117 bytes

#(loop[R""A 0 i 0](if(> i %3)R(let[?(=(mod i %)0)](recur(str R(if ?(char(+(mod A 26)97))i))(if ?(inc A)A)(+ i %2)))))

Such imperative. :/

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

C (gcc/clang), 80 68 bytes

f(a,b,c,i){for(i=0;i<=c;i+=b)i%a?printf("%d",i):putchar(i/a%26+97);}

Port of my Java answer. Try it online here.

\$\endgroup\$
  • \$\begingroup\$ Suggest printf(L"搥挥"+!(i%a),i%a?i:i/a%26+97) instead of i%a?printf("%d",i):putchar(i/a%26+97) \$\endgroup\$ – ceilingcat Sep 30 '18 at 22:31
0
\$\begingroup\$

Jelly, 14 bytes

Żmµ⁵ḍaÄ$aị¥Øao

Try it online!

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

Python 2 and 3 - 123 128 Bytes

d=-1
lambda a,b,c:[s()if i%a<1else i for i in range(0,c+1,b)]
def s():global d;d=(d+1)%26;return'abcdefghijklmnopqrstuvwxyz'[d]

You'll need to put f= in front of the lambda and then call it with f(a,b,c) but I'm pretty sure that's allowed.

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

Ruby, 59 58 55 bytes

->a,b,c{[w=?a]+b.step(c,b).map{|x|x%a>0?x:w=w.next[0]}}

Try it online!

Returns a list

\$\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.