16
\$\begingroup\$

Given a set of two strings guaranteed to be the same length, print their criss cross.

The criss cross of two strings is obtained as follows.

  1. Yield the second character of the second string, then the first character of the first string.
  2. Yield the first character of the second string, then the second character of the first string.
  3. Discard the first character of each string.
  4. If the strings have more than one character each, go back to step 1.

For example, if the two strings are

Truck
Tower

the criss cross is

oTTrwroueuwcrcek

as illustrated in the following diagram.

diagram

Each color represents a different iteration of criss-crossing. The numbers show the corresponding character's indices in the output.

\$\endgroup\$
2
  • \$\begingroup\$ You shouldn't accept an answer until at the very least around a week has passed, it might be beaten at any time. \$\endgroup\$
    – Pavel
    Jul 2 '17 at 6:26
  • \$\begingroup\$ @Phoenix okay I'll keep that in mind next time (this was my first question on this stackexchange) \$\endgroup\$
    – K Split X
    Jul 2 '17 at 19:52

11 Answers 11

8
\$\begingroup\$

JavaScript (ES6), 51 bytes

f=([G,...O],[L,...F])=>O[0]?F[0]+G+L+O[0]+f(O,F):''

f=([G,...O],[L,...F])=>O[0]?F[0]+G+L+O[0]+f(O,F):''

console.log(f("Truck", "Tower"))

\$\endgroup\$
7
\$\begingroup\$

Jelly, 10 8 bytes

żṚj@¥2\U

Try it online!

How it works

żṚj@¥2\U  Main link. Arguments: s, t (strings)

          Arguments:    "Truck", Tower"
ż         Ziphwith; create all pairs of corresponding characters.
          Return value: ["TT", "ro", "uw", "ce", "kr"].
     2\   Reduce each pair of adjacent strings by the quicklink to the left.
    ¥       Combine the two links to the left into a dyadic chain.
 Ṛ            Reverse the left string.
  j@          Join the second string, using the previous result as separator.
          Map:          "TT", "ro" -> join("ro", "TT") -> "rTTo"
                        "ro", "uw" -> join("uw", "or") -> "uorw"
                        etc.
          Return value: ["rTTo", "uorw", "cwue", "kecr"]
       U  Upend; reverse each string.
          Return value: ["oTTr", "wrou", "euwc", "rcek"]
          (implicit) Flatten and print.
\$\endgroup\$
3
  • \$\begingroup\$ Wow thats pretty amazing \$\endgroup\$
    – K Split X
    Jun 30 '17 at 22:53
  • \$\begingroup\$ Explanation, please? \$\endgroup\$
    – Shaggy
    Jun 30 '17 at 23:24
  • \$\begingroup\$ @Shaggy I've edited my answer. \$\endgroup\$
    – Dennis
    Jun 30 '17 at 23:38
4
\$\begingroup\$

Python 3, 56 bytes

f=lambda s,t:s[1:]and t[1]+s[0]+t[0]+s[1]+f(s[1:],t[1:])

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 44 38 bytes

Crossed out 44 is still 44

[_]#_=""
(a:b)#(x:y)=y!!0:a:x:b!!0:b#y

Slightly less golfed / maybe a little more readable:

[_]      # [_]      = ""
(a:b:bs) # (x:y:ys) = y:a:x:b:((b:bs) # (y:ys))
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Fun Fact: On my browser at least, crossed out 44 is no longer 44 in the header :o \$\endgroup\$
    – hyper-neutrino
    Jun 30 '17 at 23:42
3
\$\begingroup\$

PowerShell, 71 bytes

param($a,$b)-join($a|% t*y|%{$b[+$i],$a[$i-1],$b[$i-1],$a[+$i]*!!$i++})

Try it online!

Less golfed:

