odd question

K

Karl Pech

Hi,

I'm currently working on the following exercise:
---
You have given the following function:
def f2(i, j, k):
return ((i | j) & k) | (i & j)

Find a useful utilization for this function.
---

Actually I couldn't figure out so far, what exactly is
a "useful utilization". Can anybody of you help me?

Thanks!

Regards
Karl

[P.S. I have even written a small program, which should
show me, what this formula does with numbers, but I couldn't
find anything "interesting" or regular in the output-file.
This is the source of the program:
---
import string

def testit(i, j, k):
return ((i | j) & k) | (i & j)

q = []
results = [[], [], [], [], [], [], [], [], [], []]

fout = open("out.txt", "w")

for x in range(10):
for y in range(10):
for z in range(10):
a = [x, y, z]
a.sort()
if a in q:
continue
else:
results[testit(x, y, z)].append([x, y, z])
q.append(a)


for x in range(len(results)):
for y in range(len(results[x])):
fout.write(string.strip(str(results[x][y]), "[]")+" : "+str(x)+'\n')
 
W

wes weston

Karl said:
Hi,

I'm currently working on the following exercise:
---
You have given the following function:
def f2(i, j, k):
return ((i | j) & k) | (i & j)

Find a useful utilization for this function.
---

Actually I couldn't figure out so far, what exactly is
a "useful utilization". Can anybody of you help me?

Thanks!

Regards
Karl

[P.S. I have even written a small program, which should
show me, what this formula does with numbers, but I couldn't
find anything "interesting" or regular in the output-file.
This is the source of the program:
---
import string

def testit(i, j, k):
return ((i | j) & k) | (i & j)

q = []
results = [[], [], [], [], [], [], [], [], [], []]

fout = open("out.txt", "w")

for x in range(10):
for y in range(10):
for z in range(10):
a = [x, y, z]
a.sort()
if a in q:
continue
else:
results[testit(x, y, z)].append([x, y, z])
q.append(a)


for x in range(len(results)):
for y in range(len(results[x])):
fout.write(string.strip(str(results[x][y]), "[]")+" : "+str(x)+'\n')

((i | j) & k) | (i & j)

i j k f
- - - -
0 0 0
0 0 1
0 1 0
0 1 1 1 (i | j) & k
1 0 0
1 0 1 1
1 1 0 1 (i & j)
1 1 1 1 (i & j)

Karl,
Hum; could it be true where 2 or more of i,j,k are
true?
wes
 
L

Larry Bates

Sure wish this group was around to help me
with my homework when I was taking CS 101
back 30 years ago <grin>.

Larry

wes weston said:
Karl said:
Hi,

I'm currently working on the following exercise:
---
You have given the following function:
def f2(i, j, k):
return ((i | j) & k) | (i & j)

Find a useful utilization for this function.
---

Actually I couldn't figure out so far, what exactly is
a "useful utilization". Can anybody of you help me?

Thanks!

Regards
Karl

[P.S. I have even written a small program, which should
show me, what this formula does with numbers, but I couldn't
find anything "interesting" or regular in the output-file.
This is the source of the program:
---
import string

def testit(i, j, k):
return ((i | j) & k) | (i & j)

q = []
results = [[], [], [], [], [], [], [], [], [], []]

fout = open("out.txt", "w")

for x in range(10):
for y in range(10):
for z in range(10):
a = [x, y, z]
a.sort()
if a in q:
continue
else:
results[testit(x, y, z)].append([x, y, z])
q.append(a)


for x in range(len(results)):
for y in range(len(results[x])):
fout.write(string.strip(str(results[x][y]), "[]")+" : "+str(x)+'\n')

((i | j) & k) | (i & j)

i j k f
- - - -
0 0 0
0 0 1
0 1 0
0 1 1 1 (i | j) & k
1 0 0
1 0 1 1
1 1 0 1 (i & j)
1 1 1 1 (i & j)

Karl,
Hum; could it be true where 2 or more of i,j,k are
true?
wes
 
J

Jiri Barton

Hello Karl,

I don't know the answer, but you can rewrite the expresion like this

return (i & k) | (j & k) | (i & j)

with the same meaning: all the combinations.

Supposing the variables are just plain booleans, f2 returns true if any two
of the three variables are true (or all three). It returns false if two
variables are false, or all the three are false.

My next suggestion is, maybe they are not integers. They might as well be
booleans.

jbar
 
H

Heike C. Zimmerer

Karl Pech said:
I'm currently working on the following exercise:
---
You have given the following function:
def f2(i, j, k):
return ((i | j) & k) | (i & j)

Find a useful utilization for this function.
---

Depending on k, you either get i & j or i | j. It is left as an
exercise to find out why. Don't know if it's really useful, however.


Greetings from your friendly Usenet homework department,

Heike
 
J

Jiri Barton

Hey again,

this f2 might be useful when doing the binary addition:

def addthem(m, n):
"""m + n"""

transition = 0
result = []
while m | n | transition:
transition = f2(m & 1, n & 1, transition)
result.insert(0, (m ^ n ^ transition) & 1)
m >>= 1
n >>= 1
return result

However, I don't know how to create an integer, because I need to insert the
current bit at the beginning of the result integer that is being created -
I don't know how to do it with just shifting operations, so I used the
list:)

jbar
 
J

Jiri Barton

Just before I go to bed...

How real the utilization should be?

i - True, if your girlfriend #1 is at the door
j - True, if your girlfriend #2 is at the door
k - True, if your girlfriend #3 is at the door

if f2(i, j, k):
#real bummer!!


jbar
 
G

Grant Edwards

I'm currently working on the following exercise:
---
You have given the following function:
def f2(i, j, k):
return ((i | j) & k) | (i & j)

Find a useful utilization for this function.

I'll help by reminding you that posting homework questions to
Usenet will likely get as many red herring responses as serious
ones. :) c.l.p tends to be a bit kinder towards that sort of
thing, but in other groups I've seen some brilliant "joke"
answers for homework questions.
 
D

Dan Bishop

Heike C. Zimmerer said:
Depending on k, you either get i & j or i | j. It is left as an
exercise to find out why. Don't know if it's really useful, however.

It's useful in digital logic: It represents the carry-out from a full
adder. Of course, Python already has a perfectly good + operator.
 
K

Karl Pech

Hello Grant (and everybody else), ;)
I'll help by reminding you that posting homework questions to
Usenet will likely get as many red herring responses as serious
ones. :) c.l.p tends to be a bit kinder towards that sort of
thing, but in other groups I've seen some brilliant "joke"
answers for homework questions.

