Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: David Brown Newsgroups: comp.lang.c Subject: Re: Baby X is bor nagain Date: Thu, 20 Jun 2024 13:18:40 +0200 Organization: A noiseless patient Spider Lines: 145 Message-ID: References: <20240613002933.000075c5@yahoo.com> <20240613174354.00005498@yahoo.com> <20240617002924.597@kylheku.com> <20240618115650.00006e3f@yahoo.com> <20240618184026.000046e1@yahoo.com> <877celzx14.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 20 Jun 2024 13:18:41 +0200 (CEST) Injection-Info: dont-email.me; posting-host="658fd1c388c737e8ccffa21e0a91fad6"; logging-data="2688387"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PDSC3ZQ2IFuLj+12PkVXcepqdbhH1Ae4=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Cancel-Lock: sha1:i4wACEQKmNL7I1SLSAASKKxn0+k= Content-Language: en-GB In-Reply-To: Bytes: 8424 On 20/06/2024 10:37, Malcolm McLean wrote: > On 20/06/2024 08:55, David Brown wrote: >> On 19/06/2024 22:42, Malcolm McLean wrote: >>> On 19/06/2024 18:49, David Brown wrote: >>>> On 19/06/2024 18:52, Malcolm McLean wrote: >>>> >>>>> Yes, but that's not quite what we want. A typical input would go. >>>> >>>> It's extremely hard to guess what you want (not "we", but "you" - no >>>> one else wants this kind of thing) when you have bizarre >>>> requirements and only give bits of them.  So modifying the Python >>>> code is left as an exercise if you are interested, especially as it >>>> is off-topic. >>>> >>> This was a work project, so "we". I would never set up such a system. >>> But we hasd light-themed and dark-themed icons and they hsd to be >>> arranged just so so that the program would find them and show the >>> right theme. And as you can imagine, it was a nuisance to we >>> programmers to set up the resource scripts so that eveything was right. >>> >>> So why not get Python to do the job? But there wasn't much >>> enthusiasm. So, despite not knowing Python, I decided to have a go, >>> and I got a sorted list of icons quite easily, and it looked >>> promising. But now the special requirement for a little deviation >>> from alphabetical sort. And I couldn't work out how to do that. >>> >>> And it wasn't what I was supposed to be doing or paid to do. We used >>> to have a weekly meeting where we discussed work done. If I said "oh, >>> and I spent an afternoon knocking up this little Python script to >>> help get those resource files together", then that's OK. If I say >>> that was my main focus for the week, no, and if I say I spent >>> substantial time on it and it didn't even work - well that really >>> wouldn't go down well. >>> So I had to abandon it once it became clear that it would take many >>> hours of trawling through docs and online tips to try to work out a >>> way. And no-one has posted a solution here. And, whilst there will be >>> a way, I suspect that it just doesn't use the mainstream langage >>> facilities. I suspect that Python isn't really a programming language >>> - a language designed to make it easy to apply arbitrary transforms >>> to data - it's a scripting language - a language designed to make it >>> easy to call pre-existings code to do the things it is designed to do. >>> >>> But maybe I'm unfair. >>> >> >> It's not so much that you are being unfair, it is that you are arguing >> from a position of almost total ignorance.  It's fine to say you know >> little about Python and can't comment much on it.  It is /not/ fine to >> say (and demonstrate) that you know almost nothing about a language >> and then make wild claims about what it can and cannot do. >> >> This is not a Python group, so I have not bothered writing code for >> your weird requirements.  Suffice it to say that it would not be hard, >> and it would use "mainstream language facilities" (I take that to mean >> the language and its standard libraries, rather than third-party tools). >> >> Yes, Python is a "programming language" by any reasonable definition >> of that term.  (It is /also/ useful as a scripting language - >> languages can be suitable for more than one task.)  Yes, it is >> designed to make it easy to "apply arbitrary transforms to data" - it >> is usually very much easier to do this than in C, at the cost of less >> efficient run-time performance.  And no language has pre-existing code >> or standard functions to handle your highly unusual sorting >> requirements - such things always need their own code. >> >> >> You've made it clear you know nothing about the language.  Fair enough >> - we all know almost nothing about almost all programming languages. >> But trust someone who does. >> > Yes but I just don't. Did you think I was lying? Did you think I was mistaken? > Everyone says "oh yes, that is easy - goto a Python group" and so on. > No-one actually comes up with the code. > > In C, what we do is write a special version of strcmp > > int strcmp_light_and_dark(const char *a, const char *b) > { >     int i; > >     for (i = 0; a[i] && b[i]; i++) >     { >         if (a[i] != b[i]) >         { >            if (a[i] == 'L' && b[i] == 'D' && i > 0 && a[i-1] == '_') >                return -1; >             if (a[i] == 'D' && b[i] == 'L' && i > 0 && a[i-1] = '_') >                return 1; >            break; >         } >     } > >     return a[i] - b[i]; > > } In Python, you write a key function. I'm still not clear on exactly what you are asking for, especially since your function above does not match your earlier description. My obvious thought here is to replace "_D_" with "_Z_", and sort alphabetically : sorted(xs, key = lambda x : x.replace("_D_", "_Z_")) Now, I'm sure you will think of some other way to change your goalposts and come up with a new, inconsistent set of rules. Perhaps it was all too long ago for you to remember. > > So easy to do. And we can't pass that to qsort. We have to write a > little wrapper to convert the pointers for us. > You say you /can't/ pass your function to qsort, and yet you still think it is easier? > But C hs that sort of flexibility as a result of stripping features away > from the language. > C has never stripped features away from any language. It is simply a relatively small language - it's flexible, yes, but you need to write lots of things manually that are built in in higher-level languages. And of course anything like this that you can write in C, you can write in Python, if that's what you want. It will be /slower/ in Python, and is unlikely to be the best way to writing things, but it is entirely possible : def strcmp_light_and_dark(a : str, b : str) -> int n = min(len(a), len(b)) for i in range(n) : if a[i] != b[i] : if (a[i] == 'L' && b[i] == 'D' && i > 0 && a[i-1] == '_') : return -1 if (a[i] == 'D' && b[i] == 'L' && i > 0 && a[i-1] == '_') : return 1; break return a[i] > b[i]; This can't be passed to Python's sort functions either, as they use key functions - but the wrapper is provided by Python as functools.cmp_to_key.