Update I did manage to make a client-side version of this.
In his blog, Mikhail shows a picture of a mystery object, noting that visitors don't always know what it is. Well, it turns out to be a clock that shows the hh:mm:ss as binary values. Cute.
In a comment, Mikhail adds "Actually, it is possible to ask [interview] candidate to write code that would light LEDs for this kind of clock." Well, shoot, that sounded like fun. Although I don't have any LEDs handy. Plus I usually work in ASP.NET Web pages, which don't exactly lend themselves to an interactive clock.
I played around with it just to get some ideas. My first attempt is hacked together. Turning the time digits into a string representation of binary (e.g., 0110 for 6, say) wasn't bad, and gave me a rare opportunity to use integer division. Turning the binary string into a representation on the page was a challenge. I wanted to use radio buttons to kind of approximate the look that Mikhail's clock has -- pseudo-LEDs stacked vertically. I tried a RadioButtonList, but wouldn't you know it, the buttons in the list are mutually exclusive by design. In the end, I decided to conjure up ungrouped radio buttons dynamically. A second problem was reversing the representation. I stumbled across an indirect solution for that one by using an array as a buffer for the strings.
This is what it looks like, big whoop:
If you want to see it as a page, click here.
It would be interesting to turn this into a more interactive clock using client script. The basic technique would have to be scrapped (the number of radio buttons would need to be fixed, not dynamic), which is probably why I won't be jumping on that opportunity real soon. But it'll be something to keep around for a rainy day. Along with the notion of using GDI+ to create the LED look.
Anyway, here's the code for the page. The layout is hard-coded as a table, which I figured was ok.<%@ Page Language="VB" Debug="true" %>
<script runat="server">
Sub Page_Load()
Dim currentTime As DateTime = DateTime.Now
Dim timeString As String = currentTime.ToString("HH:mm:ss")
lblTimeString.Text = timeString
IntToBinary(CInt( timeString.SubString(0,1) ), hoursTens, 2)
IntToBinary(CInt( timeString.SubString(1,1) ), hoursOnes, 4)
IntToBinary(CInt( timeString.SubString(3,1) ), minutesTens, 3)
IntToBinary(CInt( timeString.SubString(4,1) ), minutesOnes, 4)
IntToBinary(CInt( timeString.SubString(6,1) ), secondsTens, 3)
IntToBinary(CInt( timeString.SubString(7,1) ), secondsOnes, 4)
End Sub
Sub IntToBinary(num As Integer, container As Control, bitcount As Integer)
Dim binaryArray(bitCount) As Integer
Dim ctr As Integer = bitCount
While ctr > 0
If num mod 2 = 1 Then
binaryArray(ctr) = 1
Else
binaryArray(ctr) = 0
End If
num = num \ 2 ' Integer division, no remainder
ctr -= 1
End While
For i As Integer = 1 to binaryArray.Length - 1
Dim rb As New RadioButton
rb.checked = binaryArray(i)
container.Controls.Add(rb)
container.Controls.Add(New LiteralControl("<br>"))
Next
End Sub
</script>
<html>
<head>
<style>
TD { VERTICAL-ALIGN: bottom }
TD.timestring { TEXT-ALIGN: center }
.clock {
background-color: black;
border-color: gray;
border-width: 2;
border-style: solid;
padding: 2;
font-weight: bold;
color: lightgreen;
font-family: arial;
}
</style>
</head>
<body>
<form runat="server">
<table>
<tr>
<asp:tablecell id="hoursTens" runat="server"></asp:tablecell>
<asp:tablecell id="hoursOnes" runat="server"></asp:tablecell>
<td width="10"></td>
<asp:tablecell id="minutesTens" runat="server"></asp:tablecell>
<asp:tablecell id="minutesOnes" runat="server"></asp:tablecell>
<td width="10"></td>
<asp:tablecell id="secondsTens" runat="server"></asp:tablecell>
<asp:tablecell id="secondsOnes" runat="server"></asp:tablecell>
</tr>
<tr>
<td class="timestring" colspan="8">
<br />
<asp:Label id="lblTimeString"
runat="server" cssclass="clock">
</asp:Label>
</td>
</tr>
</table>
</form>
</body>
</html>