Hehe, I know this, but I just hadn't any ideas so I posted the question
here.
I hoped that since I have wrote a small program for this task, everybody
would see that I actually worked on the task on my own but didn't get
anything
useful. That's still better than posting questions like: "how much is 2+2? I
don't
know the answer. :(", @)))
without trying to solve them on his own.

Anyway, thanks everybody for your help!


Regards
Karl
 
H

Harald Massa

---
You have given the following function:
def f2(i, j, k):
return ((i | j) & k) | (i & j)

Find a useful utilization for this function.

That's easy.

This function is a possible proof that it is possible to write pearllike
(=writeonly) code in Python too.
 
P

Peter Hansen

Harald said:
That's easy.

This function is a possible proof that it is possible to write pearllike
(=writeonly) code in Python too.

Or any language that supports simple math and boolean operations?

There's nothing Perl-like about that, any more than a statement
to print output to the console could be considered 'BASIC-like'.

-Peter
 
B

Bruno Desthuilliers

Harald said:
That's easy.

This function is a possible proof that it is possible to write pearllike
(=writeonly) code in Python too.
I thought anyone using a programming language had at least some notions
of boolean algrebra...
 
H

Heike C. Zimmerer

Bruno Desthuilliers said:
I thought anyone using a programming language had at least some
notions of boolean algrebra...

I once had someone complaining that I've used an exclusive or in my
code without any comment explaining what that funny thing was for ...
He really was upset.

Heike
 
H

Harald Massa

Bruno,
I thought anyone using a programming language had at least some
notions of boolean algrebra...

putting down the truth-table is of course an exercise.
But for any means: to factor out theses boolean operators into a function
named "f2" AND "forgetting the doc string" will not help in a "more than
toy" programm.

Yes, I could take this code and "run it on paper"
Yes, I could cut and paste it into the interactive interpreter

But: it is still write only. What does the code "expect"? Most people
here expected i,j,k to be boolean. But "int" is perfectly valid .... with
automatic longs like in python 2.3 and up, there number is only limited
by memory size.

what outcome do you expect from
f2(8097123123123,123123123123,98723423123)

Does "some notions of boolean algebra" give any help now?

Yours,

Harald
 
H

Heike C. Zimmerer

Harald Massa said:
Bruno,


putting down the truth-table is of course an exercise.
But for any means: to factor out theses boolean operators into a function
named "f2" AND "forgetting the doc string" will not help in a "more than
toy" programm.

Yes, I could take this code and "run it on paper"
Yes, I could cut and paste it into the interactive interpreter

But: it is still write only. What does the code "expect"? Most people
here expected i,j,k to be boolean.

I surely didn't and I think most people didn't either since the
bitwise operators were used instead of the boolean ones. So I'd
expect at least some of the operands to possibly be ordinals.

The most readable way of expressing ((i | j) & k) | (i & j) ist to
write it down exactly like this. To a programmer, it shouldn't make
much difference if he'd used "+" and "*" instead of "|" and "&".

Greetings,

Heike
 
K

Konstantin Veretennicov

Bruno Desthuilliers said:
I thought anyone using a programming language had at least some notions
of boolean algrebra...

Actually my first guess was set operations :)
Set(['e', 't', 'o'])

f2 returns set of items that are common for at least two sets (i, j, k).
If you draw it on paper you'll get a nice shamrock leaf :)
You can also apply it to integers, which are sets of bits.

- kv
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
salsusa

Latest Threads

Top