<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Andrew Shitov</title>
    <description>The latest articles on DEV Community by Andrew Shitov (@andrewshitov).</description>
    <link>https://dev.to/andrewshitov</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1156302%2Fcbc65b3f-7688-49e1-864b-8e3ed1748660.jpeg</url>
      <title>DEV Community: Andrew Shitov</title>
      <link>https://dev.to/andrewshitov</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andrewshitov"/>
    <language>en</language>
    <item>
      <title>Sorting numbers in Raku with the help of ChatGPT</title>
      <dc:creator>Andrew Shitov</dc:creator>
      <pubDate>Fri, 08 Sep 2023 07:57:21 +0000</pubDate>
      <link>https://dev.to/andrewshitov/sorting-numbers-in-raku-with-the-help-of-chatgpt-4279</link>
      <guid>https://dev.to/andrewshitov/sorting-numbers-in-raku-with-the-help-of-chatgpt-4279</guid>
      <description>&lt;p&gt;In this post I will show you the solution to the &lt;a href="https://theweeklychallenge.org/blog/perl-weekly-challenge-233/#TASK2"&gt;Task 2 of this week’s Weekly Challenge 233&lt;/a&gt;. This time I was using the help of ChatGPT, which offered a very nice solution.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You are given an array of integers.&lt;br&gt;
Write a script to sort the given array in increasing order based on the frequency of the values. If multiple values have the same frequency then sort them in decreasing order.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I asked ChatGPT the following: ‘Create a program. Here is the description of the task.’ To make sure the result is in the Raku programming language, I also added the following instruction in my local settings so that it knows which programming language I prefer by default.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WpQcrjqz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwmso4rxp5iti24b26gg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WpQcrjqz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwmso4rxp5iti24b26gg.png" alt="Image description" width="800" height="506"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The solution generated by ChatGPT was quite good with a small mistake.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ChatGPT: Sure, here’s a Raku program that implements the described sorting algorithm for the given task:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sub frequency-sort(@arr) {
    my %freq;

    %freq{$_}++ for @arr;
    my @sorted = @arr.sort: {
        %freq{$^b} &amp;lt;=&amp;gt; %freq{$^a} ||
        $^b &amp;lt;=&amp;gt; $^a
    };

    return @sorted;
}

my @ints = (1, 1, 2, 2, 2, 3);
my @sorted = frequency-sort(@ints);

say "Input:  @ints";
say "Output: @sorted";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, Raku would not interpolate arrays, so &lt;code&gt;say "Input: @ints"&lt;/code&gt; prints the string literary: &lt;code&gt;Input: @ints&lt;/code&gt;. Second, in the sorting rule, sorting by frequency must be ascending rather than descending, so we need to switch the placeholder variables &lt;code&gt;$^b&lt;/code&gt; and &lt;code&gt;$^a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In the rest, ChatGPT did a good work. It computed the frequency using a loop, and I especially liked how it addressed the requirement to sort by two dimensions.&lt;/p&gt;

&lt;p&gt;Here is the full program with the above-mentioned corrections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sub frequency-sort(@arr) {
    my %freq;
    %freq{$_}++ for @arr;

    my @sorted = @arr.sort: {
        %freq{$^a} &amp;lt;=&amp;gt; %freq{$^b} ||
        $^b &amp;lt;=&amp;gt; $^a
    };

    return @sorted;
}

my @tests =
    (1, 1, 2, 2, 2, 3),
    (2, 3, 1, 3, 2),
    (-1, 1, -6, 4, 5, -6, 1, 4, 1);

say frequency-sort($_) for @tests;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This program prints the following output for the given test cases:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ raku ch-2.raku
[3 1 1 2 2 2]
[1 3 3 2 2]
[5 -1 4 4 -6 -6 1 1 1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>raku</category>
      <category>algorithms</category>
      <category>challenge</category>
      <category>rakulang</category>
    </item>
  </channel>
</rss>
