Skip to main content
added 62 characters in body
Source Link
lebatsnok
  • 403
  • 4
  • 7

R, 196 189 170 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i)"[<-"(x,i:(j<-i+1),c(a<-substr(x[i],1,l(x[j])),sub(a,x[j],x[i])));while(!is.na(o(x)))x=d(x,o(x));x} 

A human-readable version:

f2<f<-function(x){ l=nchar;   o=function# find the first line in x that is longer than the next line # if no such line exists o(x) will be NA o = function(y) which(diff(l(y))<0)[1]   # od(x,i) --> the no. ofclips firstthe line thati isin longerx, thanadding the next lineremainder orto NAx[i+1] d=functiond = function(x,i) "[<-"(x,i:(j<-i+1), c( a<-substr(x[i],1,l(x[j])), sub(a,x[j],x[i]))) # a --> clipped x[i], sub(a,x[j],x[i]) --> expanded x[j]   # d(x,i) --> clips the line i in x, adding the remainder to x[i+1] while(!is.na(o(x)))x=d(x,o(x));x } 

How it works:

  1. Take the first "bad" line, i.e., line which is longer than the next line, take the "extra" part and add it to the next line
  2. IfCheck if there are any "bad" lines left, if yes go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 

R, 196 189 170 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i)"[<-"(x,i:(j<-i+1),c(a<-substr(x[i],1,l(x[j])),sub(a,x[j],x[i])));while(!is.na(o(x)))x=d(x,o(x));x} 

A human-readable version:

f2<-function(x){ l=nchar; o=function(y)which(diff(l(y))<0)[1] # o(x) --> the no. of first line that is longer than the next line or NA d=function(x,i)"[<-"(x,i:(j<-i+1), c( a<-substr(x[i],1,l(x[j])), sub(a,x[j],x[i]))) # a --> clipped x[i], sub(a,x[j],x[i]) --> expanded x[j]   # d(x,i) --> clips the line i in x, adding the remainder to x[i+1] while(!is.na(o(x)))x=d(x,o(x));x } 

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 

R, 196 189 170 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i)"[<-"(x,i:(j<-i+1),c(a<-substr(x[i],1,l(x[j])),sub(a,x[j],x[i])));while(!is.na(o(x)))x=d(x,o(x));x} 

A human-readable version:

