Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 114113 bytes

Expects (N,)(n,)(o) and returns false for strange, true for regular, 2 for wrong.

N=>n=>g=(N,n,o)=>(U="toUpperCase",g=(x,y=U[+y=(U="toUpperCase")[+!x])=>~N--?g(o,n--?y:y=p=y[U](),x&&x+y):(s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online! Try it online!

(N, n,=> o)n => // inputouter valuesfunctions taking N and n
g = ( U = "toUpperCase", // store "toUpperCase" in U
 g = (// inner recursive function taking:
 o, // g is a recursive function// taking:
 the offset x,o
 x, // a string x (initially undefined)
 y = U[+!x](  // a string y initialized to "o"by ifusing xthe
 U = "toUpperCase" // first 2 characters of "toUpperCase":
 )[+!x] // is undefined// or "t" is- "o" if x is defined
undefined (1st iteration) => // (for the first 2 iterations)
 ~N-- ?"t" if x is defined (2nd iteration)
) => // if N is not equal to -1//
~N-- ? // if N != //-1 (decrement N afterwards):
 g( // do a recursive call:
 n--o, ? // if n is not equalpass too 0unchanged
 n-- ? // // if n != 0 (decrement n afterwards):
 y // use y unchanged
 : // else:
 y = p = y[U](), // update y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
: // else:
 ( s = //
  s = x.substr( // saves in= srelevant andslice returnof ...x
 x.substr(o, p.length) // ... the// relevant slice of x
)() at offset o and of size p.length
 ) // invoke g with x and y undefined//
 )[U]() == p ? // if the resultif s in upper case matchesis equal to p:
 s == p // compare s with p (Boolean value)
:  : // else:
 2  2 // return 2 for 'wrong'

JavaScript (ES6), 114 bytes

Expects (N,n,o) and returns false for strange, true for regular, 2 for wrong.

(N,n,o)=>(U="toUpperCase",g=(x,y=U[+!x])=>~N--?g(n--?y:y=p=y[U](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online!

(N, n, o) => // input values
( U = "toUpperCase", // store "toUpperCase" in U
 g = ( // g is a recursive function taking:
 x, // a string x y = U[+!x] // a string y initialized to "o" if x
 // is undefined or "t" is x is defined
 ) => // (for the first 2 iterations)
 ~N-- ? // if N is not equal to -1
 // (decrement N afterwards):
 g( // do a recursive call:
 n-- ? // if n is not equal to 0
 // (decrement n afterwards):
 y // use y unchanged
 : // else:
 y = p = y[U](), // update y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
: // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
:  // else:
 2  // return 2

JavaScript (ES6), 113 bytes

Expects (N)(n)(o) and returns false for strange, true for regular, 2 for wrong.

N=>n=>g=(o,x,y=(U="toUpperCase")[+!x])=>~N--?g(o,n--?y:y=p=y[U](),x&&x+y):(s=x.substr(o,p.length))[U]()==p?s==p:2

Try it online!

N => n => // outer functions taking N and n
g = ( // inner recursive function taking:
 o, // the offset o
 x, // a string x (initially undefined)
 y = (  // a string y initialized by using the
 U = "toUpperCase" // first 2 characters of "toUpperCase":
 )[+!x] // - "o" if x is undefined (1st iteration) // - "t" if x is defined (2nd iteration)
) => //
~N-- ? // if N != -1 (decrement afterwards):
 g( // do a recursive call:
 o, // pass o unchanged
 n-- ? // if n != 0 (decrement afterwards):
 y // use y unchanged
 : // else:
 y = p = y[U](), // update y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
: // else:
 ( //
  s = x.substr( // s = relevant slice of x
 o, p.length // at offset o and of size p.length
 ) //
 )[U]() == p ? // if s in upper case is equal to p:
 s == p // compare s with p (Boolean value)
 : // else:
 2 // return 2 for 'wrong'
saved 3 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 117114 bytes

(N,n,o)=>(i=1,U="toUpperCase",g=(x,y=U[i]y=U[+!x])=>N+i=>~N--?g(i+nn--?y:y=p=y[U](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online! Try it online!

(N, n, o) => // input values
( i = 1,  // initialize i to 1
 U = "toUpperCase", // store "toUpperCase" in U
 g = ( // g is a recursive function taking:
 x, // a string x
 y = U[i]  U[+!x] // a string y initialized to "o" orif "t"x
 // for iis =undefined 1or and"t" iis =x 0is respectivelydefined
 ) => // N +(for ithe first 2 iterations)
 ~N-- ? // if -iN is not equal to N-1
 // (decrement iN afterwards):
 g( // do a recursive call:
 i + n-- ? // if -in is not equal to 0
 // (decrement n afterwards):
 y // use y unchanged
 : // else:
 y = p = y[U](), // udpateupdate y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
 : // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
: // else:
 2 // return 2

JavaScript (ES6), 117 bytes

(N,n,o)=>(i=1,U="toUpperCase",g=(x,y=U[i])=>N+i--?g(i+n?y:y=p=y[U](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online!

(N, n, o) => // input values
( i = 1,  // initialize i to 1
 U = "toUpperCase", // store "toUpperCase" in U
 g = ( // g is a recursive function taking:
 x, // a string x
 y = U[i]  // a string y initialized to "o" or "t"
 // for i = 1 and i = 0 respectively
 ) => // N + i-- ? // if -i is not equal to N
 // (decrement i afterwards):
 g( // do a recursive call:
 i + n ? // if -i is not equal to n:
 y // use y unchanged
 : // else:
 y = p = y[U](), // udpate y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
 : // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
: // else:
 2 // return 2

JavaScript (ES6), 114 bytes

(N,n,o)=>(U="toUpperCase",g=(x,y=U[+!x])=>~N--?g(n--?y:y=p=y[U](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online!

(N, n, o) => // input values
( U = "toUpperCase", // store "toUpperCase" in U
 g = ( // g is a recursive function taking:
 x, // a string x
 y = U[+!x] // a string y initialized to "o" if x
 // is undefined or "t" is x is defined
 ) => // (for the first 2 iterations)
 ~N-- ? // if N is not equal to -1
 // (decrement N afterwards):
 g( // do a recursive call:
 n-- ? // if n is not equal to 0
 // (decrement n afterwards):
 y // use y unchanged
 : // else:
 y = p = y[U](), // update y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
 : // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
: // else:
 2 // return 2
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 118117 bytes

(N,n,o)=>(i=1,U="toUpperCase",g=(x,y="ba"[i]y=U[i])=>N+i--?g(i+n?y:y=p=y[U="toUpperCase"]y=p=y[U](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online! Try it online!

We recursively build all patterns \$F_0\$ to \$F_N\$, using letters in lower case (ironically using the 'a''o' and 'b''t' from the string 'toUpperCase' for golfing reasons).

When \$F_n\$ is reached, we convert it to upper case and, save it in \$p\$ and use the transformed string for the recursion. All subsequent regular occurrences of \$p\$ will therefore appear in upper case as well.

(N, n, o) => // input values
( i = 1, // initialize i to 1
 gU = ("toUpperCase", // store "toUpperCase" in U
 g = (  // g is a recursive function taking:
 x, // a string x
 y = "ba"[i]U[i]  // a string y initialized to "a""o" or "b""t"
 // for i = 1 and i = 0 respectively
 ) => //
 N + i-- ? // if -i is not equal to N
 // (decrement i afterwards):
 g( // do a recursive call:
 i + n ? // if -i is not equal to n:
 y // use y unchanged
 : // else:
 y = p = y[  y[U](), // udpate y and p to U = "toUpperCase" // y in upper case
 ](), //
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
 : // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
: // else:
 2 // return 2

JavaScript (ES6), 118 bytes

(N,n,o)=>(i=1,g=(x,y="ba"[i])=>N+i--?g(i+n?y:y=p=y[U="toUpperCase"](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online!

We recursively build all patterns \$F_0\$ to \$F_N\$, using 'a' and 'b'.

When \$F_n\$ is reached, we convert it to upper case and save it in \$p\$. All subsequent regular occurrences of \$p\$ will appear in upper case as well.

(N, n, o) => // input values
( i = 1, // initialize i to 1
 g = ( // g is a recursive function taking:
 x, // a string x
 y = "ba"[i] // a string y initialized to "a" or "b"
 // for i = 1 and i = 0 respectively
 ) => //
 N + i-- ? // if -i is not equal to N
 // (decrement i afterwards):
 g( // do a recursive call:
 i + n ? // if -i is not equal to n:
 y // use y unchanged
 : // else:
 y = p = y[  // udpate y and p to U = "toUpperCase" // y in upper case
 ](), //
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
 : // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
: // else:
 2 // return 2

JavaScript (ES6), 117 bytes

(N,n,o)=>(i=1,U="toUpperCase",g=(x,y=U[i])=>N+i--?g(i+n?y:y=p=y[U](),x&&x+y):s=x.substr(o,p.length))()[U]()==p?s==p:2

Try it online!

We recursively build all patterns \$F_0\$ to \$F_N\$ using letters in lower case (ironically using the 'o' and 't' from the string 'toUpperCase' for golfing reasons).

When \$F_n\$ is reached, we convert it to upper case, save it in \$p\$ and use the transformed string for the recursion. All subsequent regular occurrences of \$p\$ will therefore appear in upper case as well.

(N, n, o) => // input values
( i = 1, // initialize i to 1
 U = "toUpperCase", // store "toUpperCase" in U
 g = (  // g is a recursive function taking:
 x, // a string x
 y = U[i]  // a string y initialized to "o" or "t"
 // for i = 1 and i = 0 respectively
 ) => //
 N + i-- ? // if -i is not equal to N
 // (decrement i afterwards):
 g( // do a recursive call:
 i + n ? // if -i is not equal to n:
 y // use y unchanged
 : // else:
 y = p = y[U](), // udpate y and p to y in upper case
 x && x + y // if x is defined, pass x + y
 // (pass undefined otherwise)
 ) // end of recursive call
 : // else:
 s = // save in s and return ...
 x.substr(o, p.length) // ... the relevant slice of x
)() // invoke g with x and y undefined
[U]() == p ? // if the result in upper case matches p:
 s == p // compare s with p (Boolean value)
: // else:
 2 // return 2
added a commented version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added the new test cases
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Post Undeleted by Arnauld
fixed for n<2
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Post Deleted by Arnauld
rephrasing
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
minor update
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 7 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /