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

Commonmark migration
Source Link

#Java 8, (削除) 231 (削除ここまで)(削除) 193 (削除ここまで)(削除) 185 (削除ここまで)(削除) 122 (削除ここまで)(削除) 103 (削除ここまで) 78 bytes

Java 8, (削除) 231 (削除ここまで)(削除) 193 (削除ここまで)(削除) 185 (削除ここまで)(削除) 122 (削除ここまで)(削除) 103 (削除ここまで) 78 bytes

s->s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]

Try it here.

-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.
-25 bytes by converting it from Java 7 to Java 8 (and now returning a boolean instead of integer).

193 bytes answer:

int c(char[]s){if(s.length!=5)return 0;int a=s[0],b=s[1],c=s[2],d=s[3],e=s[4],z=b-a,y=c-b,x=d-c,w=e-d;return e!=a?0:(z>0&y>0&x<0&w<0)|(z<0&y>0&x>0&w<0)|(z>0&y<0&x<0&w>0)|(z<0&y<0&x>0&w>0)?1:0;}

Explanation:

  • If the length of the string isn't 5, we return false
  • If the first character doesn't equal the last character, we return false
  • Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return true if it complies to any of them (and false otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

#Java 8, (削除) 231 (削除ここまで)(削除) 193 (削除ここまで)(削除) 185 (削除ここまで)(削除) 122 (削除ここまで)(削除) 103 (削除ここまで) 78 bytes

s->s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]

Try it here.

-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.
-25 bytes by converting it from Java 7 to Java 8 (and now returning a boolean instead of integer).

193 bytes answer:

int c(char[]s){if(s.length!=5)return 0;int a=s[0],b=s[1],c=s[2],d=s[3],e=s[4],z=b-a,y=c-b,x=d-c,w=e-d;return e!=a?0:(z>0&y>0&x<0&w<0)|(z<0&y>0&x>0&w<0)|(z>0&y<0&x<0&w>0)|(z<0&y<0&x>0&w>0)?1:0;}

Explanation:

  • If the length of the string isn't 5, we return false
  • If the first character doesn't equal the last character, we return false
  • Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return true if it complies to any of them (and false otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

Java 8, (削除) 231 (削除ここまで)(削除) 193 (削除ここまで)(削除) 185 (削除ここまで)(削除) 122 (削除ここまで)(削除) 103 (削除ここまで) 78 bytes

s->s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]

Try it here.

-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.
-25 bytes by converting it from Java 7 to Java 8 (and now returning a boolean instead of integer).

193 bytes answer:

int c(char[]s){if(s.length!=5)return 0;int a=s[0],b=s[1],c=s[2],d=s[3],e=s[4],z=b-a,y=c-b,x=d-c,w=e-d;return e!=a?0:(z>0&y>0&x<0&w<0)|(z<0&y>0&x>0&w<0)|(z>0&y<0&x<0&w>0)|(z<0&y<0&x>0&w>0)?1:0;}

Explanation:

  • If the length of the string isn't 5, we return false
  • If the first character doesn't equal the last character, we return false
  • Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return true if it complies to any of them (and false otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

Converted to Java 8 and boolean for -25 bytes
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394

#Java 78, (削除) 231 (削除ここまで) (削除) 193 (削除ここまで) (削除) 185 (削除ここまで) (削除) 122 (削除ここまで) 103(削除) 103 (削除ここまで) 78 bytes

int c(char[]s){return s->s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]?1:0;}

Try it here.

-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.
-25 bytes by converting it from Java 7 to Java 8 (and now returning a boolean instead of integer).

It returns an int (0 or 1) instead of a boolean, because OP didn't explicit asked for a truthy/falsey return-type, but "The output can be true/false, 1/0, 1/Null, etc." instead.

Explanation:

  • If the length of the string isn't 5, we return 0false (false)
  • If the first character doesn't equal the last character, we return 0false (false)
  • Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return 1true (true) if it complies to any of them (and 0false (false) otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

Ungolfed & test code:

Try it here.

class M{
 static int c(char[]s){
 return s.length == 5
 && (s[1]-s[0])*(s[3]-s[2]) < 0 & (s[2]-s[1])*(s[4]-s[3]) < 0
 & s[4]==s[0]
 ? 1
 : 0;
 }
 public static void main(String[] a){
 System.out.print(c("ALPHA".toCharArray()) + ",");
 System.out.print(c("EAGLE".toCharArray()) + ",");
 System.out.print(c("HARSH".toCharArray()) + ",");
 System.out.print(c("NINON".toCharArray()) + ",");
 System.out.print(c("PINUP".toCharArray()) + ",");
 System.out.print(c("RULER".toCharArray()) + ",");
 System.out.print(c("THEFT".toCharArray()) + ",");
 System.out.println(c("WIDOW".toCharArray()));
 System.out.print(c("CUBIC".toCharArray()) + ",");
 System.out.print(c("ERASE".toCharArray()) + ",");
 System.out.print(c("FLUFF".toCharArray()) + ",");
 System.out.print(c("LABEL".toCharArray()) + ",");
 System.out.print(c("MODEM".toCharArray()) + ",");
 System.out.print(c("RADAR".toCharArray()) + ",");
 System.out.print(c("RIVER".toCharArray()) + ",");
 System.out.print(c("SWISS".toCharArray()) + ",");
 System.out.print(c("TRUST".toCharArray()) + ",");
 System.out.print(c("KNEES".toCharArray()) + ",");
 System.out.print(c("QUEEN".toCharArray()) + ",");
 System.out.print(c("GROOVE".toCharArray()) + ",");
 System.out.print(c("ONLY".toCharArray()) + ",");
 System.out.print(c("CHARACTER".toCharArray()) + ",");
 System.out.print(c("OFF".toCharArray()) + ",");
 System.out.print(c("IT".toCharArray()) + ",");
 System.out.print(c("ORTHO".toCharArray()) + ",");
 System.out.print(c("RULES".toCharArray()) + ",");
 System.out.print(c("".toCharArray()));
 }
}

Output:

1,1,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

#Java 7, (削除) 231 (削除ここまで) (削除) 193 (削除ここまで) (削除) 185 (削除ここまで) (削除) 122 (削除ここまで) 103 bytes

int c(char[]s){return s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]?1:0;}

