Author |
Message |
Flikerator
|
Posted: Thu May 26, 2005 7:24 am Post subject: Help real quick, I gotta go in 4 mins eep |
|
|
Im going to a contest soon and I only have a few minutes before I leave I know its chance someone will answer but I can try. Why doesn't this work, it removes all letters.
code: | var word : string := "hel3lOE1asa5d"
var neword : string := ""
for i : 1 .. length (word)
if ord (word (i)) < 65 and ord (word (i)) > 90 and ord (word (i)) < 97 and ord (word (i)) > 122 then
neword += word (i)
put ord (word (i))
end if
end for
put neword |
25 bits if I get the answer before I leave |
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Thu May 26, 2005 7:39 am Post subject: (No subject) |
|
|
What do you mean removes all the letters? |
|
|
|
|
|
MysticVegeta
|
Posted: Thu May 26, 2005 7:43 am Post subject: (No subject) |
|
|
code: | var word : string := "hel3lOE1asa5d"
var neword : string := ""
for i : 1 .. length (word)
if (ord (word (i)) > 65 and ord (word (i)) < 90) or (ord (word (i)) > 97 and ord (word (i)) < 122) then
neword += word (i)
put ord (word (i))
end if
end for
put neword
|
you mean this? |
|
|
|
|
|
cornputer
|
Posted: Thu May 26, 2005 9:55 am Post subject: (No subject) |
|
|
because of the and at the second part
turing doesnt take the second part as (condition1 +condition2)or (1 + 2 )
it takes condition 1 AND (1 or 2) AND condition 3
not sure if im totally right but ive tried a similar error and i think it was like this |
|
|
|
|
|
Flikerator
|
Posted: Thu May 26, 2005 12:52 pm Post subject: (No subject) |
|
|
Im back, came second I think
There were no questions with that so its all good.
I jsut wanted all the uppercase and lowercase to be taken out of the string leaving the rest. If anyone has an answer to it ill give em 10 bits, it has to use ord.
if word (i) = "a" then
Is something I want to avoid, thats the whole reason for the ord |
|
|
|
|
|
MysticVegeta
|
Posted: Thu May 26, 2005 3:34 pm Post subject: (No subject) |
|
|
code: | var word : string := "ASDasdaASI/././D123JsE"
var ne : string := ""
for s : 1 .. length (word)
if (word (s) >= "A" and word (s) <= "Z") or (word (s) >= "a" and word (s) <= "z") then
ne := ne
else
ne += word (s)
end if
end for
put ne
|
This?
Offtopic: what contest? |
|
|
|
|
|
Token
|
Posted: Thu May 26, 2005 4:58 pm Post subject: (No subject) |
|
|
code: | var word : string := "hel3lOE1asa5d"
var neword : string := ""
for i : 1 .. length (word)
if ord (word (i)) > 64 and ord (word (i)) < 91 or ord (word (i)) > 96 and ord (word (i)) < 122 then
else
neword += word (i)
end if
end for
put neword
|
You had the right idea, just a little off |
|
|
|
|
|
Cervantes
|
Posted: Thu May 26, 2005 5:51 pm Post subject: (No subject) |
|
|
It should be noted that, depending on what you intend to do with the program, the second variable may be unnecessary, for one could just do this:
code: |
word := word (1 .. i - 1) + word (i + 1 .. *)
|
But that brings me to a question:
code: |
var word : string := "hel3lOE1asa5d"
for i : 1 .. length (word)
if ord (word (i)) > 64 and ord (word (i)) < 91 or ord (word (i)) > 96 and ord (word (i)) < 122 then
else
word := word (1 .. i - 1) + word (i + 1 .. *)
end if
end for
put word
|
I'm getting an error: substring index is greater than length of string. Do you guys get the error as well? (Over the years I've sometimes noticed Turing behaving irrationally. I've failed to find anything wrong with my code, so I'm wondering if it's Turing itself. ) Does turing highlight the if statement? It seems to me that if there is an error it should be at the word := word (1 etc.
I get the same error when I rearrange the if statement so we don't have that hanging "else" there:
code: |
var word : string := "hel3lOE1asa5d"
for i : 1 .. length (word)
%if the character, word (i), is not a letter.
if ord (word (i)) < 65 or ord (word (i)) > 122 or ord (word (i)) > 90 and ord (word (i)) < 97 then
word := word (1 .. i - 1) + word (i + 1 .. *)
end if
end for
put word
|
Even the word assignment line should not be a proble. Assuming i = 1:
word (1 .. i - 1) will be word (1 .. 0) which equates to nothing. We are allowed to have the right number one less than the left number.
word (i + 1 .. *) will be word (2 .. *), which should be fine.
Assuming i = length (word):
word (1 .. i - 1) should be fine. word (i + 1 .. *) will be word (* + 1 .. *) which is again a case of the right side being one less than the left side.
When that happens (right side 1 less than left) it just skips over it.
Anyone have any ideas as to why there's an error? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Token
|
Posted: Thu May 26, 2005 8:07 pm Post subject: (No subject) |
|
|
yah man thats probibly turing just being screwy again, it has done that to me before too, i made this calculator program and it kept telling me that the square root of 9 was 2.999999 lol it was messssssssssed up. |
|
|
|
|
|
MysticVegeta
|
Posted: Fri May 27, 2005 11:19 am Post subject: (No subject) |
|
|
Token wrote: code: | var word : string := "hel3lOE1asa5d"
var neword : string := ""
for i : 1 .. length (word)
if ord (word (i)) > 64 and ord (word (i)) < 91 or ord (word (i)) > 96 and ord (word (i)) < 122 then
else
neword += word (i)
end if
end for
put neword
|
You had the right idea, just a little off
eh? so he needs to use (ord)? because this works too |
|
|
|
|
|
Token
|
Posted: Fri May 27, 2005 2:23 pm Post subject: (No subject) |
|
|
yah, urs is rite, it works, but he needed to use ord, which is the same thing as what your doing becuase it automatically recognises the ascii value in yours. |
|
|
|
|
|
MysticVegeta
|
Posted: Fri May 27, 2005 3:26 pm Post subject: (No subject) |
|
|
why make it so confusing using ord? |
|
|
|
|
|
Cervantes
|
Posted: Fri May 27, 2005 3:36 pm Post subject: (No subject) |
|
|
1.) It wouldn't be confusing if it were commented.
2.) Comparing the ordinal value of each character to a set of numbers (we can use ranges, because of the way the ASCII chart is set up) saves us from having to do 52 (26 * 2) if statements or a case structure with 52 labels or an index comparing the character to a string with 52 characters.
It's a lot easier!
EDIT: Hey, whaddya know, you're code would work too. I missed the >= and <= operators and thought you'd only checked for a's and z's.
In any case, when comparing two characters, the computer converts them to their ordinal values and compares those integers. So, essentially it's the same thing. |
|
|
|
|
|
MysticVegeta
|
Posted: Fri May 27, 2005 4:24 pm Post subject: (No subject) |
|
|
Cervantes wrote: when comparing two characters, the computer converts them to their ordinal values and compares those integers. So, essentially it's the same thing.
i win? just kidding |
|
|
|
|
|
Cervantes
|
Posted: Fri May 27, 2005 4:43 pm Post subject: (No subject) |
|
|
It's a question of how you like to think about it. Personally, I find the ord method easier to understand. It means you have to open up the ascii chart, but it means you don't have to go the extra step of thinking about how the computer compares characters. Plus, since you've already got the ascii chart open, you can look at the range and everything within it. For things other than the alphabet and numbers, you'd have to open it up regardless. So using the ord method is more coding, but, for me at least, it's easier to understand. |
|
|
|
|
|
|