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 7 7704 je***********@y ahoo.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.2999999999999 998
3.3*'1'
Traceback (most recent call last):
File "<pyshell#1 1>", line 1, in -toplevel-
3.3*'1'
TypeError: can't multiply sequence to non-int Thanks,
Jeff je***********@y ahoo.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 "concatenat ive 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
<je***********@ yahoo.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
On 25 May 2005 17:23:45 -0700, je***********@y ahoo.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.tombstoneze ro.net/dan/>
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.goo glegroups.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 je***********@y ahoo.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".
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: j_mckitrick |
last post by:
I used to like Objective-C and Gnustep, but I gave up for lack of
support and recognition of the language/platform under Unix. But it
did offer NSNotifications, a basic message class that could be posted
and received by all subscribers. Other than global variables used to
share data across threads, does Python have anything similar?
jonathon
|
by: Tim Henderson |
last post by:
Hi
The question why are there no sorted dictionaries in python, seems to
pop up with unseeming regularity. That question in itself in
nonsensical sense dictionaries are hash-maps, however should python
have a sorted map type object is a good question.
clearly many people like have a sorted map, and sorting the keys every
time seems rather wasteful, as does keeping a separate sorted list of
the keys.
|
by: AbrahamLincolnIllinois |
last post by:
Hello all.
On page 479, the 2nd edition of the "Learning Python" book, this code
appears
class Derived(Base):
def __init__(self, arg, *args, **kw):
self.__init__(self, *args, **kw)
Surely self.__init__ should be
|
by: Mike Meng |
last post by:
Hi all,
I just finished reading Learning Python 3rd ed, and am doing my
first Python application, which retrieves and process text and XML
documents from Web. Python helped me to write the application in a few
hours, I'm very happy with its productivity. But the performance is not
satisfactory. I decide to optimized it in Python before trying C/C++
extensions. But I don't know Python much and have no clu to tune my
program. Also, I...
|
by: AlbaClause |
last post by:
I'm just learning Python, and I have a question about os.path.join(dirpath,
name) and its use. Simply put, I haven't figured out how to use it.
I was looking through the Python reference material in the wee hours of the
morning and checking out some of the modules. I was keenly interested in
the os module, as it is necessary for me to learn this stuff in order to
begin my first real Python project.
I was looking at os.walk(top) when...
| |
by: Paddy3118 |
last post by:
This month there was/is a 1000+ long thread called:
"merits of Lisp vs Python"
In comp.lang.lisp.
If you followed even parts of the thread, AND previously
used only one of the languages AND (and this is the
crucial bit), were persuaded to have a more positive view
of the other language; (deep breath, this is a long, as
well as grammatically incorrect sentence), THEN WHY NOT
POST ON WHAT ARGUMENTS PERSUADED YOU.
|
by: ureuffyrtu955 |
last post by:
Python is a good programming language, but "Python" is not a good
name.
First, python also means snake, Monty Python. If we search "python" in
google, emule, many results are not programming resource. If we search
PHP, all results are programming resource.
Second, python also means snake, snake is not a good thing in western
culture. Many people dislike any things relevant to snake. We must
have high regard for the custom.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |