challenge バイナリクロック

 時刻を二進数相当の表現で出力する時計アプリケーションを書いてください。
 20:18の場合,例えば以下の様な出力をするイメージです。

出力例:
 ■□■□□
□■□□■□
 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
#! c:\ruby\bin\ruby.exe -Ks

String.class_eval do |string|
    def words
        self.split(//)
    end
    def fix_width(width, padding)
        (self.words.size > width) ? self : (padding * (width - self.words.size) + self)
    end
end

Fixnum.class_eval do |fixnum|
    alias :to_s_orig :to_s
    def to_s(base, width)
        binary = self.to_s_orig(base).fix_width(width, "0")
    end
end

class BinaryClock
    attr_accessor :now
    def initialize
        self.now = Time.now
    end
    def print
        output(self.now.hour.to_s(2, 5))
        output(self.now.min.to_s(2, 6))
    end
private
    def output(binary)
        puts binary.words.map { |f| f == "0" ? "□" : "■" }.join.fix_width(6, " ")
    end
end

BinaryClock.new.print

Posted feedbacks - Other

VBAでもごくシンプルに。

VBAの場合、数値を二進数に変換する関数を実装する必要があった。

 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
Option Explicit

Public Sub BinaryClock()
    Dim Hour As Integer         '時
    Dim Min As Integer          '分
    Dim BinHour As String       '二進数(時)
    Dim BinMin As String        '二進数(分)
    Dim DispStrHour As String   '表示用文字列(時)
    Dim DispStrMin As String    '表示用文字列(分)

    '// 現在時刻の時・分を取得する。
    Hour = DatePart("h", Now)
    Min = DatePart("n", Now)
    
    '// 時・分を二進数に変換する。
    BinHour = IntToBin(Hour)
    BinMin = IntToBin(Min)
    
    '// 二進数を表示用文字列に変換する。
    DispStrHour = BinToDispStr(BinHour)
    DispStrMin = BinToDispStr(BinMin)
    
    '// 表示用文字列をメッセージボックスに出力する。
    Call MsgBox(DispStrHour & vbNewLine & DispStrMin, _
        vbInformation, _
        "バイナリクロック")
End Sub

'// 数値を二進数に変換する。
Private Function IntToBin(ByVal N As Integer) As String
    Dim Bin As String   '二進数
    
    Bin = ""
    Do While N > 1
        Bin = N Mod 2 & Bin
        N = N \ 2
    Loop
    Bin = N & Bin
    
    IntToBin = Bin
End Function

'// 二進数を表示用文字列に変換する。
Private Function BinToDispStr(ByVal Bin As String) As String
    Dim DispStr As String   '表示用文字列
    Dim i As Integer        'インデックス
    
    DispStr = ""
    For i = 1 To Len(Bin)
        If Mid(Bin, i, 1) = "0" Then
            DispStr = DispStr & "□"
        Else
            DispStr = DispStr & "■"
        End If
    Next
    
    BinToDispStr = DispStr
End Function

Squirrelで。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function tobits(n, bits)
{
    local ret = "";
    for(local m = 1<<(bits-1); m!=0; m=m>>1) ret += n&m ? "■" : "□";
    return ret;
}
function binclock(d)
{
    print(format(" %s\n", tobits(d.hour, 5)));
    print(format("%s\n", tobits(d.min, 6)));
}

binclock(date());

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
Option Explicit

Function formatClock(num, cols)
    Dim strResult, currentNum, i

    strResult = ""
    currentNum = num
    While currentNum > 0
        If currentNum Mod 2 = 0 Then
            strResult = "□" & strResult
        Else
            strResult = "■" & strResult
        End If
        currentNum = Int(currentNum / 2)
    Wend

    For i = 1 To (cols - Len(strResult))
        strResult = "□" & strResult
    Next

    formatClock = strResult
End Function

' 現在日時を取得
Dim currentDate
currentDate = Now()

' バイナリクロック形式にフォーマットして表示
WScript.Echo(formatClock(Hour(currentDate), 5))
WScript.Echo(formatClock(Minute(currentDate), 6))

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)

Index

Feed

Other

Link

Pathtraq

loading...