

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A pseudo-midterm exam for the cmsc330 course during the summer 2006 semester. The exam covers various topics in ruby programming, including conditional statements, arrays, sorting, hashes, and regular expressions. It also includes questions related to writing ruby programs, understanding formal regular expressions, and converting nfas to dfas.
Typology: Exams
1 / 2
This page cannot be seen from the preview
Don't miss anything!


INSTRUCTOR: GUILHERME FONSECA
(1) (20 points) What is the output of each Ruby program below? Ignore any possible warning message. All programs are syntatically valid. (a) if 0 puts "ab" elsif "" puts "cd" else puts "ef" end (b) a = b = [3, 1, 2] a.sort! puts b (c) a = [[1,2], [1,2,1], [-1], [1,3]] puts a.sort.inspect (d) h = Hash.new([]) h["a"] = 3 h["b"] = h["b"] + [5] h["c"] = h["c"] + [7] puts "#{h["a"]} #{h["b"][0]} #{h["c"][-1]}"
(2) (10 points) Write a ruby program that reads several whole numbers from the standard input, and prints them sorted by the absolute value. In Ruby, if x is a Fixnum, x.abs returns the absolute value of x. The input numbers have no leading zeroes or spaces, and are presented as one number per line. Output them as one number per line too.
(3) (21 points) Write a formal regular expression for each of the languages below. The alphabet is Σ = {a, b}. The only operators allowed are ∗ and | (do not write a Ruby regular expression). (a) {w | the number of a’s before the first b in w is at least 3 } w is trivially in the language if there is no b. (b) {w | the number of a’s before the first b in w is at most 5 } w is trivially in the language if there is no b. (c) The intersection of the two languages above.
(4) (8 points) Write a formal regular expression for each of the languages below. The alphabet is Σ = {a, b, c}. The only operators allowed are ∗ and | (do not write a Ruby regular expression). (a) {w | there is no a adjacent to b} 1
(5) (21 points) Write a DFA for each of the languages below. The alphabet is Σ = {a, b}. (a) {w | w has at least one a, at least one b, and all the a’s occur before all the b’s } (b) {w | #a(w) = 1 (mod 3) and #b(w) = 0 (mod 4)} (c) {w | w ends with aba}
(6) (10 points) Convert the following NFA to a NFA without ε transitions. a a b
S T U V ε (^) ε ε
(7) (10 points) Convert the following NFA to a DFA.
S T
V U
a
a
a b
b
a
a
(8) (10 points) Convert the following formal regular expression to a NFA:
b(a|aba)∗(bb)∗|bab
2