f<-function(x){ l=nchar;   # find the first line in x that is longer than the next line # if no such line exists o(x) will be NA o = function(y) which(diff(l(y))<0)[1]   # d(x,i) --> clips the line i in x, adding the remainder to x[i+1] d = function(x,i) "[<-"(x,i:(j<-i+1), c(a<-substr(x[i],1,l(x[j])), sub(a,x[j],x[i]))) # a --> clipped x[i], sub(a,x[j],x[i]) --> expanded x[j] while(!is.na(o(x)))x=d(x,o(x));x } 

How it works:

  1. Take the first "bad" line, i.e., line which is longer than the next line, take the "extra" part and add it to the next line
  2. Check if there are any "bad" lines left, if yes go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 
added 109 characters in body
Source Link
lebatsnok
  • 403
  • 4
  • 7

R, 196 189189 170 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i){j=i+1;xi=x[i];xj=x[j];"[<"[<-"(x,i:j(j<-i+1),c(a<-substr(xix[i],1,l(xjx[j])),"substr<-"sub(xi,1a,l(xj)x[j],xjx[i])))};while(!is.na(o(x)))x=d(x,o(x));x} 

A human readable-readable version:

f<f2<-function(x){ l=nchar; o=function(y)which(diff(l(y))<0)[1] d=function# o(x,i){  --> the no. j=i+1;xi=x[i];xj=x[j]; of first line that is longer than the next line or NA d=function(x,i)"[<-"(x,i:j(j<-i+1), c( a<-substr(xix[i],1,l(xjx[j])),"substr<-" sub(xia,1x[j],l(xjx[i]),xj)) # a --> clipped x[i], sub(a,x[j],x[i]) --> expanded x[j] } # d(x,i) --> clips the line i in x, adding the remainder to x[i+1] while(!is.na(o(x)))x=d(x,o(x));x }     

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 

R, 196 189 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i){j=i+1;xi=x[i];xj=x[j];"[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))};while(!is.na(o(x)))x=d(x,o(x));x} 

A human readable version:

f<-function(x){ l=nchar; o=function(y)which(diff(l(y))<0)[1] d=function(x,i){  j=i+1;xi=x[i];xj=x[j];  "[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj))) } while(!is.na(o(x)))x=d(x,o(x));x }    

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 

R, 196 189 170 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i)"[<-"(x,i:(j<-i+1),c(a<-substr(x[i],1,l(x[j])),sub(a,x[j],x[i])));while(!is.na(o(x)))x=d(x,o(x));x} 

A human-readable version:

f2<-function(x){ l=nchar; o=function(y)which(diff(l(y))<0)[1] # o(x) --> the no. of first line that is longer than the next line or NA d=function(x,i)"[<-"(x,i:(j<-i+1), c( a<-substr(x[i],1,l(x[j])), sub(a,x[j],x[i]))) # a --> clipped x[i], sub(a,x[j],x[i]) --> expanded x[j]  # d(x,i) --> clips the line i in x, adding the remainder to x[i+1] while(!is.na(o(x)))x=d(x,o(x));x }   

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 
added 1 character in body
Source Link
lebatsnok
  • 403
  • 4
  • 7

R, 196196 189 bytes

function(x){l=nchar;o=function(y)headwhich(diff(l(y),-1)>l(y<0)[-1];d=function[1];d=function(x,i){j=i+1;xi=x[i];xj=x[j];"[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))};while(any!is.na(o(x)))x=d(x,which(o(x))[1]);x} 

A human readable version:

f<-function(x){ l=nchar; o=function(y)headwhich(diff(l(y),-1)>l(y<0)[-1];[1] d=function(x,i){ j=i+1;xi=x[i];xj=x[j]; "[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))   } while(any!is.na(o(x)))x=d(x,which(o(x))[1]);x }  

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 

R, 196 bytes

function(x){l=nchar;o=function(y)head(l(y),-1)>l(y)[-1];d=function(x,i){j=i+1;xi=x[i];xj=x[j];"[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))};while(any(o(x)))x=d(x,which(o(x))[1]);x} 

A readable version:

f<-function(x){ l=nchar; o=function(y)head(l(y),-1)>l(y)[-1]; d=function(x,i){ j=i+1;xi=x[i];xj=x[j]; "[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))   } while(any(o(x)))x=d(x,which(o(x))[1]);x } 

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 

R, 196 189 bytes

function(x){l=nchar;o=function(y)which(diff(l(y))<0)[1];d=function(x,i){j=i+1;xi=x[i];xj=x[j];"[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj)))};while(!is.na(o(x)))x=d(x,o(x));x} 

A human readable version:

f<-function(x){ l=nchar; o=function(y)which(diff(l(y))<0)[1] d=function(x,i){ j=i+1;xi=x[i];xj=x[j]; "[<-"(x,i:j,c(substr(xi,1,l(xj)),"substr<-"(xi,1,l(xj),xj))) } while(!is.na(o(x)))x=d(x,o(x));x }  

How it works:

  1. Take the first line which is longer than the next line, take the "extra" part and add it to the next line
  2. If there are any "bad" lines left, go to #1

(Or in other words, "superfluous" parts fall down until everything that can fall down has fallen down.)

Input: a character vector.

x<-readLines(textConnection("Programming\nPuzzles\n&\nCode\nGolf")) f(x) # [1] "P" "Prog" "&uzz" "Coderam" "Golflesming" 
Source Link
lebatsnok
  • 403
  • 4
  • 7
Loading