2013年4月24日 星期三

NCNU LISP HW5


For 1.31, you have to write the `term' function.
That is (the fucntion will return a float.)
              (term 1) --> 2/3 = 0.6666666...
              (term 2) --> 4/3 = 1.3333333...
              (term 3) --> 4/5 = 0.8
              
For 1.32, you have to write the `accumulate'. It can be done either in the way of linear recusive or linear iterative. Only one version is required.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(define (term x)
(do-term x 2 3)
)
(define (do-term x numerator denominator)
   (cond ((= x 1)   (/ (* numerator 1.0) denominator))
((even? x) (do-term (- x 1) (+ numerator 2) denominator))
(else      (do-term (- x 1) numerator (+ denominator 2)))
   )
)
(define (even? n)
        (= (remainder n 2) 0)
)



(define (accumulate combiner null-value term1 a next b)
(if (> a b) null-value
                    (combiner (term1 a) (accumulate combiner null-value term1 (next a) next b))
)
)

(define (product term2 a next b)
(accumulate * 1 term2 a next b)
)
(define (sum term3 a next b)
(accumulate + 0 term3 a next b)
)
(define (inc x)
(+ x 1)
)
(define (identify x)
x
)

2013年4月16日 星期二

UNIX IN NCNU filtering numbers (python)


與作業七相同, 但改用 python 來作.

#!/usr/local/bin/python
import sys
import string
lower=string.atoi(sys.argv[1]);
upper=string.atoi(sys.argv[2]);
count=0;
line=sys.stdin.readlines();
long=len(line);
single=[]
for i in range(0,long):
  line[i]=line[i][:-1];
  single=line[i].split();
  long2=len(single)
  for j in range(0,long2):
    single[j]=string.atoi(single[j]);
    if (single[j]>=lower):
      if (single[j]<=upper):
        if (count<5):
          print single[j],;
          count=count+1;
        else:
          print single[j];
          count=0;        

UNIX IN NCNU filtering numbers (shell script)


寫一 sh 的 script file, 例如為 ex7.sh
從參數讀進 lower bound 與 upper bound,
從 stdin 讀進 numbers.
把介於 lower bound 與 upper bound 的數以六個一列印出.
如下例所示.
$ cat N 
3 4 5 6 7 8 9 10
9 88 23 
121
98 78 34 288
345 909
3 2 1
$ ./ex7.sh 40 50 < N
$ ./ex7.sh 4 50 < N
     4      5      6      7      8      9 
    10      9     23     34 
$ ./ex7.sh 1 10 < N
     3      4      5      6      7      8 
     9     10      9      3      2      1 
$ ./ex7.sh 11 100 < N
    88     23     98     78     34 

///////////////////////////////////////////////////////////////////////

#!/bin/bash
count=0
lower=$1
upper=$2
while read line;do
  for number in $line;do
        if [ "$number" -le "$upper" ];then
                 if [ "$lower" -le "$number" ];then
                        if [ "$count" -lt 5 ];then
                                echo -n "$number "
                                count=`expr $count + 1`
                        else
                                echo $number
                                count=0
                        fi
                fi
        fi
  done
done
if [ "$count" -gt 0 ];then
echo
fi

UNIX IN NCNU writing sh script

寫一 sh 的 script file, 例如為 ex6.sh. 其執行結果與作業五類似. 如下例所示.
$ ./ex6.sh 
klim: 29747 29957 29958
root: 0 1 2 3 52 62 137 160 174 184 185 196 210 215 223 248 260 262 263 292 298 301 310 316 317 319 321 324 325 326 327 341 1755 12321 29745
smmsp: 236 12322
daemon: 176
$ ps -e -o user,pid | ./ex5.awk 
klim: 29747 29962 29963
smmsp: 236 12322
daemon: 176
root: 0 1 2 3 52 62 137 160 174 184 185 196 210 215 223 248 260 262 263 292 298 301 310 316 317 319 321 324 325 326 327 341 1755 12321 29745

///////////////////////////////////////////////////////////////////////

