Language detail: Pnuts
Coverage: 31.06%
|
number of '+' ratings |
contribution for coverage |
Unsolved challenges
- echoクライアント (Nested Flatten)
- LL Golf Hole 4 - 文章から単語の索引を作る (Nested Flatten)
- LL Golf Hole 3 - 13日の金曜日を数え上げる (Nested Flatten)
- LL Golf Hole 2 - 文字列に含まれる単語の最初の文字を大文字にする (Nested Flatten)
- tailの実装 (Nested Flatten)
codes
重複無し乱数
(Nested
Flatten)
1 2 3 4 | import java.util.*
function bingo(n) {Collections.shuffle(x = list(range(1,n))); x}
println(bingo(10))
|
全ての組み合わせ
(Nested
Flatten)
すこし簡略化しました。
1 2 3 4 5 6 7 8 9 10 | function cp(lists){
if (size(lists) == 0){
yield {}
} else {
for (lss : cp(lists[1..])){
for (ls : lists[0]) yield {ls} + lss
}
}
}
printAll(cp([{1,2,3,4}, "abc"]))
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function cp(lists){
if (size(lists) == 1){
yield project(lists[0], {_x->{_x}})
} else {
for (r : cp(lists[1..])){
for (m:r){
for (a:project(lists[0], {e-> {e}+ m})) yield {a}
}
}
}
}
function listcp(lists) list(project(cp(lists), {x->x[0]}))
println(listcp([[1,2,3,4], "abc"]))
|
複数行のコメントアウト
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 | /*
Java風コメントアウト
*/
`
バッククォートで囲んだ部分は文字列リテラルになる
`
if (false){
// コメントアウトではないけど if 文を使うことも
}
|
JPEGをGETして色反転して保存
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 | import java.awt.image.*
b=list(project(range(255,0), byte))
invert_op = LookupOp(ByteLookupTable(0,[b,b,b]), null)
function invert(imagefile, dest){
im = readImage(imagefile)
im2 = makeImage(im.width, im.height, null, im.type)
invert_op.filter(im, im2)
writeImage(im2, dest)
}
invert("white-pants.jpg", "black-pants.jpg")
|
アクセスログのIPアドレスを逆引き
(Nested
Flatten)
dnsjava使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | concurrency = 100
n = 1000
inputfile = "access_log"
outputfile "access_log_out"
function processLog(input){
nameservice = LRUcache(10000, reverseDns)
pool = threadPool(concurrency)
t = Object[n]
i = 0
for (line: readLines(input)){
idx = line.indexOf(' ')
t[i] = async({line, idx -> {->[nameservice[line[0..idx-1]], line[idx..]]}}(line, idx), pool)
if (++i >= n){
for (j: 0..n-1){
r0, r1 = t[j]()
println(r0, r1)
}
i = 0
}
}
}
import org.xbill.DNS.*
function reverseDns(hostIp){
answers = Lookup(ReverseMap.fromAddress(hostIp), Type.PTR, DClass.IN).run()
(answers == null || answers.length == 0) ? hostIp : answers[0].rdataToString()
}
input = reader(inputfile, "ASCII")
output = writer(outputfile, "ASCII")
addShutdownHook({->output.close(); input.close()})
getContext().setWriter(output)
processLog(input)
|
整数の漢数字表記
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | function num2K(num){
table = {0=>"", 1=>"十",2=>"百",3=>"千",4=>"万",8=>"億",12=>"兆",16=>"京"}
table2 = ["","一","二","三","四","五","六","七","八","九"]
if (num == 0) return "零"
i = 0
s = {}
for (v : project(reverse(string(num)), {a->a-'0'})){
if (table.containsKey(i)){
s.add(table2[v] + table[i] + (i > 3 ? " " : "" ))
} else if (v > 1){
s.add(table2[v] + table[i%4])
} else if (v != 0){
s.add(table[i%4])
}
i++
}
join("", reverse(s))
}
print(">>"); flush(); println(num2K(readLine(System.in)))
|
モノクロ画像の類似検索
(Nested
Flatten)
Core2 Duo1.83GHz、pnuts -J-server -O で起動して1.3秒 (配列の初期化を除く)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | N_WORDS = 1024*768/32
fb = new int[100][N_WORDS]
for (j:0..99){
for (i:0..N_WORDS-1) fb[j][i] = random()
}
distance = makeProxy(Integer.getMethod("bitCount", [int]))
function sim(f1,f2){
d = 0
for (i:0..N_WORDS-1) d += distance(f1[i]^f2[i])
d
}
function sim(f1,f2, min){
d = 0
for (i:0..N_WORDS-1) {
d += distance(f1[i]^f2[i])
if (d > min) return d
}
d
}
function findOne(f0, buffers){
idx = 0
min = sim(f0, buffers[0])
for (i: 1.. size(buffers)-1){
f = buffers[i]
m = sim(f0, f, min)
if (m < min){
min = m
idx = i
}
}
idx
}
s=currentTimeMillis()
println(findOne(fb[0], fb[1..])+1, "th image")
println((currentTimeMillis()-s), " msec")
|
入出力の中継
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 | function relay(A, B){
rt = Runtime.getRuntime()
a = rt.exec(A)
b = rt.exec(B)
t1 = fork({->read(a.inputStream, b.outputStream); a.waitFor(); b.destroy() })
t2 = fork({->read(b.inputStream, a.outputStream); b.waitFor(); a.destroy() })
t1.join()
t2.join()
}
|
「組合せ型の最小完全ハッシュ関数」の逆関数
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function c(n, r){
k = 1
for (i = (r+1); i <= n; i++) k *= i
for (i = 2; i <= (n-r); i++) k /= i
k
}
function d(n, r, val){
pat = int[n]
for (idx : 1 .. n){
x = c(n - idx, r)
if (val >= x){
pat[idx - 1] = 1
r--
val -= x
}
}
pat
}
|
長方形の交差判定
(Nested
Flatten)
1 2 3 | function overlap(r1,r2) {
r1.l < r2.r && r2.l < r1.r && r1.b < r2.t && r2.b < r1.t
}
|
ローカル変数の一覧を取得
(Nested
Flatten)
この技は pnuts -pure で起動したときだけ使えます。
1 2 3 4 5 6 7 | import pnuts.tools.StackFrameInspector
function f(){
x=1
y=2
StackFrameInspector.localSymbols(getContext())
}
println(f())
|
RFC 4180対応版 CSVレコードの分解
(Nested
Flatten)
別途csvモジュールが必要。 http://pnuts.org/extensions/csv/
1 2 3 4 5 | use("csv")
for (columns: readCSV("csv.txt")){
i=0
for(c:columns) println(++i, " => ", c)
}
|
メソッド名一覧の表示
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 | class Foo {
test_foo(){println("test_foo")}
test_foo2(){println("test_foo2")}
test_foo3(){println("test_foo3")}
public_foo(){println("public_foo")}
}
f = Foo()
for(m:f.class.methods[{m->m.name.startsWith("test_")}]){
m.invoke(f, [])
}
|
Tiny MML
(Nested
Flatten)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import javax.sound.midi.*
import static javax.sound.midi.ShortMessage.*
code = [69, 71, 60, 62, 64, 65, 67]
TONE = 120
function play(mml){
mes = new ShortMessage()
rcvr = MidiSystem.getReceiver()
for (c: mml) {
if (c >= 'a' && c <= 'g') {
co = code[c - 'a']
mes.setMessage(NOTE_ON, 0, co, TONE)
rcvr.send(mes, -1)
sleep(500)
mes.setMessage(NOTE_OFF, 0, co)
rcvr.send(mes, -1)
} else {
sleep(500)
}
}
rcvr.close()
}
play("cdefedcrefgagfercrcrcrcrcdefedcr")
|
マップの通り抜け
(Nested
Flatten)
next >>
使用例: println(solve(stringReader( ".+.... .+.+++ .+.+.+ .+++.+ +....+")))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | function solve(f){
m = list()
for (line : scanLines(f)) m.add(list(project(line, {c->c=='+' ? 1 : 0})))
rows = size(m)
columns = size(m[0])
active = list()
for (j:0..columns - 1) if (m[0][j] == 1) { m[0][j] = 2 ; active.add([0,j]) }
function step(i, j, activeList){
if (i + 1 == rows - 1 && m[i+1][j] == 1) return true
for (x,y : {[i,j+1], [i,j-1], [i-1,j],[i+1,j]}){
if (x >= 0 && y >= 0 && y <= columns-1 && m[x][y] == 1) {m[x][y]++; activeList.add([x,y])}
}
m[i][j] = 3 // no longer active
false
}
for (k: 0..(rows - 2) * columns + 1){
newActiveList = list()
for (i,j : active){
if (step(i, j, newActiveList)) return true
}
if (size(newActiveList) < 1) return false
active = newActiveList
}
false
}
|

odz #4335() [ Pnuts ] Rating0/0=0.00
たまには Pnuts などを
function sum_perms(n, m) { if (m == 1) { yield [n] } else if (n == 0) { yield new int[m] } else { for (i : n..0) { for (rest : sum_perms(n - i, m - 1)) { yield [i] + rest } } } } function main(args) { if (args.length == 2) { n, m = project(args, function(o) { int(o) }) } else { n, m = [5, 3] } for (t : sum_perms(n, m)) { println(join(' ', t)) } } main($args[1..])Rating0/0=0.00-0+
[ reply ]