Polyglot Programming: TypeScript, Go, & Rust

Count Trees Typescript Solution



Polyglot Programming: TypeScript, Go, & Rust

Check out a free preview of the full Polyglot Programming: TypeScript, Go, & Rust course

The "Count Trees Typescript Solution" Lesson is part of the full, Polyglot Programming: TypeScript, Go, & Rust course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen walks through the TypeScript solution to the count trees problem.


Transcript from the "Count Trees Typescript Solution" Lesson

>> So let's copy and paste this and let's do this first in TypeScript. Now I did add this question at the very, very, very end, and so let's see if I successfully do it first try. I assume I'll do this first try, if I have a mod in the chat you can even do a bet, will I get this?

First try, I will not be upset. All right, so let's do a quick function getInput, string, return, grave, paste, there we go. Yes, I do not, okay, so fun, little side note, I do not do this. Okay, you're just not gonna see me do that, I'm sorry. I know there's some people out there like, well, you should man.

First off, you're not actually technically saving any amount of space here, right? It's pretty much the same, but there's lexical binding to this and I don't need this, so why would I include a feature I'm not gonna use when writing? Okay, maybe that's a little too pedantic for most people writing JavaScript, for me personally, if I'm not gonna use this, I don't want to include it.

Okay, anyways, weird conversation, okay, we can go on. So how should we go about doing this? Does anyone have any ideas that they think would be a good idea here? Cuz I have some pretty fun tricks, but it would be kind of good to hear what you think.

Should we create any new structs, should we create any enums? How should we do this?
>> At least an enum for the tree and snow.
>> Okay, let's do that right now. I don't know what, let's see. Thing, we'll just call a thing, cuz honestly, who knows what this thing's called, a place, right?

There we go, we'll call it a thing, make it easy, right? There we go, we have Tree and Snow, that makes sense. So that means if we're gonna do that, we're gonna have to first parse the string, right? So getInput, and let's split as we always do on the new line, correct?

Everyone's kinda with me there, and then now I need to do something a little bit different. I need to do a map, And again, split on the empty string, this will just simply blow our string into individual characters, kind of a nice little trick to do right there.

And then we can map that and we can say if x equals that return a Thing Snow, else Thing Tree. Is that about looking good, const things? There we go, so now we have this double thing array, right? And so now we really need to take this and actually start from 00, and walk our way through and make sure that we're always wrapping.

And so wrapping of course, whenever you hear this, you should always think modulo. I know this is not a course on interview questions, but it's actually a really good thing to always remember, always use modulo when this happens. So that means we need to find out first, what is the length of our columns, right?

How many columns do we have? It is a square, so I can do something like this, collen = things(0).length, right? That is really the length of our columns, correct? Awesome. After that, we can start walking through this and count how many trees we hit. So I'm gonna go like this, treeCount = 0, and now we can do a nice, let's just use a delicious closure to capture this.

And so we can go things, (, that's terrible name, thingRow, this of course is a list of this, and one kind of neat aspect about this is that what information do we know? We know that every single time we go down, we also need to go three over.

So let's do a little fun trick here. Let's go like this, let's also include our position, right? Cuz this is our row that we're currently at, which means that we can do something like this. thingRow(i * 3 % collen), right? So starting at the 0 throw, i * 3 is still 0.

At the first row, we're over 3, at the second row we're over 6, at the third row we're over 9. And you can see how that works, and then we're just always wrapping around. So we're always just walking to the side and going through them, all right? Awesome, and now we can go like this.

If this thing equals Thing.Tree, well, we can go treeCount++; and of course, I'm on that semicolon camp, ("TreeCount", treeCount), boom, there we go. So we have that right there, we've walked through, we've grabbed them, did the sweet modulo right there, everything seems about correct. If you're worried about order of operations, you can always look up how it executes, or you can just do this just to guarantee it in your head.

If it's easier to read you can do that. But it should have the same precedent as multiplication so that should come first. And now if we do, do an MPX TS node part three, it should show us hopefully 7, yep, it shows us 7. Okay, so that's how many trees we should hit is 7.

So there we go, we've done it, we've correctly done this. We don't actually technically have to do an enum if you really think about it, we actually only need to go over each line. Since we already have this, we can grab the string character at that position. And then from there we can just do the little modulo math, if it's a pound sign count them all together, we're good to go.

And so it's kind of a little clever way we could do this. Anyways, just thought I'd throw that out there, sounds like a little fun thing, I don't know, I think it's kinda fun, I don't know.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now