Skip to main content
saved 1 char
Source Link
algorithmshark
  • 8.8k
  • 2
  • 33
  • 55

J - 2019 char

Couldn't help it ;)

(}.@}:#~0>2*#~0,0>2*/\2-/\])

Explanation follows:

  • 2-/\] - Over each pair of elements in the argument (each 2-item long infix), take the difference.
  • 2*/\ - Now over each pair of the new list, take the product.
  • 0> - Test whether each result is less than 0. This only happens if the multiplicands had alternating signs, i.e. it doesn't happen if they had the same sign or either was zero.
  • }.@0, - Declare that the first element isn't an extreme element.
  • }: - Cut off the first and the last element, because thosethat can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff abovean extreme either.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}.@}:#~0>2*#~0,0>2*/\2-/\]) 1 2 1
2
   (}.@}:#~0>2*#~0,0>2*/\2-/\]) 0 1 0 1 0
1 0 1
   (}.@}:#~0>2*#~0,0>2*/\2-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}.@}:#~0>2*#~0,0>2*/\2-/\]) 3 4 4 4 2 5
2

J - 20 char

Couldn't help it ;)

(}.@}:#~0>2*/\2-/\])

Explanation follows:

  • 2-/\] - Over each pair of elements in the argument (each 2-item long infix), take the difference.
  • 2*/\ - Now over each pair of the new list, take the product.
  • 0> - Test whether each result is less than 0. This only happens if the multiplicands had alternating signs, i.e. it doesn't happen if they had the same sign or either was zero.
  • }.@}: - Cut off the first and the last element, because those can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff above.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}.@}:#~0>2*/\2-/\]) 1 2 1
2
   (}.@}:#~0>2*/\2-/\]) 0 1 0 1 0
1 0 1
   (}.@}:#~0>2*/\2-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}.@}:#~0>2*/\2-/\]) 3 4 4 4 2 5
2

J - 19 char

Couldn't help it ;)

(}:#~0,0>2*/\2-/\])

Explanation follows:

  • 2-/\] - Over each pair of elements in the argument (each 2-item long infix), take the difference.
  • 2*/\ - Now over each pair of the new list, take the product.
  • 0> - Test whether each result is less than 0. This only happens if the multiplicands had alternating signs, i.e. it doesn't happen if they had the same sign or either was zero.
  • 0, - Declare that the first element isn't an extreme element.
  • }: - Cut off the last element, because that can't possibly be an extreme either.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}:#~0,0>2*/\2-/\]) 1 2 1
2
   (}:#~0,0>2*/\2-/\]) 0 1 0 1 0
1 0 1
   (}:#~0,0>2*/\2-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}:#~0,0>2*/\2-/\]) 3 4 4 4 2 5
2
bugfix, and saved two characters
Source Link
algorithmshark
  • 8.8k
  • 2
  • 33
  • 55

J - 2220 char

Couldn't help it ;)

(}.@}:#~0=2+#~0>2*/\2*@\2-/\])

Explanation follows:

  • 2*@2-/\] - Over each pair of elements in the argument (each 2-item long infix), take the signum of the difference. This is equal to 1 if the difference is positive, 0 if zero, and -1 if negative.
  • 2+2*/\ - Now over each pair of thesethe new pairslist, addtake the elementsproduct.
  • 0=0> - Test whether each result is equal toless than 0. This only happens if 1 is added to -1the multiplicands had alternating signs, i.e. it doesn't happen if one sidethey had a positive difference and the other had a negativesame sign or either was zero.
  • }.@}: - Cut off the first and the last element, because those can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff above.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}.@}:#~0=2+#~0>2*/\2*@\2-/\]) 1 2 1
2
   (}.@}:#~0=2+#~0>2*/\2*@\2-/\]) 0 1 0 1 0
1 0 1
   (}.@}:#~0=2+#~0>2*/\2*@\2-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}.@}:#~0=2+#~0>2*/\2*@\2-/\]) 03 14 24 04 12 05
2 0 1

J - 22 char

Couldn't help it ;)

(}.@}:#~0=2+/\2*@-/\])

Explanation follows:

  • 2*@-/\] - Over each pair of elements in the argument (each 2-item long infix) take the signum of the difference. This is equal to 1 if the difference is positive, 0 if zero, and -1 if negative.
  • 2+/\ - Now over each of these new pairs, add the elements.
  • 0= - Test whether each result is equal to 0. This only happens if 1 is added to -1, i.e. if one side had a positive difference and the other had a negative.
  • }.@}: - Cut off the first and the last element, because those can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff above.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}.@}:#~0=2+/\2*@-/\]) 1 2 1
2
   (}.@}:#~0=2+/\2*@-/\]) 0 1 0 1 0
1 0 1
   (}.@}:#~0=2+/\2*@-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}.@}:#~0=2+/\2*@-/\]) 0 1 2 0 1 0
2 0 1

J - 20 char

Couldn't help it ;)

(}.@}:#~0>2*/\2-/\])

Explanation follows:

  • 2-/\] - Over each pair of elements in the argument (each 2-item long infix), take the difference.
  • 2*/\ - Now over each pair of the new list, take the product.
  • 0> - Test whether each result is less than 0. This only happens if the multiplicands had alternating signs, i.e. it doesn't happen if they had the same sign or either was zero.
  • }.@}: - Cut off the first and the last element, because those can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff above.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}.@}:#~0>2*/\2-/\]) 1 2 1
2
   (}.@}:#~0>2*/\2-/\]) 0 1 0 1 0
1 0 1
   (}.@}:#~0>2*/\2-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}.@}:#~0>2*/\2-/\]) 3 4 4 4 2 5
2
Source Link
algorithmshark
  • 8.8k
  • 2
  • 33
  • 55

J - 22 char

Couldn't help it ;)

(}.@}:#~0=2+/\2*@-/\])

Explanation follows:

  • 2*@-/\] - Over each pair of elements in the argument (each 2-item long infix) take the signum of the difference. This is equal to 1 if the difference is positive, 0 if zero, and -1 if negative.
  • 2+/\ - Now over each of these new pairs, add the elements.
  • 0= - Test whether each result is equal to 0. This only happens if 1 is added to -1, i.e. if one side had a positive difference and the other had a negative.
  • }.@}: - Cut off the first and the last element, because those can't possibly be extrema, and we lost information for them anyway by doing the pairs stuff above.
  • #~ - Use the true values on the right side to pick items from the list on the left side.

Usage:

   (}.@}:#~0=2+/\2*@-/\]) 1 2 1
2
   (}.@}:#~0=2+/\2*@-/\]) 0 1 0 1 0
1 0 1
   (}.@}:#~0=2+/\2*@-/\]) i.0   NB. i.0 is the empty list (empty result also)

   (}.@}:#~0=2+/\2*@-/\]) 0 1 2 0 1 0
2 0 1