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 - Batchfile

バッチで。

時分秒を 6桁で表示します。

callを使用するとサブルーチンを呼び出す分だけ時間がかかり、正
確に 1秒を刻めなくなってしまうので、処理をすべて展開しました。

# なお、12行目は 2桁の数値の上位桁が 0である時、 8進数として
# 解釈されないようにするための処理です。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@echo off
setlocal enabledelayedexpansion
  set m=0
  set n=0
  set s=
  set t=
  
  :_
    for /f "delims=." %%t in ("%TIME%") do set t=%%t
    cls
    for %%t in (%t::= %) do (
      set /a n=1%%t-100
      set s=
      for /l %%i in (1,1,6) do (
        set /a m=n%%2
        if !m! equ 0 (set s=□!s!) else (set s=■!s!)
        set /a n/=2
      )
      echo !s!
    )
    ping -n 2 127.0.0.1 > NUL
  goto _
endlocal

Index

Feed

Other

Link

Pathtraq

loading...