あみだくじ
Posted feedbacks - Other
おなじ方法のものを, bourne shell + sed で
1 2 3 4 5 6 7 | #! /bin/sh
cat $1;t="mktemp tmp.XXXXXX";F=`$t`;R=`$t`;t=`$t`;head -n1 $1>$F;cat $1|sed \
-e1d >$R;while ! test 0 = `cat $R | wc -l`;do X=;r=;i=1;p=0;L=`head -n1 $R|sed\
-e 's/|-|/-/g'|sed -e 's/|/+/g'`\ `head -n1 $R |sed -e 's/-/ /g'`;for c in $L
do case $c in -)r=$r" \\"`expr 1 + $i`" \\$i";p=2;;+)r=$r" \\"$i;p=1;;\|)
X=$X'\(.\) ';;esac;i=`expr $i + $p`;done;s=s/`echo $X`/`echo $r`/;cat $F|sed \
-e "$s">$t; cp $t $F;cat $R|sed -e '1d' > $t;cp $t $R;done; cat $F;rm $R $F $t;
|
先頭の行-を ['A';'B';'C';'D';'E']の形へ、2行目以降の行を [[' ';' ';'-';' '];[' ' .......]...]の形へ直してから、リストで処理しました。
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 | let amida =
["A B C D E";
"| | |-| |";
"|-| | |-|";
"| |-| |-|";
"|-| |-| |";
"|-| | | |"]
let amidaData = List.map (fun (s:string ) -> s.ToCharArray()) amida
// 先頭の行-> ['A';'B';'C';'D';'E']の形へ
let topData = amidaData
|> List.hd
|> List.of_array
|> List.filter (fun c -> c <> ' ')
// 2行目以降-> [[' ';' ';'-';' '];[' ' .......]...]の形へ
let otherData = amidaData
|> List.tl
|> List.map List.of_array
|> List.map (List.filter (fun c -> c <> '|'))
let f lst1In lst2In =
let rec fSub l1 l2 res =
match l1,l2 with
|h11::h12::t1 , h21::t2 when h21 = '-' -> fSub (h11::t1) t2 (h12 ::res)
|h11::t1 , h21::t2 -> fSub t1 t2 (h11 :: res)
|h11 ::t1 , _ -> (h11 :: res)
|_ -> failwith "data Error"
List.rev (fSub lst1In lst2In [])
let res = List.fold f topData otherData
//res = ['B';'D';'C';'A';'E'] これを答えの形へ
let resStrLst = List.map (fun c -> c.ToString()) res
let resStr1 = List.fold (fun s ele -> s + " " + ele) "" resStrLst
let resStr2 = resStr1.Trim()
let answer = amida @ [resStr2]
List.iter (printfn "%A") answer
|




greentea #4476() Rating4/6=0.67
[ reply ]