-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.

It returns an int (0 or 1) instead of a boolean, because OP didn't explicit asked for a truthy/falsey return-type, but "The output can be true/false, 1/0, 1/Null, etc." instead.

Explanation:

  • If the length of the string isn't 5, we return 0 (false)
  • If the first character doesn't equal the last character, we return 0 (false)
  • Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return 1 (true) if it complies to any of them (and 0 (false) otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

Ungolfed & test code:

Try it here.

class M{
 static int c(char[]s){
 return s.length == 5
 && (s[1]-s[0])*(s[3]-s[2]) < 0 & (s[2]-s[1])*(s[4]-s[3]) < 0
 & s[4]==s[0]
 ? 1
 : 0;
 }
 public static void main(String[] a){
 System.out.print(c("ALPHA".toCharArray()) + ",");
 System.out.print(c("EAGLE".toCharArray()) + ",");
 System.out.print(c("HARSH".toCharArray()) + ",");
 System.out.print(c("NINON".toCharArray()) + ",");
 System.out.print(c("PINUP".toCharArray()) + ",");
 System.out.print(c("RULER".toCharArray()) + ",");
 System.out.print(c("THEFT".toCharArray()) + ",");
 System.out.println(c("WIDOW".toCharArray()));
 System.out.print(c("CUBIC".toCharArray()) + ",");
 System.out.print(c("ERASE".toCharArray()) + ",");
 System.out.print(c("FLUFF".toCharArray()) + ",");
 System.out.print(c("LABEL".toCharArray()) + ",");
 System.out.print(c("MODEM".toCharArray()) + ",");
 System.out.print(c("RADAR".toCharArray()) + ",");
 System.out.print(c("RIVER".toCharArray()) + ",");
 System.out.print(c("SWISS".toCharArray()) + ",");
 System.out.print(c("TRUST".toCharArray()) + ",");
 System.out.print(c("KNEES".toCharArray()) + ",");
 System.out.print(c("QUEEN".toCharArray()) + ",");
 System.out.print(c("GROOVE".toCharArray()) + ",");
 System.out.print(c("ONLY".toCharArray()) + ",");
 System.out.print(c("CHARACTER".toCharArray()) + ",");
 System.out.print(c("OFF".toCharArray()) + ",");
 System.out.print(c("IT".toCharArray()) + ",");
 System.out.print(c("ORTHO".toCharArray()) + ",");
 System.out.print(c("RULES".toCharArray()) + ",");
 System.out.print(c("".toCharArray()));
 }
}

Output:

1,1,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0

#Java 8, (削除) 231 (削除ここまで) (削除) 193 (削除ここまで) (削除) 185 (削除ここまで) (削除) 122 (削除ここまで) (削除) 103 (削除ここまで) 78 bytes

s->s.length==5&&(s[1]-s[0])*(s[3]-s[2])<0&(s[2]-s[1])*(s[4]-s[3])<0&s[4]==s[0]

Try it here.

-38 bytes thanks to @dpa97 for reminding me to use char[] instead of String.
-63 bytes thanks to @KarlNapf's derived formula.
-25 bytes by converting it from Java 7 to Java 8 (and now returning a boolean instead of integer).

Explanation:

  • If the length of the string isn't 5, we return false
  • If the first character doesn't equal the last character, we return false
  • Then we check the four valid cases one by one (let's indicate the five characters as 1 through 5), and return true if it complies to any of them (and false otherwise):
    1. If the five characters are distributed like: 1<2<3>4>5 (i.e. ALPHA)
    2. If the five characters are distributed like: 1>2<3<4>5 (i.e. EAGLE, HARSH, NINON, PINUP)
    3. If the five characters are distributed like: 1<2>3>4<5 (i.e. RULER)
    4. If the five characters are distributed like: 1>2>3<4<5 (i.e. THEFT, WIDOW)

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

replaced http://codegolf.stackexchange.com/ with https://codegolf.stackexchange.com/
Source Link

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer @KarlNapf's Python 2 answer).

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

These four rules can be simplified to 1*3<0 and 2*4<0 (thanks to @KarlNapf's Python 2 answer).

Realized I can golf 19 more bytes by removing the temporary int variables and return directly
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
-63 bytes thanks to @KarlNapf's derived formula
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
Added explanation of the four rules now that the checks are golfed to `z*y>0` instead of `z>0&y>0`
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
-8 bytes by multiplying the positive checks (since if one of them is negative or zero, the check would return false)
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
String to char[] for -39 bytes
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
Added which of the four rules applies to which word
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading
Source Link
Kevin Cruijssen
  • 136.2k
  • 14
  • 154
  • 394
Loading

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