| Bytes | Lang | Time | Link |
|---|---|---|---|
| 013 | AGL | 250215T224924Z | ErikDaPa |
| 081 | Haskell | 250624T171001Z | Ibozz91 |
| 068 | Perl 5 p | 250220T190244Z | Xcali |
| 078 | C clang | 250218T155452Z | jdt |
| 006 | Husk | 250215T210554Z | Glory2Uk |
| 078 | Javascript ES6 | 161101T193257Z | Robert H |
| 081 | JavaScript ES6 | 161028T205143Z | Neil |
| 240 | Java 7 | 161031T103120Z | Kevin Cr |
| 009 | 05AB1E | 161028T180635Z | Magic Oc |
| 095 | R | 161030T105211Z | Billywob |
| 100 | PHP | 161029T103713Z | chococha |
| 096 | PHP | 161028T191820Z | Jör |
| 056 | Groovy | 161029T235405Z | GolfIsAG |
| 321 | Racket | 161029T161933Z | rnso |
| 164 | C | 161029T094438Z | Steadybo |
| 054 | Ruby | 161028T221523Z | Lee W |
| 050 | Python 2 | 161028T204721Z | Karl Nap |
| 010 | Jelly | 161028T191854Z | Jonathan |
| 075 | Python 3 | 161028T191723Z | C. Smith |
| 054 | Python 2 | 161028T192110Z | xnor |
| 012 | Pyth | 161028T172047Z | PurkkaKo |
AGL, 13 bytes
- a programming language i made! check out the link to my github repo.
[1] for increasing wavy, [-1] for decreasing wavy, otherwise not wavy.
s#2{-.-}/⌀-^
Explanation:
s#2{-.-}/⌀-^
s# => convert into codepoints
2{-.-}/ => find pairwise deltas
⌀- => filter out 0s
^ => deduplicate
Haskell, 81 bytes
import Data.List
f s=[b&&t==s,b&&(reverse t)==s]where t=sort s;b=(length$nub t)>2
Perl 5 -p, 68 bytes
$_=(s/(.)\1*/$1.*?/g>2)*(($a=join'',A..Z)=~/$_/<=>(reverse$a)=~/$_/)
Outputs -1/0/1 for decreasing wavy/not wavy/increasing wavy.
C (clang), 82 78 bytes
d;f(*s,*r,n){for(*r=n>3?strcmp(s,++s):0;*s;*r*=!d|d<1^*r>0)d=strcmp(s-1,s++);}
-4 bytes thanks to @ceilingcat!
Returns:
- < 0 for Increasing Wavy
- > 0 for Decreasing Wavy
- 0 for not Wavy
Husk, 7 6 bytes
e#<¹#>
Otputs a list of two values \$[a, b]\$:
- \$a > 1, b = 0:\$ raising
- \$a = 0, b > 1:\$ decreasing
- otherwise the word is not wavy
Commented:
#> -- count adjacent pairs where the 1st character is "greater" than the 2nd
#<¹ -- count adjacent pairs where the 1st character is "less" than the 2nd
e -- add to the list
-- output implicitly
Javascript (ES6), 84 80 78 bytes
i=>new Set(s=[...i]).size>2?[i,s.reverse().join``].indexOf(s.sort().join``):-1
Where wavy increasing is 0, decreasing is 1, and -1 is not wavy.
Thanks to @Neil for helping me save 2 bytes.
JavaScript (ES6), 84 81 bytes
s=>(new Set(t=[...s]).size>2)*(!t.some((c,i)=>c>s[i+1])-!t.some((c,i)=>c<s[i+1]))
Assumes the input is all in the same case. Returns 1 for raising wavy, -1 for decreasing wavy, 0 or -0 (both are falsy) for not wavy. Edit: Saved 3 bytes thanks to @RobertHickman.
Java 7, 254 240 bytes
import java.util.*;int c(String s){char[]a=s.toCharArray(),x=a.clone();Arrays.sort(x);return s.replaceAll("(.)\\1{1,}","$1").length()<3?0:Arrays.equals(a,x)|Arrays.equals(x,(new StringBuffer(s).reverse()+"").toCharArray())?a[0]>a[1]?1:2:0;}
Outputs 0 if the input string isn't wavy, 1 if it's a raising wave, and 2 if it's a decreasing wave.
Ungolfed & test code:
import java.util.*;
class M{
static int c(String s){
char[] a = s.toCharArray(),
x = a.clone();
Arrays.sort(x);
return s.replaceAll("(.)\\1{1,}", "$1").length() < 3
? 0
: Arrays.equals(a, x) | Arrays.equals(x, (new StringBuffer(s).reverse()+"").toCharArray())
? a[0] > a[1]
? 1
: 2
: 0;
}
public static void main(String[] a){
System.out.print(c("ADEPT") + ", ");
System.out.print(c("BEGIN") + ", ");
System.out.print(c("BILL") + ", ");
System.out.print(c("BOSS") + ", ");
System.out.print(c("BOOST") + ", ");
System.out.print(c("CHIMP") + ", ");
System.out.println(c("KNOW"));
System.out.print(c("SPONGE") + ", ");
System.out.print(c("SPOON") + ", ");
System.out.print(c("TROLL") + ", ");
System.out.println(c("WOLF"));
System.out.print(c("WATCH") + ", ");
System.out.print(c("EARTH") + ", ");
System.out.print(c("NINON") + ", ");
System.out.print(c("FOO") + ", ");
System.out.print(c("BAR") + ", ");
System.out.print(c("WAVE") + ", ");
System.out.print(c("SELECTION") + ", ");
System.out.print(c("YES") + ", ");
System.out.print(c("NO") + ", ");
System.out.print(c("DEFINITION") + ", ");
System.out.print(c("WATER") + ", ");
System.out.print(c("WINE") + ", ");
System.out.print(c("CODE") + ", ");
System.out.print(c("AAAHHHH") + ", ");
System.out.print(c("I") + ", ");
System.out.print(c("MM") + ", ");
System.out.println(c("ABCA"));
}
}
Output:
2, 2, 2, 2, 2, 2, 2
1, 1, 1, 1
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
05AB1E, 11 9 bytes (Thanks to Adnan)
Dg2›iÇü‹Ù
Wavy Cases:
0 - Decreasing Wavy
1 - Increasing Wavy
Not Wavy Cases:
[0,1] - Not wavy, initially decreasing, but then has an increase/equality that broke the pattern.
[1,0] - Not wavy, initially increasing, but then has a decrease/equality that broke the pattern
Input String - Not possible to be wavy in the first place due to length.
Explanation:
Dg2›iÇü‹Ù # Full program
D # Push 2 copies of input.
g2›i # If length is greater than 2.
Ç # Push ASCII values for all characters in the string.
ü # Push pairwise array.
‹ # Vectorize 1 if negative difference, 0 if positive difference.
Ù # Uniquify using right most unique values first.
# Else just print the string back, letting them know it's not valid input.
R, 96 95 bytes
function(x,d=diff(rle(utf8ToInt(x))$v))if(any(d>0)&any(d<0)|sum(1|d)<2)3 else`if`(all(d<1),2,1)
Returns:
1for wavy and raising2for wavy and decreasing3for non-wavy
Explained
d=diff(rle(utf8ToInt(x))$v): Generates a variabledby first converting the string into it'sASCIIvalues usingutf8ToIntwhich conveniently returns a vector. Subsequently perform run length encoding usingrle.rle(...)$vreturns the non-repeating values of the sequence (i.e. collapsing all runs). Finally take the difference.if(any(d>0)&any(d<0)|sum(1|d)<2)3: If at least one of the differences are positive and at least one negative, or if the difference sequence has less than2elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return3else``if``(all(d<1),2,1): Else if all differences are negative, return2for wavy and decreasing, else return1for wavy and raising.
Try all the test cases at R-fiddle (note that it's named such that it can be vectorized for the test cases).
PHP, 100 bytes
$n=$m=$l=str_split($argv[1]);sort($n);rsort($m);echo(($n==$l)-($m==$l))*(count(array_unique($l))>2);
Returns:
-1for wavy, decreasing.0for not wavy.1for wavy, raising.
PHP, 96 Bytes
for(;($t=$argv[1])[++$i];)$s+=$r[]=$t[$i-1]<=>$t[$i];echo(max($r)-min($r)<2)*(0<=>$s)*(1<$s*$s);
or 98 Bytes
$s=str_split($t=$argv[1]);sort($s);echo(-($t==strrev($j=join($s)))|$t==$j)*!!count_chars($t,3)[2];
0 not wavy 1 raising -1 decreasing
Groovy - 56 bytes
{d=it[0];c=[0]*3;it.each{a->c[(a<=>d)]=1;d=a};c[1..-1]}
Outputs [1,0] for raising wavy, [0,1] for decreasing wavy, [0,0] for single character input or [1,1] for non-wavy.
NOTE: Assumes input is either a String or a char[] and all letters are of the same case.
Racket 321 bytes
(let*((ld(λ(sl)(for/list((i(sub1(length sl))))(-(list-ref sl(add1 i))(list-ref sl i)))))(l(ld(remove-duplicates(map
(λ(x)(char->integer x))(string->list s)))))(am andmap)(N"Not WAVY")(d displayln))(cond[(<(length l)2)(d N)][(am(λ(x)(>= x 0))l)
(d"YES; RAISING")][(am(λ(x)(<= x 0))l)(d"YES; DECREASING")][else(d N)])))
Ungolfed:
(define (f s)
(let* ((ld (lambda(sl) ; sub-fn to get differences in list elements
(for/list ((i (sub1(length sl))))
(- (list-ref sl (add1 i))
(list-ref sl i) ) )))
(l (ld
(remove-duplicates
(map
(lambda(x)
(char->integer x))
(string->list s)))))
(am andmap)
(N "Not WAVY")
(d displayln))
(cond
[(< (length l) 2)(d N)]
[(am (lambda(x) (>= x 0)) l) (d "YES; RAISING")]
[(am (lambda(x) (<= x 0)) l) (d "YES; DECREASING")]
[else (d N)]
)))
Testing:
(f "ADEPT"); > YES > RAISING
(f "BEGIN"); > YES > RAISING
(f "BILL"); > YES > RAISING
(f "BOSS"); > YES > RAISING
(f "BOOST"); > YES > RAISING
(f "CHIMP"); > YES > RAISING
(f "KNOW"); > YES > RAISING
(f "SPONGE"); > YES > DECREASING
(f "SPOON"); > YES > DECREASING
(f "TROLL"); > YES > DECREASING
(f "WOLF"); > YES > DECREASING
(f "WATCH")
(f "EARTH")
(f "NINON")
(f "FOO")
(f "BAR")
(f "WAVE")
(f "SELECTION")
Output:
YES; RAISING
YES; RAISING
YES; RAISING
YES; RAISING
YES; RAISING
YES; RAISING
YES; RAISING
YES; DECREASING
YES; DECREASING
YES; DECREASING
YES; DECREASING
Not WAVY
Not WAVY
Not WAVY
Not WAVY
Not WAVY
Not WAVY
Not WAVY
C, 164 bytes
main(){char s[99];scanf("%s",s);char *c=&s;int p=*c;while(*c^0){if(p>*c){if(c-&s[0]>1)return 0;while(*c^0){if(p<*c)return 0;p=*c;c++;}return 2;}p=*c;c++;}return 1;}
Returns 0 if not wawy, 1 if wawy and raising, 2 if decreasing.
Ruby, 54 bytes
->w{c=w.chars.uniq;c==(s=c.sort)?2:(c==s.reverse)?1:0}
Returns 0 if the word is not wavy, 1 if backwards wavy, and 2 if forwards wavy.
Python 2, 53 52 50 bytes
Expects input enclosed in quotes, e.g. "watch"
As unnamed lambda:
lambda s:(sum(map(cmp,s[1:],s))+1)/min(len(s)-1,3)
Sums the sign of difference between each letters and integer-divides by len-1. If all were 1 (increasing) the sum is len-1 it displays 1, similar for decreasing -1 and for mixed 1,-1 the sum is smaller than len-1 so it displays 0.
-1 byte for changing cmp,s[1:],s[:-1]) to cmp,s[1:],s)+1
Jelly, 10 bytes
OIṠḟ0µL’aQ
TryItOnline! or run all test cases
Returns:
[1] for wavy increasing
[-1] for wavy decreasing
something else otherwise ([], [0], [-1,1], or [1,-1])
(Declared as unnecessary: For a single value for each OIṠḟ0µL’aQS (11 bytes) will return 1, -1, and 0 respectively.)
How?
OIṠḟ0µL’aQ - Main link: s
O - cast to ordinals
I - incremental differences
Ṡ - sign (-1 for decreasing, 0 for no change, 1 for increasing)
ḟ0 - filter out zeros
µ - monadic chain separation
L - length
’ - decremented
a - and
Q - unique items
Python 3, 77 75 bytes
lambda x:(len(set(x))>2)*(list(x)==sorted(x)or(list(x)==sorted(x)[::-1])*2)
Assumes all letters are of the same case.
Returns:
0if not wavy1if forwards wavy2if backwards wavy
Removed unnecessary spaces thanks @ETHproductions
Python 2, 54 bytes
lambda s:[2<len(set(s))<s[::b]==sorted(s)for b in-1,1]
Takes input as a list of characters. Outputs:
[False, True] for ascending
[True, False] for descending
[False, False] for neither
Checks if the sorted input string equals its original or reverse. Does so by slicing with step sizes of 1 and -1. At the same time, we check whether the word has at least 2 distinct letters.
If "exit with error" can be used an output for the neither case, we can go down to 51 bytes:
lambda s:[s,s[::-(len(set(s))>2)]].index(sorted(s))