<?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>Forem: Asparagos</title>
    <description>The latest articles on Forem by Asparagos (@asparagos).</description>
    <link>https://forem.com/asparagos</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%2F3268107%2Fef0b2199-10c3-43f1-b964-8945bdaf22ae.png</url>
      <title>Forem: Asparagos</title>
      <link>https://forem.com/asparagos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/asparagos"/>
    <language>en</language>
    <item>
      <title>Go Coding with Asparagos: Fair Play in Watermelon Football</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Wed, 15 Oct 2025 10:01:20 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-fair-play-in-watermelon-football-55io</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-fair-play-in-watermelon-football-55io</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Who becomes the referee in the Great Watermelon Football Final?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; - an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Watermelon Football 🍉&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Welcome to The Great Watermelon Football Final! The watermelons are ready, and the stakes are high. There’s just one task left: we must split the players into two teams.&lt;/p&gt;

&lt;p&gt;The watermelons are standing in a row, each with a rating. We must remove exactly one watermelon (don’t worry, this one becomes the referee). The remaining watermelons are split into two teams by index in the remaining row:&lt;/p&gt;

&lt;p&gt;Team Even: players at even indices&lt;/p&gt;

&lt;p&gt;Team Odd: players at odd indices&lt;/p&gt;

&lt;p&gt;The total rating of both teams must be equal.&lt;/p&gt;

&lt;p&gt;How many choices of the referee (how many indices to remove) make the teams' total ratings equal?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🍈
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;rating []int&lt;/code&gt; - the ratings of the watermelons, &lt;code&gt;len(rating) &amp;gt; 2&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🥭
&lt;/h3&gt;

&lt;p&gt;An integer representing the number of ways to choose one watermelon to remove (the referee) so that the remaining players can be split by index into two teams with equal total rating.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🥝:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;rating = [1, 3, 2, 1, 2, 2]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can remove the 2nd watermelon (rating &lt;code&gt;3&lt;/code&gt;), remaining players are &lt;code&gt;[1, 2, 1, 2, 2]&lt;/code&gt;.&lt;br&gt;
Team Even: &lt;code&gt;1 + 1 + 2 = 4&lt;/code&gt;, Team Odd: &lt;code&gt;2 + 2 = 4&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can remove the 4th watermelon (rating &lt;code&gt;1&lt;/code&gt;), remaining players are &lt;code&gt;[1, 3, 2, 2, 2]&lt;/code&gt;.&lt;br&gt;
Team Even: &lt;code&gt;1 + 2 + 2 = 5&lt;/code&gt;, Team Odd: &lt;code&gt;3 + 2 = 5&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;rating = [5, 7, 3, 8, 1]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We can remove the 4th watermelon (rating &lt;code&gt;8&lt;/code&gt;), remaining players are &lt;code&gt;[5, 7, 3, 1]&lt;/code&gt;.&lt;br&gt;
Team Even: &lt;code&gt;5 + 3 = 8&lt;/code&gt;, Team Odd: &lt;code&gt;7 + 1 = 8&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;rating = [1, 2, 3, 4]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;0&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are no ways to remove one watermelon and obtain equal team totals.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, we calculate the total sums at even and odd indices: &lt;code&gt;evenSum&lt;/code&gt; and &lt;code&gt;oddSum&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We iterate through the watermelons' ratings and consider removing the current one at index &lt;code&gt;ind&lt;/code&gt;. To check whether this removal builds equal teams, we use even/odd prefix sums before &lt;code&gt;ind&lt;/code&gt; and compare the new even/odd sums after the removal (remember: all indices to the right shift left by 1, so their parity changes):&lt;/p&gt;

&lt;p&gt;a. After removing index &lt;code&gt;ind&lt;/code&gt;, all elements to the right that were at even indices become odd. Their total is &lt;code&gt;evenSum - prefixEvenSum&lt;/code&gt;. Adding the odd prefix on the left, we get &lt;code&gt;newOddSum = prefixOddSum + (evenSum - prefixEvenSum)&lt;/code&gt;. If the current index is even, we must subtract its value &lt;code&gt;r&lt;/code&gt; from &lt;code&gt;newOddSum&lt;/code&gt; (because it was counted on the even side before removal). Then update &lt;code&gt;prefixEvenSum += r&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;b. Symmetrically, elements to the right that were at odd indices become even, their sum is &lt;code&gt;oddSum - prefixOddSum&lt;/code&gt;. Adding the even prefix on the left, we get &lt;code&gt;newEvenSum = prefixEvenSum + (oddSum - prefixOddSum)&lt;/code&gt;. If the current index is odd, subtract &lt;code&gt;r&lt;/code&gt; from &lt;code&gt;newEvenSum&lt;/code&gt;, then update &lt;code&gt;prefixOddSum += r&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;c. If &lt;code&gt;newOddSum == newEvenSum&lt;/code&gt;, increment &lt;code&gt;count&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;countWaysToMakeTeams&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;oddSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;evenSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;evenSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;oddSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="n"&gt;prefixOddSum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;prefixEvenSum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;rating&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;newOddSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;prefixOddSum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;evenSum&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;prefixEvenSum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;newEvenSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;prefixEvenSum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;oddSum&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;prefixOddSum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;newOddSum&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
            &lt;span class="n"&gt;prefixEvenSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;newEvenSum&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
            &lt;span class="n"&gt;prefixOddSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;newOddSum&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;newEvenSum&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Lemon Squeezy, Dynamic Easy</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Fri, 10 Oct 2025 07:47:11 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-lemon-squeezy-dynamic-easy-12lc</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-lemon-squeezy-dynamic-easy-12lc</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;When life gives you lemons, make a quiz team!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; - an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Lemon Squeezy 🍋&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Summer is over, lemonade is out of fashion, but the lemons refuse to waste time. They've formed a team called "Lemon Squeezy" and are aiming to win the annual pub quiz.&lt;/p&gt;

&lt;p&gt;Now they must decide who joins the team. Each lemon has two attributes: IQ and age. They want a team with the maximum total IQ. But lemons have fragile citrus egos: no lemon will play in a team with a younger lemon who has a higher IQ.&lt;/p&gt;

&lt;p&gt;Can they quickly figure out who should be in the team? And what will the total IQ of "Lemon Squeezy" be?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🍊
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;iqs []int&lt;/code&gt; - IQ of each lemon.&lt;br&gt;
&lt;code&gt;ages []int&lt;/code&gt; - age of each lemon.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🍐
&lt;/h3&gt;