param($a,$b)
-join(
    $a|foreach ToCharArray|foreach{  # transform each string from $a to an array and apply for each char...
        $CrissCross = $b[+$i],$a[$i-1],$b[$i-1],$a[+$i]
        $NotFirst   = !!$i++         # =0 if $i is 0; =1 if $i is not 0
        $CrissCross*$NotFirst        # repeat the $CrissCross array $NotFirst times
    }                                # and implicitly output
) # join all outputed chars to one string and implicitly output it as result
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Whaaaaaaat? In 71 bytes???????????? \$\endgroup\$
    – wasif
    Mar 26 at 15:37
2
\$\begingroup\$

PHP>=7.1, 64 bytes

for([,$a,$b]=$argv;$c=$b[++$i];)echo$c,$a[$i-1],$b[$i-1],$a[$i];

PHP Sandbox Online

\$\endgroup\$
2
+300
\$\begingroup\$

PowerShell, 203 bytes

param($a,$b)$s=$k=@()
function q($x){$u=$x|% t*y;($u|%{$u[--$q]})-join''}
$a|% t*y|%{$s+=$_+$b[($i++)]}
$n=1;$s|%{$k+="$($s[$n][0])$(q($s[$n-1]))$($s[$n][1])";$n++}
($k[0..($k.length-2)]|%{q($_)})-join''

Try it online!

golfed, used negative indexing iteration for reversing array

PowerShell, 210 bytes

param($a,$b)$s=$k=@()
function q($x){$u=$x|% t*y;[array]::reverse($u);$u-join''}
$a|% t*y|%{$s+=$_+$b[($i++)]}
$n=1;$s|%{$k+="$($s[$n][0])$(q($s[$n-1]))$($s[$n][1])";$n++}
($k[0..($k.length-2)]|%{q($_)})-join''

Try it online!

To Criss-Cross the string, I use the following algorithm:

  • Take two strings: Truck and Tower
  • Zip them and form a new array. Two zip them means to pair each character in the string with another, so it results an array with these 5 elements: TT ro uw ce kr
  • Iterate the array in 0,1 1,2 2,3 pairs. So it results a new array in TT ro ro uw uw ce ce kr form.
  • Iterate the array again and swap the elements in each iteration and reverse the new second element. So it results ro TT uw or ce wu kr ec
  • Reverse each element in the array again and join them oTTrwroueuwcrcek!

