By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

Q: "...Learning with Python" ...a property that addition and multiplication have...

P: n/a
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?

Thanks,

Jeff

Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


je***********@yahoo.com wrote:
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?
3.3*1 3.2999999999999998
3.3*'1'


Traceback (most recent call last):
File "<pyshell#11>", line 1, in -toplevel-
3.3*'1'
TypeError: can't multiply sequence to non-int

Thanks,

Jeff


Jul 19 '05 #2

P: n/a
je***********@yahoo.com wrote:
"Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"


Existence of inverses?

E.g. the additive inverse of 3 is -3, but there are
no "concatenative inverses" of strings.

(BTW, I would consider this more about thinking like a
mathematician than a computer scientist!)

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
Jul 19 '05 #3

P: n/a

<je***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?


Go back to concatenation instead of repetition.

TJR

Jul 19 '05 #4

P: n/a
On 25 May 2005 17:23:45 -0700,
je***********@yahoo.com wrote:
I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?" I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?


Thinking like an old embedded systems programmer, for many types of
numbers (ints and floats, but not longs), multiplication and addition
are constant time, constant space operations. String concatenation and
repetition are not.

As already mentioned, there are no inverses. Similarly, there aren't
even inverse operations (e.g., it makes little sense to divide a string
by a number, let alone a number by a string).

For floating point numbers (and integers that can overflow),
multiplication and addition are not necessarily exact. Unless your
strings get too big for the memory manager, concatenation and
replication are exact.

And now that I reread the question, "a property that addition and
multiplication have" is "the distributive property," but the
distributive property of multiplcation over addition does not translate
to a distributive property of repetition over concatenation:

2 * ( 3 + 4 ) == 14
2 * 3 + 2 * 4 == 14

but

2 * ( "ABC" + "DEFG" ) == "ABCDEFGABCDEFG"
2 * "ABC" + 2 * "DEFG" == "ABCABCDEFGDEFG"

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
Jul 19 '05 #5

P: n/a
The inablility to work with negative values.

Addition can do the following:
5 + (-4) read as 5 plus the value negative four.

Multiplication can do the following:
5 * (-1) read as 5 times the value negative one.

String concatination can not subtract the sub-string 'lo' from 'hello'.
'hello' - 'lo' is invalid.

string repetition can not repeat a value negative times:
'hello' * -3 is invalid.
'hello' * 2.75 is also invalid, in that you can not repeat a fractional
amount.

-Dave
(Python Newbie)
<je***********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Greetings.

I'm reading "How to think like a computer scientist: Learning with
Python" and there's a question regarding string operations. The
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?

Thanks,

Jeff

--
http://mail.python.org/mailman/listinfo/python-list


Jul 19 '05 #6

P: n/a
je***********@yahoo.com writes:
question is, "Can you think of a property that addition and
multiplication have that string concatenation and repetition do not?"

I thought it was the commutative property but "<string>"*3 is
equivalent to 3*"<string>". Any ideas?


Um, string concatenation is not commutative. "a"+"b" is not the same
as "b"+"a".
Jul 19 '05 #7

P: n/a
GMane Python wrote:
string repetition can not repeat a value negative times:
'hello' * -3 is invalid.


Well, it's not invalid:

py> 'a' * -1
''
py> 'a' * -42
''

but you could definitely say that
S * N1 == S * N2
does not imply that
N1 == N2

STeVe
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.