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

J

jeffbernstein

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
 
M

mensanator

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.2999999999999998

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

Greg Ewing

"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!)
 
T

Terry Reedy

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
 
D

Dan Sommers

On 25 May 2005 17:23:45 -0700,
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
 
G

GMane Python

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)
 
P

Paul Rubin

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".
 
S

Steven Bethard

GMane said:
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
 

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

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top