入力された文から繰り返し単語をすべて削除します。
入力は次のようにcat dog cat dog bird dog Snake snake Snake
なり、出力はになりますcat dog bird Snake snake
。単語を区切るスペースは常に1つです。
出力順序は入力と同じでなければなりません。(例を参照)
句読点を処理する必要はありませんが、大文字の処理が必要です。
Snake
またsnake
、単に異なるものとして扱われます
入力された文から繰り返し単語をすべて削除します。
入力は次のようにcat dog cat dog bird dog Snake snake Snake
なり、出力はになりますcat dog bird Snake snake
。単語を区切るスペースは常に1つです。
出力順序は入力と同じでなければなりません。(例を参照)
句読点を処理する必要はありませんが、大文字の処理が必要です。
Snake
またsnake
、単に異なるものとして扱われます
回答:
{1↓∊∪(∊∘' '⊂⊢)' ',⍵}
これにより、右側の文字列を受け入れて文字列を返す名前のないモナド関数が作成されます。
説明:
' ',⍵} ⍝ Prepend a space to the input string
(∊∘' '⊂⊢) ⍝ Split the string on spaces using a fork
∪ ⍝ Select the unique elements
{1↓∊ ⍝ Join into a string and drop the leading space
デニスのおかげで2バイト節約できました!
(この回答を参照)
EcmaScript 6準拠のブラウザーで以下のスニペットを実行してテストします(セット、スプレッド演算子、テンプレート文字列、矢印関数を実装しています-私はFirefoxを使用しています)。
注:Setへの変換はすべての重複をドロップし、Setは元の順序を維持します。
f=s=>[...Set(s.split` `)].join` `
function test() { O.innerHTML=f(I.value) }
test()
#I { width: 70% }
<input id=I value="cat dog cat dog bird dog Snake snake Snake"/><button onclick="test()">-></button>
<pre id=O></pre>
TeaScriptはゴルフ用のJavaScriptです。
xs` `u()j` `
This is pretty short. It splits on each space, filters out duplicates, then rejoins.
cdfghijklmnopstuvw
are reserved for variables, they are all pre-initialized to 0. b
is also reserved for a variable name, it is pre-initialized to an empty string
$args|select -u
Whoa, an actual entry where PowerShell is somewhat competitive? That's unpossible!
Takes the string as input arguments, pipes to Select-Object
with the -Unique
flag. Spits out an array of strings, preserving order and capitalization as requested.
PS C:\Tools\Scripts\golfing> .\remove-repeated-words-from-string.ps1 cat dog cat dog bird dog Snake snake Snake
cat
dog
bird
Snake
snake
If this is too "cheaty" in assuming the input can be as command-line arguments, then go for the following, at 24 21 Bytes (saved some bytes thanks to blabb). Interestingly, using the unary operator in this direction happens to also work if the input string is demarcated with quotes or as individual arguments, since the default -split
is by spaces. Bonus.
-split$args|select -u
(\w+)\b(?<=\b\1\b.+)
Save the file with a trailing linefeed and run it with the -s
flag.
This is fairly straight forward in that it matches a single word, and the lookbehind checks whether that same word has appeared in the string before. The trailing linefeed causes Retina to work in Replace mode with an empty replacement string, removing all matches.
StringRiffle@*Keys@*Counts@*StringSplit
StringRiffle[]
.
Keys@Counts
instead of DeleteDuplicates
Keys@Counts
preserve order?
Counts[list]
gives an association whose keys are in the same order as they first occur as elements of list.
Well this is why we're all waiting for Pyth5, could have been 5 bytes.
jdoxzN{cz
#include<iostream>
#include<string>
#include<list>
#include<sstream>
#include<algorithm>
using namespace std;main(){string s;getline(cin,s);list<string>m;stringstream b(s);while(getline(b,s,' '))if(find(m.begin(),m.end(),s)==m.end())m.push_back(s);for(auto a:m)cout<<a<<' ';cout<<endl;}
I don't see a whole lot of C++ answers compared to golfing languages, so why not. Note that this uses C++11 features, and so if your compiler is stuck in the dark ages sufficiently old enough, you may need to pass a special compilation switch to make it use the C++11 standard. For g++
, it's -std=c++11
(only needed for versions < 5.2). Try it online
set
? It allows no duplicates by design. Just push into it.
set
is not guaranteed to have the items in the same order they were added.
" "/?" "\
FYI, this is a function.
" "\ Split the input on spaces
? Find all the unique elements
" "/ Join them back together
unique(d,'stable')
where d
is d = {'cat','dog','cat','dog','bird','dog','Snake','snake','Snake'}
.
The result is 'cat' 'dog' 'bird' 'Snake' 'snake'
d
is already assigned. You can rectify this by using a function handle: @(d)unique(d,'stable')
, at the cost of 4 bytes.
l=[]
for x in input().split():l+=[x][x in l:]
print(*l)
Yeesh, this is long. Unfortunately, Python's set
doesn't keep the order of the elements, so we have to do the work ourselves. We iterate through the input words, keeping a list l
of elements that aren't yet in l
. Then, we print the contents of l
space-separated.
A string version of l
would not work if some words are substrings of other words.
String.Join(" ",s.Split().Distinct());
s
, I think you should get it as an argument.
As a whole program the only way you would write it is 21 bytes long
say $*IN.words.unique # 21 bytes
As a lambda expression the shortest is 14 bytes
*.words.unique # 14 bytes
say ( *.words.unique ).('cat dog cat dog bird dog Snake snake Snake')
my &foo = *.words.unique;
say foo $*IN;
While the output is a List, if you put it in a stringifying context it will put a space between the elements. If it was a requirement to return a string you could just add a ~
to the front ~*.words.unique
.
If snippets were allowed, you could shorten it to 13 bytes by removing the *
.
$_ = 'cat dog cat dog bird dog Snake snake Snake';
say .words.unique
turns out the full program version is shorter
s=input().split(' ')
print(' '.join(e for i,e in enumerate(s)if e not in s[:i]))
Did it without regex, I am happy
function c(a)l={}return a:gsub("%S+",function(b)if l[b]then return""else l[b]=true end end)end
... return""else l[b]=true end end...
with ...return""end l[b]=""end...
.
BEGIN{RS=ORS=" "}!c[$0]++
$ printf "cat dog cat dog bird dog Snake snake Snake" | awk 'BEGIN{RS=ORS=" "}!c[$0]++'
cat dog bird Snake snake $
$
function(s){o={};s.split(' ').map(function(w){o[w]=1});a=[];for(w in o)a.push(w);return a.join(' ')}
// way too long for JS :(
Interestingly, this is almost identical to the non-repeating characters thing.
set x to(display dialog""default answer"")'s text returned's words set o to"" repeat with i in x considering case if not i is in o then set o to o&i&" " end end o
I didn't actually know the considering keyword before this. the more you know...
*\S=${$0;$0}@set{$0;}
(Very similar to the unique character solution, as there are no arrays in Gema, so allowing built-in unique functions not helps us much.)
Sample run:
bash-4.3$ gema '*\S=${$0;$0}@set{$0;}' <<< 'cat dog cat dog bird dog Snake snake Snake'
cat dog bird Snake snake
Assuming $s
is the input string.
print_r(array_flip(explode(' ',$s)));