24
\$\begingroup\$

Introduction

Let's observe the following string:

ABCDEFGHIJKLMNOP

If we swap the ends of the string, which are these:

ABCDEFGHIJKLMNOP
^^            ^^

We get the following result:

BACDEFGHIJKLMNPO

After that, we delete the ends of the string, which in this case are B and O. The result is:

ACDEFGHIJKLMNP

If we repeat the process, we get the following list:

N     Result

2     ADEFGHIJKLMP
3     AEFGHIJKLP
4     AFGHIJKP
5     AGHIJP
6     AHIP
7     AP

You can see that for N = 5, the result is AGHIJP. At N = 7, the length of the string is smaller than 3, so N > 7 is considered invalid in this case.

The Task

Given a string S with at least length 4 and the number of repetitions N > 0, output the final result. You can assume that N is always valid.

Test cases

Input                               >  Output

N = 3, S = ABCDEFGHIJKLMNOP         >  AEFGHIJKLP
N = 1, S = Hello                    >  Hlo
N = 2, S = 123321                   >  11

This is , so the submission with the least amount of bytes wins! For the simplicity, you can assume that the string will only contain alphanumeric characters.

\$\endgroup\$
  • \$\begingroup\$ Question: Is it okay to take N in unary with something like ' as the counting character? For example: ''123321? \$\endgroup\$ – daavko Feb 19 '16 at 13:24
  • \$\begingroup\$ @daavko Yes, unary is acceptable by default \$\endgroup\$ – Adnan Feb 19 '16 at 13:39
  • \$\begingroup\$ @Adnan Unary format can be used for N, but can it be a string, with quotes? I mean, for N=3 take '111' (as opposed to 111) \$\endgroup\$ – Luis Mendo Feb 19 '16 at 15:21
  • \$\begingroup\$ @LuisMendo Yes, you may use that \$\endgroup\$ – Adnan Feb 19 '16 at 15:23
  • \$\begingroup\$ It looks to me like we skip 1 and remove N - is this allowed as an answer or does the code need to swap delete and repeat? \$\endgroup\$ – Alex Carlsen Feb 22 '16 at 13:14

33 Answers 33

1
\$\begingroup\$

bash + GNU coreutils, 93 83 Bytes

s=$2
for i in `seq $1`;do
s=`sed 's/\(.\).\(.*\).\(.\)/\1\2\3/'<<<$s`
done
echo $s

I wanted to find an answer in pure sed, but I am not sure if counters are possible this way...

Invocation:

./testgolf.sh 3 ABCDEFGHIJKLMNOP

Explanation:

s=$2  # Store second parameter into variable
for i in `seq $1`;do   # loop number of first parameter times...
s=`sed 's/\(.\).\(.*\).\(.\)/\1\2\3/'<<<$s`
#         +-1-+ +-2--+ +-3-+
# sed splits the string into 3 placeholders
# to create the new string by use of substitution.
# s=$`...` stores the output into the variable.
# <<<$s sends the content of the variable to stdin of sed.
done
echo $s  # output content of the variable now.
\$\endgroup\$
  • \$\begingroup\$ Welcome to Programming Puzzles and Code Golf. This is a great first answer, however I think it could be improved by adding a code breakdown and explanation. \$\endgroup\$ – wizzwizz4 Feb 21 '16 at 21:06
1
\$\begingroup\$

PHP 76 Bytes

while($n>=0){$s=substr($s,0,1).substr($s,2,-2).substr($s,-1,1);$n--;}echo$s;

Explanation is one word, substr!

I assumed $n and $s are inputs. Correct me if this is incorrect, or if <?php is required.

Long hand

<?php

$n = 3;
$s = 'ABCDEFGHIJKLMNOP';

while ($n >= 0) {
    $s = substr($s, 0, 1) . substr($s, 2, -2) . substr($s, -1, 1);
    $n--;
}
echo $s;
\$\endgroup\$
  • \$\begingroup\$ You should make it a function. Also, it might be shorter to remove the loop. \$\endgroup\$ – CalculatorFeline Feb 28 '16 at 4:50
  • \$\begingroup\$ I tried my best at making it a recursive function just now, but I ended up with many more bytes. \$\endgroup\$ – Goose Feb 29 '16 at 14:06
0
\$\begingroup\$

Python, 78 59 bytes

Golfed

def s(t,n):return t if n==0 else s(t[0]+t[2:-2]+t[-1],n-1)

Try it here!

Ungolfed

def swapDeleteRepeat (text, n):
    if n == 0:
        return text
    else:
        return swapDeleteRepeat (text[0] + text[2:-2] + text[-1], n - 1)
\$\endgroup\$
  • \$\begingroup\$ This is exactly the same idea as in @DenkerAffe's answer, with a longer function declaration and conditional. \$\endgroup\$ – Dennis Mar 3 '16 at 19:33

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.