Skip to main content
Incorporated improvement.
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Haskell, 3433 bytes

(%)=scanl1 f s=or[s==scanl1 q s|q<-[min,max]]s=s==max%s||s==min%s 

Try it online!Try it online!

Thanks to Ørjan Johansen for 1 byte with aliasing scanl1 infix.

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

Then, s==scanl1 max s if s is sorted in increasing order. The same expression with min checks ifwhether the list is decreasing. In theThe code, [s==scanl1 q s|q<-[min,max]] checks both these optionsthe two cases and combines them with or|| sees if either is true.

Compare to other expressions:

(%)=scanl1;f s=s==max%s||s==min%s f s=or[s==scanl1 q s|q<-[min,max]]   f s=s==scanl1 max s||s==scanl1 min s f s=any(\q->scanl1 q s==s)[min,max] f s=any((==s).(`scanl1`s))[min,max] f s=elem s$(`scanl1`s)<$>[min,max] 

Haskell, 34 bytes

f s=or[s==scanl1 q s|q<-[min,max]] 

Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

Then, s==scanl1 max s if s is sorted in increasing order. The same expression with min checks if the list is decreasing. In the code, [s==scanl1 q s|q<-[min,max]] checks both these options and or sees if either is true.

Compare to other expressions:

f s=or[s==scanl1 q s|q<-[min,max]]   f s=s==scanl1 max s||s==scanl1 min s f s=any(\q->scanl1 q s==s)[min,max] f s=any((==s).(`scanl1`s))[min,max] f s=elem s$(`scanl1`s)<$>[min,max] 

Haskell, 33 bytes

(%)=scanl1 f s=s==max%s||s==min%s 

Try it online!

Thanks to Ørjan Johansen for 1 byte with aliasing scanl1 infix.

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

The same with min checks whether the list is decreasing. The code checks the two cases and combines them with ||.

Compare to other expressions:

(%)=scanl1;f s=s==max%s||s==min%s f s=or[s==scanl1 q s|q<-[min,max]] f s=s==scanl1 max s||s==scanl1 min s f s=any(\q->scanl1 q s==s)[min,max] f s=any((==s).(`scanl1`s))[min,max] f s=elem s$(`scanl1`s)<$>[min,max] 
added 64 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Haskell, 34 bytes

f s=or[s==scanl1 q s|q<-[min,max]] 

Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

Then, s==scanl1 max s if s is sorted in increasing order. The same expression with min checks if the list is decreasing. In the code, [s==scanl1 q s|q<-[min,max]] checks both these options and or sees if either is true. This is twp bytes shorter than a more direct check

Compare to other expressions:

f s=or[s==scanl1 q s|q<-[min,max]] f s=s==scanl1 max s||s==scanl1 min s f s=any(\q->scanl1 q s==s)[min,max] f s=any((==s).(`scanl1`s))[min,max] f s=elem s$(`scanl1`s)<$>[min,max] 

Haskell, 34 bytes

f s=or[s==scanl1 q s|q<-[min,max]] 

Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

Then, s==scanl1 max s if s is sorted in increasing order. The same expression with min checks if the list is decreasing. In the code, [s==scanl1 q s|q<-[min,max]] checks both these options and or sees if either is true. This is twp bytes shorter than a more direct check:

f s=s==scanl1 max s||s==scanl1 min s 

Haskell, 34 bytes

f s=or[s==scanl1 q s|q<-[min,max]] 

Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

Then, s==scanl1 max s if s is sorted in increasing order. The same expression with min checks if the list is decreasing. In the code, [s==scanl1 q s|q<-[min,max]] checks both these options and or sees if either is true.

Compare to other expressions:

f s=or[s==scanl1 q s|q<-[min,max]] f s=s==scanl1 max s||s==scanl1 min s f s=any(\q->scanl1 q s==s)[min,max] f s=any((==s).(`scanl1`s))[min,max] f s=elem s$(`scanl1`s)<$>[min,max] 
added 945 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676

Haskell, 3534 bytes

f s=elem s[scanl1 q s|q<-[min,max]] 
f s=or[s==scanl1 q s|q<-[min,max]] 

Try it online!Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

This equals to the original list exactlyThen, s==scanl1 max s if its is sorted in increasing order. The same expression with min gives cumulative minima, which leaves invariant any decreasingchecks if the list is decreasing. In the code, [scanl1[s==scanl1 q s|q<-[min,max]] generateschecks both the cumulative minima and maxima,these options and elem sor checkssees if the original list is either oneis true. This is one bytetwp bytes shorter than a more direct check:

f s=s==scanl1 max s||s==scanl1 min s 
f s=s==scanl1 max s||s==scanl1 min s 

Haskell, 35 bytes

f s=elem s[scanl1 q s|q<-[min,max]] 

Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

This equals to the original list exactly if it is sorted in increasing order. The same expression with min gives cumulative minima, which leaves invariant any decreasing list. In the code, [scanl1 q s|q<-[min,max]] generates both the cumulative minima and maxima, and elem s checks if the original list is either one. This is one byte shorter than a more direct check:

f s=s==scanl1 max s||s==scanl1 min s 

Haskell, 34 bytes

f s=or[s==scanl1 q s|q<-[min,max]] 

Try it online!

Haskell is an interesting language to golf sorting-based challenges because it does not have a built-in sort, barring a lengthy import Data.List. This encourages finding a way to do the task by hand without explicitly sorting.

The code uses scanl1, which folds an operation over the list from left to right, keeping track of the intermediate results. So, scanl1 max has the effect of listing the cumulative maxima of the list, i.e. the maxima of progressively longer prefixes. For example, scanl1 max [3,1,2,5,4] == [3,3,3,5,5].

Then, s==scanl1 max s if s is sorted in increasing order. The same expression with min checks if the list is decreasing. In the code, [s==scanl1 q s|q<-[min,max]] checks both these options and or sees if either is true. This is twp bytes shorter than a more direct check:

f s=s==scanl1 max s||s==scanl1 min s 
added 945 characters in body
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading
Source Link
xnor
  • 149.7k
  • 26
  • 287
  • 676
Loading