Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

This night methinks is but the daylight sick. -- William Shakespeare, "The Merchant of Venice"


comp / comp.lang.python / Re: Flubbed it in the second interation through the string: range error... HOW?

SubjectAuthor
o Re: Flubbed it in the second interation through the string: range error... HOW?Thomas Passin

1
Subject: Re: Flubbed it in the second interation through the string: range error... HOW?
From: Thomas Passin
Newsgroups: comp.lang.python
Date: Wed, 29 May 2024 15:44 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: list1@tompassin.net (Thomas Passin)
Newsgroups: comp.lang.python
Subject: Re: Flubbed it in the second interation through the string: range
error... HOW?
Date: Wed, 29 May 2024 11:44:07 -0400
Lines: 84
Message-ID: <mailman.68.1716997458.2909.python-list@python.org>
References: <1064052561.5558929.1716987352732.ref@mail.yahoo.com>
<1064052561.5558929.1716987352732@mail.yahoo.com>
<407cdc1c-f579-452f-afca-5af50f14d2cb@tompassin.net>
<4dddd92a-e0ce-44b7-b376-2de9ee6026a3@mrabarnett.plus.com>
<46e34db5-6042-4923-acba-604d5ec648d9@tompassin.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de NRBvLKNI+oNg7ptYPNFi+A8viVnPfMT1tg5be0cwlH1w==
Cancel-Lock: sha1:5wsESyyxTzlxhJi2NeSg1/jj6F4= sha256:+I/bXudY1hwGPcA1u0TnH3CtHnonGmd49I3/5RalA4Q=
Return-Path: <list1@tompassin.net>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=tompassin.net header.i=@tompassin.net header.b=o7QqLPUe;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.040
X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'def': 0.04; 'space,':
0.07; '"""': 0.09; 'anyway,': 0.09; 'elif': 0.09; 'skip:\xc2 20':
0.09; 'something,': 0.09; 'that.': 0.15; '[snip]': 0.16;
'conversion': 0.16; 'iterate': 0.16; 'odd': 0.16; 'ones.': 0.16;
'readability': 0.16; 'received:10.0.0': 0.16; 'received:64.90':
0.16; 'received:64.90.62': 0.16; 'received:64.90.62.162': 0.16;
'received:dreamhost.com': 0.16; 'row,': 0.16; 'subject:second':
0.16; 'subject:string': 0.16; 'wrote:': 0.16; 'problem': 0.16;
'probably': 0.17; 'to:addr:python-list': 0.20; 'all,': 0.20;
'code': 0.23; '(and': 0.25; 'anyone': 0.25; 'space': 0.26; 'else':
0.27; '>>>': 0.28; 'header:User-Agent:1': 0.30; 'am,': 0.31;
'think': 0.32; 'question': 0.32; 'attach': 0.32; 'python-list':
0.32; 'received:10.0': 0.32; 'received:mailchannels.net': 0.32;
'received:relay.mailchannels.net': 0.32; 'specified': 0.32; 'but':
0.32; "i'll": 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34;
'failed': 0.35; 'track': 0.35; 'count': 0.36; "skip:' 10": 0.37;
'could': 0.38; '8bit%:14': 0.38; 'thanks': 0.38; 'two': 0.39;
'enough': 0.39; 'use': 0.39; 'true.': 0.40; 'want': 0.40;
'should': 0.40; 'included': 0.61; 'detail': 0.61; 'skip:\xc2 10':
0.62; 'format': 0.62; 'email': 0.63; 'between': 0.63; 'your':
0.64; 'among': 0.65; 'touch': 0.65; 'header:Received:6': 0.67;
'received:64': 0.67; 'subject:through': 0.69; 'them,': 0.70;
'little': 0.73; '8bit%:100': 0.76; '(you': 0.76; "'this": 0.84;
'catch': 0.84; 'characters': 0.84; 'converted': 0.84; 'good,':
0.84; 'indexes,': 0.84; 'subject: \n ': 0.84;
'\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0': 0.84; 'retain': 0.91;
'wondered': 0.91
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1716997454; a=rsa-sha256;
cv=none;
b=tiUa+TKb7UnNOzkVcECaWee1tAJajo9SO82f9n+D7yjcLhPB2rw3yehZdM4kZ/xNFk4MmW
+PZfRQa6rpdBAMOV5xuqYrOvxZRPxAGbgCY9aHnyWcWo7ftVSTVr3ZH3MSMDHGtQExTFP1
BrjaqzwmmthEbLEKVrQvHfw64vbzT6J3ViApTFi3QMbFwHfkgaLIKz1LPJClq1rqyOec2k
JVW5jiWQVKQ99sydGUUsIP+yd7bacMRJcrXxJoQy+MhtZ7l12FALTCK1FLeZ7gufhcx2FQ
kk9RlpTOP6c3mXlhYsKNAUmBgxb9aWURwigZBDuFG6ivO+cGD6jnhocJDHoDmQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed;
d=mailchannels.net; s=arc-2022; t=1716997454;
h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
to:to:cc:mime-version:mime-version:content-type:content-type:
content-transfer-encoding:content-transfer-encoding:
in-reply-to:in-reply-to:references:references:dkim-signature;
bh=ph+Z66Ro9VSMrJVm1jYGa9AMcR5RMXCuYvBpTfZCe1s=;
b=Nj298oLgN8e6ouq73rB+0mBlsTMYCZicWuY8s9ORN8A4CNB/AlBx5qvVbSeXcMm3Z5xsgA
Evf/jiij1EtoqbB8+XyrRlVGI8bmcXzZ7pNppcRevRkkSdICmvZSb8VkfW33Jo3YFFZlAU
P04S2GfjOXBkwnO6DfjlX9lcOhj1zhuZOSi65Kn8FQBH4L3E2G8jUq/ef98T7rx2Iz0Wew
1ThSkhSMRZxY0h20NYHdXZ4+Sk61g62UzFpQNooDve20iUt2Aom5mGnp6F1fgvCDN8N8l9
Scn6BO4f3F6MKPB0XckWY9iuTxhn6ad8puQIX/iuYdGWhBnHJLjviXzOXDyQNA==
ARC-Authentication-Results: i=1; rspamd-5d55749bb4-q9b84;
auth=pass smtp.auth=dreamhost smtp.mailfrom=list1@tompassin.net
X-Sender-Id: dreamhost|x-authsender|tpassin@tompassin.net
X-MC-Relay: Neutral
X-MailChannels-SenderId: dreamhost|x-authsender|tpassin@tompassin.net
X-MailChannels-Auth-Id: dreamhost
X-Hysterical-Power: 089e1e0b1dabb296_1716997454712_2306109981
X-MC-Loop-Signature: 1716997454712:970713507
X-MC-Ingress-Time: 1716997454712
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tompassin.net;
s=dreamhost; t=1716997449;
bh=ph+Z66Ro9VSMrJVm1jYGa9AMcR5RMXCuYvBpTfZCe1s=;
h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding;
b=o7QqLPUeDWOZ75fDy3ZKF5L8+7Gdz642GELWjBHYvqPTHRGdI2xOPgACGZjRVroso
FeIZFYw4qUE0yAlyYwQ+gJ0jWPlkvN5sICc41NuQIgz6OJMUrFs7o5MOebYAO1dY5i
RvL9p584OG8vNrE+CvrB9Ij8aNPvt7Q9a0udp/wPKPlGrHTtJK+n/ZkYXe6pt/oQ3D
2xKd7BdEnHiyvUtI6c/tDVJSIYgr0/24SyUcUZztQainiUENlZoelCmyGKqRCLEr9S
Db5v7wzeiS2jqLguC09qP3QTo8Ui85dDT1OvtUiiLpN44lxuszFi8CSYmt31ikedts
BIkBlGor/75HQ==
User-Agent: Mozilla Thunderbird
Content-Language: en-US
In-Reply-To: <4dddd92a-e0ce-44b7-b376-2de9ee6026a3@mrabarnett.plus.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <46e34db5-6042-4923-acba-604d5ec648d9@tompassin.net>
X-Mailman-Original-References: <1064052561.5558929.1716987352732.ref@mail.yahoo.com>
<1064052561.5558929.1716987352732@mail.yahoo.com>
<407cdc1c-f579-452f-afca-5af50f14d2cb@tompassin.net>
<4dddd92a-e0ce-44b7-b376-2de9ee6026a3@mrabarnett.plus.com>
View all headers

