| use v6; | |
| my $set_abcdef = set 'abcdef'.comb(/./); | |
| sub npr_word($word) { | |
| # too slow | |
| $word.chars == 8 and $set_abcdef (<=) (set $word.comb(/./)); | |
| } | |
| sub MAIN($dict_file='/usr/share/dict/british-english') { | |
| my @words = slurp($dict_file).words; | |
| say grep { npr_word($_) }, @words; | |
| } | |
craft the code in Perl6
implement the same function as in “craft the code in clojure” “http://tapestryjava.blogspot.com/2013/02/crafting-code-in-clojure.html
| use v6; | |
| sub sorted_keys(%map1,%map2) { | |
| my @keys = %map1.keys, %map2.keys; | |
| if @keys { join(",", @keys.uniq.sort) } | |
| else {"<none>"} | |
| } | |
| my %map1 = 1..6; | |
| my %map2 = 3..10; | |
| say sorted_keys(%map1,%map2); | |
| # vim: filetype=perl6:\n |
swap in C
long time no see, C!
I come to see again, finally.
| # include <stdio.h> | |
| void swap0(int a, int b) // change | |
| { | |
| int temp; | |
| printf(" %d\n", temp); | |
| temp = a; | |
| printf(" %d\n", temp); | |
| a = b; | |
| printf(" %d\n", a); | |
| b = temp; | |
| printf(" %d\n", temp); | |
| } | |
| void swap1(int* a, int* b) //do not change | |
| { | |
| int* temp; | |
| printf(" %d\n", *temp); | |
| temp = a; | |
| printf(" %d\n", *temp); | |
| a = b; | |
| printf(" %d\n", *a); | |
| b = temp; | |
| printf(" %d\n", *temp); | |
| } | |
| void swap2(int* a, int* b) // change | |
| { | |
| int temp; | |
| printf(" %d\n", temp); | |
| temp = *a; | |
| printf(" %d\n", temp); | |
| *a = *b; // same with a=b | |
| printf(" %d\n", *a); | |
| *b = temp; | |
| printf(" %d\n", temp); | |
| } | |
| int main() | |
| { | |
| int x = 10; | |
| int y = 20; | |
| printf("%d, %d\n", x, y); | |
| swap1(&x,&y); | |
| printf("%d, %d\n", x, y); | |
| return 0; | |
| } |
install BioPerl
1. install cpanm
2.cpanm –sudo -f BioPerl
done!
5 decimal places of pi
“Given that Pi can be estimated using the function 4 * (1 – 1/3 + 1/5 – 1/7 + …) with more terms giving greater accuracy, write a function that calculates Pi to an accuracy of 5 decimal places.” http://programmers.blogoverflow.com/2012/08/20-controversial-programming-opinions/
| import scala.math._ | |
| /* cacluate pi | |
| Pi = 4*(1 - 1/3 + 1/5 -1/7+....) | |
| write a function that calculates pi to an accuracy of 5 decimal places | |
| */ | |
| // 1/(2*x+1)*4<1e-6 | |
| //4*1e6<2*x+1 | |
| // x>(4*1e6-1)/2 | |
| // x>2*1e6 | |
| def npi(n: Int) = pow(-1,n)/(2*n+1) | |
| val N: Int = 2e6.toInt ; | |
| val pi = (0 to N).map(npi).sum*4 | |
| println(pi) | |
| // calculate the area of a circle | |
| def circleArea(r:Double) = pi*r*r | |
| val a = 3 | |
| println(circleArea(3)) | |
| // vim: set ts=4 sw=4 et: |
Minimum Scalar Product
min Scalar
| sub min_scalar(@xs,@ys) { | |
| [+] @xs.sort <<*>> @ys.sort.reverse; | |
| } | |
| # test | |
| say min_scalar([1..5],[1,0,1,0,1]); # 6 | |
| say min_scalar([1,3,-5],[-2,4,1]); # -25 |
Cellular Automaton
| sub deci_bin($n) { | |
| return $n if $n==0 || $n==1; | |
| my $k = $n div 2; | |
| my $b = $n % 2; | |
| my $E = deci_bin($k); | |
| return $E~$b; | |
| } | |
| sub automata($rule,$row) { | |
| my @keys="111","110","101","100","011","010","001","000"; | |
| my @vals=substr(deci_bin($rule).comb.reverse.join ~'0'x 8,0,8).comb.reverse; | |
| my %rule= @keys Z, @vals; | |
| my @automata="0"x $row ~"1"~"0" x $row; | |
| #say @automata[*-1]; | |
| for 1..$row { | |
| my $next='0'; | |
| my $i=0; | |
| repeat while $i<=(@automata[*-1].chars)-3 { | |
| my $str=substr(@automata[*-1],$i,3); | |
| $next= $next ~ %rule{$str}; | |
| $i++; | |
| } | |
| $next~='0'; | |
| push @automata, $next; | |
| } | |
| @automata; | |
| } | |
| sub MAIN($rule,$row) { | |
| for automata($rule, $row) { say $_.subst(rx/'0'/,' ',:g)}; # ; is not allowed after for | |
| } |
Min Stack II
# last time, I build a min stack using perl6’s class.
# the problem is that the perl6 already have pop, push, min method.
# so, to solve the problem of name conflict, I have to rename the name.
# but there is another solution.
# use the multi instead of method.
# TIMTOWTDI
| class Minstack { | |
| has @.stack; | |
| has @.min; | |
| } | |
| multi push(Minstack $mstack, $item) { | |
| push $mstack.stack, $item; | |
| if $mstack.min==0 or $mstack.min[*-1] > $item { | |
| push $mstack.min, $item; | |
| } | |
| } | |
| multi pop(Minstack $mstack) { | |
| my $item=pop $mstack.stack; | |
| if $item == $mstack.min[*-1] { | |
| pop $mstack.min; | |
| } | |
| return $item; | |
| } | |
| multi min(Minstack $mstack) { | |
| $mstack.min[*-1]; | |
| } | |
| my $min=Minstack.new; | |
| push $min, 5; | |
| push $min, 3; | |
| push $min, 10; | |
| push $min, 2; | |
| say(pop $min); # 2 | |
| say(min $min); # 3 | |
| say(pop $min); # 10 | |
| say(min $min); # 3 | |
| say(pop $min); # 3 | |
| say(min $min); # 5 | |
| say(pop $min); # 5 |
Min Stack
The problem: Min Stack
Design a data structure that provides push and pop operations, like a stack, plus a third operation that finds the minimum element. All three operations must perform in constant time. You may assume that all elements are distinct.
Use perl6’s class to implement
| class Minstack { | |
| has @.stack; | |
| has @.min; | |
| method mpush($item) { | |
| push @.stack, $item; | |
| if @.min==0 or @.min[*-1] > $item { | |
| push @.min, $item; | |
| } | |
| } | |
| method mpop() { | |
| my $item=pop @.stack; | |
| if $item == @.min[*-1] { | |
| pop @.min; | |
| } | |
| return $item; | |
| } | |
| method mmin() { | |
| @.min[*-1]; | |
| } | |
| } | |
| my $min=Minstack.new; | |
| $min.mpush(5); | |
| $min.mpush(3); | |
| $min.mpush(10); | |
| say $min.mpop(); | |
| say $min.mmin(); |
TODO: use multi……
TODO list in perl6
1 -TODO list in perl6
1.1 basic function
| sub view ($file='todo.org') { | |
| my $todo=open "/home/echo/Dropbox/todo/$file"; | |
| my @lines= $todo.lines; | |
| $todo.close; | |
| say " there are "~ +@lines ~ " tasks"; | |
| my $order=1; | |
| for @lines -> $line { | |
| say "$order"~"."~"$line"; | |
| $order++; | |
| } | |
| } | |
| sub viewdone() { | |
| view("done.org"); | |
| } | |
| sub add() { | |
| my $task= prompt "add a task,please\n xxxxxxxxxxxxxxxx\n"; | |
| my $todo=open '/home/echo/Dropbox/todo/todo.org',:a ; | |
| $todo.say($task); | |
| $todo.close; | |
| say "adding...task\n\n "~ "$task\n"; | |
| } | |
| sub done() { | |
| my $todo=open '/home/echo/Dropbox/todo/todo.org'; | |
| my %tasks; | |
| my $i=1; | |
| for $todo.lines -> $line { | |
| say "$i"~"."~"$line"; | |
| %tasks{$i}=$line; | |
| $i++; | |
| } | |
| $todo.close; | |
| my $task = prompt "done task number?\n"; | |
| my $done = open '/home/echo/Dropbox/todo/done.org',:a; | |
| $done.say(%tasks{$task}); | |
| %tasks.delete($task); | |
| my $write = open '/home/echo/Dropbox/todo/todo.org',:w; | |
| for %tasks.values { | |
| $write.say($_); | |
| } | |
| $write.close; | |
| } |
1.2 interact with the outside;
1.2.1 dispatch-using hash
| sub MAIN ($op="v"){ #$op="v" eqv $op='v' | |
| my %All = v=>&view, a=>&add, d =>&done, vd=&viewdone; | |
| %All{$op}(); | |
| } |
1.2.2 dispathc-using multi
| multi MAIN('a'){ view ; add ;} | |
| multi MAIN('v'){ view ;} | |
| multi MAIN('d'){ done ;} | |
| multi MAIN('vd'){ viewdone;} |