Language detail: Other

Coverage: 72.59%
number of '+' ratings
contribution for coverage

Unsolved challenges

codes

Feed

Used modules

next >>

IPv4アドレスのマスクの変換 (Nested Flatten)
もっと美しく書ける気がします。
これが今の僕の限界です orz
 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
namespace Doukakuorg

open System
open System.Text.RegularExpressions

type C253 public () =
    member this.MaskToBit ip =
        if Regex.IsMatch(ip, @"^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$") then
            ip.Split('.')
                |> Array.map (fun a -> Convert.ToInt32(a))
                |> Array.map (fun a -> Convert.ToString(a, 2))
                |> Array.fold (fun s n -> s + n) ""
                |> (fun a -> a.Replace("0", "").Length)
        else
            0

    member this.BitToMask bit =
        if 1 <= bit && bit <= 32 then
            (new String('1', bit)).PadRight(32, '0')
                |> (fun a -> [for i in 0..8..(a.Length-1) do yield a.[i .. (i+7)]])
                |> List.map (fun a -> Convert.ToInt32(a, 2))
                |> List.fold (fun s n -> String.Format("{0}.{1}", s, n)) ""
                |> (fun a -> a.[1..])
        else
            "0.0.0.0"
疑似並行処理 (Nested Flatten)

Clojure1.1.0

1
2
3
4
5
6
7
8
9
(def out (ref []))

