-8
\$\begingroup\$

Challenge :

Given a string rotate every char by its index. e.g --> 'Hello' should be 'Hfnos'.


Input :

Input is given as string s length of string is 2 < s ≤ 100. Input will either be alphabet , number or one of the following whitespace , ! , @ , # , $ , % , ^ , & , * , ( , )


Output :

Output will be a string with all characters rotated by their index. Example given above.


Examples :

Input                     Output

Hello World!              Hfnos%]vzun,
Who                       Wiq
PPCG                      PQEJ

Note :

This is so shortest code in bytes in each language wins.

\$\endgroup\$
16
  • 4
    \$\begingroup\$ what happens when a character goes over ~? e.g. what's the output for ~~ (or is that not a valid input?) \$\endgroup\$
    – dzaima
    Apr 19, 2018 at 17:12
  • 2
    \$\begingroup\$ How about {|}? Is it invalid too? Please be more specific. Also, if the resulting code poing cannot go beyond 126, it's a shift rather than rotation, isn't it? \$\endgroup\$
    – Luis Mendo
    Apr 19, 2018 at 17:15
  • 2
    \$\begingroup\$ But if the length of s can be 3000 ... \$\endgroup\$
    – Shaggy
    Apr 19, 2018 at 17:16
  • 9
    \$\begingroup\$ Perhaps consider taking some more time to refine your answers and avoid confusion. The Sandbox can help you with that. \$\endgroup\$ Apr 19, 2018 at 17:24
  • 1
    \$\begingroup\$ @AsoneTuhid Stating to use the Sandbox is useless against him.. I think he's allergic to it or something.. In every single one of his challenges (except his Diamond challenge) it was suggested to use the Sandbox (see these screenshots as prove of those comments). And he only used it once thus far for about 5 hours (the yellow circled one). Muhammad, I know it can be annoying to wait 72+ hours when you have a good idea for a challenge, but you're kinda starting to piss me (and others) off by not using the Sandbox when everyone suggests it every time again.. \$\endgroup\$ Apr 20, 2018 at 7:50

3 Answers 3

1
\$\begingroup\$

Haskell, 37 36 28 bytes

zipWith($)(iterate(succ.)id)

Try it online!

How it works

iterate(succ.)id     -- starting with the identity function 'id' this
                     -- creates an infinite list of function where the next
                     -- element has one more 'succ' (the successor function)
                     -- composed to it than the current element:
                     -- -> [id, succ.id, succ.succ.id, succ.succ.succ.id, ...]
zipWith ($) (   )    -- combine the above list and the input string
                     -- elementwise with the function application operator '$'.
                     -- 1st char c0:            id $ c0 =            c0
                     -- 2nd char c1:       succ.id $ c1 =       succ c1
                     -- 3rd char c2:  succ.succ.id $ c2 = succ (succ c2)
                     -- etc. 

Edit: -8 bytes thanks to @Angs

\$\endgroup\$
2
  • 1
    \$\begingroup\$ zipWith($)(iterate(succ.)id) saves some bytes \$\endgroup\$
    – Angs
    Apr 19, 2018 at 17:30
  • 1
    \$\begingroup\$ Also 28 bytes: zipWith(\n c->[c..]!!n)[0..] \$\endgroup\$
    – Laikoni
    Apr 19, 2018 at 20:41
-1
\$\begingroup\$

Python 2, 47 bytes

lambda I:[chr(ord(c)+i)for i,c in enumerate(I)]

Try it online!

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

Japt, 2 bytes

c+

Try it out here

\$\endgroup\$
5
  • \$\begingroup\$ JS, 31 bytes - will post when/if challenge is reopened. \$\endgroup\$
    – Shaggy
    Apr 19, 2018 at 17:51
  • \$\begingroup\$ This isn't rotation. \$\endgroup\$ Apr 19, 2018 at 18:36
  • \$\begingroup\$ @PeterTaylor, what do you mean? It does exactly what the challenge asks. \$\endgroup\$
    – Shaggy
    Apr 19, 2018 at 18:45
  • \$\begingroup\$ The challenge says "rotate every char by its index". Rotation implies wrapping around. It's not clear exactly what permutation should be applied, so the question isn't really answerable at all at the moment, but this definitely isn't it. \$\endgroup\$ Apr 19, 2018 at 18:50
  • \$\begingroup\$ @PeterTaylor, nowhere in the spec does it mention wrapping around, nor is it covered in the test cases. \$\endgroup\$
    – Shaggy
    Apr 19, 2018 at 18:52

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