#!/bin/bash
index=-1
tmp=" "
ps -e -o user,pid | sed '1d' | sort >>doc.txt
while read user pid;do
        #printf "%s : %s\n" $user $pid
        if [ "$tmp" = "$user" ];then
                        number[$index]=${number[$index]}' '$pid
                        name[$index]=$user':'
        else
                index=`expr $index + 1`
                number[$index]=${number[$index]}' '$pid
                name[$index]=$user':'
        fi
        tmp=$user
done<doc.txt

for((i=0;i<=index;i=i+1))
do
        echo  "${name[$i]}${number[$i]}"
done
rm doc.txt

UNIX IN NCNU writing awk script

將 ps -ef 的輸出的
  • 第一行砍掉
  • 將同一 user 的 pid 順序印出
$ ps -ef | ./ex5.awk 
klim: 27048 27141 27142
smmsp: 236 12322
daemon: 176
root: 0 1 2 3 52 62 137 160 174 184 185 196 210 215 223 248 260 262 263 292 298 301 310 316 317 319 321 324 325 326 327 341 1755 12321 27046

///////////////////////////////////////////////////////////////////////

這邊耍蠢了,不知道為啥我CALL內建的sort竟然不能用,於是我就自己寫了一個bubble sort來用

其實這邊的for*2可以改成內建的sort使用

#!/bin/awk -f
/^UID/{next}
!/UID/{
name=""
name=$1
user[name]=name
number[name]=number[name]" "$2
}
END{
  answer=""
  for(j in number)
  {
     n=split(number[j],tmp," ")
    for(change1=1;change1<=n;change1++)
    {
      for (change2=1;change2<=n-1;change2++)
      {
        if(tmp[change2]>tmp[change2+1])
        {
         temp=tmp[change2];
         tmp[change2]=tmp[change2+1];
         tmp[change2+1]=temp;
        }
      }
    }
    for(i=1;i<=n;i++)
    {
      if(i>1)
      after[j]=after[j]" "tmp[i]
      else
      after[j]=tmp[i]
    }
  }
  for( i in user )
     printf "%s : %s\n",i,after[i]

UNIX IN NCNU writing sed script

作業四: writing sed script
將適當的 sed 的 commands 寫成一個 script file, 例如為 ex4.sed,
如底下方式執行, 將 ps -ef 的輸出的
  • 第一行砍掉
  • user 為 root 的砍掉
  • 其餘的只顯示前二個欄位, 中間空一格
    $ ps -ef | sed -f ex4.sed
    s9832100 18776
    daemon 176
    smmsp 236
    s9832100 18749
    klim 18818
    klim 18817
    smmsp 12322
    klim 18641
    
    
    ///////////////////////////////////////////////////////////////////
    
    
    
    
    
    
    
    
    1d
    /root/d
    s/\ \ [01] [0-9a-zA-Z\:\-\/\ *].*$//g

2013年4月9日 星期二

NCNU LISP HW4


(define (expmod base exp m)
(iterative-expmod base 1 exp m)
)
(define (iterative-expmod base1 base2 exp m)
(cond ((= exp 0) base2)
     ((even? exp) (iterative-expmod (mod (square base1) m) base2 (/ exp 2) m)  )
     (else        (iterative-expmod base1 (mod (* base1 base2) m) (- exp 1) m)  )
)
)
(define (mod a b)
(remainder a b)
)
(define (even? n)
(= (remainder n 2) 0)
)
(define (square x)
(* x x)
)

2013年4月8日 星期一

c++ UVA100


#include<iostream>
using namespace std;
int main()
{
unsigned long long i,j,length,tmp,max;
while(cin>>i>>j)
{
max=0;
cout<<i<<" "<<j<<" ";
if(i>j)                                               //很重要,我沒直接想到
{
unsigned long long change;
change=i;
i=j;
j=change;
}
for(unsigned long long k=i;k<=j;k++)
{
length=0;
tmp=k;
while(tmp!=1)
{
length++;
if(tmp%2==1)
tmp=3*tmp+1;
else
tmp=tmp/2;
}
if(tmp==1)
length++;
if(length>max)
max=length;
}
cout<<max<<endl;
}

return 0;
}