(defn put [sq]
  (doall (map #(dosync (Thread/sleep 10) (alter out conj %)) sq)))

(pcalls #(put [1 2 3 4 5 6 7 8 9 10])
        #(put [:a :b :c :d :e :f :g :h :i :j]))

(println @out)
クリップボードへの転送 (Nested Flatten)

Clojure版です。Java版のやり方を真似ています。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
(import '(java.awt Toolkit))
(import '(java.awt.datatransfer DataFlavor StringSelection))

(defn paste [clipboard s]
  (let [selection (StringSelection. s)]
    (. clipboard setContents selection selection)))

(defn getdata [clipboard]
  (. clipboard getData DataFlavor/stringFlavor))
  
(defn main [args]
     (let [clipboard (.. Toolkit (getDefaultToolkit) (getSystemClipboard))]
       (if (= (first args) "-")
     (println (getdata clipboard))
     (paste clipboard (first args)))))

(main *command-line-args*)
文字列で+を表示する (Nested Flatten)
clojure に挑戦してみました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
(defn plus [st]
  (let [len (.length st)
        line-w (inc (* len 3))
        coll (apply concat
               (interleave
                 (partition len (range 1 line-w))
                 (list (repeat len len)(repeat len (* len 2))'())))
        coll-y (concat (repeat (inc len) 0) coll
                 (repeat (dec len) (* len 3))(reverse coll))
        coll-x (apply concat (reverse (split-at (* len 3) coll-y)))
        matrix (map (fn [x y] (vector x y)) coll-x coll-y)
        str-12 (apply concat (repeat 12 (seq st)))
        ms-map (apply hash-map (interleave matrix str-12))]
    (apply str
      (map #(apply str %)
        (interpose "\n"
          (partition (inc line-w)
            (for [y (range 0 line-w) x (range 0 (inc line-w))]
              (format "%s" (get ms-map (vector x y) " ")))))))))

(println (plus "doukaku"))

はじめまして。 Golfと違って高階関数を使えるのがウレシイ。 ふつうにハンドルを切ってます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import Data.List

r [x,y] = [-y, x]
l [x,y] = [ y,-x]

ds = scanr id [0,1] [r,r,l,r,r,l,r,r,l,r,r]

place str = snd $ mapAccumL phi [len,len] [ (d, ch) | d<-ds, ch<-str ]
  where phi pos (d, ch) = (zipWith (+) pos d, (pos, ch))
        len = length str

cross str = unlines [ [ maybe ' ' id $ lookup [x,y] $ place str | y<-is ] | x<-is ]
  where is = [0..3*length str]

main = putStrLn . cross =<< getLine

久しぶりに投稿します。関数型言語といえばfold。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
let printPlus = function
    | null | "" -> ()
    | str ->
        let len = String.length str
        let moves = [(1, 0); (0, 1); (1, 0); (0, 1); (-1, 0); (0, 1);
                     (-1, 0); (0, -1); (-1, 0); (0, -1); (1, 0); (0, -1)]
        let plot map (x, y) (dx, dy) =
            ((map, (x, y)), str) ||> Seq.fold (fun (m, (x', y')) c ->
                (Map.add (x', y') c m, (x' + dx, y' + dy)))
        let plots =
            ((Map.empty, (len, 0)), moves) ||> List.fold (fun (m, xy) mv -> plot m xy mv)
            |> fst
        [for y in 0 .. (len * 3) ->
            [for x in 0 .. (len * 3) ->
                match Map.tryFind (x, y) plots with Some(c) -> string c | None -> " "]
            |> String.concat ""]
        |> List.iter (printfn "%s")

printPlus "doukaku"
文字数チェックはしてないです。
 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
40
41
42
43
44
type CyclicString(s : string) =
  let len = s.Length
  let mutable c = -1
  member this.Item(n : int) = s.[n % len]
  member this.Length = len
  member this.get() = 
    c <- if c + 1 = len then 0 else c + 1
    this.[c]

type Char2d(xSize, ySize) =
  let ch2d = Array2D.create xSize ySize ' '
  let mutable _pos = (0, 0)
  member this.pos
    with get() = _pos
    and  set(p) = _pos <- p
  member this.put ch =
    let (x, y) = _pos
    ch2d.[x,y] <- ch
  member this.printOut() = 
    for y = 0 to ySize - 1 do
      for x = 0 to xSize - 1 do
        printf "%c" ch2d.[x, y]
      printfn ""

let cross (str) =
   let up    pos = (fst pos, snd pos - 1)
   let down  pos = (fst pos, snd pos + 1)
   let left  pos = (fst pos - 1, snd pos)
   let right pos = (fst pos + 1, snd pos)
   let command = seq [right; up; right; down; right; down; left; down; left; up; left; up]
   let cs = CyclicString(str)
   let len = cs.Length
   let ch2d = Char2d(len * 3 + 1, len * 3 + 1)
   ch2d.pos <- (0, len)

   let interpreter f =
     for i = 1 to len do
       ch2d.put <| cs.get()
       ch2d.pos <- f ch2d.pos

   Seq.iter interpreter command
   ch2d.printOut()

do cross "doukaku"
バイナリクロック (Nested Flatten)

F#で。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
let strBit order chOn chOff n =
    let rec sub rem res counter =
        if counter = order then
            res
        else
            let ch = if (rem &&& 1) <> 0 then
                        chOn
                     else 
                        chOff
            sub (rem >>> 1) (ch :: res) (counter+1)
    new string( Array.ofList(sub n [] 0))

let tm = System.DateTime.Now
[tm.Hour;tm.Minute]
  |> List.map (strBit 6 '■' '□')
  |> List.iter (fun s -> printfn "%s" s)
文字列の八方向検索 (Nested Flatten)

代り映えしませんが。

 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
40
41
42
43
44
let dLst = [((1,0),"右");((1,1),"右下");((0,1),"下");((-1,1),"左下");
            ((-1,0),"左");((-1,-1),"左上");((0,-1),"上");((1,-1),"右上");]

let isInBound cMax rMax (c,r) = 
    0 <= c && c < cMax && 0 <= r && r < rMax   

let isMatch (arr:char[][]) (cMax,rMax) (str:string) strLength (c,r) (((dx,dy),_)) =
    let rec isMsub  count =
        if count = strLength  then
            true
        else
           let (c_Xpos,c_Ypos) = (c+count*dx,r+count*dy)
           if isInBound cMax rMax (c_Xpos,c_Ypos) = false then
              false
           else
             if str.[count] <> arr.[c_Ypos].[c_Xpos] then
                false
             else
               isMsub  (count + 1) 
    isMsub 0         

let getSolutions (arr:char[][]) (str:string) =
    let cMax = arr.[0].Length
    let rMax = arr.Length
    let strLen = str.Length
    let check = isMatch arr (cMax,rMax) str strLen //部分適用
    [
     for c in 0 .. cMax-1 do
        for r in 0 .. rMax-1 do
            for dir in dLst do
                if check (c,r) dir then
                    let ((_,_),dirName) = dir
                    yield ((c,r),dirName)
        ] 

let tArr=[|
            [|'リ';'オ';'ウ';'ウ';'リ';'ウ'|];
            [|'ウ';'オ';'リ';'ウ';'オ';'リ'|];
            [|'オ';'リ';'リ';'オ';'リ';'ウ'|];
            [|'リ';'リ';'オ';'オ';'ウ';'オ'|]
                |]

let tarStr = "ウオリ"
List.iter (fun res -> printfn "%A" res ) (getSolutions tArr tarStr)
箱詰めパズルの判定 (Nested Flatten)
Explorerでは積み木の形が認識できませんでした。
地道な解法です。5の裏返しを認めない場合は、置き方の総数は99通り。
認めた場合は117通り。
どちらにしろ、組み合わせの総数は12通り、詰め方は22通りという結果となりました。
実行結果は次のようになります。

置き方の総数...99

[1; 1; 1; 1]
[1; 1; 2; 2]
[1; 1; 4; 4]
[1; 2; 4; 4]
[1; 3; 3; 4]
[1; 4; 4; 5]
[2; 2; 2; 2]
[2; 2; 4; 4]
[3; 3; 3; 3]
[3; 3; 4; 5]
[4; 4; 4; 4]
[4; 4; 5; 5]

組み合わせの総数...12

異なる詰め方の個数...22

[1; 1; 1; 1]

1|1|1|1
       
1|1|1|1
       
1|1|1|1
       
1|1|1|1


[1; 1; 2; 2]

1|2 2|1
       
1|2 2|1
  - -  
1|2 2|1
       
1|2 2|1


[1; 1; 2; 2]

2 2|2 2
       
2 2|2 2
- - - -
1 1 1 1
- - - -
1 1 1 1


[1; 1; 4; 4]

1|4 4|1
    -  
1|4|4|1
       
1|4|4|1
  -    
1|4 4|1


[1; 1; 4; 4]

4|4 4 4
  - -  
4 4 4|4
- - - -
1 1 1 1
- - - -
1 1 1 1


[1; 2; 4; 4]

4 4|2 2
  -    
4|4|2 2
    - -
4|4 4 4
- - - -
1 1 1 1


[1; 2; 4; 4]

4 4|4 4
  - -  
4|2 2|4
       
4|2 2|4
- - - -
1 1 1 1


[1; 2; 4; 4]

4 4 4|1
- -    
2 2|4|1
    -  
2 2|4|1
- -    
4 4 4|1


[1; 2; 4; 4]

4 4 4|4
- -    
2 2|4|4
    -  
2 2|4 4
- - - -
1 1 1 1


[1; 3; 3; 4]

3|4 4|1
  -    
3 3|4|1
  -    
3|3|4|1
-   -  
3 3 3|1


[1; 3; 3; 4]

4 4|3|1
  -    
4|3 3|1
  -    
4|3|3|1
-   -  
3 3 3|1


[1; 4; 4; 5]

4 4|5|1
  -    
4|5 5|1
    -  
4|5|4|1
- -    
4 4 4|1


[1; 4; 4; 5]

4 4|5|4
  -    
4|5 5|4
    -  
4|5|4 4
- - - -
1 1 1 1


[1; 4; 4; 5]

5 5|4 4
-   -  
4|5 5|4
  - -  
4 4 4|4
- - - -
1 1 1 1


[2; 2; 2; 2]

2 2|2 2
       
2 2|2 2
- - - -
2 2|2 2
       
2 2|2 2


[2; 2; 4; 4]

4|4 4 4
  - -  
4 4 4|4
- - - -
2 2|2 2
       
2 2|2 2


[3; 3; 3; 3]

3|3 3 3
  -   -
3 3|3|3
  - -  
3|3|3 3
-   -  
3 3 3|3


[3; 3; 4; 5]

5 5|4 4
-   -  
3|5 5|4
  - -  
3 3|3|4
  -   -
3|3 3 3


[4; 4; 4; 4]

4|4 4 4
  - -  
4 4 4|4
- - - -
4|4 4 4
  - -  
4 4 4|4


[4; 4; 4; 4]

4|4 4 4
  - -  
4 4 4|4
- - - -
4 4 4|4
  - -  
4|4 4 4


[4; 4; 4; 4]

4 4 4|4
- -    
4 4|4|4
  - -  
4|4|4 4
    - -
4|4 4 4


[4; 4; 5; 5]

5 5|4 4
-   -  
4|5 5|4
  - -  
4|5 5|4
  -   -
4 4|5 5
  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
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
let pieces = [|
        ((1,0),[|(0,0);(1,0);(2,0);(3,0)|]);((1,1),[|(0,0);(0,1);(0,2);(0,3)|]);
        ((2,0),[|(0,0);(1,0);(0,1);(1,1)|]);
        ((3,0),[|(0,0);(1,0);(2,0);(1,1)|]);((3,1),[|(0,0);(0,1);(1,1);(0,2)|]);                       
        ((3,2),[|(0,0);(-1,1);(0,1);(1,1)|]);((3,3),[|(0,0);(-1,1);(0,1);(0,2)|]);                       
        ((4,0),[|(0,0);(1,0);(2,0);(0,1)|]);((4,1),[|(0,0);(1,0);(1,1);(1,2)|]);                       
        ((4,2),[|(0,0);(0,1);(0,2);(1,2)|]);((4,3),[|(0,0);(-2,1);(-1,1);(0,1)|]);
        ((4,4),[|(0,0);(1,0);(2,0);(2,1)|]);((4,5),[|(0,0);(1,0);(0,1);(0,2)|]);                       
        ((4,6),[|(0,0);(0,1);(1,1);(2,1)|]);((4,7),[|(0,0);(0,1);(-1,2);(0,2)|]);
        ((5,0),[|(0,0);(1,0);(1,1);(2,1)|]);((5,1),[|(0,0);(-1,1);(0,1);(-1,2)|]);                       
//5の逆転     ((5,2),[|(0,0);(0,1);(1,1);(1,2)|]);((5,3),[|(0,0);(1,0);(-1,1);(0,1)|])
             |]
let Mdim = 4 
let isPutable p (x,y) (arr:int[,])=
    let isThatPositionPutable (x1,y1) = 
        0 <= x+x1 && 0 <= y+y1 && 
        x+x1<Mdim && y+y1<Mdim && arr.[y+y1,x+x1] = 0
    let (_,rPositions) = p     
    Array.forall isThatPositionPutable rPositions    

let put p (x,y) (arr:int[,]) =
    let putOneLocation pID  (x1,y1) = arr.[y+y1,x+x1] <- pID
    let ((pId,_),rPositions) = p
    Array.map (putOneLocation pId) rPositions

let remove p (x,y) (arr:int[,]) =
   let removeOneLocation (x1,y1) = arr.[y+y1,x+x1] <- 0
   let (_,rPositions) = p     
   Array.map removeOneLocation rPositions

let findNextPutLoc (arr:int[,]) =
    let mutable isFirst = true
    let  result = ref None
    for y in 0 .. Mdim-1 do
       for x in 0 .. Mdim-1 do
            if arr.[y,x] = 0 && isFirst then
                result := Some((x,y))
                isFirst <- false
    !result
 
let check in_arr =
    let sucHisLst = ref []  
    let rec search hist  (arr:int[,]) =
        if findNextPutLoc arr = None then //成功!全部置けた
            sucHisLst := hist:: !sucHisLst
        else
            let (x,y) = (findNextPutLoc arr).Value
            for p in pieces do
                if isPutable p (x,y) arr then
                    put p (x,y) arr |> ignore
                    search (((x,y),p)::hist) arr
                    remove p (x,y) arr |> ignore
    search [] in_arr                       
    sucHisLst
 
let initArr = Array2D.create Mdim Mdim 0 //0は何も置かれていない状態 
let solutions = !(check initArr)

printfn "\n置き方の総数...%d\n" solutions.Length

///ここから「箱につめることができる積み木の組み合わせの総数」の処理

let extractKindlistAndSort eles  =
    let t = List.map (fun ((x,y),((k,_),_)) -> k) eles
    List.sort t

let combSet = solutions 
              |> List.map extractKindlistAndSort 
              |> Set.ofList
Set.iter (fun x -> printfn "%A" x ) combSet
printfn "\n組み合わせの総数...%d\n" combSet.Count

///ここから「上記総数を、異なる詰め方の個数別にカウント
// (箱の回転・裏返しで一致するものは同一視します)」の処理

//縦横2*Mdimの配列を準備して境界線を引きながら、もう一度積み木を置いていく。
//それから、回転裏返しで一致するものを除きながら、解のリストを作る。

let Connected = -1
let Discreet = -2

let put2 p (x,y) (arr:int[,]) =
    let putOneLocation pID  (x1,y1) = arr.[2*y+2*y1,2*x+2*x1] <- pID
    let ((pId,_),rPositions) = p
    Array.map (putOneLocation pId) rPositions

let upDateConnection (arr:int[,]) =
    for c in 0 .. Mdim - 1 do
        for r in 0 .. Mdim - 2 do
            if arr.[2*r,2*c] <> 0 && arr.[2*r,2*c]=arr.[2*r + 2,2*c] && arr.[2*r+1,2*c] = 0
                then arr.[2*r+1,2*c] <- Connected 
            if arr.[2*r,2*c] <> arr.[2*r + 2,2*c] && arr.[2*r+1,2*c] = 0
                then arr.[2*r+1,2*c] <- Discreet 
    for c in 0 .. Mdim - 2 do
        for r in 0 .. Mdim - 1 do
            if arr.[2*r,2*c] <> 0 && arr.[2*r,2*c]=arr.[2*r,2*c+2] && arr.[2*r,2*c+1] = 0
                then arr.[2*r,2*c+1] <- Connected 
            if arr.[2*r,2*c] <> arr.[2*r,2*c+2] && arr.[2*r,2*c+1] = 0
                then arr.[2*r,2*c+1] <- Discreet 
             
let rotateArr (arr:int[,]) =
    let tempArr = Array2D.create (2*Mdim-1) (2*Mdim-1) 0
    for c in 0 .. 2*Mdim - 2 do
        for r in 0 .. 2*Mdim - 2 do
            tempArr.[r,(2*Mdim-1) - c - 1 ] <- arr.[c,r]
    tempArr

let reverseArr (arr:int[,]) =
    let tempArr = Array2D.create (2*Mdim-1) (2*Mdim-1) 0
    for c in 0 .. 2*Mdim - 2 do
        for r in 0 .. 2*Mdim - 2 do
            tempArr.[r,c] <- arr.[c,r]
    tempArr

let makeUpPA ele =
    let tempArr = Array2D.create (2*Mdim-1) (2*Mdim-1) 0
    let t = List.fold (fun s ((x,y),((k,id),a)) ->
                            put2 ((k,id),a)(x,y) tempArr |>ignore;
                            upDateConnection tempArr|> ignore ;
                            k :: s) [] ele
    ((List.sort t),tempArr)

let isEqueWRR oriP uP oriArr arr = //回転反転すると等しくなる 
    let rec isEqueWRRSub arr1 arr2 count =
        if count = 4 then 
            false
        else
          if oriArr = arr1 || oriArr = arr2 then
            true
          else
            isEqueWRRSub (rotateArr arr1) (rotateArr arr2) (count + 1)
    if oriP <> uP then
        false
    else
        isEqueWRRSub arr (reverseArr arr) 0
              
let makeUpSolutions2 sols =
    let isInclude ele lst =
        let (usedPiece,arr0) = ele
        List.exists(fun (u,arr) ->isEqueWRR usedPiece u arr0 arr) lst

    let rec musSub lst res =
        match lst with
        |[]
            ->res
        |hd::tl 
            ->let t = makeUpPA hd
              if (isInclude t res)  then
                musSub tl res
              else
                musSub tl (t::res)
    musSub sols []


let solutions2 = (makeUpSolutions2 solutions)

printf "異なる詰め方の個数...%d"  solutions2.Length

///ここまでで答え終わり
//ここから見やすく答えを表示

let dispEach (kLst, (arr : int [,])) =
    printfn "\n\n%A" kLst
    for r in 0 .. 2*Mdim - 2 do
        printfn ""
        for c in 0 .. 2*Mdim - 2 do
          let st =  
                match arr.[r,c] with
                |i  when i = 0 || i = Connected -> " "
                |i  when i = Discreet && (c % 2 = 1 && r % 2 = 0) -> "|"
                |i  when i = Discreet && (c % 2 = 0 && r % 2 = 1) -> "-"
                |i   -> i.ToString()
          printf "%s" st     
    printfn ""

List.iter (fun ele -> dispEach ele) (List.sort solutions2)
文字列で+を表示する (Nested Flatten)

LOGO風に

 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
40
41
type NextTurnDirection =
    |Right
    |Left
let cdr (dx,dy) = ((-1)*dy,dx) //cdrはchangeDirectionRightの略
let cdl (dx,dy) = (dy,(-1)*dx) //cdlはchangeDirectionLeftの略

let doWork (s:string) =
    let len = s.Length
    let canvasArr = Array2D.create (3*len + 1) (3*len + 1) ' ' 
    let startPos = (len,0)
    let getNextTrunDirection turnN = 
        if turnN % 3 = 1 then Left else Right //3回に1回左折
    let rec drawCanvas stepNum turnNum (x,y) (dx,dy) cbfvosp =
       // cbfvospはcanBeFirstVisitOnStartPointの略
       if (x,y) = startPos && cbfvosp = false then
            ()
       else
          canvasArr.[x,y] <- s.[stepNum] 
          match (stepNum,turnNum) with
          |(sn,tn)  when sn = len - 1 && (getNextTrunDirection tn) = Right
                ->  drawCanvas 0 (tn + 1) (x+dx,y+dy) (cdr (dx,dy)) false
          |(sn,tn)  when sn = len - 1 && (getNextTrunDirection tn) = Left
                ->  drawCanvas 0 (tn + 1) (x+dx,y+dy) (cdl (dx,dy)) false   
          |(sn,tn)
                ->  drawCanvas (stepNum+1) tn (x+dx,y+dy) (dx,dy) false
    drawCanvas 0 0  startPos (1,0) true  
    for i in 0 .. 3*len do
        printfn ""
        for j in 0 .. 3*len do
            printf "%c" canvasArr.[j,i]

open System

[<STAThread()>] 
[<EntryPoint>] 
let main(args) = 
    if (args.Length = 0 || args.[0].Length <2) then
        printf "引数が不正です。"
    else
        doWork args.[0]
    0
西暦 to 和暦 (Nested Flatten)
VBScript版です。
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Option Explicit

msgbox toWareki("1868/12/2")
msgbox toWareki("1926/12/24")
msgbox toWareki("2007/12/01")
msgbox toWareki("1926/12/25")
msgbox toWareki("1868/1/2")
msgbox toWareki("1868/100/2")

Function toWareki(yyyymmdd)
    Dim dtYYYYMMDD, intYYYYMMDD

    On Error Resume Next
    dtYYYYMMDD = CDate(yyyymmdd)
    If Err.Number <> 0 Then
        toWareki = "範囲外"
        Err.Clear
        Exit Function
    End If

    intYYYYMMDD = Int(formatYYYYMMDD(dtYYYYMMDD))
    If intYYYYMMDD < 18680908 Then
        toWareki = "範囲外"
        Exit Function
    End If

    If intYYYYMMDD >= 18680908 And intYYYYMMDD <= 19120730 Then
        toWareki = toWareki & "明治" & Year(dtYYYYMMDD) - 1867 & "年" & Month(dtYYYYMMDD) & "月" & Day(dtYYYYMMDD) & "日"
    End If

    If intYYYYMMDD >= 19120730 And intYYYYMMDD <= 19261225 Then
        toWareki = toWareki & "大正" & Year(dtYYYYMMDD) - 1911 & "年" & Month(dtYYYYMMDD) & "月" & Day(dtYYYYMMDD) & "日"
    End If

    If intYYYYMMDD >= 19261225 And intYYYYMMDD <= 19890107 Then
        toWareki = toWareki & "昭和" & Year(dtYYYYMMDD) - 1925 & "年" & Month(dtYYYYMMDD) & "月" & Day(dtYYYYMMDD) & "日"
    End If

    If intYYYYMMDD >= 19890107 Then
        toWareki = toWareki & "平成" & Year(dtYYYYMMDD) - 1988 & "年" & Month(dtYYYYMMDD) & "月" & Day(dtYYYYMMDD) & "日"
    End If
End Function

Function formatYYYYMMDD(dt)
    Dim yyyy, mm, dd, result

    yyyy = Year(dt)
    mm = Month(dt)
    dd = Day(dt)

    result = ""
    result = result & Replace(Space(4 - Len(yyyy)), " ", "0") & yyyy
    result = result & Replace(Space(2 - Len(mm)), " ", "0") & mm
    result = result & Replace(Space(2 - Len(dd)), " ", "0") & dd

    formatYYYYMMDD = result
End Function
環境変数の取得 (Nested Flatten)
VBScript版です。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Set wshell = CreateObject("WScript.Shell")
Set env = wshell.Environment("SYSTEM")

' PATH環境変数を表示
WScript.Echo("PATH環境変数の値:" & env("PATH"))

' 全ての環境変数を表示
For Each e In env
    WScript.Echo(e)
Next
与えた条件を満たす候補 (Nested Flatten)
VBScript版です。
  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
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
Option Explicit

Dim i, j, numOperand, operandPatterns, operands

' 指定されたオペレータから、オペランドの数を取得する(and/orの数+1)
numOperand = countOperands(WScript.Arguments)

' 順列を生成する
operandPatterns = permBinaryBoolean(numOperand)

' 全てのパターンをテストする
For Each operands In operandPatterns
    If test(operands, WScript.Arguments) Then
        printArray(operands)
    End If
Next


Function test(operands, operators)
    Dim operandIndex, currentOperand, operator, i, j, copyOperands

    operandIndex = 0

    copyOperands = arraycopy(operands)

    currentOperand = copyOperands(0)
    operandIndex = operandIndex + 1

    i = 0
    For Each operator In operators
        ' not演算子は先読みして適用する
        If i <> operators.Count - 1 Then
            j = 1
            While operators(i + j) = "not"
                copyOperands(operandIndex) = Not copyOperands(operandIndex)
                j = j + 1
            Wend
        End If

        ' and/or演算子を適用する
        If LCase(operator) = "and" Then
            currentOperand = currentOperand And copyOperands(operandIndex)
            operandIndex = operandIndex + 1
        ElseIf LCase(operator) = "or" Then
            currentOperand = currentOperand Or copyOperands(operandIndex)
            operandIndex = operandIndex + 1
        ElseIf LCase(operator) = "not" Then
            ' 何もしない
        Else
            WScript.Echo "illegal operator: " & operator
        End If

        i = i + 1
    Next

    test = currentOperand
End Function

Function arraycopy(arr)
    Dim i

    Dim arrcopy()
    ReDim arrcopy(UBound(arr))

    For i = 0 to UBound(arr)
        arrcopy(i) = arr(i)
    Next

    arraycopy = arrcopy
End Function

Function countOperands(operators)
    Dim operator, result

    result = 0
    For Each operator In operators
        If LCase(operator) = "and" or LCase(operator) = "or" Then
            result = result + 1
        End If
    Next

    countOperands = result + 1
End Function

Function permBinaryBoolean(num)
    Dim result()
    ReDim result(2 ^ num - 1)

    For i=0 to UBound(result)
        result(i) = toBinaryBooleanArray(i, num)
    Next

    permBinaryBoolean = result
End Function

Function toBinaryBooleanArray(num, cols)
    Dim i, currentNum

    Dim result()
    ReDim result(cols - 1)

    currentNum = num

    For i = (cols-1) To 0 Step -1
        If currentNum >= (2 ^ i) Then
            result(cols - i - 1) = True
            currentNum = currentNum - (2 ^ i)
        Else
            result(cols - i - 1) = False
        End If
    Next

    toBinaryBooleanArray = result
End Function

Function printArray(arr)
    Dim a, firstFlg

    firstFlg = True
    WScript.StdOut.Write("(")
    For Each a In arr
        If firstFlg Then
            firstFlg = False
            WScript.StdOut.Write(a)
        Else
            WScript.StdOut.Write(", " & a)
        End If
    Next
    WScript.StdOut.WriteLine(")")
End Function

Function printArray2d(array2d)
    Dim i, j
    For i=0 to UBound(array2d)
        WScript.StdOut.Write(i)
        For j=0 to UBound(array2d(i))
            WScript.StdOut.Write("|" & array2d(i)(j))
        Next
        WScript.StdOut.WriteLine()
    Next
End Function
逆順になるあみだくじ (Nested Flatten)
VBScript版です。
C言語版(http://ja.doukaku.org/comment/740/)を移植しました。
 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Option Explicit

Dim n, keta, under, tmp, i, j, k

If WScript.Arguments.Count < 1 Then
    WScript.Echo "usage: " & WScript.ScriptName & " n"
    WScript.Quit(1)
End If
n = WScript.Arguments(0)

If n < 2 Then
    n = 2
End If

keta = 1
tmp = n + 1
Do
    If tmp < 10 Then
        Exit Do
    End If

    keta = keta + 1
    tmp = tmp / 10
Loop

Dim amida()
ReDim amida(n)

For i = 0 to (n-1)
    amida(i) = i
Next

For i = 0 to (keta-1)
    under = under & "_"
Next

For i = 0 to (n-1)
    WScript.StdOut.Write(amida(i) & makeSpace(keta))
Next
WScript.StdOut.WriteLine()

' バブルソート
For i = 0 to (n-2)
    For j = 0 to (n-2)
        If amida(j) < amida(j+1) Then
            tmp = amida(j)
            amida(j) = amida(j+1)
            amida(j+1) = tmp
            For k = 0 to (n-1)
                If k = j Then
                    WScript.StdOut.Write("|" & under)
                Else
                    WScript.StdOut.Write("|" & makeSpace(keta))
                End If
            Next
            WScript.StdOut.WriteLine()
        End If
    Next
Next

' 最後の線と数字の表示
For i = 0 to (n-1)
    WScript.StdOut.Write("|" & makeSpace(keta))
Next
WScript.StdOut.WriteLine()

For i = 0 to (n-1)
    WScript.StdOut.Write(amida(i) & makeSpace(keta))
Next
WScript.StdOut.WriteLine()

Function makeSpace(keta)
    Dim result, i

    result = ""
    For i=1 to keta
        result = result & " "
    Next

    makeSpace = result
End Function
Hello, world! (Nested Flatten)
1
printf("Hello, world!")
1
print("Hello, woeld!");
入出力の中継 (Nested Flatten)
http://ja.doukaku.org/comment/9931/
の補足。
テスト用プログラムを入れ忘れました。。
 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
Option Explicit

Dim param, fs, ts, count

param = WScript.Arguments(0)

Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.CreateTextFile("c:\" & param & ".txt")

count = 0
Do
    If count > 10 Then
        WScript.Quit(0)
    End If

    If WScript.StdIn.AtEndOfStream = False Then
        ts.WriteLine(WScript.StdIn.ReadLine)
    End If
    WScript.StdOut.WriteLine(param & "/" & count)

    WScript.Sleep(1000)
    count = count + 1
Loop

ts.Close()
VBScriptです。

コードをtest.vbs、
以下のテストプログラムをhoge.vbsとして保存し、
同一フォルダに格納します。
以下のコマンドラインで実行できます。

cscript test.vbs "cscript hoge.vbs proc1" "cscript hoge.vbs proc2"
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Option Explicit

Dim wshell, oexec1, oexec2

Set wshell = CreateObject("WScript.Shell")

' 2つのプロセスを起動
Set oexec1 = wshell.Exec(WScript.Arguments(0))
Set oexec2 = wshell.Exec(WScript.Arguments(1))

' 両方のプロセスが起動している間、一方の標準出力を、もう一方の標準入力へつなぐ。
While oexec1.Status = 0 And oexec2.Status = 0
    If oexec1.StdOut.AtEndOfStream = False Then
        oexec2.StdIn.WriteLine(oexec1.StdOut.ReadLine())
    End If
    If oexec2.StdOut.AtEndOfStream = False Then
        oexec1.StdIn.WriteLine(oexec2.StdOut.ReadLine())
    End If
Wend
Hello, world! (Nested Flatten)
1
print("Hello, world!")
next >>

Index

Feed

Other

Link

Pathtraq

loading...