Skip to main content
Commonmark migration
Source Link

#R, 96 95 bytes

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:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns 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 than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).

#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:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns 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 than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).

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:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns 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 than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).

added 18 characters in body
Source Link
Billywob
  • 3.5k
  • 13
  • 16

#R, 9696 95 bytes

function(x,d=diff(rle(utf8ToInt(x))$v))if(any(d>0)&any(d<0)|length|sum(d1|d)<2)3 else`if`(all(d<1),2,1) 

Returns:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns the non-repeating values of the sequence (i.e. collapsing all runs). Finally take the difference.
  • if(any(d>0)&any(d<0)|length|sum(d1|d)<2)3: If at least one of the differences are positive and at least one negative, or if the difference sequence has less than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).

#R, 96 bytes

function(x,d=diff(rle(utf8ToInt(x))$v))if(any(d>0)&any(d<0)|length(d)<2)3 else`if`(all(d<1),2,1) 

Returns:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns the non-repeating values of the sequence (i.e. collapsing all runs). Finally take the difference.
  • if(any(d>0)&any(d<0)|length(d)<2)3: If at least one of the differences are positive and at least one negative, or if the difference sequence has less than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).

#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:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns 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 than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).

Source Link
Billywob
  • 3.5k
  • 13
  • 16

#R, 96 bytes

function(x,d=diff(rle(utf8ToInt(x))$v))if(any(d>0)&any(d<0)|length(d)<2)3 else`if`(all(d<1),2,1) 

Returns:

  • 1 for wavy and raising
  • 2 for wavy and decreasing
  • 3 for non-wavy

Explained

  • d=diff(rle(utf8ToInt(x))$v): Generates a variable d by first converting the string into it's ASCII values using utf8ToInt which conveniently returns a vector. Subsequently perform run length encoding using rle. rle(...)$v returns the non-repeating values of the sequence (i.e. collapsing all runs). Finally take the difference.
  • if(any(d>0)&any(d<0)|length(d)<2)3: If at least one of the differences are positive and at least one negative, or if the difference sequence has less than 2 elements (equivalent to the original word having less than 3 characters), the word is non-wavy and return 3
  • else``if``(all(d<1),2,1): Else if all differences are negative, return 2 for wavy and decreasing, else return 1 for 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).