Re: Fun Test Problem

new topic     » goto parent     » topic index » view thread      » older message » newer message

Using Euphoria 4 syntax ...

include std/io.e 
include std/text.e 
include std/sequence.e 
include std/convert.e 
 
/* ** Get control data ** 

   This is one line containing three integers: 
      #1 = Maximum length of a word 
      #2 = Number of words in dictionary 
      #3 = Number of test cases in sample. 
*/ 
 
sequence dict = {} 
integer dictsize = 0 
 
integer inpf = open("input.txt", "r") 
if inpf = -1 then 
	puts(1, "Unable to open input.txt\n") 
	abort(0) 
end if 
 
integer outf = open("output.txt", "w") 
if outf = -1 then 
	puts(1, "Unable to open output.txt\n") 
	abort(0) 
end if 
 
object aLine 
 
aLine = gets(inpf) 
if atom(aLine) then 
	puts(1, "!! No data in input file\n") 
	abort(1) 
end if 
 
aLine = split(trim(aLine)) 
if length(aLine) != 3 then 
	puts(1, ` 

____ 
	!! Control data format error. 
	Expecting first line to contain exactly three integers 
	`) 

	abort(1) 
end if 
 
integer max_word_size = to_integer(aLine[1]) 
integer max_dict_size = to_integer(aLine[2]) 
integer max_test_size = to_integer(aLine[3]) 
 
for i = 1 to max_dict_size do 
	aLine = gets(inpf) 
	if atom(aLine) then 
		puts(1, "!!Expected more dictionary entries.\n") 
		abort(1) 
	end if 
	dict = append(dict, trim(aLine)) 
end for 
 
integer this_case = 0 
integer index 
integer state 
sequence pattern 
 
for i = 1 to max_test_size do 
	aLine = gets(inpf) 
	if atom(aLine) then 
		puts(1, "!!Expected more test entries.\n") 
		abort(1) 
	end if 
	 
	aLine = trim(aLine) 
	this_case += 1 
	pattern = {} 
	index = 1 
	state = 0 
	while index <= length(aLine) do 
		switch aLine[index] do 
			case '(' then 
				if state = 0 then 
					state = 1 
					pattern = append(pattern, {}) 
				else 
					printf(1, "%s\n", {aLine}) 
					printf(1, "!!Not expecting '(' at column %d\n", index) 
					abort(1) 
				end if 
				 
			case ')' then 
				if state = 1 then 
					state = 0 
				else 
					printf(1, "%s\n", {aLine}) 
					printf(1, "!!Not expecting ')' at column %d\n", index) 
					abort(1) 
				end if 
				 
			case else 
				if state = 0 then 
					pattern = append(pattern, {aLine[index]}) 
				else 
					pattern[$] &= aLine[index] 
				end if 
		end switch 
		index += 1 
	end while 
	 
	if state != 0 then 
		printf(1, "%s\n", {aLine}) 
		puts(1, "!!Missing ')'\n") 
		abort(1) 
	end if 
 
	-- At this point, we have a pattern set up, 
	-- so we check each word in the dictionary against the pattern.	 
	integer case_count = 0 
	for j = 1 to length(dict) label "dict_scan" do 
		sequence this_word = dict[j] 
		if length(this_word) != length(pattern) then 
			continue 
		end if 
		 
		for k = 1 to length(this_word) do 
			if find(this_word[k], pattern[k]) = 0 then 
				continue "dict_scan" 
			end if 
		end for 
	 
		case_count += 1 
		 
	end for 
	 
	printf(outf, "Case #%d:%d\n", {i, case_count}) 
end for 
 
close(inpf) 
close(outf) 
-- end of code -- 
 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu