Sujet : Re: Whaddaya think?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 17. Jun 2024, 13:44:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4pb3v$lbo4$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 16/06/2024 19:14, bart wrote:
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'.
That's nice, but irrelevant - the OP can use the Python version if he decides writing the C version is not fun any more, but your language is useless to everyone but you.
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.
A piping syntax is, IMHO, also a nice feature (though again the OP will have no direct use of your language).
Some people might like to do this all with shell pipes:
cat data.txt | xargs -n 1 | sort -n | xargs
That kind of thing can quickly get more awkward as the details change, such as if the data is separated by commas - by the time you have figured out the "awk" or "sed" commands needed, you'd be much faster with Python.