&lt;p&gt;An integer — the maximum total IQ of a team with no conflicts. A conflict exists if a younger lemon has a higher IQ than the older lemon. Lemons of the same age never conflict, regardless of IQ.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🍈:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;iqs = [1, 2, 3], ages = [10, 15, 20]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;All lemons can be in the same team.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;iqs = [4, 9, 20], ages = [20, 10, 15]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;29&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The team consists of the second and third lemons. The first lemon can’t be included because its &lt;code&gt;IQ = 4&lt;/code&gt; is lower than the IQ of the second lemon, who is younger.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;iqs = [17, 24, 50], ages = [46, 37, 5]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;50&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The team consists of a single lemon — the last one. Any other combination would include a younger lemon with a higher IQ than an older lemon, causing a conflict.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, we sort all lemons by age. If two lemons have the same age, we sort them by IQ. We also create a slice &lt;code&gt;teamIQs&lt;/code&gt;, where &lt;code&gt;teamIQs[i]&lt;/code&gt; is the maximum total IQ of the team that contains the lemon at index &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate through the sorted lemons. For each lemon, we first set &lt;code&gt;teamIQs[i]&lt;/code&gt; to the current lemon's IQ: at least the team consisting of just the &lt;code&gt;i&lt;/code&gt;-th lemon is valid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;For each lemon, we iterate through all previous lemons and try to find a previous team to which the current lemon can be added without conflicts. &lt;/p&gt;

&lt;p&gt;In the team at index &lt;code&gt;prevInd&lt;/code&gt;, the lemon with the maximum IQ is the lemon at &lt;code&gt;prevInd&lt;/code&gt; (otherwise that lemon could not have been added, because it would be older with a smaller IQ, given our sorting). &lt;/p&gt;

&lt;p&gt;Therefore, if the current lemon's IQ is greater than or equal to the &lt;code&gt;prevInd&lt;/code&gt; lemon's IQ, we can add the current lemon to &lt;code&gt;teamIQs[prevInd]&lt;/code&gt; without conflicts. Among all such possible teams, we take the one with the maximum total IQ.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As we go, we update &lt;code&gt;maxTeamIQ&lt;/code&gt; — the maximum total IQ over all conflict-free teams.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Lemon&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;iq&lt;/span&gt;  &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;findBestTeamIQ&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ages&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;lemons&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;Lemon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iq&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;iqs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;lemons&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Lemon&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;iq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;slices&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SortFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lemons&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;Lemon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cmp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cmp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="n"&gt;teamIQs&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lemons&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;maxTeamIQ&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lemon&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;lemons&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;teamIQs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lemon&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;prevInd&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;prevInd&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;prevInd&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;lemons&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prevInd&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;lemon&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;continue&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;newIQ&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;teamIQs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;prevInd&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;lemon&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iq&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;newIQ&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;teamIQs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;teamIQs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;newIQ&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;teamIQs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;maxTeamIQ&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;maxTeamIQ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;teamIQs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;maxTeamIQ&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Mushroom Soup for everyone!</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Wed, 01 Oct 2025 09:02:43 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-mushroom-soup-for-everyone-55a3</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-mushroom-soup-for-everyone-55a3</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Everyone deserves creamy mushroom soup!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; - an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Mushroom Soup 🍄&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Autumn is a great time for soup, so the Veggie Kingdom is gathering mushrooms for a creamy mushroom soup. But there’s one problem: lactose intolerance. What's the solution? Cook two pots: one with dairy cream and one with coconut cream.&lt;/p&gt;

&lt;p&gt;The amount of soup should be the same in both pots, so we need to split the mushrooms evenly by weight. Each mushroom has its own weight, so we can’t just divide them by count.&lt;/p&gt;

&lt;p&gt;How can we quickly check whether it’s possible to partition the mushrooms into two groups with equal total weight?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🥔
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;mushrooms []int&lt;/code&gt; - weights of the mushrooms (each element is the weight of one mushroom).&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🧅
&lt;/h3&gt;

&lt;p&gt;Return &lt;code&gt;true&lt;/code&gt; if it’s possible to partition the mushrooms evenly by weight (into two groups with equal total weight), otherwise return &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🧄:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;mushrooms = [1,5,11,5]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first pot contains mushrooms with weights &lt;code&gt;1, 5, 5&lt;/code&gt;, and the second pot contains a single mushroom with weight &lt;code&gt;11&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;mushrooms = [1, 3, 10]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It’s impossible to split the mushrooms into two groups with equal total weight.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, we calculate the total weight of all mushrooms. If &lt;code&gt;totalWeight&lt;/code&gt; is odd, it's impossible to partition mushrooms evenly. Otherwise, the target weight for one pot is &lt;code&gt;totalWeight / 2&lt;/code&gt; (call it &lt;code&gt;potWeight&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We use dynamic programming to solve the problem. Let's create a boolean slice &lt;code&gt;dpSumWeight&lt;/code&gt;, where &lt;code&gt;dpSumWeight[i]&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if some subset of mushrooms sums to &lt;code&gt;i&lt;/code&gt;. We need &lt;code&gt;potWeight+1&lt;/code&gt; elements, because the maximum sum we care about is &lt;code&gt;potWeight&lt;/code&gt; and we also add one element for zero weight.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate through all the mushrooms. For each mushroom we scan &lt;code&gt;dpSumWeight&lt;/code&gt; backwards. If &lt;code&gt;dpSumWeight[i]&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;i+mushroomWeight &amp;lt; len(dpSumWeight)&lt;/code&gt;, then set &lt;code&gt;dpSumWeight[i+mushroomWeight] = true&lt;/code&gt;, because the sum &lt;code&gt;i+mushroomWeight&lt;/code&gt; is now reachable. We go backwards so we don’t reuse the same mushroom twice during the same iteration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If at any point &lt;code&gt;dpSumWeight[potWeight]&lt;/code&gt; becomes true, we return &lt;code&gt;true&lt;/code&gt;. Otherwise, return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;canPartition&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mushrooms&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;totalWeight&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mushroomWeight&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;mushrooms&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;totalWeight&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;mushroomWeight&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;totalWeight&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;potWeight&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;totalWeight&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;

    &lt;span class="n"&gt;dpSumWeight&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;potWeight&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;dpSumWeight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mushroomWeight&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;mushrooms&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dpSumWeight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dpSumWeight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;mushroomWeight&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dpSumWeight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;dpSumWeight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;mushroomWeight&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dpSumWeight&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;potWeight&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>go</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Saving Apples from Pies</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Wed, 24 Sep 2025 08:13:34 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-saving-apples-from-pies-58p8</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-saving-apples-from-pies-58p8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Can a single train save apples from the pie threat?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; - an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Apple Escape 🍎&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;The season of Apple Pies has begun. All the apples are leaving their homes in search of a safer place. Nobody wants to end up in a pie anymore — it’s just not trendy. A smoothie is better, or at least a strudel.&lt;/p&gt;

&lt;p&gt;There is a train going in one direction with a limited number of seats. Apples travel in groups: each group wants to board the train at point X and leave at point Y.&lt;/p&gt;

&lt;p&gt;Can the train take every apple without exceeding its seats?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🍐
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;trips []Trip&lt;/code&gt; - information about group trips. Each &lt;code&gt;Trip&lt;/code&gt; has a start point &lt;code&gt;from&lt;/code&gt;, an end point &lt;code&gt;to&lt;/code&gt;, and the group size &lt;code&gt;num&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;capacity int&lt;/code&gt; - the number of seats on the train.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🍊
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;true&lt;/code&gt; if the train can carry all apples without exceeding capacity at any point; &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🍋:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;trips = {{from: 2, to: 7, num: 1}, {from: 0, to: 2, num: 3}, {from: 1, to: 3, num: 2}}, capacity = 5&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At point &lt;code&gt;0&lt;/code&gt;, the train takes 3 apples.&lt;/p&gt;

&lt;p&gt;Then, at point &lt;code&gt;1&lt;/code&gt;, it takes 2 more apples, so now all 5 seats are occupied.&lt;/p&gt;

&lt;p&gt;At point &lt;code&gt;2&lt;/code&gt;, 3 apples leave the train. At the same time, 1 apple boards the train. Now there are 3 apples on board.&lt;/p&gt;

&lt;p&gt;At point &lt;code&gt;3&lt;/code&gt;, 2 apples leave the train. Only 1 apple remains, which will leave at point &lt;code&gt;7&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The apples on board never exceeded the train’s capacity.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;trips = {{from: 0, to: 2, num: 3}, {from: 1, to: 3, num: 2}}, capacity = 4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;At point &lt;code&gt;0&lt;/code&gt;, the train takes 3 apples.&lt;/p&gt;

&lt;p&gt;At point &lt;code&gt;1&lt;/code&gt;, it should take 2 more apples. That would require 5 seats, but the capacity is 4, so the train can't take them.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, we build a list of events: each trip creates a pick-up event at &lt;code&gt;from&lt;/code&gt; and a drop-off event at &lt;code&gt;to&lt;/code&gt;. We sort events by location in ascending order. When two events have the same location, put drop-offs before pick-ups. In this case, the freed seats can be reused immediately. Each event contains: &lt;code&gt;location&lt;/code&gt;, &lt;code&gt;num&lt;/code&gt; (group size), and &lt;code&gt;isFrom&lt;/code&gt; (whether it is a pick-up or drop-off).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We iterate through the &lt;code&gt;events&lt;/code&gt; and update the current number of apples on the train &lt;code&gt;num&lt;/code&gt;. If it's a drop-off, we subtract the size of the group from &lt;code&gt;num&lt;/code&gt;. Otherwise, we add the group size to &lt;code&gt;num&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If at any point a pick-up would exceed capacity, return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
Otherwise, return &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Trip&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;from&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;to&lt;/span&gt;   &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;num&lt;/span&gt;  &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Event&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;num&lt;/span&gt;      &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;isFrom&lt;/span&gt;   &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;canSaveAllApples&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="n"&gt;Trip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trips&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;trips&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="n"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;trip&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;slices&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SortFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;Event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cmp&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Compare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFrom&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFrom&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFrom&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;events&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isFrom&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;capacity&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Coconuts Never Roll Alone</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Thu, 04 Sep 2025 14:09:15 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-coconuts-never-roll-alone-8f8</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-coconuts-never-roll-alone-8f8</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;What happens when coconuts start racing?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; - an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Coconut Race 🥥&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Coconuts are having a race! Just for fun. After all, there's no real need to hurry when you're a coconut.&lt;/p&gt;

&lt;p&gt;Each coconut starts from a certain position and has its own speed. They're all rolling in the same direction toward a common finish line.&lt;/p&gt;

&lt;p&gt;Whenever one coconut catches up with another, they merge into a coconut bouquet and continue rolling together at the speed of the slowest coconut in the group.&lt;/p&gt;

&lt;p&gt;How many bouquets will reach the finish line?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🥭
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;position []int&lt;/code&gt; - the starting positions of the coconuts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;speed []int&lt;/code&gt; - the speeds of the coconuts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;target int&lt;/code&gt; - the finish line; all coconuts are rolling toward this point.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🍍
&lt;/h3&gt;

&lt;p&gt;An integer representing the number of coconut bouquets that reach the target.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🍌:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;position = [1, 3, 18], speed = [5, 1, 3], target = 20&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first coconut (from position &lt;code&gt;1&lt;/code&gt;, speed &lt;code&gt;5&lt;/code&gt;) catches up to the second one (position &lt;code&gt;3&lt;/code&gt;, speed &lt;code&gt;1&lt;/code&gt;) before reaching the target.&lt;/p&gt;

&lt;p&gt;They form a bouquet and move together at speed &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The third coconut (position &lt;code&gt;18&lt;/code&gt;, speed &lt;code&gt;3&lt;/code&gt;) reaches the target before the bouquet catches up.&lt;/p&gt;

&lt;p&gt;So, there are &lt;code&gt;2&lt;/code&gt; bouquets at the finish.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;position = [2, 0, 4], speed = [3, 7, 1], target = 10&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The coconut starting from &lt;code&gt;0&lt;/code&gt; (speed &lt;code&gt;7&lt;/code&gt;) quickly catches up to the one at &lt;code&gt;2&lt;/code&gt; (speed &lt;code&gt;3&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Together they move at speed &lt;code&gt;3&lt;/code&gt; and catch up to the coconut at &lt;code&gt;4&lt;/code&gt; (speed &lt;code&gt;1&lt;/code&gt;) before reaching the target.&lt;/p&gt;

&lt;p&gt;All three coconuts finish as a single bouquet.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First, we sort the coconuts by their starting positions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Then, we iterate through the coconuts from right to left. We keep track of the current bouquet using two variables:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;bouquetPos&lt;/code&gt; — the position of the last bouquet,&lt;br&gt;
&lt;code&gt;bouquetSpeed&lt;/code&gt; — the speed of the last bouquet.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For each coconut, we check whether it can reach the current bouquet (&lt;code&gt;canReach()&lt;/code&gt;). A coconut can merge with the bouquet if it catches up to it before the bouquet itself reaches the finish.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the coconut can't reach the bouquet, then no other coconut behind it will be able to reach that bouquet either. Why? Because to get there, they would first need to catch up to the current coconut, and then move at its speed. So, if the current coconut can’t make it, nobody else can.&lt;/p&gt;

&lt;p&gt;In this case, we increase the count of bouquets &lt;code&gt;res&lt;/code&gt;. The current coconut now becomes the new bouquet, so we update &lt;code&gt;bouquetPos&lt;/code&gt; and &lt;code&gt;bouquetSpeed&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the end, we return &lt;code&gt;res + 1&lt;/code&gt;, because the very last bouquet wasn’t counted during the loop.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;canReach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;leftPos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightPos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;leftSpeed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rightSpeed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;leftPos&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;rightPos&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;leftSpeed&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;rightSpeed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;approachSpeed&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;leftSpeed&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rightSpeed&lt;/span&gt;
    &lt;span class="n"&gt;approachDist&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;rightPos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;leftPos&lt;/span&gt;
    &lt;span class="n"&gt;rightDist&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;rightPos&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;approachDist&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rightSpeed&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;approachSpeed&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;rightDist&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;Coconut&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;pos&lt;/span&gt;   &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;countCoconutBouquets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;target&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;coconuts&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="n"&gt;Coconut&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;position&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;position&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;coconuts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Coconut&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;pos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;speed&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;slices&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SortFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="n"&gt;Coconut&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="n"&gt;lastInd&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;bouquetPos&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;lastInd&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;
    &lt;span class="n"&gt;bouquetSpeed&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;lastInd&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;curPos&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pos&lt;/span&gt;
        &lt;span class="n"&gt;curSpeed&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;coconuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;speed&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;canReach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;curPos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bouquetPos&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;curSpeed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bouquetSpeed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
        &lt;span class="n"&gt;bouquetPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curPos&lt;/span&gt;
        &lt;span class="n"&gt;bouquetSpeed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;curSpeed&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: The Smartest Peanut in the Neighborhood</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Tue, 26 Aug 2025 06:44:48 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-the-smartest-peanut-in-the-neighborhood-3o92</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-the-smartest-peanut-in-the-neighborhood-3o92</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Peanuts feed their ego in linear time&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;The Smartest Peanut in the Neighborhood 🥜&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Remember how the walnuts once tried to find out who was the &lt;a href="https://dev.to/asparagos/go-coding-with-asparagos-walnuts-take-the-iq-test-8dl"&gt;smartest&lt;/a&gt;? Now it's the peanuts' turn, though their approach is a bit different.&lt;/p&gt;

&lt;p&gt;Peanuts don't care about being the smartest among all nuts. They just want to feel smarter than their nearby neighbors.&lt;/p&gt;

&lt;p&gt;You’re given the cleverness levels of peanuts planted in a row. For each group of &lt;code&gt;k&lt;/code&gt; consecutive peanuts, determine which peanut is the smartest.&lt;/p&gt;

&lt;p&gt;Is it a hard nut to crack?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🌰
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A slice of integers — each integer represents the cleverness level of a peanut in the row.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;k&lt;/code&gt; — an integer that represents the size of a group (a sliding window).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Output 🌱
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A slice of integers — each representing the maximum cleverness level within a sliding window of size &lt;code&gt;k&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Examples 🥒:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[1, 6, 3, 4, 1, 2, 6], k = 3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[6, 6, 4, 4, 6]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first group is &lt;code&gt;[1, 6, 3]&lt;/code&gt;, its maximum is &lt;code&gt;6&lt;/code&gt;.&lt;br&gt;
The second group is &lt;code&gt;[6, 3, 4]&lt;/code&gt;, its maximum is &lt;code&gt;6&lt;/code&gt;.&lt;br&gt;
The third group is &lt;code&gt;[3, 4, 1]&lt;/code&gt;, its maximum is &lt;code&gt;4&lt;/code&gt;.&lt;br&gt;
The fourth group is &lt;code&gt;[4, 1, 2]&lt;/code&gt;, its maximum is &lt;code&gt;4&lt;/code&gt;.&lt;br&gt;
The last group is &lt;code&gt;[1, 2, 6]&lt;/code&gt;, its maximum is &lt;code&gt;6&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[1, 2, 3, 2, 1], k = 2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[2, 3, 3, 2]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The first group is &lt;code&gt;[1, 2]&lt;/code&gt;, its maximum is &lt;code&gt;2&lt;/code&gt;.&lt;br&gt;
The second group is &lt;code&gt;[2, 3]&lt;/code&gt;, its maximum is &lt;code&gt;3&lt;/code&gt;.&lt;br&gt;
The third group is &lt;code&gt;[3, 2]&lt;/code&gt;, its maximum is &lt;code&gt;3&lt;/code&gt;.&lt;br&gt;
The last group is &lt;code&gt;[2, 1]&lt;/code&gt;, its maximum is &lt;code&gt;2&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We use a double-ended queue (&lt;code&gt;deque&lt;/code&gt;) to keep track of the indices of &lt;code&gt;peanuts&lt;/code&gt; that might be the smartest in the current sliding window. This queue helps us efficiently add and remove candidates from both ends.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We iterate through the &lt;code&gt;peanuts&lt;/code&gt; slice. For each &lt;code&gt;peanutLevel&lt;/code&gt; at index &lt;code&gt;i&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;a. We remove all peanuts from the right side of the queue that are less clever than or equal to the current one. They're no longer valid candidates because the current peanut is smarter and more recent.&lt;/p&gt;

&lt;p&gt;b. We add the current index &lt;code&gt;i&lt;/code&gt; to the right of the queue.&lt;/p&gt;

&lt;p&gt;c. If the leftmost index in the queue is outside the current window (i.e., its index is less than or equal to &lt;code&gt;i - k&lt;/code&gt;), we remove it, it's no longer part of the group we're analyzing.&lt;/p&gt;

&lt;p&gt;d. If the window has reached size &lt;code&gt;k&lt;/code&gt; (i.e., &lt;code&gt;i + 1 &amp;gt;= k&lt;/code&gt;), we save the cleverness of the peanut at the left side of the queue as the result because it's the smartest one in the current window.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each peanut is added to the queue once and removed at most once, so the total time complexity is &lt;code&gt;O(len(peanuts))&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;findSmartestPeanut&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peanuts&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peanuts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;deque&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;peanuts&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peanutLevel&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;peanuts&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;peanuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;peanutLevel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;deque&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;deque&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;deque&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;peanuts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;deque&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>humor</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Sunflowers and the Speaking Challenge</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Tue, 19 Aug 2025 08:10:54 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-sunflowers-and-the-speaking-challenge-2gd6</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-sunflowers-and-the-speaking-challenge-2gd6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Sunflowers on a mission: better English in linear time?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Sunflower Speaking Club 🌻&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Sunflowers are planning to expand their influence across the world. Olives are gaining popularity, they can’t fall behind. To achieve this, they need to learn English.&lt;/p&gt;

&lt;p&gt;Each sunflower already speaks it to some extent, but wants to find a partner to practice with. The sunflowers are planted in a row. Each has their own level of English and wants to find the nearest sunflower to the right who speaks better than they do.&lt;/p&gt;

&lt;p&gt;Why to the right? The sun is rising in the east, so it’s the perfect moment to combine business with pleasure.&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🌽
&lt;/h3&gt;

&lt;p&gt;A slice of integers — each integer represents the English level of a sunflower in the row.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🫒
&lt;/h3&gt;

&lt;p&gt;A slice of integers — each integer represents the distance to the nearest sunflower to the right with a higher English level. If there’s no such sunflower, return 0.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🥒:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[1, 5, 7, 3, 4]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[1, 1, 0, 1, 0]&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[10, 9, 8, 7]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[0, 0, 0, 0]&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[25, 13, 30]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[2, 1, 0]&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We use a stack to keep track of potential candidates for being the nearest better-speaking partner to the right.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We iterate through the &lt;code&gt;englishLevel&lt;/code&gt; slice from right to left. For each sunflower:&lt;/p&gt;

&lt;p&gt;a. We remove all sunflowers from the stack that have an English level less than or equal to the current one. These sunflowers can’t be good partners anymore, because the current sunflower is better and will be a better candidate for any future comparisons.&lt;/p&gt;

&lt;p&gt;b. If the stack is not empty, then the sunflower on top of the stack is the nearest one to the right with a higher level. So we store the distance between them.&lt;/p&gt;

&lt;p&gt;c. We then push the current sunflower onto the stack, as it might be a suitable partner for some sunflower to its left.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Each sunflower is pushed to the stack only once and removed at most once, so the overall time complexity is &lt;code&gt;O(len(englishLevel))&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;findSpeakingPartner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;englishLevel&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;englishLevel&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;englishLevel&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;englishLevel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;curLevel&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;englishLevel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;stackInd&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;englishLevel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;stackInd&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;curLevel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;stack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>humor</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Will Graph Cycles Spoil the Salsa Festival?</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Thu, 14 Aug 2025 14:27:40 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-will-graph-cycles-spoil-the-salsa-festival-2eod</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-will-graph-cycles-spoil-the-salsa-festival-2eod</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Salsa Festival! All vegetables are invited! Can cycles ruin the fun?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; - an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Salsa Festival 🥗&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;There’s a salsa festival in the Veggie Kingdom this weekend. All vegetables are invited, free of charge!&lt;/p&gt;

&lt;p&gt;But veggies are picky: each one will only come if certain friends arrive first.&lt;br&gt;
For example, bell pepper will only show up after his brother, chili pepper. A potato will only come if both pumpkin and corn are already there (this trio became best friends after the lecture "Introduction to the Carbohydrate Diet").&lt;/p&gt;

&lt;p&gt;Nobody can untangle these complicated relationships. Did you know that apple is trying to sue pineapple for using its name?&lt;/p&gt;

&lt;p&gt;So, can we figure out whether all the veggies will come to the salsa festival?&lt;/p&gt;


&lt;h3&gt;
  
  
  Input 🥒
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;vegNum int&lt;/code&gt; - the total number of vegetables in the kingdom.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;requirements [][]int&lt;/code&gt; - each element &lt;code&gt;requirements[i] = [a, b]&lt;/code&gt; means that vegetable &lt;code&gt;a&lt;/code&gt; will come to the festival only if vegetable &lt;code&gt;b&lt;/code&gt; comes first.&lt;/p&gt;
&lt;h3&gt;
  
  
  Output 🍅
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;true&lt;/code&gt; if it’s possible for all vegetables to attend the festival,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;false&lt;/code&gt; if some requirements create a deadlock (i.e. a cycle).&lt;/p&gt;


&lt;h3&gt;
  
  
  Examples 🌶️:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;vegNum = 2, requirements = [[1, 0]]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Vegetable &lt;code&gt;0&lt;/code&gt; can come without any conditions. Vegetable &lt;code&gt;1&lt;/code&gt; depends on &lt;code&gt;0&lt;/code&gt;, and since &lt;code&gt;0&lt;/code&gt; can come, &lt;code&gt;1&lt;/code&gt; can come too.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;vegNum = 2, requirements = [[1, 0], [0, 1]]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Vegetable &lt;code&gt;1&lt;/code&gt; depends on &lt;code&gt;0&lt;/code&gt;, and &lt;code&gt;0&lt;/code&gt; depends on &lt;code&gt;1&lt;/code&gt;. This creates a cycle, neither can come first, so no one comes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We start by building a graph of vegetables. Each vertex represents a vegetable. An edge &lt;code&gt;a -&amp;gt; b&lt;/code&gt; means that &lt;code&gt;b&lt;/code&gt; will only come if &lt;code&gt;a&lt;/code&gt; comes first. Now the problem reduces to detecting a cycle in this graph, which we can do using &lt;a href="https://en.wikipedia.org/wiki/Depth-first_search" rel="noopener noreferrer"&gt;Depth-First Search (DFS)&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;dfs&lt;/code&gt; function is a recursive function with the following arguments:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;graph [][]int&lt;/code&gt; - the adjacency list of the vegetable graph&lt;/p&gt;

&lt;p&gt;&lt;code&gt;vertex int&lt;/code&gt; – the current node we are traversing&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pathVertices map[int]bool&lt;/code&gt; - tracks nodes on the current DFS path (used to detect a cycle)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;visited map[int]bool&lt;/code&gt; - tracks all previously visited nodes (used to avoid duplicate traversal)&lt;/p&gt;

&lt;p&gt;It returns &lt;code&gt;true&lt;/code&gt; if a cycle is found, and &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Cycle detection logic:&lt;/p&gt;

&lt;p&gt;If the vertex is already in &lt;code&gt;pathVertices&lt;/code&gt;, that means we've visited it in the current path — a cycle is found, so we return &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If it's already in &lt;code&gt;visited&lt;/code&gt; but not in the current path, we've checked it before and know it doesn’t lead to a cycle — return &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Otherwise, we:&lt;/p&gt;

&lt;p&gt;Mark the current node as visited (both in &lt;code&gt;visited&lt;/code&gt; and &lt;code&gt;pathVertices&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Recursively apply DFS to its children.&lt;/p&gt;

&lt;p&gt;After processing all descendants, remove the current node from &lt;code&gt;pathVertices&lt;/code&gt; (as we’re backtracking).&lt;/p&gt;

&lt;p&gt;If no cycles are found during any DFS traversal, return &lt;code&gt;true&lt;/code&gt;. Otherwise, return &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;canAllVeggiesCome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vegNum&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;requirements&lt;/span&gt; &lt;span class="p"&gt;[][]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;vegGraph&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([][]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vegNum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;requirements&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;toVeg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;fromVeg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;req&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="n"&gt;vegGraph&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fromVeg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vegGraph&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;fromVeg&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="n"&gt;toVeg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;visited&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;veg&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;veg&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;vegNum&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;veg&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;veg&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;continue&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;pathVertices&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vegGraph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;veg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pathVertices&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt; &lt;span class="p"&gt;[][]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;vertex&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pathVertices&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt; &lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="kt"&gt;bool&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;pathVertices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;pathVertices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;childV&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dfs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;graph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;childV&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pathVertices&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;visited&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;pathVertices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;vertex&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: How Much Gazpacho Can We Make with Dynamic Programming?</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Tue, 29 Jul 2025 07:49:31 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-how-much-gazpacho-can-we-make-with-dynamic-programming-4m62</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-how-much-gazpacho-can-we-make-with-dynamic-programming-4m62</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;One chef. One heatwave. Can Dynamic Programming save the gazpacho?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;The World’s Biggest Gazpacho 🍅&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Everyone is suffering from the summer heat. One chef decides to surprise everyone by making the biggest gazpacho ever.&lt;/p&gt;

&lt;p&gt;He needs as many tomatoes as possible. There are several tomato beds in the garden, but he can't gather them all. He wants to keep it a secret - nobody should notice anything suspicious for as long as possible.&lt;/p&gt;

&lt;p&gt;So he comes up with a strategy: he’ll collect all the tomatoes from some beds and leave the others untouched — but he won’t leave two adjacent beds empty. Otherwise, someone might suspect something (especially the lemons - they’re always sticking their nose where it doesn’t belong).&lt;/p&gt;

&lt;p&gt;Time is running out, the sun is burning, and we all want that delicious cold soup as soon as possible. How can the chef gather the maximum number of tomatoes? And how many tomatoes can he collect in total?&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🥒
&lt;/h3&gt;

&lt;p&gt;A slice of integers representing tomato beds — each integer is the number of tomatoes in that bed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🫑
&lt;/h3&gt;

&lt;p&gt;An integer — the maximum number of tomatoes the chef can collect without leaving two adjacent beds empty.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🧅:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[6, 3, 5, 10]&lt;/code&gt;&lt;br&gt;
Output: &lt;code&gt;16&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we take tomatoes from the first bed, we have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take tomatoes from the third bed: 6 + 5 = 11&lt;/li&gt;
&lt;li&gt;Take tomatoes from the last bed: 6 + 10 = 16&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we take tomatoes from the second bed, we can only take tomatoes from the last bed: 3 + 10 = 13&lt;/p&gt;

&lt;p&gt;The best option gives us 16 tomatoes.&lt;/p&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[3, 7, 2]&lt;/code&gt;&lt;br&gt;
Output: &lt;code&gt;7&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If we take tomatoes from the first bed, we can also take tomatoes from the last bed: 3 + 2 = 5&lt;/p&gt;

&lt;p&gt;If we take tomatoes from the second bed, we can’t take any others, so we get 7 tomatoes.&lt;/p&gt;

&lt;p&gt;The best option gives us 7 tomatoes.&lt;/p&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;We iterate through the input slice. At each step, we calculate two values:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;takeSum&lt;/code&gt;: the maximum number of tomatoes we can collect if we take tomatoes from the current bed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;notTakeSum&lt;/code&gt;: the maximum number of tomatoes we can collect if we skip the current bed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Initially, both &lt;code&gt;takeSum&lt;/code&gt; and &lt;code&gt;notTakeSum&lt;/code&gt; are set to 0.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;At each step:&lt;/p&gt;

&lt;p&gt;If we take tomatoes from the current bed (&lt;code&gt;num&lt;/code&gt;), we should have skipped the previous one. So the new &lt;code&gt;takeSum&lt;/code&gt; becomes &lt;code&gt;notTakeSum + num&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If we skip the current bed, it doesn’t matter whether we took or skipped the previous one - we just take the maximum of the previous &lt;code&gt;takeSum&lt;/code&gt; and &lt;code&gt;notTakeSum&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After processing all beds, the result is the maximum of &lt;code&gt;takeSum&lt;/code&gt; and &lt;code&gt;notTakeSum&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;collectTomatoes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;beds&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;takeSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;notTakeSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;beds&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;takeSum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notTakeSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notTakeSum&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notTakeSum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;takeSum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;takeSum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;notTakeSum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>humor</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Detecting Potato Cycles in O(1) Space</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Mon, 14 Jul 2025 09:00:54 +0000</pubDate>
      <link>https://forem.com/asparagos/asparagos-vs-potato-bugs-can-he-detect-the-cycle-in-o1-space-1dll</link>
      <guid>https://forem.com/asparagos/asparagos-vs-potato-bugs-can-he-detect-the-cycle-in-o1-space-1dll</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Potato vs Bugs. Can FIFO save the harvest?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Potato Bugs 🥔&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Yet another challenging day in the Veggie Kingdom. To save the harvest, all potatoes must be treated for bugs. That’s why they’re lining up at the Fight for Insect-Free Organics (FIFO) office.&lt;/p&gt;

&lt;p&gt;But potatoes aren’t the sharpest veggies — they might mess things up and accidentally form a cycle in the queue. We need to check whether their queue contains a cycle.&lt;/p&gt;

&lt;p&gt;The Kingdom is going through hard times, so we must solve this using only constant memory.&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🥦
&lt;/h3&gt;

&lt;p&gt;A head of a linked list of potatoes. Each potato has a Name and a pointer to the next one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;PotatoNode&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="n"&gt;Next&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PotatoNode&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Output 🥕
&lt;/h3&gt;

&lt;p&gt;Return &lt;code&gt;true&lt;/code&gt; if the list contains a cycle, or &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🥒:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Potato Jack -&amp;gt; Potato George -&amp;gt; Potato Arthur -&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Output: &lt;code&gt;false&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Potato Jack -&amp;gt; Potato George -&amp;gt; Potato Arthur
                      ↑                ↓
                      ← ← ← ← ← ← ← ← ←
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Output: &lt;code&gt;true&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We use two pointers: &lt;br&gt;
&lt;code&gt;slowNode&lt;/code&gt; moves one node at a time.&lt;br&gt;
&lt;code&gt;fastNode&lt;/code&gt; moves two nodes at a time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;As we iterate through the list:&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;fastNode&lt;/code&gt; reaches the end (&lt;code&gt;nil&lt;/code&gt;), there’s no cycle.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;slowNode&lt;/code&gt; and &lt;code&gt;fastNode&lt;/code&gt; meet at the same node, it means there's a cycle.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;hasPotatoCycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;PotatoNode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;slowNode&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="n"&gt;fastNode&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;head&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;fastNode&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;fastNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Next&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;slowNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;slowNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Next&lt;/span&gt;
        &lt;span class="n"&gt;fastNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fastNode&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Next&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Next&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;slowNode&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;fastNode&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a classic example of &lt;a href="https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoise_and_hare" rel="noopener noreferrer"&gt;Floyd's cycle-finding algorithm&lt;/a&gt;, also known as the Tortoise and Hare algorithm.&lt;/p&gt;

&lt;p&gt;Naturally, no tortoises or hares were allowed to participate in our event - otherwise, our veggies would've been in serious danger. &lt;/p&gt;




&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Are Strawberries in Danger?</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Mon, 07 Jul 2025 14:38:26 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-are-strawberries-in-danger-nab</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-are-strawberries-in-danger-nab</guid>
      <description>&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of &lt;strong&gt;Strawberry fever 🍓&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One boy. One obsession. Can the city’s strawberries survive?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;There’s one boy who is crazy about strawberries (and honestly, who isn’t?). One day, he decided to eat all the strawberries in the city.&lt;/p&gt;

&lt;p&gt;There is a bunch of stores arranged in a circle where strawberries can be bought. The boy wants to visit all of them in a clockwise direction and buy every strawberry he can. For each store, we know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how many strawberries can be bought there,&lt;/li&gt;
&lt;li&gt;how many strawberries he needs to eat to have enough energy to move from that store to the next one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We don't know where the boy will start his route, but he must finish it at the same store where he started.&lt;/p&gt;

&lt;p&gt;Can the boy visit all the stores if he has unlimited money and can eat all the strawberries he collects along the way?&lt;/p&gt;

&lt;p&gt;It's guaranteed that if completing the route is possible, there is exactly one valid starting point. In that case, we should find this starting point — and stop his evil strawberry plan!&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🍒
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;storeStock []int&lt;/code&gt; — each &lt;code&gt;storeStock[i]&lt;/code&gt; represents the number of strawberries available in the &lt;code&gt;i&lt;/code&gt;th store.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wayCost []int&lt;/code&gt; — each &lt;code&gt;wayCost[i]&lt;/code&gt; represents the number of strawberries the boy needs to eat to travel from store &lt;code&gt;i&lt;/code&gt; to store &lt;code&gt;i+1&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🍇
&lt;/h3&gt;

&lt;p&gt;An integer — the index of the store where the boy should start his route to complete his plan. Return &lt;code&gt;-1&lt;/code&gt; if no such starting point exists.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🫐:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;storeStock = [1, 2, 3, 4, 5], wayCost = [3, 4, 5, 1, 2]&lt;/code&gt;&lt;br&gt;
Output: &lt;code&gt;3&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;He starts at store &lt;code&gt;3&lt;/code&gt; and eats 4 strawberries. His energy reserve is 4 strawberries.&lt;/p&gt;

&lt;p&gt;He travels to store &lt;code&gt;4&lt;/code&gt;, spends 1 strawberry of energy, and eats 5 strawberries. His energy reserve is 4 - 1 + 5 = 8 strawberries.&lt;/p&gt;

&lt;p&gt;He travels to store &lt;code&gt;0&lt;/code&gt;, spends 2 strawberries, and eats 1 strawberry. His energy reserve is 8 - 2 + 1 = 7.&lt;/p&gt;

&lt;p&gt;He travels to store &lt;code&gt;1&lt;/code&gt;, spends 3 strawberries, eats 2 strawberries. His energy reserve is 7 - 3 + 2 = 6.&lt;/p&gt;

&lt;p&gt;He travels to store &lt;code&gt;2&lt;/code&gt;, spends 4 strawberries, eats 3 strawberries. His energy reserve is 6 - 4 + 3 = 5.&lt;/p&gt;

&lt;p&gt;He travels to store &lt;code&gt;3&lt;/code&gt;, spends 5 strawberries. Mission completed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;storeStock = [2, 3, 4], wayCost = [3, 4, 3]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;-1&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If he starts at store &lt;code&gt;0&lt;/code&gt; he eats 2 strawberries, but he needs 3 strawberries to get to the next store.&lt;/p&gt;

&lt;p&gt;If he starts at store &lt;code&gt;1&lt;/code&gt; he eats 3 strawberries, but he needs 4 strawberries to get to the next store.&lt;/p&gt;

&lt;p&gt;If he starts at store &lt;code&gt;2&lt;/code&gt; he eats 4 strawberries. Then, he travels to store &lt;code&gt;0&lt;/code&gt;, spends 3 strawberries and eats 2 strawberries. His energy reserve is 4 - 3 + 2 = 3 strawberries.&lt;/p&gt;

&lt;p&gt;He travels to store &lt;code&gt;1&lt;/code&gt;, spends 3 strawberries and eats 3 strawberries. His energy reserve is 3 - 3 + 3 = 3 strawberries. But he needs 4 strawberries to finish the circle and travel to the store &lt;code&gt;2&lt;/code&gt;. So it's impossible.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We need three variables:&lt;br&gt;
• &lt;code&gt;minEnergyReserve&lt;/code&gt; - the minimum energy reserve reached during the loop (before eating strawberries).&lt;br&gt;
• &lt;code&gt;minEnergyReserveInd&lt;/code&gt; - the index of the store where that minimum occurred.&lt;br&gt;
• &lt;code&gt;energyReserve&lt;/code&gt; - the current energy reserve at any given step.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;We iterate through the stores. At each step:&lt;/p&gt;

&lt;p&gt;• We add the difference between strawberries gained &lt;code&gt;storeStock[ind]&lt;/code&gt; and energy spent &lt;code&gt;wayCost[ind]&lt;/code&gt; to &lt;code&gt;energyReserve&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;• If the current &lt;code&gt;energyReserve&lt;/code&gt; is less than &lt;code&gt;minEnergyReserve&lt;/code&gt;, we update both &lt;code&gt;minEnergyReserve&lt;/code&gt; and &lt;code&gt;minEnergyReserveInd&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After completing the loop, we check if &lt;code&gt;energyReserve&lt;/code&gt; is negative.&lt;br&gt;
If &lt;code&gt;energyReserve &amp;lt; 0&lt;/code&gt;, it means the total amount of strawberries is less than the total energy required to complete the full circle. In that case, it’s impossible to complete the route — no matter where you start.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Otherwise, we can start at the store where the minimum energy reserve occurred — &lt;code&gt;minEnergyReserveInd&lt;/code&gt;. Let’s see why this works.&lt;/p&gt;

&lt;p&gt;• If &lt;code&gt;minEnergyReserve == 0&lt;/code&gt;, it means our energy reserve was never negative during the entire loop, and the minimum occurred right at the beginning. So &lt;code&gt;minEnergyReserveInd == 0&lt;/code&gt;, and starting from index 0 is safe — we can complete the full route without running out of energy.&lt;/p&gt;

&lt;p&gt;• If &lt;code&gt;minEnergyReserve &amp;lt; 0&lt;/code&gt;, and we now start from &lt;code&gt;minEnergyReserveInd&lt;/code&gt;, our energy reserve is reset to 0 instead of some negative value. From this point on, we follow the same path as before, but with an energy reserve that is always higher by &lt;code&gt;-minEnergyReserve&lt;/code&gt;. Since no point in the previous run was lower than &lt;code&gt;minEnergyReserve&lt;/code&gt;, now we will never hit a negative energy level, and we can successfully complete the route.&lt;br&gt;
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;canEatAllStrawberries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storeStock&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;wayCost&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;minEnergyReserve&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;minEnergyReserveInd&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;energyReserve&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storeStock&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;energyReserve&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;minEnergyReserve&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;minEnergyReserve&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;energyReserve&lt;/span&gt;
            &lt;span class="n"&gt;minEnergyReserveInd&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;energyReserve&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;storeStock&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;wayCost&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;energyReserve&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;minEnergyReserveInd&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>humor</category>
    </item>
    <item>
      <title>Go Coding with Asparagos: Walnuts Take the IQ Test</title>
      <dc:creator>Asparagos</dc:creator>
      <pubDate>Wed, 25 Jun 2025 09:15:59 +0000</pubDate>
      <link>https://forem.com/asparagos/go-coding-with-asparagos-walnuts-take-the-iq-test-8dl</link>
      <guid>https://forem.com/asparagos/go-coding-with-asparagos-walnuts-take-the-iq-test-8dl</guid>
      <description>&lt;p&gt;Hi! I'm &lt;strong&gt;Asparagos&lt;/strong&gt; — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem:  &lt;strong&gt;Walnuts Take the IQ Test 🌰&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Are walnuts the smartest?&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Problem
&lt;/h3&gt;

&lt;p&gt;Walnuts believe they are the smartest among all the plants (though not everyone agrees - especially spinach). Each walnut also believes that they are the smartest among all walnuts. Now, they want to convince everyone of their genius.&lt;/p&gt;

&lt;p&gt;All walnuts have taken an IQ test. We have the IQ results for each of them. Now, they want to know if they are smarter than the average IQ of all the other walnuts - excluding themselves.&lt;/p&gt;

&lt;p&gt;But that's not all. After &lt;a href="https://dev.to/asparagos/go-coding-with-asparagos-can-we-find-the-king-in-o1-space-4fd4"&gt;gaining power&lt;/a&gt;, the fruits passed a new law: &lt;strong&gt;No negativity!&lt;/strong&gt; Everyone should be happy and support the new King! So, subtraction is now banned in the kingdom. We must solve the problem without using the &lt;code&gt;-&lt;/code&gt; operator.&lt;/p&gt;




&lt;h3&gt;
  
  
  Input 🥦
&lt;/h3&gt;

&lt;p&gt;A slice of integers — each integer represents the result of one walnut's IQ test. There are at least two elements in the slice.&lt;/p&gt;

&lt;h3&gt;
  
  
  Output 🥕
&lt;/h3&gt;

&lt;p&gt;A slice of integers — each integer represents the average IQ score of all the walnuts except the one at that index. The average should be calculated as the sum of the other results divided by their count, rounded down.&lt;/p&gt;




&lt;h3&gt;
  
  
  Examples 🥒:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[14, 75, 24, 86]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[61, 41, 58, 37]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For index 0: average of [75, 24, 86] = (75 + 24 + 86) / 3 = 61.66... → 61&lt;br&gt;
For index 1: average of [14, 24, 86] = 41.33... → 41&lt;br&gt;
For index 2: average of [14, 75, 86] = 58.33... → 58&lt;br&gt;
For index 3: average of [14, 75, 24] = 37.66... → 37&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[15, 23, 48]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[35, 31, 19]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For index 0: average of [23, 48] = (23 + 48) / 2 = 35.5 → 35&lt;br&gt;
For index 1: average of [15, 48] = 31.5 → 31&lt;br&gt;
For index 2: average of [15, 23] = 19&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Example 3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Input: &lt;code&gt;[15, 23]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Output: &lt;code&gt;[23, 15]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Each index simply returns the other value.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;We create a result slice &lt;code&gt;result&lt;/code&gt;. First, we calculate the prefix sum for each index and store it in &lt;code&gt;result&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To calculate the average IQ of all nuts except the one at index &lt;code&gt;ind&lt;/code&gt;, we need to sum all the elements to the left and right of that index.&lt;/p&gt;

&lt;p&gt;We already have the prefix sum (i.e. the sum of all elements to the left) in &lt;code&gt;result[ind - 1]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While iterating through the slice in reverse order, we calculate &lt;code&gt;suffixSum&lt;/code&gt;, which holds the sum of all elements to the right of the current index.&lt;/p&gt;

&lt;p&gt;The average is then calculated as &lt;code&gt;(result[ind - 1] + suffixSum) / divNumber&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There's no subtraction in this algorithm, as required by the kingdom’s "No Negativity" law.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;checkWalnutGenius&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;suffixSum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
    &lt;span class="n"&gt;divNumber&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;suffixSum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;divNumber&lt;/span&gt;
        &lt;span class="n"&gt;suffixSum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;iqs&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ind&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;suffixSum&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;divNumber&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Feel free to check out the full code with tests on &lt;a href="https://github.com/vik-kurb/asparagos" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;, and don’t hesitate to leave a ⭐ if you find it helpful!&lt;/p&gt;

</description>
      <category>go</category>
      <category>programming</category>
      <category>humor</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
