Zigzag Bytes
A string zigzags if its bytes go up, then down, then up, then down again, and so on…
Note that a zigzag can start with a zig or a zag (an increase or a decrease).
Here are some strings that zigzag:
1212 2121 xyxyx 13254 SwApCaSeFuNnY z!g zag >:^)
Here are some strings that don’t zigzag:
1232 1221 zigzag !!!!!!
Write a program which outputs all lines from the input which zigzag. Your program must also zigzag.
NOTE: Each line will consist of 4-50 bytes of printable ASCII.
Judge
(async function*(context: Context): Challenge { const enc = new TextEncoder(), getPattern = function(str: string): string { // Represents a string as a zigzag pattern, using `/`, `\\`, and `=` const bytes = enc.encode(str); let pattern = ""; for (let idx = 1; idx < bytes.length; idx++) pattern += bytes[idx] > bytes[idx - 1] ? "/" : bytes[idx] < bytes[idx - 1] ? "\\" : "=" return pattern; }, isZigZag = function(str: string): boolean { return /^\/?(\\\/)*\\?$/.test(getPattern(str)) }, prettyPrint = function(str: string): string { var out = ""; for(let byte of enc.encode(str)) out += byte >= 32 && byte <= 126 ? String.fromCharCode(byte) : // printable ASCII "."; // not printable ASCII return out; } yield context.registerTestCase(new TestCase("Source Code Zigzag Test", isZigZag(context.code) ? "Pass" : "Fail", { Text: prettyPrint(context.code)+"\n"+getPattern(context.code) })); yield* context.runFilterCases(`1212 2121 xyxyx 13254 SwApCaSeFuNnY z!g zag >:^) 1232 1221 zigzag !!!!!! nu0z(g;Z08,<1g;haq>S6I)SIK2C.4 q$I;["gFp3F nn0z(g;Z08,<1g;haq>S6I)SIK2C.4 q$I;["gFp3F '%D+oay#r;YA[5=0?:Z)PO\`%H8F?\`,Y%4/0'/&7 )%q%<4zr '%D+oay#r;YA[5=0?:Z)OP\`%H8F?\`,Y%4/0'/&7 )%q%<4zr \\uovCs4f@S-@4K3ew$Dn4U*J/F&0/>"\\3 \`[c,A(shi/uC ^c #"~ao!92F8ymw!+ 8'lDwej2U>LAtCa!t451T.b>geUu 3%71r<E.o?m%@)}<\`PdW[7;"Y=d2z\` C!@;[=D'K.8'^?f.3*udecs1=:C4i9C?g[rp{L\\G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