Sujet : Re: Whaddaya think?
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 16. Jun 2024, 18:14:55
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4n6ie$58k0$1@dont-email.me>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
On 16/06/2024 16:56, David Brown wrote:
On 16/06/2024 17:09, DFS wrote:
On 6/16/2024 12:44 AM, Janis Papanagnou wrote:
On 16.06.2024 06:17, Keith Thompson wrote:
>
For the original problem, where the input consists of digits and
whitespace, you could read a character at a time and accumulate the
value of each number. (You probably want to handle leading signs as
well, which isn't difficult.)
>
Yes. Been there, done that. Sometimes it's good enough to go back
to the roots if higher-level functions are imperfect or quirky.
>
That is admittedly reinventing the
wheel, but the existing wheels aren't entirely round. You still
have to dynamically allocate the array of ints, assuming you need
to store all of them rather than processing each value as it's read.
>
A subclass of tasks can certainly process data on the fly but for
the general solution there should be a convenient way to handle it.
>
I still prefer higher-level languages that take the burden from me.
>
nums = []
with open('data.txt','r') as f:
for nbr in f.read().split():
nums.append(int(nbr))
print(*sorted(nums))
>
nums = sorted(map(int, open('data.txt', 'r').read().split()))
OK, a bit of a challenge for my scripting language. I managed this first:
nums := sort(mapv(toval, splitstring(readstrfile("data.txt"))))
It needed a change to 'splitstring' to allow a default separator consisting of white space of any length. And a one-line helper function 'toval' since the usual candidates, special built-ins, were not valid for 'mapv'.
It also works like this:
nums := readstrfile("data.txt") -> splitstring -> mapv(toval) -> sort
But only by chance since the 'piped' argument is the last one of multi-parameter functions, rather than the first.