Info 2, Kilencedik heti előadás: ruby példák

Utolsó módosítás: 2009. április 9.

Állatos játék

h9g0.rb

# Állat kitalálós játék

def yesno()
        input = readline()
        ok = false
        reply = false
        while !ok
                if ?i == input[0] || ?I == input[0]
                        ok = true
                        reply = true
                elsif ?n == input[0] || ?N == input[0]
                        ok = true
                        reply = false
                else
                        print("Igen vagy nem választ várok.\n")
                end
        end
        reply
end

tree = [true, "Tud repülni?", [false, "sárgarigó"], [false, "macska"]]
while true
        print("Gondoljon egy állatra!\n")
        cur = tree
        while cur[0]
                print("Kérdezek. ")
                puts(cur[1])
                if yesno()
                        cur = cur[2]
                else
                        cur = cur[3]
                end
        end
        print("Rákérdezek: ")
        guess = cur[1]
        print(guess)
        puts("?")
        if yesno()
                puts("Remek!  Játsszunk még!")
        else
                puts("Feladom.  Mire gondolt?")
                real = readline().chomp()
                print( \
                        "Mondjon egy olyan eldöntendő kérdést, ami megkülönbözteti ezt a két állatot: ", \
                        guess, " és ", real, "!\n");
                question = readline()
                print( \
                        "Mi lenne a válasz erre a kérdésre, ha a gondolt állat a ", \
                        real, "?\n")
                reply = yesno()
                cur[0] = true
                cur[1] = question
                if reply
                        cur[2] = [false, real]
                        cur[3] = [false, guess]
                else
                        cur[2] = [false, guess]
                        cur[3] = [false, real]
                end
                print("Köszönöm, legközelebb már ezt is tudni fogom.\n")
        end
end

Számrejtvény

h9g1.rb

# számrejtvény
input = [6,1,3,4]
$solution = 24
$poss = []
def check(freq, str, val)
        t = [freq, str, val]
        #p(t)
        $poss.push(t)
        all = true
        j = 0
        while j < freq.size
                if 0 == freq[j]
                        all = false
                end
                j += 1
        end
        if all && ($solution - val).abs() < 1e-6
                print($solution, " = ", str, "\n")
        end
end
n = 0
while n < input.size()
        freq = []
        j = 0
        while j < input.size()
                freq[j] = 0
                j += 1
        end
        freq[n] = 1
        check(freq, input[n].to_s(), input[n])
        n += 1
end
iter = 0
while iter < input.size()
        max = $poss.size()
        m = 0
        while m < max
                n = 0
                while n < max
                        clash = false
                        freq = []
                        j = 0
                        while j < input.size()        
                                freq[j] = $poss[m][0][j] + $poss[n][0][j]
                                if 1 < freq[j]
                                        clash = true
                                end
                                j += 1
                        end
                        if !clash
                                check(freq, \
                                        "(" + $poss[m][1] + "+" + $poss[n][1] + ")", \
                                        $poss[m][2] + $poss[n][2])
                                check(freq, \
                                        "(" + $poss[m][1] + "-" + $poss[n][1] + ")", \
                                        $poss[m][2] - $poss[n][2])
                                check(freq, \
                                        "(" + $poss[m][1] + "*" + $poss[n][1] + ")", \
                                        $poss[m][2] * $poss[n][2])
                                if 0 != $poss[n][2]
                                        check(freq, \
                                                "(" + $poss[m][1] + "/" + $poss[n][1] + ")", \
                                                $poss[m][2] / Float($poss[n][2]))
                                end
                        end
                        n += 1
                end
                m += 1
        end
        iter += 1
end

Gyorsított számrejtvény

h9g2.rb

# számrejtvény
input = [6,1,3,4]
$solution = 24
$found = []
$poss = [[]]
def check(depth, freq, str, val)
        p([freq, str, val, depth])
        $poss[depth].push([freq, str, val])
        all = true
        j = 0
        while j < freq.size
                if 0 == freq[j]
                        all = false
                end
                j += 1
        end
        if all && ($solution - val).abs() < 1e-6
                announce = $solution.to_s + " = " + str + "\n"
                print announce
                $found.push(announce)
        end
end
n = 0
while n < input.size()
        freq = []
        j = 0
        while j < input.size()
                freq[j] = 0
                j += 1
        end
        freq[n] = 1
        check(0, freq, input[n].to_s(), input[n])
        n += 1
end
depth = 1
while depth < input.size()
        $poss[depth] = []
        mdepth = 0
        while mdepth < depth
                warn mdepth
                mmax = $poss[mdepth].size()
                ndepth = depth - mdepth - 1
                nmax = $poss[ndepth].size()
                m = 0
                while m < mmax
                        a = $poss[mdepth][m]
                        n = 0
                        while n < nmax
                                b = $poss[ndepth][n]
                                clash = false
                                freq = []
                                j = 0
                                while j < input.size()        
                                        freq[j] = a[0][j] + b[0][j]
                                        if 1 < freq[j]
                                                clash = true
                                        end
                                        j += 1
                                end
                                if !clash
                                        check(depth, freq, \
                                                "(" + a[1] + "+" + b[1] + ")", \
                                                a[2] + b[2])
                                        check(depth, freq, \
                                                "(" + a[1] + "-" + b[1] + ")", \
                                                a[2] - b[2])
                                        check(depth, freq, \
                                                "(" + a[1] + "*" + b[1] + ")", \
                                                a[2] * b[2])
                                        if 0 != b[2]
                                                check(depth, freq, \
                                                        "(" + a[1] + "/" + b[1] + ")", \
                                                        a[2] / Float(b[2]))
                                        end
                                end
                                n += 1
                        end
                        m += 1
                end
                mdepth += 1
        end
        depth += 1
end
j = 0
while j < $found.size
        print $found[j]
        j += 1
end