October 24, 2010

GDC

It's good to have two pupil at home.

They explain me that
gdc(a,b) . lcm(a,b) = a . b

Equally nice to have i-net it explain that because
max(a,b) + min(a,b) = a + b

Can you see connection?

Trivial explanation is in fact that divisibility define lattice on R with meet given by gdc and join by lcm.

October 18, 2010

`three pieces of `four

Hi,

One morning Me, Iri and BGD were coming home after swimming. Cap of hot tee was long anticipated and we bought to it 3 peaces of `cremsnit (aka. Napoleon in U.S.).

Already far from backerei we realize - there 4 of us to share 3 peaces. How to share?

BGD suggest that we split each unit by 4, formally
Solution 1:
3u = 3 * 4 * u/4 = 4 * 3u/4  

I wanted extra unit for myself (irrational part of deal), than we can take module and split it even !
Solution 2:
|2u + i1u| = 1u + 2 * 2 * u/2 = 4 * u/2

Ups.. nobody liked my idea much.

Iri comes with another split where two pieces divided by half and one by four:
Solution 3:
2 * 2u/2 + 1 * 4u/4 = 4 * u/2 + 4 * u/4 = 4 * (u/2 + u/4) 

She argued that this fractal approach guaranty to work if we were N modulo 4:
N * (u/2 + u/4 + .. + u/N)

At the time of last solution we already finished tee.

I told kids folks tail about smart crow who helped divide cheese ball between two baby foxes. After initial split, ether fox wasn't happy with it piece so crow have to eat smaller one and start all over again. Eventually they reach atomic cheese unit. You can imagine how small it was but everybody left happy, including stomachached craw.

Kids agreed that crows solution was fare.
It makes me think about kind of education I provide to them.

October 2, 2010

Smell of Flower

Hi,


I'm programmer. It feels like being a Cylon most of the time and I'm not alone in this.

There is this program which freaks me out. It emulate sunrise and sunset by tuning color temperature on my monitors.

/home/../bin/xflux -l 45.0 -g 21.0 -k 3400

Being totally pro geek I giving you idea: install F.Lux and invite your girl for sunrise date in front of the monitor.

I'm looking forward for `X.Flower program appearing soon.

September 21, 2010

Raf's Problem

Another nice problem for testing your programming skills:

Given a String (x) containing only characters a-z, write a function (f)
that returns a base 10 integer, which converts the String as if it were
a base 26 numeral. Function f is bijective**.


Here are some example runs:


x      | f(x)
empty  | 0
a      | 1
b      | 2
z      | 26
aa     | 27
az     | 52
ba     | 53
bz     | 78
aaa    | 703
aaz    | 728
... 
aza    | 1353

Using your preferred programming language, implement function f.

* - it's known as Raf's problem and I saw it on Tony Morris `lambda blog
** - BUG: this function is injective since we can't use `spaces in number to string encoding ***
*** - we can make use of spaces if numeral separator is two-spaced but it is not what was asked - see example for encoding 26 and 27


Following is sample solutions done in Scala / Python / Haskell. Not sure about complexity level but it seems that python isn't shortest language anymore??

Haslell
str2num = foldl (\acc x -> acc * 26 + (ord x - (ord 'a') + 1)) 0

Scala
def str2num(str: String): Int = {
    val base = 26
    val offset = 'a'.toInt -1
    var res = 0
    for (c <- str) {
      val digit = if (c == ' ') 0 else (c.toInt - offset)
      res = res * base + digit
    }
    res
  }

  def num2str(n: Int): String = {
    val base = 26
    val offset = 'a'.toInt -1

    def aux(n: Int): List[Char] = (n / base, n % base) match {
      case (0, 0) => List(' ')
      case (0, r) => List((r + offset).toChar)
      case (q, 0) => 'z' :: aux(q-1)
      case (q, r) => (r + offset).toChar :: aux(q)
    }


    val res = aux(n).reverse.mkString
    if (res == " ") res else res.trim
  }

  test("Raf's Problem"){
    // x   | f(x)
    val tests = Map (
      " "   ->  0,
      "a"   ->  1,
      "b"   ->  2,
      "z"   ->  26,
      "aa"  ->  27,
      "az"  ->  52,
      "ba"  ->  53,
      "bz"  ->  78,
      "aaa" ->  703,
      "aaz" ->  728,
      "aza" ->  1353)
    for (t <- tests) {
      println(t._1 + " -> " + t._2 + " => <" + num2str(t._2) + ">")
      assert(str2num(t._1) == t._2)
      assert(t._1 == num2str(t._2))
    }
  }

Python
import string


def code(number):
    if number>0 and number<27:
        return chr(number+96)

def decode(s):
    if s in string.ascii_lowercase:
        return ord(s)-96


def coding(number):
    if number==0:
        return ""
    s=[]
    while number>0:
        x=number%26
        if x==0:
            x=26
            number=number-26
        y=code(x)    
        t=[y]+s
        s=t
        number=number/26

    return ''.join(s).split()[0]

def decoding(s):
    l=len(s)
    num=0
    for i in s:
        if not i==' ':
            c=decode(i)
            num=num*26+c
    return num



Super python
#!/usr/bin/python
 
from math import pow
from sys import argv

"""Details
16.09.2010
Given a String (x) containing only characters a-z, write a function (f) that returns a base 10 integer, which converts the String as if it were a base 26 numeral. Function f is bijective.

Nicu Marcu
*.*@*-*.com
"""

letters=map(chr, range(97,123))
 
def str2num(text):
 to_str2num=[]
 for l in text:
  index=1
  for ll in letters:
   if l == ll:
    to_str2num.append(index)
   index+=1
 index=0
 result=0
 for l in reversed(to_str2num):
  p = int(pow(26,index))
  result+=l*p
  index+=1
 
 return result
  

def num2str(nr):
 result=[]
 
 a = divmod(int(nr),26)
 rez = a[0]
 rest = a[1]
 
 if rez==0:
  result.append(rest)
  return result
 if rez==1 and rest==0:
  result.append(26)
  return result
 while(1):
  if rest==0:
   result.append(26)
   rez-=1
  else:
   result.append(rest)
  if rez==0:
   return result
  if rez<=26:
   result.append(rez)
   return result
   break
  else:
   a = divmod(rez,26)
   rez = a[0]
   rest = a[1]
 
def main():
 funct = argv[1]
 arg = argv[2]
 if funct=="str2num":
  print str2num(arg)
 if funct=="num2str":
  a = reversed(num2str(arg))
  world=""
  for i in a:
   world+=letters[i-1]
 
  print world
 return 1
main()

September 3, 2010

H2G2

Hi,





"In many of the more relaxed civilizations on the Outer Eastern Rim of the Galaxy, the Hitchhiker's Guide has already supplanted the great Encyclopaedia Galactica as the standard repository of all knowledge and wisdom, for though it has many omissions and contains much that is apocryphal, or at least wildly inaccurate, it scores over the older, more pedestrian work in two important respects.


"First, it is slightly cheaper; and secondly it has the words DON'T PANIC inscribed in large friendly letters on its cover.









UPDATE:
We make few coves for road safety...

Steam-punk version
Old t-shirt recycled

Autumn colors

Late Autumn collection