39
\$\begingroup\$

Your challenge is to write a program or function that, when given two strings of equal length, swaps every other character and outputs/returns the resulting strings in either order.

Examples

"Hello," "world!" --> "Hollo!" "werld,"
"code" "golf" --> "codf" "gole"
"happy" "angry" --> "hnpry" "aagpy"
"qwerty" "dvorak" --> "qvertk" "dworay"
"1, 2, 3" "a, b, c" --> "1, b, 3" "a, 2, c"
"3.141592653589" "2.718281828459" --> "3.111291623489" "2.748582858559"
"DJMcMayhem" "trichoplax" --> "DrMcMoylex" "tJichapham"
"Doorknob" "Downgoat" --> "Doonkoot" "Dowrgnab"
"Halloween" "Challenge" --> "Hhlloeegn" "Caallwnee"

Rules

  • The strings will only contain ASCII chars (32-126).
  • The strings will always be the same length, and will never be empty.
  • You may accept input in any suitable format: separate parameters, items in an array, separated by one or more newlines, even concatenated. The only restriction is that one string must come fully before the other (e.g. a1\nb2\nc3 for "abc", "123" is invalid).
  • The output may be in either order (i.e. you can start swapping from the first or the second char), and in any valid format mentioned above. (2-item array, separated by newline(s), concatenated, etc.)

Scoring

This is , so the shortest code in bytes for each language wins.

\$\endgroup\$
4
  • 13
    \$\begingroup\$ +1 for DrMcMoylex. :D \$\endgroup\$
    – DJMcMayhem
    Oct 31, 2016 at 20:45
  • 4
    \$\begingroup\$ "Dowrgnab" anagrams to "Downgrab" ( ͡° ͜ʖ ͡°) \$\endgroup\$ Nov 1, 2016 at 3:44
  • \$\begingroup\$ You should point out explicitly that the "in either order" rule means swapping can begin from the first character or the second. \$\endgroup\$
    – DLosc
    Nov 2, 2016 at 5:08
  • \$\begingroup\$ @DrMcMoylex Take the code, golf example. If we swap starting from second character, we get: codf, gole. Starting from first character: gole, codf. \$\endgroup\$
    – DLosc
    Nov 3, 2016 at 5:17

36 Answers 36

1
2
0
\$\begingroup\$

Java, 68 bytes

(a,b)->{for(int i=a.length;--i>0;){char t=a[--i];a[i]=b[i];b[i]=t;}}

Ungolfed and testing

import java.util.Arrays;
import java.util.Collection;
import java.util.function.BiConsumer;

public class Main {

  static BiConsumer<char[], char[]> func = (left, right) -> {
      for (int i = left.length; --i > 0;) {
        char temp = left[--i];
        left[i] = right[i];
        right[i] = temp;
      }
    };

  public static void main(String[] args) {
    test("Hello,","world!", "Hollo!", "werld,");
    test("code", "golf", "codf", "gole");
    test("happy", "angry", "hnpry", "aagpy");
  }

  private static void test(String left, String right, String x, String y) {
    char[] leftChars = left.toCharArray();
    char[] rightChars = right.toCharArray();
    func.accept(leftChars, rightChars);
    Collection mixed = Arrays.asList(new String(leftChars), new String(rightChars));
    if (mixed.containsAll(Arrays.asList(x, y))) {
      System.out.println("OK");
    } else {
      System.out.printf("NOK: %s, %s -> %s%n", left, right, mixed);
    }
  }
}
\$\endgroup\$
0
\$\begingroup\$

APL, 12

{↓(⍳⍴⊃⍵)⊖↑⍵}

Explanation: {...} defines a function, ⍵ is the right argument. The take (↑) creates a matrix out of the two strings, then rotates each column (⊖) n times, where n is the part in parenthesis (⍳⍴⊃⍵). That's defined as the iota of the length of the first argument. (Ex: length=5 ==> 1 2 3 4 5). So first column is rotated once, second twice (getting back to original positions), third column three times, etc...

Try it at tryapl.org

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

Scala, 85 bytes

(_:String)zip(_:String)zip(Stream from 0)map{case(t,z)=>if(z%2==0)t else t.swap}unzip

take an anonymous string parameter, zip it with another string parameter, zip it with an stream of natural numbers, swap every second tuple of chars and unzip to get a tuple of sequences of chars

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

Swift 3, 129 107 bytes

func c(n:inout[String],b:inout[String]){
for(j,c)in n.enumerated(){
if j&1<1{n[j]=b[j];b[j]=c}}
print(n,b)}

Original

The function receives two arrays of strings as parameters assuming both have same length, as follows:

c(n:["c", "o", "d", "e"],b:["g", "o", "l", "f"])

Produces following output

codf gole

Edit 1:

Get rid of the internal tuple and change the parameters of the function to be inout. Instead of string concatenation manipulate the parameters directly.

Example input:

var a:Array<String> = ["c", "o", "d", "e"]
var b:Array<String> = ["g", "o", "l", "f"]

c(n:&a,b:&b)

Ouput changed to be array of strings as in:

["g", "o", "l", "e"] ["c", "o", "d", "f"]

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

Husk, 7 bytes

Tz*İ_ze

Try it online!

Outputs are reversed.

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

Ly, 11 bytes

ir>ir[<o>o]

Try it online!

This takes the two strings on separate input lines, and outputs the interlaced strings concatenated together. At a high level... It reads the two strings onto separate stacks, then just loops printing the top the each stack.

ir          - read first string onto the stack, reverse it
  >ir       - switch to new stack, read second string and reverse it
     [    ] - loop until the stack is empty
      <o    - switch to first stack and print a char
        >o  - switch to the second stack and print a char
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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