Code explanation

  • First take two parameters $a and $b, it is done using param() block
  • Then define two empty arrays $s and $k, in Powershell Empty array is @()
  • Then define a function q to reverse strings (I wonder why there is no string .reverse() method on .NET), First it takes a string $x, then defines $u as the character array of $x (t*y is the shorthand for ToCharArray() .NET string method, Powrshell can automatically expand it using Foreach loop), then reverse the $u array in-place using the reverse() static method of [array] type (:: is used to invoke a static method), and at last, join the $u array with seperator '' meaning nothing (-join is an operator which does that), which result in the reversed string
  • Then again iterate over the character array of first string ($a, I have said in the previous step how character array works), and build a string with the each element of array ($_ represents that, it is an automatic variable which contains the iterated element in an object supplied from pipeline) and $i++th index of $b string (You can see that I haven't defined $i anywhere, because it is not needed, in powershell non-declared variables are automatically assigned to 0, ++ operator increments a variable, and surrounding parentheses around this increments and then outputs the variable, no need to explicitly increment and use latter), and each builded string is added to the $s array through the assignment operator += (Does the 2nd step in algorithm, no builtin Zip function in powershell)
  • Then I declare another variable $n to value of 1 (a semicolon ; is used to seperate statements and fit in a single line), iterate over the $s array, and build a string with the first character in $nth indexed element of the $s array, then $n-1th indexed element in the $s array and then the second (In fact the last, but negative indexing with -1 would cost one more precious byte) character in $nth indexed element of the $s array and append to $k array using += assignment operator, and increment $n variable using ++ operator at end (Does the 3rd and 4th step in algorithm, In python and some other languages it would be easier using enumeration and slicing)
  • Then I iterate in the $k array with the last element removed (In powershell .NET .RemoveAt() method for removing array elements using index don't work for normal arrays), to do this I used indexing, first started index from 0 and ended 2 subtracted from array length, and .. range operator is used to multiple index here, and I then apply q() function (reverse, that I built earlier), and parenthesise the whole expression to form an array, and then join it with '' seperator using -join operator, and the string is implicitly outputted (Does the last step in algorithm, .NET has severe lack of builtins, at least it should have a map function!)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Wasif, please read the Tips for golfing in PowerShell and use it! For example, Reversing an array and other tips. \$\endgroup\$
    – mazzy
    Mar 26 at 14:35
  • 1
    \$\begingroup\$ @mazzy thanks for the tips, I have golfed the solution more, I will add it to the post soon \$\endgroup\$
    – wasif
    Mar 26 at 15:59
  • 1
    \$\begingroup\$ I believe you will make a shorter code \$\endgroup\$
    – mazzy
    Mar 26 at 16:03
2
\$\begingroup\$

J, 14 bytes

[:,19&A.@,;._3

Try it online!

J's "Max cubes" verb ;._3 is made to order here. It works by creating a maximal "sliding window" and then using that to tile a matrix. So for example if we just box things to see how it works:

echo i.2 6

0 1 2 3  4  5
6 7 8 9 10 11

echo <;._3 i.2 6

┌───┬───┬───┬────┬─────┐
│0 1│1 2│2 3│3  4│ 4  5│
│6 7│7 8│8 9│9 10│10 11│
└───┴───┴───┴────┴─────┘

Now we just flatten each of those boxes ,, apply the required permutation 19&A., and flatten the final result [:,.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Great answer! This answer qualifies for 200 rep from this bounty if you want it. \$\endgroup\$
    – user
    Mar 28 at 16:57
  • 1
    \$\begingroup\$ @OriginalOriginalOriginalVI Sweet! I'll edit per your instructions. \$\endgroup\$
    – Jonah
    Mar 28 at 16:58
2
\$\begingroup\$

K (ngn/k), 16 bytes

,/{(|x),y}.'+2''

Try it online!

Takes (implicit) input as a list of two strings, e.g. f ("Tower"; "Truck").

  • +2'' take 2-length sliding windows, transposing the results to obtain (("To";"Tr");("ow";"ru");("we";"uc");("er";"ck"))
  • {(|x),y}.' apply the desired transformation to each pair of the above
  • ,/ flatten the result
\$\endgroup\$
0
1
\$\begingroup\$

C++14, 115 112 bytes

As unnamed lambda, parameters should be like std::string:

#define P putchar(
[](auto A,auto B){for(int i=0;++i<A.size()&&i<B.size();P B[i]),P A[i-1]),P B[i-1]),P A[i]));}

Ungolfed and usage:

#include<iostream>
#include<string>

using namespace std;

#define P putchar(
auto f=
[](auto A,auto B){
 for(int i=0;
     ++i<A.size() && i<B.size();
     P B[i]),
     P A[i-1]),
     P B[i-1]),
     P A[i]));
}
;

int main(){
 string A="Truck",B="Tower";
 f(A,B);
}
\$\endgroup\$
1
\$\begingroup\$

Factor, 101 bytes

2 clump [ reverse 1 group ] map concat swap 2 clump [ 1 group ] map concat swap [ write write ] 2each

Try it online!

\$\endgroup\$
3
  • 2
    \$\begingroup\$ This isn't a valid answer because it's neither a full program (which underflows) nor a function. Also, you can shorten it quite a bit without grouping and by using 2merge. Try it online! \$\endgroup\$
    – chunes
    Apr 1 at 0:40
  • \$\begingroup\$ @chunes Of-coarse it's valid, as a stack based language it expects the input strings to be on the stack, so it's a full program. The OP didn't restrict the answer to functions either. Thanks for the 2merge hint! \$\endgroup\$
    – hdrz
    Apr 1 at 9:10
  • 1
    \$\begingroup\$ It's true that stack languages expect input to be on the stack, but unless what you wrote is a function, you have to put things on the stack yourself with readln etc. Consider your program has stack effect ( x x -- ). A full program would have stack effect ( -- ). \$\endgroup\$
    – chunes
    Apr 1 at 9:20

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.