Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Nothing so needs reforming as other people's habits. -- Mark Twain


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

SubjectAuthor
* Re: Flubbed it in the second interation through the string: range error... HOW?MRAB
`- Re: Flubbed it in the second interation through the string: range error... HOW?Stefan Ram

1
Subject: Re: Flubbed it in the second interation through the string: range error... HOW?
From: MRAB
Newsgroups: comp.lang.python
Date: Wed, 29 May 2024 14:59 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: python@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Flubbed it in the second interation through the string: range
error... HOW?
Date: Wed, 29 May 2024 15:59:50 +0100
Lines: 62
Message-ID: <mailman.66.1716994794.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>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 4KVVDLWrF0WrerrIFIS53AL+f1r4oV8/+VulKcAbeaww==
Cancel-Lock: sha1:YWpEWH6dtWgvyOgfCln3E+rDcFo= sha256:pelcQdbz9Hk4AR/HH6v1uWC14mhiUQZBR15lhWjLEOY=
Return-Path: <python@mrabarnett.plus.com>
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=plus.com header.i=@plus.com header.b=Zb4WARBj;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.025
X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'def': 0.04; 'space,':
0.07; '"""': 0.09; 'elif': 0.09; 'from:addr:python': 0.09;
'received:192.168.1.64': 0.09; 'that.': 0.15; '[snip]': 0.16;
'conversion': 0.16; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16;
'odd': 0.16; 'ones.': 0.16; 'readability': 0.16;
'received:plus.net': 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;
'way.': 0.22; 'code': 0.23; '(and': 0.25; 'else': 0.27; 'header
:User-Agent:1': 0.30; 'am,': 0.31; 'attach': 0.32; 'good.': 0.32;
'python-list': 0.32; 'specified': 0.32; 'received:192.168.1':
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; 'received:192.168': 0.37; 'could': 0.38; 'thanks':
0.38; 'enough': 0.39; 'use': 0.39; 'true.': 0.40; 'want': 0.40;
'should': 0.40; 'included': 0.61; 'detail': 0.61; 'format': 0.62;
'received:212': 0.62; 'email': 0.63; 'your': 0.64; 'among': 0.65;
'touch': 0.65; 'subject:through': 0.69; 'little': 0.73; '(you':
0.76; "'this": 0.84; 'characters': 0.84; 'converted': 0.84;
'good,': 0.84; 'indexes,': 0.84; 'subject: \n ': 0.84; 'retain':
0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1716994791; bh=MBwbXoQ6fEc0WQU30cw9xEwALq3pBF+LimhbaXMhM6w=;
h=Date:Subject:To:References:From:In-Reply-To;
b=Zb4WARBjlk87ihfw7CPn7ENkovBYI2CnyJv1tBPyi2iDTf2b/1olfVv9dIvDAWgr9
/Urhfda9/OLCxLDLnuMbKLD7+3txM/FME0KEVcXuuJBBJUP6oftZINDmpdbDKfdW7w
9BzTfQxIof1gOxLwd/DKd3t/VLkLERMVr2Lz1137Prf1oN0D2LrzCWVMJjYxcWceId
f/2lTNMkVwlsh0ZqGlB3QTR7yDeo0yrwSxywc2rAj20GX+Rwr9N/6aPIOa1d08+aJW
EPOkDb1Y0Gn6LGzoXKyJJsQNf5lCrZAArrlpc0M4D5+gZ2kGkr9of8dFbIycdscuy1
taz0nXGwi5kTA==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=NPx2+F6g c=1 sm=1 tr=0 ts=665742e7
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=lLJAOk_WU69A3Ky1:21 a=IkcTkHD0fZMA:10 a=IthHKYKO5fTywuQcvI0A:9
a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <407cdc1c-f579-452f-afca-5af50f14d2cb@tompassin.net>
X-CMAE-Envelope: MS4xfGfnbFkuw51nnGhQ9GubL5eooN0VsvXZ3tO1gn9A9NaGFJNSFj0Gc43YBeuqXLsJOph1Igx586kV9SppQbNVGdzpF8fU+tiMv74i7aIpssjcZBDPP/TV
ddNFNel+y736QVmTUaz0KS0vjnHMb1rKsoG29AHne2uI/DBYmtqi1D9Rfzwfd+/pmhUUB+hnODF48joXODC3qPLuyob7/aa33JU=
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: <4dddd92a-e0ce-44b7-b376-2de9ee6026a3@mrabarnett.plus.com>
X-Mailman-Original-References: <1064052561.5558929.1716987352732.ref@mail.yahoo.com>
<1064052561.5558929.1716987352732@mail.yahoo.com>
<407cdc1c-f579-452f-afca-5af50f14d2cb@tompassin.net>
View all headers

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.

> 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:
>
> 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]

Subject: Re: Flubbed it in the second interation through the string: range error... HOW?
From: Stefan Ram
Newsgroups: comp.lang.python
Organization: Stefan Ram
Date: Wed, 29 May 2024 18:09 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: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Flubbed it in the second interation through the string: range error... HOW?
Date: 29 May 2024 18:09:07 GMT
Organization: Stefan Ram
Lines: 40
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <uppercase-20240529190808@ram.dialup.fu-berlin.de>
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> <mailman.66.1716994794.2909.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de P1dU5sQJzG2H62QFtqCfMAAvhX3OA7evd4GgAPlOMLZz6c
Cancel-Lock: sha1:SbanTd03pJkfBwzzQ+VGMQuSdkY= sha256:paeQo6/RsJ1NWdgVpDwlW+FUJrzIftuIWro3tSDnfhU=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
View all headers

MRAB <python@mrabarnett.plus.com> wrote or quoted:
>Small mistake there. The original code converted to uppercase on even
>indexes, whereas your code does it on odd ones.

Here follows my humble effort.

import doctest

def alternate_uppercase( s ):
"""
Alternates the case of alphabetic characters in a given string.

Args:
s (str): The input string.

Returns:
str: The string with alternating uppercase and lowercase letters.

Examples:
>>> alternate_uppercase( 'Python is awesome!' )
'PyThOn Is AwEsOmE!'

>>> alternate_uppercase( 'ab,c,,d,,,e,,,,f' )
'Ab,C,,d,,,E,,,,f'

>>> alternate_uppercase( '' )
''
"""
result =[ None ]* len( s )
letter_count = 0
for char_pos, char in enumerate( s ):
if char.isalpha():
result[ char_pos ]=\
( char.lower if letter_count%2 else char.upper )()
letter_count += 1
else:
result[ char_pos ]= char
return ''.join( result )

doctest.testmod()

1

rocksolid light 0.9.8
clearnet tor