Skip to main content
deleted 199 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

Julia, 165 155 151 129129 102 bytes

s->(x=splitg(s,r"\W",keep=1<0d=[]);d=r=[];for i=1:endof=join(x);filter(i->i!=0,[(c=lcfirst(x[i]w)[1])∈d?(r=[r;i])0:(d=[d;c]d=[d;c];w)end;deleteat!for w=split(xs,rr"\W",keep=1<0);join(x])," "))

This is an anonymousa function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function fg(s, d=[])
    # Split on unwanted characters, omitting them from the array
string into an array xon =unwanted split(scharacters, r"\W",then keep=false)
for
    # Initialize an arrayeach toword, holdif the indicesfirst ofletter wordshas tobeen removeencountered, populate
    # as well as onethis toelement holdof the first letters ofarray eachwith word
0, otherwise note the dfirst =letter
 r = []

 # and use #the Locateword. allThis indicesresults forin removalan byarray checkingof thewords firstand letterzeros.
    #x of= each[(c word= againstlcfirst(w)[1]) the firstd letters? of0 the: remaining(d words
= [d; c]; w) for iw = 1:endofsplit(xs, r"\W", keep=1<0)]
  
    # Remove the (czeros, =keeping lcfirst(x[i])[1])only the dwords. ?Note (rthat =this [r;works
 i]) : (d =# [d;even c])
if the word is end

the string "0" since #0 Remove!= words"0".
 from the array byz index
= filter(i -> i deleteat!(x= 0, rx)

    # Join the array back into a string, separated byand spacesreturn
    return join(xz, " ")
end

Saved 2653 bytes with help from Sp3000!

Julia, 165 155 151 129 bytes

s->(x=split(s,r"\W",keep=1<0);d=r=[];for i=1:endof(x);(c=lcfirst(x[i])[1])∈d?(r=[r;i]):(d=[d;c])end;deleteat!(x,r);join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    # as well as one to hold the first letters of each word
    d = r = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:endof(x)
        (c = lcfirst(x[i])[1])  d ? (r = [r; i]) : (d = [d; c])
    end

    # Remove words from the array by index
    deleteat!(x, r)

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 26 bytes with help from Sp3000!

Julia, 165 155 151 129 102 bytes

g(s,d=[])=join(filter(i->i!=0,[(c=lcfirst(w)[1])∈d?0:(d=[d;c];w)for w=split(s,r"\W",keep=1<0)])," ")

This is a function that accepts a string and returns a string.

Ungolfed:

function g(s, d=[])
    # Split the string into an array on unwanted characters, then for
    # each word, if the first letter has been encountered, populate
    # this element of the array with 0, otherwise note the first letter
    # and use the word. This results in an array of words and zeros.
    x = [(c = lcfirst(w)[1])  d ? 0 : (d = [d; c]; w) for w = split(s, r"\W", keep=1<0)]
 
    # Remove the zeros, keeping only the words. Note that this works
    # even if the word is the string "0" since 0 != "0".
    z = filter(i -> i != 0, x)

    # Join into a string and return
    return join(z, " ")
end

Saved 53 bytes with help from Sp3000!

added 44 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

Julia, 165 155 151151 129 bytes

s->(x=split(s,r"\W",keep=1<0);d=[];for;d=r=[];for i=1:(L=endofendof(x)),j=i+1:L lcfirst;(c=lcfirst(x[i])[1]==lcfirst[1])∈d?(x[j]r=[r;i])[1]&&:(d=[d;j]d=[d;c])end;deleteat!(x,sort(∪(d))r);join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    # as well as one to hold the first letters of each word
    d = r = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:(L=endofendof(x)), j 
 = i+1:L
      (c = lcfirst(x[i])[1]) == lcfirstd ? (x[j]r = [r; i])[1] &&: (d = [d; j]c])
    end

    # Remove words from the array by index
    deleteat!(x, sort(∪(d))r)

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 326 bytes thanks towith help from Sp3000!

Julia, 165 155 151 bytes

s->(x=split(s,r"\W",keep=1<0);d=[];for i=1:(L=endof(x)),j=i+1:L lcfirst(x[i])[1]==lcfirst(x[j])[1]&&(d=[d;j])end;deleteat!(x,sort(∪(d)));join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    d = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:(L=endof(x)), j = i+1:L
        lcfirst(x[i])[1] == lcfirst(x[j])[1] && (d = [d; j])
    end

    # Remove words from the array by index
    deleteat!(x, sort(∪(d)))

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 3 bytes thanks to Sp3000!

Julia, 165 155 151 129 bytes

s->(x=split(s,r"\W",keep=1<0);d=r=[];for i=1:endof(x);(c=lcfirst(x[i])[1])∈d?(r=[r;i]):(d=[d;c])end;deleteat!(x,r);join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    # as well as one to hold the first letters of each word
    d = r = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:endof(x) 
        (c = lcfirst(x[i])[1])  d ? (r = [r; i]) : (d = [d; c])
    end

    # Remove words from the array by index
    deleteat!(x, r)

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 26 bytes with help from Sp3000!

added 40 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119

Julia, 165 155 152151 bytes

s->(x=split(s,r"\W",keep=1<0);d=[];for i=1:(L=endof(x)),j=i+1:L lcfirst(x[i])[1]==lcfirst(x[j])[1]&&push![1]&&(d,jd=[d;j])end;deleteat!(x,sort(∪(d)));join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    d = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:(L=endof(x)), j = i+1:L
        lcfirst(x[i])[1] == lcfirst(x[j])[1] && push!(d, j= [d; j])
    end

    # Remove words from the array by index
    deleteat!(x, sort(∪(d)))

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 3 bytes thanks to Sp3000!

Julia, 165 155 152 bytes

s->(x=split(s,r"\W",keep=1<0);d=[];for i=1:(L=endof(x)),j=i+1:L lcfirst(x[i])[1]==lcfirst(x[j])[1]&&push!(d,j)end;deleteat!(x,sort(∪(d)));join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    d = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:(L=endof(x)), j = i+1:L
        lcfirst(x[i])[1] == lcfirst(x[j])[1] && push!(d, j)
    end

    # Remove words from the array by index
    deleteat!(x, sort(∪(d)))

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 3 bytes thanks to Sp3000!

Julia, 165 155 151 bytes

s->(x=split(s,r"\W",keep=1<0);d=[];for i=1:(L=endof(x)),j=i+1:L lcfirst(x[i])[1]==lcfirst(x[j])[1]&&(d=[d;j])end;deleteat!(x,sort(∪(d)));join(x," "))

This is an anonymous function that accepts a string and returns a string. To call it, assign it to a variable.

Ungolfed:

function f(s)
    # Split on unwanted characters, omitting them from the array
    x = split(s, r"\W", keep=false)

    # Initialize an array to hold the indices of words to remove
    d = []

    # Locate all indices for removal by checking the first letter
    # of each word against the first letters of the remaining words
    for i = 1:(L=endof(x)), j = i+1:L
        lcfirst(x[i])[1] == lcfirst(x[j])[1] && (d = [d; j])
    end

    # Remove words from the array by index
    deleteat!(x, sort(∪(d)))

    # Join the array back into a string, separated by spaces
    return join(x, " ")
end

Saved 3 bytes thanks to Sp3000!

added 40 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119
Loading
added 14 characters in body
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119
Loading
Source Link
Alex A.
  • 24.7k
  • 5
  • 38
  • 119
Loading