Computer Science Canada

DWITE NOVEMBER 25 2005 PROBLEM 3 SOLUTION

Author:  xXInsanityXx [ Sat Nov 26, 2005 3:09 pm ]
Post subject:  DWITE NOVEMBER 25 2005 PROBLEM 3 SOLUTION

Hi i was wondering if you guys can give me suggestions,feedback, and simplifications to this code. Please feel free to criticize as much as you want.

Please and Thank You

NOTE: PLEASE DO NOT CLAIM THIS CODE AS YOURS -not that its that great-

Author:  MysticVegeta [ Sat Nov 26, 2005 11:20 pm ]
Post subject: 

May I ask where the code is?

Author:  Hikaru79 [ Sun Nov 27, 2005 12:01 am ]
Post subject: 

Attached to his post?

Author:  MysticVegeta [ Sun Nov 27, 2005 1:00 am ]
Post subject: 

I cannot see it Confused

Author:  Hikaru79 [ Sun Nov 27, 2005 1:03 am ]
Post subject: 

Hm, wierd =/ I'll upload it to this post. Can you see it on mine?

Author:  MysticVegeta [ Sun Nov 27, 2005 11:35 am ]
Post subject: 

yeah, heres mine by the ways.
Turing:
var fi, fo : int

open : fi, "DATA31.txt", get
open : fo, "OUT31.txt", put

var lines : string
var numbers : array 1 .. 5 of int
var syl : string := "24682"
var current, high, lineno, original : int := 0


fcn wordCount (word : string) : int
    var count := 0
    for s : 1 .. length (word)
        if word (s) = " " or word (s) = "-" then
            count += 1
        end if
    end for
    result count + 1
end wordCount

proc errors
    for x : 1 .. 5
        numbers (x) := strint (syl (x)) - numbers (x)
        if numbers (x) not= 0 then
            current := abs (numbers (x))
            if current > high then
                original := numbers (x)
                high := current
                lineno := x
            end if
        end if
    end for
end errors

for s : 1 .. 5
    for x : 1 .. 5
        get : fi, lines : *
        numbers (x) := wordCount (lines)
    end for
    errors
    if abs (original) = original then
        put : fo, "LINE ", lineno, " - ", original, " SYLLABLE(S) TOO FEW"
    else
        put : fo, "LINE ", lineno, " - ", abs (original), " SYLLABLE(S) TOO MANY"
    end if
    lineno := 0
    original := 0
    current := 0
    high := 0
end for
close (fi)
close (fo)


I dont know if this is the original one or the bad one.. I sorta screwed up my folder because I was p!ssed earlier. lol Laughing

Author:  Cervantes [ Sun Nov 27, 2005 11:53 am ]
Post subject: 

MysticVegeta: Weren't you supposed to put nothing if all the lines are correct? And won't that put "LINE 0 - 0 SYLLABLE(S) TOO FEW"?

Author:  Hikaru79 [ Sun Nov 27, 2005 12:34 pm ]
Post subject: 

This sort of problem is where Java's beautiful library comes in handy. One line,
Java:
StringTokenizer st = new StringTokenizer (in.readLine()," -");
and the whole question just falls apart Very Happy When you're on a timed contest, this sort of thing is great.

Author:  MysticVegeta [ Sun Nov 27, 2005 2:01 pm ]
Post subject: 

Cervantes wrote:
MysticVegeta: Weren't you supposed to put nothing if all the lines are correct? And won't that put "LINE 0 - 0 SYLLABLE(S) TOO FEW"?


Never said that one the question that all lines will be correct.

Hikaru: What does the String.Tokenizer do?

Author:  Hikaru79 [ Sun Nov 27, 2005 2:10 pm ]
Post subject: 

MysticVegeta wrote:
Cervantes wrote:
MysticVegeta: Weren't you supposed to put nothing if all the lines are correct? And won't that put "LINE 0 - 0 SYLLABLE(S) TOO FEW"?


Never said that one the question that all lines will be correct.

Hikaru: What does the String.Tokenizer do?


StringTokenizer splits up a string based on the presence of certain characters. Notice how when I call the StringTokenizer constructor, I gave it an argument " -"? What that does is take a String, and break it into a different piece whenever it finds a space or a '-' character -- basically solving the problem for you. Then you simply have to count the amount of Tokens that StringTokenizer gives you, and voila, you know how many syllables there are Smile

Author:  MysticVegeta [ Sun Nov 27, 2005 2:33 pm ]
Post subject: 

Dont you need two of these commands, 1 for " ", and other for "-"? Also, mind telling me the stringSplit command in Java? Thanks a lot Smile

Author:  Hikaru79 [ Sun Nov 27, 2005 2:54 pm ]
Post subject: 

MysticVegeta wrote:
Dont you need two of these commands, 1 for " ", and other for "-"?

Nope Smile The second parameter is optional, but if you give it, it treats every single character in the string as a delimiter. No need to tokenize the tokens twice, that would just be painful. StringTokenizer is a powerful tool, I think I use it on just about every contest I've ever written.

MysticVegeta wrote:
Also, mind telling me the stringSplit command in Java? Thanks a lot Smile

I've never heard of a stringSplit method, sorry Sad

Author:  Cervantes [ Sun Nov 27, 2005 3:16 pm ]
Post subject: 

MysticVegeta wrote:
Dont you need two of these commands, 1 for " ", and other for "-"?

