Skip to main content
Commonmark migration
Source Link

R, 65 61 bytes

#R, 65 61 bytes -2 bytes thanks to MickyT

-2 bytes by changing the indexing

returns an anonymous function.

function(s,n,x=nchar(s))`if`(n>x,'',substring(s,x:n-n+1,n:x))

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

x:n-n+1 is equivalent to 1:(x-n+1) since : takes precedence over sums/differences

Try it online!

#R, 65 61 bytes -2 bytes thanks to MickyT

-2 bytes by changing the indexing

returns an anonymous function.

function(s,n,x=nchar(s))`if`(n>x,'',substring(s,x:n-n+1,n:x))

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

x:n-n+1 is equivalent to 1:(x-n+1) since : takes precedence over sums/differences

Try it online!

R, 65 61 bytes

-2 bytes thanks to MickyT

-2 bytes by changing the indexing

returns an anonymous function.

function(s,n,x=nchar(s))`if`(n>x,'',substring(s,x:n-n+1,n:x))

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

x:n-n+1 is equivalent to 1:(x-n+1) since : takes precedence over sums/differences

Try it online!

dropped four bytes
Source Link
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105

#R, 6565 61 bytes -2 bytes thanks to MickyT

-2 bytes by changing the indexing

returns an anonymous function.

function(s,n){,x=nchar(s);`if`)`if`(n>x,'',substring(s,1:(x:n-n+1),n:x))}

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

Try it online!x:n-n+1 is equivalent to 1:(x-n+1) since : takes precedence over sums/differences

Try it online!

#R, 65 bytes

returns an anonymous function.

function(s,n){x=nchar(s);`if`(n>x,'',substring(s,1:(x-n+1),n:x))}

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

Try it online!

#R, 65 61 bytes -2 bytes thanks to MickyT

-2 bytes by changing the indexing

returns an anonymous function.

function(s,n,x=nchar(s))`if`(n>x,'',substring(s,x:n-n+1,n:x))

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

x:n-n+1 is equivalent to 1:(x-n+1) since : takes precedence over sums/differences

Try it online!

Source Link
Giuseppe
  • 28.8k
  • 3
  • 31
  • 105

#R, 65 bytes

returns an anonymous function.

function(s,n){x=nchar(s);`if`(n>x,'',substring(s,1:(x-n+1),n:x))}

substring cycles through the indices (as opposed to substr which does not), and if the starting index is less than 1, it defaults to 1 instead, so it checks and returns the empty string.

Try it online!