programming logical functions: myand, myor and mynot
Paul Sidorsky
paulsid at home.com
Wed Nov 14 22:33:38 EST 2001
Gernot wrote:
> I want to implement the functions myand, myor and mynot. They should have
> the same effect as the known boolean operators and, or and not.
>
> The trick is that I am not allowed to use the boolean operators and, or and
> not.
> This is what I tried:
[snip]
> I think, the idea is not that wrong, but anyway, it doesn´t work! It has
> something to do with "bool (x)", I think. Maybe this is something like a
> module which i have to "activate" first??
Assuming this is for a CS problem, I don't know what the instructor has in
mind, of course, but I expect he/she would consider the above approach (if it
had worked) as missing the point. It's more likely the instructor wants (or
would be more impressed by) a generic solution that would work in any language,
especially if this is for a "fundamentals of programming" course.
The way I would approach this problem (as I understand it) is to convert each
ASCII character of the strings to a number using ord() and then use
mathematical and relational operators (+, -, <, >, ==, etc.), assuming they're
allowed. (I would think they would have to be.) The conversion is easy with
list comprehensions (Python 2.0 and up):
xlist = [ord(i) for i in x]
ylist = [ord(i) for i in y]
This will give you two lists of numbers, the elements of which you can then use
the other operators on to mimic the logical operators. Exactly how to do that
I won't say because I expect that's what the instructor really wants you to
figure out.
I hope that's a push in the right direction. But of course I don't know
exactly what the goal and specific restrictions are here so I may be way off
base.
--
======================================================================
Paul Sidorsky Calgary, Canada
paulsid at home.com http://members.home.net/paulsid/
More information about the Python-list
mailing list