# Eternity Async Tutoring

**URL:** <https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932>\
**Category:** Tutoring\
**Created:** [April 17, 2024, 11:22pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932 "2024-04-17T23:22:52Z")\
**Posts on this page:** 20\
**Page:** 38

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 20, 2025, 1:41am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/742 "2025-01-20T01:41:12Z")

</div>

> [@Elliot](#):
>
> To simplify, let’s start with only the cards from 2-9, so it’ll be a 32 card deck.
> 
> Breaking the problem down into smaller parts can help a lot. For example, you might want to start with just one suit before adding the other suits.

Why not 2-10 (when doing this I just assumed 2-10, then later going to post I saw it was 2-9)? Maybe two digit numbers have some issues? Regardless here’s what I’ve come up with:

```auto
cards = Array.new

n = 2
until n == 38 do
    if n<11
  cards[n]=["#{n}H"]
    n=n+1
    elsif n>=11 and n<20
        cards[n]=["#{n-9}S"]
    n=n+1
    elsif n>=20 and n<29
        cards[n]=["#{n-18}C"]
    n=n+1
    elsif n>=29 and n<38
        cards[n]=["#{n-27}D"]
    n=n+1
end
end

```

Is this fine?

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 20, 2025, 2:06am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/743 "2025-01-20T02:06:57Z")

</div>

That’s great! It runs. The output is approximately correct (not exactly).

There are often many ways to write code to do something. Some are considered better because they’re simpler or easier to read or for other reasons. Let’s try some revisions.

To begin with, you have `n=n+1` 4 times. Can you figure out a change so that you only have that line of code once?

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 20, 2025, 2:28am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/744 "2025-01-20T02:28:39Z")

</div>

> [@Elliot](#):
>
> Can you figure out a change so that you only have that line of code once?

This seemed to work:

```auto
cards = Array.new

n = 1
until n == 38 do
    n=n+1
    if n<11
  cards[n]=["#{n}H"]

    elsif n>=11 and n<20
        cards[n]=["#{n-9}S"]

    elsif n>=20 and n<29
        cards[n]=["#{n-18}C"]

    elsif n>=29 and n<38
        cards[n]=["#{n-27}D"]

end
end

```

it loops through everything each time, right? so i thought to put n=n+1 at the top. it does that every time then goes through the rest of the stuff below it. started at n=1, instead of n=2 so that way it can start off with 2 going into the arrays.

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 20, 2025, 2:36am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/745 "2025-01-20T02:36:03Z")

</div>

great.

next, the “end” that ends the “if” statement should be indented the same amount as the “if” so it’s easier to figure out where the “if” ends.

then, cards ends up as an array containing arrays not strings. try running these to see the results and try to adjust your code so they all output String.

```auto
cards.first.class
cards[20].class
cards.last.class

```

> [@Eternity](#):
>
> it loops through everything each time, right? so i thought to put n=n+1 at the top. it does that every time then goes through the rest of the stuff below it.

Yes, the loop applies to the block enclosed by “do” and “end”. (It’s the second “end” that ends the loop. The first “end” ends the “if” statement which gets ended first because it was more recent. Matching do and end basically works the same as figuring out which start and end parentheses match each other.)

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 20, 2025, 2:58am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/746 "2025-01-20T02:58:45Z")

</div>

> [@Elliot](#):
>
> then, cards ends up as an array containing arrays not strings. try running these to see the results and try to adjust your code so they all output String.

Yea I see what you mean. When I first ran those I got that cards are arrays. I think when I started this I was worried that I wouldn’t have the arrays containing strings. I forgot that cards[n] = “string” would mean an array with a string in it. I thought I had to put [] around the string. So here’s the most recent update again:

```auto
cards = Array.new

n = 0
until n == 38 do
    if n<9
        cards[n]="#{n+2}H"  
    elsif n>=9 and n<18
        cards[n]="#{n-7}S"

    elsif n>=18 and n<27
        cards[n]="#{n-16}C"

    elsif n>=27 and n<36
        cards[n]="#{n-25}D"

    end
    n=n+1
end

```

This time I put n=n+1 after the if stuff at the bottom so I could make starting from 0 (the first item in an array) easier for me. When I did cards.first.class it turned up nil. I assume thats because I was skipping that in my original code. I remember the video I watched on arrays just telling me that ruby would just leave stuff blank in those cases.

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 20, 2025, 3:03am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/747 "2025-01-20T03:03:02Z")

</div>

Great. OK next, you don’t need the cards to be at specific places in the deck, so just adding each new card to the end of the deck would make sense instead of specifying their position in the deck. Can you find a way to do that?

Hint: [class Array - RDoc Documentation](https://ruby-doc.org/3.4.1/Array.html)

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 20, 2025, 6:31pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/748 "2025-01-20T18:31:17Z")

</div>

> [@Elliot](#):
>
> Great. OK next, you don’t need the cards to be at specific places in the deck, so just adding each new card to the end of the deck would make sense instead of specifying their position in the deck. Can you find a way to do that?

This?:

```auto
cards = Array.new

n = 0
until n == 38 do
  if n<9
    cards << "#{n+2}H"
  elsif n>=9 and n<18
    cards << "#{n-7}S"

  elsif n>=18 and n<27
    cards << "#{n-16}C"

  elsif n>=27 and n<36
    cards << "#{n-25}D"

  end
  n=n+1
end

```

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 20, 2025, 7:05pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/749 "2025-01-20T19:05:55Z")

</div>

yes.

there are many different ways to do loops in ruby. a nicer one in this case would be `upto`. look it up in the documentation and change your code to use it instead of `until`.

hint: first, make an `upto` loop that prints the numbers from 3 to 6. get that smaller thing working before trying to change your code.

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 20, 2025, 7:33pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/750 "2025-01-20T19:33:50Z")

</div>

This seems to work:

```auto
cards = []

0.upto(38) {|n|
                  if n<9
                   cards << "#{n+2}H"
                 elsif n>=9 and n<18
                   cards << "#{n-7}S"
                 elsif n>=18 and n<27
                   cards << "#{n-16}C"
                 elsif n>=27 and n<36
                   cards << "#{n-25}D"
                  end
                  }

```

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 20, 2025, 7:37pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/751 "2025-01-20T19:37:12Z")

</div>

yes. fyi do/end and {} are interchangeable. it’s common that people use {} for one liners and do/end for multi-line blocks.

next, you could simplify the if conditions. you could get it working using fewer conditions. see if you can figure out how.

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 20, 2025, 8:08pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/752 "2025-01-20T20:08:45Z")

</div>

`[]` is more commonly used than `Array.new` so you’ll usually see it in example code, maybe because it’s shorter. But there’s nothing conceptually wrong with `Array.new`. It’s arguably clearer.

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 21, 2025, 2:34am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/753 "2025-01-21T02:34:34Z")

</div>

> [@Elliot](#):
>
> next, you could simplify the if conditions. you could get it working using fewer conditions. see if you can figure out how.

Hmm. The order doesn’t matter right? So:

```auto
cards = Array.new

0.upto(8) do|n|

		cards << "#{n+2}H"
		cards << "#{n+2}S"
		cards << "#{n+2}D"
		cards << "#{n+2}C"
end

```

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 21, 2025, 3:15pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/754 "2025-01-21T15:15:04Z")

</div>

Good. For `n+2` can you figure out a way to change it to `n` instead?

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 21, 2025, 4:52pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/755 "2025-01-21T16:52:41Z")

</div>

> [@Elliot](#):
>
> Good. For `n+2` can you figure out a way to change it to `n` instead?

```auto
cards = Array.new

2.upto(10) do|n|
		cards << "#{n}H"
		cards << "#{n}S"
		cards << "#{n}D"
		cards << "#{n}C"
end

```

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 21, 2025, 5:01pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/756 "2025-01-21T17:01:24Z")

</div>

ok next, cards should be length 2. so it’s TH not 10H. fix the tens cards.

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 21, 2025, 5:15pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/757 "2025-01-21T17:15:30Z")

</div>

```auto
cards = Array.new

2.upto(10) do|n|
	if n < 10
		cards << "#{n}H"
		cards << "#{n}S"
		cards << "#{n}D"
		cards << "#{n}C"
	elsif n==10
		n = "T"
		cards << "#{n}H"
		cards << "#{n}S"
		cards << "#{n}D"
		cards << "#{n}C"

end

end

```

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 21, 2025, 5:28pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/758 "2025-01-21T17:28:15Z")

</div>

Great that you got it working. Can you come up with any shorter or more elegant ways to do it?

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 21, 2025, 5:32pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/759 "2025-01-21T17:32:23Z")

</div>

> [@Elliot](#):
>
> Great that you got it working. Can you come up with any shorter or more elegant ways to do it?

No nothing comes to mind. I say that because last night I tried thinking of ways to get Ace. Jack, Queen, and King to work and I could only come up with this “if” statement method. Well. Kinda. I saw stuff about “case” and “when” and did something with that, but I don’t know if that is really more elegant (definitely not shorter).

---

<div class="post-metadata">

**Author:** ![Elliot](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/elliot/32/14_2.png) [@Elliot](https://discuss.criticalfallibilism.com/u/Elliot)\
**Post date:** [January 21, 2025, 6:42pm UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/760 "2025-01-21T18:42:22Z")

</div>

Can you find a way to have this section only once, not repeat it?

```auto
		cards << "#{n}H"
		cards << "#{n}S"
		cards << "#{n}D"
		cards << "#{n}C"

```

---

<div class="post-metadata">

**Author:** ![Eternity](https://discuss.criticalfallibilism.com/user_avatar/discuss.criticalfallibilism.com/eternity/32/5930_2.png) [@Eternity](https://discuss.criticalfallibilism.com/u/Eternity)\
**Post date:** [January 22, 2025, 3:59am UTC](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932/761 "2025-01-22T03:59:53Z")

</div>

> [@Elliot](#):
>
> Can you find a way to have this section only once, not repeat it?

Spent a bit of time on it before going to work and spent a bit more time after getting back, and nothing comes to mind. Any hints? I do have time to try tomorrow/will be trying tomorrow again if you wish to refrain from giving any hints for now, but yeah.

[Previous page](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932.md?page=37)

[Next page](https://discuss.criticalfallibilism.com/t/eternity-async-tutoring/1932.md?page=39)
