Folgendes Stueck Java funktioniert nicht:
Siehe http://www.java-forum.org/plauderecke/22639-java-quiz-71.html#post805970
Wieso klappt das problemlos mit Ruby?
Java:public static void main(String[] args) { Pattern p = Pattern.compile("(aa|aab?)+"); int count = 0; for (String s = ""; s.length() < 200; s += "a") { if (p.matcher(s).matches()) { count++; } } System.out.println(count); }
Siehe http://www.java-forum.org/plauderecke/22639-java-quiz-71.html#post805970
Wieso klappt das problemlos mit Ruby?
Code:
count = 0
(0..199).each do |i|
s = "a"*i
# if s =~ /(aa|aab?)+/ -> liefert ab "aa" immer true
res = s.match(/(aa|aab?)+/)
if !res.nil? and res[0]==s
count = count + 1
end
end
puts count
Code:
> time ruby test.rb
99
real 0m0.151s
user 0m0.046s
sys 0m0.015s
Zuletzt bearbeitet: