A function to take three integers and return a list of integers and alphabet-letters

10

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.

Mochesane

Posted 2018-08-08T09:29:49.353

Reputation: 119

1

Also consider using The Sandbox to get advice and feedback on questions before posting it to main.

– Jo King – 2018-08-08T09:33:39.593

3

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.

– Kevin Cruijssen – 2018-08-08T09:43:04.970

1I think all you need to do is change the title to something more descriptive and this is good to go – Jo King – 2018-08-08T10:34:04.947

Is it necessary to output a string where all the numbers/letters are concatenated or can we also return a list? – wastl – 2018-08-08T10:50:56.867

I prefer concatenation of numbers and letters but a list is also ok – Mochesane – 2018-08-08T10:53:09.953

1I 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. – Kevin Cruijssen – 2018-08-08T12:04:41.550

1Wow thank you @KevinCruijssen , this is really it. Thank you very much and next time I will use the sandbox as advised – Mochesane – 2018-08-08T12:22:35.300

1@labmann You're welcome. Now all we have to do is wait for re-open votes (you've already got 3 and need 5), and then the answer (hopefully including a PHP one) will come in. PS: You're lucky I had the time to edit your challenge. ;) And that it's a good challenge to begin with, otherwise I wouldn't have bothered. Enjoy your stay, and I indeed hope to see you use the Sandbox to perfect challenges before posting them here next time. – Kevin Cruijssen – 2018-08-08T12:26:10.333

5It seems none of your test-cases wrap around from 'z' to 'a'. Could you please include one that does? – O.O.Balance – 2018-08-08T13:17:34.197

Answers

8

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!

Ethan

Posted 2018-08-08T09:29:49.353

Reputation: 435

6

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

ETHproductions

Posted 2018-08-08T09:29:49.353

Reputation: 47 880

114 bytes taking input in the order c,a,b. – Shaggy – 2019-01-16T18:00:35.050

5

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="").

Giuseppe

Posted 2018-08-08T09:29:49.353

Reputation: 21 077

It looks like the l= is not required. TIO

– JayCe – 2018-08-10T13:54:30.747

@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! – Giuseppe – 2018-08-10T13:57:16.570

5

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!)

Lispy Louie

Posted 2018-08-08T09:29:49.353

Reputation: 289

Welcome to PPCG! Do you know what needs to be added to call the function and print the result on Try it online?

– Laikoni – 2018-08-08T18:14:47.383

1@Laikoni I added a "Try it online" to my post. I also corrected an error in my code, so thanks for reminding me! – Lispy Louie – 2018-08-08T19:06:31.667

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. – NikoNyrh – 2018-08-09T21:59:59.890

@NikoNyrh good observation! – Lispy Louie – 2018-08-09T22:18:13.477

And %1 can be replaced by % ;) – NikoNyrh – 2018-08-09T22:47:29.093

3

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)

Kevin Cruijssen

Posted 2018-08-08T09:29:49.353

Reputation: 67 575

1This seems to be working, but what language is this please. And can I break it into a php function and how? – Mochesane – 2018-08-08T10:18:35.370

1The 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. – Kevin Cruijssen – 2018-08-08T11:26:53.137

1/ݹ*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 >:). – Magic Octopus Urn – 2018-08-09T16:10:16.183

@MagicOctopusUrn Thanks! :) Forgot about the implicit ë. – Kevin Cruijssen – 2018-08-09T16:52:42.143

3

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!

Arnauld

Posted 2018-08-08T09:29:49.353

Reputation: 111 334

3

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
}

O.O.Balance

Posted 2018-08-08T09:29:49.353

Reputation: 1 499

3

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'
  }
}

Brad Gilbert b2gills

Posted 2018-08-08T09:29:49.353

Reputation: 12 713

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

– Ramillies – 2018-08-08T15:10:42.753

2

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!

vazt

Posted 2018-08-08T09:29:49.353

Reputation: 311

2

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!

Khuldraeseth na'Barya

Posted 2018-08-08T09:29:49.353

Reputation: 2 608

2

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)]

mbomb007

Posted 2018-08-08T09:29:49.353

Reputation: 21 944

Ah, right. That wasn't an option when I was using next. Nice. – mbomb007 – 2018-08-08T19:03:20.653

2

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).

Lynn

Posted 2018-08-08T09:29:49.353

Reputation: 55 648

1

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!

Luis felipe De jesus Munoz

Posted 2018-08-08T09:29:49.353

Reputation: 9 639

1

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

Taylor Scott

Posted 2018-08-08T09:29:49.353

Reputation: 6 709

1

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!

ovs

Posted 2018-08-08T09:29:49.353

Reputation: 21 408

1

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.

Khuldraeseth na'Barya

Posted 2018-08-08T09:29:49.353

Reputation: 2 608

It's not much, but -1 byte by omitting the space after the else. – O.O.Balance – 2018-08-08T14:45:54.597

1

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!

Braeden Smith

Posted 2018-08-08T09:29:49.353

Reputation: 61

1You can remove a few spaces next(q)if x%a==0else x for x in[y*b – mbomb007 – 2018-08-08T15:04:21.920

@mbomb007 Good call, didn't realize you could do that. Thanks! – Braeden Smith – 2018-08-08T15:11:42.073

1

You may like to view Tips for golfing in Python

– mbomb007 – 2018-08-08T15:13:41.850

1

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.

Helen

Posted 2018-08-08T09:29:49.353

Reputation: 125

1

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

Laikoni

Posted 2018-08-08T09:29:49.353

Reputation: 23 676

1

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

Neil

Posted 2018-08-08T09:29:49.353

Reputation: 95 035

0

Jelly, 16 bytes

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

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

Try it online!

Jonathan Allan

Posted 2018-08-08T09:29:49.353

Reputation: 67 804

0

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.

O.O.Balance

Posted 2018-08-08T09:29:49.353

Reputation: 1 499

Suggest printf(L"搥挥"+!(i%a),i%a?i:i/a%26+97) instead of i%a?printf("%d",i):putchar(i/a%26+97) – ceilingcat – 2018-09-30T22:31:20.003

0

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. :/

NikoNyrh

Posted 2018-08-08T09:29:49.353

Reputation: 2 361

0

Jelly, 14 bytes

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

Try it online!

Erik the Outgolfer

Posted 2018-08-08T09:29:49.353

Reputation: 38 134

0

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.

sonrad10

Posted 2018-08-08T09:29:49.353

Reputation: 535

0

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

G B

Posted 2018-08-08T09:29:49.353

Reputation: 11 099