コードゴルフ4月版:カッ発言葉
-
23-08-2019 - |
質問
では、以下のリストの長いトップ十字数最小のプログラム可能:
入力ファイル
Washington Washington Adams Jefferson Jefferson Madison Madison Monroe Monroe John Quincy Adams Jackson Jackson Van Buren Harrison DIES Tyler Polk Taylor DIES Fillmore Pierce Buchanan Lincoln Lincoln DIES Johnson Grant Grant Hayes Garfield DIES Arthur Cleveland Harrison Cleveland McKinley McKinley DIES Teddy Roosevelt Teddy Roosevelt Taft Wilson Wilson Harding Coolidge Hoover FDR FDR FDR FDR Dies Truman Truman Eisenhower Eisenhower Kennedy DIES Johnson Johnson Nixon Nixon ABDICATES Ford Carter Reagan Reagan Bush Clinton Clinton Bush Bush Obama
でoffに bash97 文字
cat input.txt | tr " " "\n" | tr -d "\t " | sed 's/^$//g' | sort | uniq -c | sort -n | tail -n 10
出力:
2 Nixon 2 Reagan 2 Roosevelt 2 Truman 2 Washington 2 Wilson 3 Bush 3 Johnson 4 FDR 7 DIES
休みの関係について。嬉しい位置
人のケア情報の社長です こちらの.
解決
短いシェルバージョン:
xargs -n1 < input.txt | sort | uniq -c | sort -nr | head
したい場合は大文字と小文字を区別ランキング変更 uniq -c
入 uniq -ci
.
わずかに短いものであれば、嬉しいランクに晒されている、読みや障害により不足です。この時計で46文字:
xargs -n1<input.txt|sort|uniq -c|sort -n|tail
(きストリップダ38ばたの名前の変更のための入力ファイルで"私"をします。
観測は、この特別な場合には、単語の発生以上9時まで削り3文字の落下による'n'引数を並べ替え:
xargs -n1<input.txt|sort|uniq -c|sort|tail
ここのソリューションを43文字をなくしたい"入力ファイルです。(35ばください。)
を使用 xargs -n1
分割ファイルを一つの単語から各行のが好 tr \ \\n
液などを作るの空白ます。これは、ソリューションが正しくないので、ミスを出しニクソンとして空の文字列を示す最大256ます。しかし、空の文字列ではない"です。
他のヒント
スレッドセーフで、C#、153:
ファイルを読み込む時 p
と印刷結果のコンソール:
File.ReadLines(p)
.SelectMany(s=>s.Split(' '))
.GroupBy(w=>w)
.OrderBy(g=>-g.Count())
.Take(10)
.ToList()
.ForEach(g=>Console.WriteLine(g.Count()+"|"+g.Key));
場合にだけを製造するリストが印刷、コンソールで93文字です。
6|DIES
4|FDR
3|Johnson
3|Bush
2|Washington
2|Adams
2|Jefferson
2|Madison
2|Monroe
2|Jackson
vim60
:1,$!tr " " "\n"|tr -d "\t "|sort|uniq -c|sort -n|tail -n 10
Vim36
:%s/\W/\r/g|%!sort|uniq -c|sort|tail
ウめに102件の文字(wowので、内容はオリジナル):
import List
(take 10.map snd.sort.map(\(x:y)->(-length y,x)).group.sort.words)`fmap`readFile"input.txt"
J, み55文字:
10{.\:~~.(,.~[:<"0@(+/)=/~);;:&.><;._2[1!:1<'input.txt'
(かなる方法を見つけ出すこと洗練を行うテキスト操作J...かにより配列構造をもつデータです。)
NB. ファイルの読み込み <1!:1<'input.txt' +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------... | Washington Washington Adams Jefferson Jefferson Madison Madison Monroe Monroe John Quincy Adams Jackson Jackson Van Buren Harrison DIES Tyler Polk Taylor DIES Fillmore Pierce ... +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------... NB. 分割線 <;._2[1!:1<'input.txt' +--------------+--------------+---------+-------------+-------------+-----------+-----------+----------+----------+---------------------+-----------+-----------+-------------+-----------------+---------+--------+---------------+------------+----------+----... | Washington| Washington| Adams| Jefferson| Jefferson| Madison| Madison| Monroe| Monroe| John Quincy Adams| Jackson| Jackson| Van Buren| Harrison DIES| Tyler| Polk| Taylor DIES| Fillmore| Pierce| ... +--------------+--------------+---------+-------------+-------------+-----------+-----------+----------+----------+---------------------+-----------+-----------+-------------+-----------------+---------+--------+---------------+------------+----------+----... NB. 分の言葉 ;;:&.><;._2[1!:1<'input.txt' +----------+----------+-----+---------+---------+-------+-------+------+------+----+------+-----+-------+-------+---+-----+--------+----+-----+----+------+----+--------+------+--------+-------+-------+----+-------+-----+-----+-----+--------+----+------+---... |Washington|Washington|Adams|Jefferson|Jefferson|Madison|Madison|Monroe|Monroe|John|Quincy|Adams|Jackson|Jackson|Van|Buren|Harrison|DIES|Tyler|Polk|Taylor|DIES|Fillmore|Pierce|Buchanan|Lincoln|Lincoln|DIES|Johnson|Grant|Grant|Hayes|Garfield|DIES|Arthur|Cle... +----------+----------+-----+---------+---------+-------+-------+------+------+----+------+-----+-------+-------+---+-----+--------+----+-----+----+------+----+--------+------+--------+-------+-------+----+-------+-----+-----+-----+--------+----+------+---... NB. カウントreptititions |:~.(,.~[:<"0@(+/)=/~);;:&.><;._2[1!:1<'input.txt' +----------+-----+---------+-------+------+----+------+-------+---+-----+--------+----+-----+----+------+--------+------+--------+-------+-------+-----+-----+--------+------+---------+--------+---------+----+------+-------+--------+------+---+------+------... |2 |2 |2 |2 |2 |1 |1 |2 |1 |1 |2 |6 |1 |1 |1 |1 |1 |1 |2 |3 |2 |1 |1 |1 |2 |2 |2 |1 |2 |1 |1 |1 |4 |2 |2 ... +----------+-----+---------+-------+------+----+------+-------+---+-----+--------+----+-----+----+------+--------+------+--------+-------+-------+-----+-----+--------+------+---------+--------+---------+----+------+-------+--------+------+---+------+------... |Washington|Adams|Jefferson|Madison|Monroe|John|Quincy|Jackson|Van|Buren|Harrison|DIES|Tyler|Polk|Taylor|Fillmore|Pierce|Buchanan|Lincoln|Johnson|Grant|Hayes|Garfield|Arthur|Cleveland|McKinley|Roosevelt|Taft|Wilson|Harding|Coolidge|Hoover|FDR|Truman|Eisenh... +----------+-----+---------+-------+------+----+------+-------+---+-----+--------+----+-----+----+------+--------+------+--------+-------+-------+-----+-----+--------+------+---------+--------+---------+----+------+-------+--------+------+---+------+------... NB. ソート |:\:~~.(,.~[:<"0@(+/)=/~);;:&.><;._2[1!:1<'input.txt' +----+---+-------+----+------+----------+------+---------+------+-----+------+--------+-------+-------+---------+-------+--------+-----+----------+-------+---------+-----+---+-----+------+----+------+----+------+-----+-------+----+------+-----+-------+----... |6 |4 |3 |3 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |2 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1 |1 ... +----+---+-------+----+------+----------+------+---------+------+-----+------+--------+-------+-------+---------+-------+--------+-----+----------+-------+---------+-----+---+-----+------+----+------+----+------+-----+-------+----+------+-----+-------+----... |DIES|FDR|Johnson|Bush|Wilson|Washington|Truman|Roosevelt|Reagan|Nixon|Monroe|McKinley|Madison|Lincoln|Jefferson|Jackson|Harrison|Grant|Eisenhower|Clinton|Cleveland|Adams|Van|Tyler|Taylor|Taft|Quincy|Polk|Pierce|Obama|Kennedy|John|Hoover|Hayes|Harding|Garf... +----+---+-------+----+------+----------+------+---------+------+-----+------+--------+-------+-------+---------+-------+--------+-----+----------+-------+---------+-----+---+-----+------+----+------+----+------+-----+-------+----+------+-----+-------+----... NB. 10 10{.\:~~.(,.~[:<"0@(+/)=/~);;:&.><;._2[1!:1<'input.txt' +-+----------+ |6|DIES | +-+----------+ |4|FDR | +-+----------+ |3|Johnson | +-+----------+ |3|Bush | +-+----------+ |2|Wilson | +-+----------+ |2|Washington| +-+----------+ |2|Truman | +-+----------+ |2|Roosevelt | +-+----------+ |2|Reagan | +-+----------+ |2|Nixon | +-+----------+
Perl:90
Perl:114 を含むperl、コマンドラインスイッチ、シングルクォーテーションとファイル名)
perl -nle'$h{$_}++for split/ /;END{$i++<=10?print"$h{$_} $_":0for reverse sort{$h{$a}cmp$h{$b}}keys%h}' input.txt
の不足 AWK です。
xargs -n1<input.txt|awk '{c[$1]++}END{for(p in c)print c[p],p|"sort|tail"}'
75文字です。
取得したい場合は少AWKyきを忘れてxargs:
awk -v RS='[^a-zA-Z]' /./'{c[$1]++}END{for(p in c)print c[p],p|"sort|tail"}' input.txt
私のベストしてもいいのですが、rubyこれまでに166字:
h = Hash.new
File.open('f.l').each_line{|l|l.split(/ /).each{|e|h[e]==nil ?h[e]=1:h[e]+=1}}
h.sort{|a,b|a[1]<=>b[1]}.last(10).each{|e|puts"#{e[1]} #{e[0]}"}
私が驚いたことのない掲載してcrazy Jの解決にも至っていない。
こちらは圧縮された版のシェルスクリプトを見るために合理的な解釈の入力データ(なしに前後にブランクス)のtrのsedコマンドを変更しないでくださいデータ検証の挿入'ティーです。N'に合わせた最適なポイントの確認や出力ファイルサイズ-全く同じものです。シェルニーズの少ない空間を人間よいや猫の代わりに入力I/Oリダイレクト廃棄物の空間です。
tr \ \\n<input.txt|sort|uniq -c|sort -n|tail -10
この重さを50文字を含む改行末記述します。
二以上の観測(引っ張り、他の人の回答):
tail
そのままでは相当するtail -10
'、- この場合、数字やアルファソ、
これが縮小し、さらに7文字(43どの末尾の改行コード):
tr \ \\n<input.txt|sort|uniq -c|sort|tail
使用'xargs -n1
'(prefixコマンドを与え)の代わりに'tr
"は非常に賢;では、末尾に、複数の組み込みの空間(このソリューションはありません).
vim38 作品のすべての入力
:%!xargs -n1|sort|uniq -c|sort -n|tail
Python2.6, 104 chars:
l=open("input.txt").read().split()
for c,n in sorted(set((l.count(w),w) for w in l if w))[-10:]:print c,n
python 3.1(88字)
import collections
collections.Counter(open('input.txt').read().split()).most_common(10)
改正に 前回入国 るべき保存10文字:
h = {}
File.open('f.1').each {|l|l.split(/ /).each{|e|h[e]==nil ?h[e]=1:h[e]+=1}}
h.sort{|a,b|a[1]<=>b[1]}.last(10).each{|e|puts"#{e[1]} #{e[0]}"}
Perl
86文字
94た場合、カウントの入力ファイル名を指定します。
perl -anE'$_{$_}++for@F;END{say"$_{$_} $_"for@{[sort{$_{$b}<=>$_{$a}}keys%_]}[0..10]}' test.in
さえ気にしなければどのように多くの結果を得、その75を除くと、ファイル名を指定します。
perl -anE'$_{$_}++for@F;END{say"$_{$_} $_"for sort{$_{$b}<=>$_{$a}}keys%_}' test.in
Ruby66B
puts (a=$<.read.split).uniq.map{|x|"#{a.count x} "+x}.sort.last 10
Ruby
115字
w = File.read($*[0]).split
w.uniq.map{|x| [w.select{|y|x==y}.size,x]}.sort.last(10).each{|z| puts "#{z[1]} #{z[0]}"}
Windowsバッチファイル
これは言うまでもなく最小の溶液になったのですがとにかく、したものとみなされます。:)※バッチファイルの使用の一時ファイル名 $ の保存の仮ます。
原作された、圧縮解除されたバージョンのコメント:
@echo off
setlocal enableextensions enabledelayedexpansion
set infile=%1
set cnt=%2
set tmpfile=$
set knownwords=
rem Calculate word count
for /f "tokens=*" %%i in (%infile%) do (
for %%w in (%%i) do (
rem If the word hasn't already been processed, ...
echo !knownwords! | findstr "\<%%w\>" > nul
if errorlevel 1 (
rem Count the number of the word's occurrences and save it to a temp file
for /f %%n in ('findstr "\<%%w\>" %infile% ^| find /v "" /c') do (
echo %%n^|%%w >> %tmpfile%
)
rem Then add the word to the known words list
set knownwords=!knownwords! %%w
)
)
)
rem Print top 10 word count
for /f %%i in ('sort /r %tmpfile%') do (
echo %%i
set /a cnt-=1
if !cnt!==0 goto end
)
:end
del %tmpfile%
圧縮&obfuscated版 317 文字:
@echo off&setlocal enableextensions enabledelayedexpansion&set n=%2&set l=
for /f "tokens=*" %%i in (%1)do for %%w in (%%i)do echo !l!|findstr "\<%%w\>">nul||for /f %%n in ('findstr "\<%%w\>" %1^|find /v "" /c')do echo %%n^|%%w>>$&set l=!l! %%w
for /f %%i in ('sort /r $')do echo %%i&set /a n-=1&if !n!==0 del $&exit /b
この短くすることが可能である 258 文字の場合echoは既にオフコマンドを拡張および遅延変動の拡大は:
set n=%2&set l=
for /f "tokens=*" %%i in (%1)do for %%w in (%%i)do echo !l!|findstr "\<%%w\>">nul||for /f %%n in ('findstr "\<%%w\>" %1^|find /v "" /c')do echo %%n^|%%w>>$&set l=!l! %%w
for /f %%i in ('sort /r $')do echo %%i&set /a n-=1&if !n!==0 del $&exit /b
使用量:
> filename.bat input.txt 10 & pause
出力:
6|DIES
4|FDR
3|Johnson
3|Bush
2|Wilson
2|Washington
2|Truman
2|Roosevelt
2|Reagan
2|Nixon