On 5/29/2024 10:59 AM, MRAB via Python-list wrote:
> On 2024-05-29 15:32, Thomas Passin via Python-list wrote:
>> On 5/29/2024 8:55 AM, Kevin M. Wilson wrote:
>>> Please recall, I said the format for the email failed to retain the
>>> proper indents.
>>> I'll attach a picture of the code!
>>> Purpose; to uppercase every other letter in a string.
>>>
>>> Thanks all, KMW
>>
>> Simpler is good, and readability is good.  For a simple conversion that
>> has a little touch of generality:
>>
>> s1 = 'this is a test'
>> def convert(i, ch):
>>       return ch.upper() if i % 2 else ch
>>
>> result = ''.join([convert(i, ch) for i, ch in enumerate(s1)])
>> print(result)  # tHiS Is a tEsT
>>
> [snip]
> Small mistake there. The original code converted to uppercase on even
> indexes, whereas your code does it on odd ones.

I wondered if anyone would catch that :) Anyway, I don't think the
original question was whether to start with even or odd but ways to
iterate character by character and do something, right?

>> However, this has a weakness: what to do about spaces.  Should they be
>> counted among the characters to be uppercased? or should they not be
>> included in the count of the alternation?  If you want to uppercase
>> every other letter that is not a space, things become a little more
>> complicated.  And then do you want this to apply to all whitespace or
>> only spaces?
>>
>> If you want to skip changing spaces, then you need to track the state of
>> converted characters in some way.  It would probably be easier (and more
>> readable) to use a "for x in t:" construction:

Actually, I did mess up the action for a space. It should be:

def convert(convert_this, ch):
"""Convert character ch if convert_this is True.
Don't convert spaces.
"""
if ch == ' ':
return (convert_this, ch)
if convert_this:
return (False, ch.upper())
return (True, ch)

We should never get two printable uppercased characters in a row, even
if there is a space between them, but my original convert(convert_this,
ch) did.

>> def convert(convert_this, ch):
>>       """Convert character ch if convert_this is True.
>>       Don't convert spaces.
>>       """
>>       if convert_this:
>>           if ch == ' ':
>>               return (convert_this, ch)
>>           elif convert_this:
>>               return (False, ch.upper())
>>       return (True, ch)
>>
>> convert_next = False
>> result = ''
>> for ch in s1:
>>       convert_next, ch = convert(convert_next, ch)
>>       result += ch
>> print(result)  # tHiS Is A TeSt
>>
>> There could be even more complications if you allow non-ascii characters
>> but you were asking about processing character by character so I won't
>> get into that.
>>
>> (You haven't specified the problem in enough detail to answer questions
>> like those).
>>
> [snip]
>
>

1

rocksolid light 0.9.8
clearnet tor