This channel is intended for people just starting with the Raku Programming Language (raku.org). Logs are available at irclogs.raku.org/raku-beginner/live.html Set by lizmat on 8 June 2022. |
|||
07:04
sjn left
|
|||
rcmlz | How do I find the maximum of consecutive integers with the same sign in a list? E.g. (1,4,-3,-6,5,6) shall be converted to (4,-3,6). Can I use one of the build in list-features to get a one-liner or is the „AI generated way“ like here the only way? www.perplexity.ai/search/How-do-I-...JpNRow?s=c | 07:09 | |
07:10
sjn joined
|
|||
I was thinking of .categorize or .classify but can not get my head around how to „split“ on changing sign. | 07:30 | ||
(1,2,3,-1,-2,-3,3,2,1) => (3,-1,3) | 07:32 | ||
(1,2,3,-1,-2,-3,3,2,1) => ((1,2,3), (-1,-2,-3), (3,2,1)) => (3,-1,3) is what I have in mind | 07:34 | ||
librasteve | rcmlz: are you an AI? | 10:13 | |
rcmlz | Not yet ;-) | 10:14 | |
librasteve | the example you link to looks like AI code to me | 10:15 | |
rcmlz | But I am preparing lectures for pupils that shall solve programming task using ChatGPT & Co. | ||
The idea is that the pupils "translate" to task into something that they can feed into an AI to get some code that solves the task. I work with Raku, however, I noticed that once you have a working solution in one language, translating into another one (e.g. Rust, Python, ...) works surprisingly well | 10:16 | ||
librasteve | like most of the AI code I get ... the example is wrong ... it says Output: (4 -3 6) but the actual output is (1 4) | 10:17 | |
rcmlz | Yes, that is the same issue as when you ask an AI to write an essay about something - you gotta check the details. | 10:18 | |
librasteve | and a reasonably good coder can do this in a line or two | ||
rcmlz | But you have a good starting point. | ||
librasteve | not 23 lines | ||
rcmlz | Thats why I was asking - I tried using ChatGPT and perplexity.ai to convert a working version into this "one or two lines", but they failed. | 10:19 | |
I e.g. asked e.g. how to split a list of integers on sign change in Raku? Example: (1,2,3,-1,-2,-3,3,2,1) is converted to ((1,2,3), (-1,-2,-3), (3,2,1)) and got sub split_on_sign_change(@list) { my %classified = @list.classify(*.sign); my @result = %classified.values; return @result; } my @input = (1, 2, 3, -1, -2, -3, 3, 2, 1); say split_on_sign_change(@input); # Output: ((1 2 3) (-1 -2 -3) | 10:21 | ||
(3 2 1)) which looks got but is wrong. | |||
However, if a student would come up with such a solution in an exam - would you give 0 points? | 10:22 | ||
librasteve | I would check if the code works according to the tests | 10:23 | |
rcmlz | Same here! | ||
I am using RakuChatbook from @antononcube and intend to grade "the process" from the task (things you find on codeforces.com) to a working solution in a given language that fullfills (all) testcases. So the Jupyter notebook akts like a log. | 10:33 | ||
PS: I can not. | 10:35 | ||
librasteve | yeah - bit trickier than I thought at first ... whirr | 10:37 | |
rcmlz | In case you like to see the related task: codeforces.com/problemset/problem/1343/C | 10:41 | |
So the students "achievement" would be to summarize this lengthy mathematical description to "how to split a list of integers on sign change in Raku? Example: (1,2,3,-1,-2,-3,3,2,1) is converted to ((1,2,3), (-1,-2,-3), (3,2,1))" | 10:42 | ||
lakmatiol | This specific operation is really painful in raku, python has itertools.groupby for it, but raku has nothing similar. Ran into it before when trying to do RLE | 10:55 | |
librasteve | gist.github.com/librasteve/5467a52...4ac9e666b1 | 11:22 | |
^^ ouch that hurt ... and my solution is (errr) 23 lines | 11:23 | ||
I welcome our AI overlords | |||
_____________________________._. | Using a preview feature I got this use v6.e.PREVIEW; @input = (-1, 2, 3, -1, -2, -3, 3, 2, 1); say @input.snip( (|( * >= 0, * < 0 ) xx Inf).skip(@input[0] < 0) )>>.max; | 11:29 | |
snip takes a list of matchers and splits the original list till they're exhausted | 11:31 | ||
librasteve | oooo | 11:32 | |
Nahita | another way (much less elegant than snip-based solution) >>> my @a = (1, 4, -3, -6, 5, 6) [1 4 -3 -6 5 6] >>> my @groups = @a>>.sign.rotor(2 => -1).flat.map(* - * != 0).Array.prepend(0).produce(&[+]) [0 0 1 1 2 2] >>> @a.classify({ @groups[$++] }){^@groups.tail.succ}>>.max (4 -3 6) - get the signs of each element, then take the difference (of signs) - check if the consecutive difference in signs are 0 or | 11:36 | |
not - take the cumulative sum of whether-me-and-neighbor-are-same; this will in effect form the consecutive groups - prepending 0 to account for the [0]th element as well, as the difference disregards it - classifiy the original using these group indicators - classify gives a Hash which is inherently unordered, so we take care to extract the groups in order via a range 0 .. max-group-no - then take the max of | |||
each group and report another way (third party) >>> my @a = (1, 4, -3, -6, 5, 6) [1 4 -3 -6 5 6] >>> use Iter::Able <group-conseq> >>> @a ==> group-conseq(:as(&sign)) ==> map(*.value.max) (4 -3 6) | |||
rcmlz | Oh, cool! snip and Iter::Able are very elegant - so @librasteve was right with this „good programmer can do it“ hypothese :-) Thank you all. | 11:40 | |
librasteve | since we are comparing vs. Python IterTools, I think that use Iter::Able is very frair | ||
I can't find this Iter::Able on raku.land and zef install Iter::Able fails too | 11:44 | ||
~ > zef install Iter::Able ===> Searching for: Iter::Able No candidates found matching identity: Iter::Able | 11:45 | ||
nahita: can you tell me how to install this, please? | 11:48 | ||
Nahita | uh, it's not a published module sorry, only lives on github yet | 11:51 | |
librasteve | oh - that explains it! | ||
thanks | |||
this is a really cool module and I look forward to its release on fez or similar | 11:55 | ||
Nahita | thanks! there are close to 30 functions, I thought close to 50 would be better to release it, I have some list of functions to implement, I write it time to time | 12:00 | |
lakmatiol | Itertools are standard library | 12:05 | |
Snip seems nice though | |||
librasteve | forgive me - when I see the line from itertools import groupby in the python header, that makes it look like a module you have to import rather than in the standard library - my bad | 12:28 | |
lakmatiol | some modules are in the standard library, some modules are in pypi, you gotta just memorise which is which | 12:54 | |
IG it is interesting that raku doesn't have any standard modules, every module is 3rd party. | |||
librasteve | yes - i don't know the history, but my guess is that Python started very lean and mean and that the "standard libraries" have been moved into the core distribution over time ... I think the idea of raku was to start with a much bigger set of build in functions / operators from day one ... but some areas like this have some gaps ... I would vote for a groupby-consec in the raku core ... but also happy to | 13:00 | |
live with the Iter::Able since it seems to be a bit of an edge case | |||
lakmatiol | nah, python started with a whole lot of standard library modules, just separated in some units, similar to how just about every language does it (C std* headers, C++ <array> <vector> ..., Java import java.nio.Path, haskell, prolog (though some systems will autoimport things), ) etc | 13:03 | |
librasteve | til | ||
antononcube | @rcmlz Using classify does adhere to the problem formulation: > How to split a list of integers on sign change in Raku? > Example: (1,2,3,-1,-2,-3,3,2,1) is converted to ((1,2,3), (-1,-2,-3), (3,2,1)). because it says the list to be split, but not that the (implied) order of elements to be preserved. | 14:03 | |
And the example, is "not enough" to imply that "element order condition." | 14:04 | ||
As for LLMs -- more precisely OpenAI's ChatGPT -- if functions (or tools) are used one can formulate verification tests for the solutions. Meaning, ChatGPT can come up with multiple Raku programs and give as a result the one that passes the tests. | 14:06 | ||
This is not currently implemented in "WWW::OpenAI". It is in my TODO list, but that tools feature does not always work in the projects I am doing, so it is of relatively low priority. | 14:07 | ||
Of course, we can have a chat with the LLM and get the solution we want at some point. (Maybe.) | 14:08 | ||
rcmlz | Yes, that would be an interesting option: define (with help of LLM) a sufficient test set, specifying edge cases precisely and ask for a function that passes the full set of tests - skipping this intermediate conversation steps about incorrect syntax and not correct algorithm. The equivalent in social since would be to ask LLM for an „essay on topic X with no errors and only valid literature references“. | 19:11 | |
Students dream !!! | |||
Correct - my fault. Thats the reason the task description is usually 100 instead of 2 lines of mathematical wording. | 19:15 |