asdsd

Ruby code posted by asdsda
created at 16 Dec 09:45

Edit | Back
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
59
60
61
62
63
64
require 'pp'
P={
  "q0"=>[["a","q1"]],
  "q1"=>[["b","q2"],["b","q1"]],
  "q2"=>[["b","q0"],["a","q3"]],
  "q3"=>[["a","q4"]],
  "q4"=>[["a","q0"]]
}
sigma=["a","b"]
s=["q0"]
f=["q3"]
i=0
P1=Hash.new()
q=[]
q<<s
while q.size>i do
  current=q[i]
  puts "current at start = #{current}"
  temp=[]
  puts "current=#{current}"
  sigma.each do |terminal|
    puts "terminal=#{terminal}"
    new_current=[]
    current.each do |stare|
      puts "stare=#{stare}"
      if P.has_key?(stare)
        P[stare].each do |item|
          if item[0]==terminal
            new_current<<item[1] 
          end
        end
        if new_current!=[]
           if temp!=[]
             if temp.last[0]==terminal 
              temp.pop
            end
          end
          temp<<[terminal, new_current.join]
        end
      end
      puts "new_current=#{new_current}"
      if(new_current.size>0)&&( q.index(new_current)==nil )
        q<<new_current 
      end
      P1[current.join]=temp
    end  
  end
  puts "q final=#{q}"
  i+=1
  puts
end
puts
puts



pp P1
=begin
P1.each do |stare, prods|
  prods.uniq.each do |prod|
    puts "#{stare}----#{prod[0]}--->#{prod[1]}"
  end
end
=end
1.09 KB in 5 ms with coderay