Notice that the parameter he passed in contained two characters: a space and a hyphen. I assume that it would tokenize the string based on every character in that string parameter. Seems to me that it would make more sense if it passed in an array of characters. That would also give more flexibility: you could tokenize the string at space, hyphen, and a double hyphen.

Author:  MysticVegeta [ Sun Nov 27, 2005 3:25 pm ]
Post subject: 

Yeah I noticed it before posting too but then I thought about it and thought that since "-" and " " form 1 string, maybe it would tokenize the part where it would find " -". If it does otherwise, how can we tokenize a string with more than 1 character?

Author:  Hikaru79 [ Sun Nov 27, 2005 4:41 pm ]
Post subject: 

Cervantes wrote:
MysticVegeta wrote:
Dont you need two of these commands, 1 for " ", and other for "-"?

Notice that the parameter he passed in contained two characters: a space and a hyphen. I assume that it would tokenize the string based on every character in that string parameter. Seems to me that it would make more sense if it passed in an array of characters. That would also give more flexibility: you could tokenize the string at space, hyphen, and a double hyphen.


Hmm, that's an excellent idea. I don't know why Sun never thought of that.

Perhaps I'll make a class Extending StringTokenizer which can do just that Very Happy

Author:  zylum [ Mon Nov 28, 2005 10:11 am ]
Post subject: 

you should use String.split(String s); rather than tokenizer.. its much more powerful. it accepts a string as a parameter and it returns an array of strings. the string is split based on the string you provided. you can even use regular expressions as a parameter. so for the example above ie hyphen, space, or both you would do:

String[] splitArray = stringToSplit.split("[ -]++");

Author:  bugzpodder [ Mon Nov 28, 2005 10:38 am ]
Post subject: 

umm not really (to the post above zylum). there are design issues with this idea.

for example, if we were to tokenize based on "--" and "-"
on the string

omg---omg

Author:  MysticVegeta [ Mon Nov 28, 2005 2:04 pm ]
Post subject: 

I am confused Confused Does the (" -") split both " " and "-"? also, what is the "++" for? Mad

Author:  bugzpodder [ Mon Nov 28, 2005 2:41 pm ]
Post subject: 

i believe thats regular expressions.

Author:  Cervantes [ Mon Nov 28, 2005 4:12 pm ]
Post subject: 

bugzpodder wrote:
umm not really (to the post above zylum). there are design issues with this idea.

for example, if we were to tokenize based on "--" and "-"
on the string

omg---omg


Good point. I guess you'd throw an error if one of the strings is a substring of another string. ie. "-" is a substring of "--".

Still, it could be useful.

Author:  xXInsanityXx [ Mon Nov 28, 2005 6:28 pm ]
Post subject: 

To Mysticvegeta,

Ignore my idiocy, but in this particular case, doesnt your way seem to be a bit too complicated, or elongated, and the logic is stretching much more farther than the necessity, please i want feedback and opinions from everybody. Honestly, i would love to have programming skills like mystic, but wouldnt this take much more time?

Author:  MysticVegeta [ Mon Nov 28, 2005 6:54 pm ]
Post subject: 

what the heck? Your team scored more than me! Besides if you want programming skills really good, make them like Zylum, Bugz, Cervantes, wtd, rizzix, Hikaru.... I am really a noobie compared to them Exclamation

Author:  bugzpodder [ Mon Nov 28, 2005 7:01 pm ]
Post subject: 

Cervantes wrote:
bugzpodder wrote:
umm not really (to the post above zylum). there are design issues with this idea.

for example, if we were to tokenize based on "--" and "-"
on the string

omg---omg


Good point. I guess you'd throw an error if one of the strings is a substring of another string. ie. "-" is a substring of "--".

Still, it could be useful.


what about trying to use
hello
lol
as tokens on

hellololol

there are two problems here, hello and lol overlap, and lol overlaps with itself.

too many errors thrown, i'd say.

Author:  Cervantes [ Mon Nov 28, 2005 8:17 pm ]
Post subject: 

Good point, bugzpodder.

Still, it seems like a feature that would be useful. Consider the same problem, except instead of using "-" they used "--". One wouldn't be able to use the tokenizer then.

Perhaps it would work with an precedence order. First it splits everything up based on the first element of the array, then splits those up based on the second, etc.

Author:  bugzpodder [ Mon Nov 28, 2005 10:27 pm ]
Post subject: 

acutally you can still use the tokenizer. i am just not sure the correct behaviour though, you might either get empty string between the double dash or you might not get it. its easy enough to just skip over the empty strings.

All in all, I see no point in extending tokenizer to general strings. if you want the behaviour you descirbed, you can just use the find/search command for the first string, then the second, etc...

Author:  zylum [ Mon Nov 28, 2005 11:06 pm ]
Post subject: 

can anybody post the harder problems from the last contest?

Author:  JackTruong [ Mon Nov 28, 2005 11:30 pm ]
Post subject: 

Which ones are the harder ones? I thought the hard ones were #4 and possibly #5. I can post the Java for them.

Author:  zylum [ Tue Nov 29, 2005 2:00 pm ]
Post subject: 

i mean the problems not the solutions...

Author:  xXInsanityXx [ Wed Nov 30, 2005 1:16 pm ]
Post subject: 

zylum wrote:
i mean the problems not the solutions...


go to dwite.org previous contest, november 2005, scroll down and you will find a list of the problems in pdf format read em test em with data, and solve em too Wink

Author:  MysticVegeta [ Wed Nov 30, 2005 2:34 pm ]
Post subject: 

oh. dont worry about him, he will complete all 5 in 45 mins.


: