### Learn With Me | Intro To Elixir Lesson Eleven - The |> Operator Homework

Elixir
##### Transcript

English (Auto-generated)

And welcome back to my learn with me series I just did lesson 11, which is the lesson on the pipe operator and um now I'm gonna go over the homework that he gave us. So the first one is given a map of scores such as this. Return the highest score. All right. So he does say that I recommend you use all the things we learned in previous lessons to accomplish this, including the pipe operator, pattern matching um in a map, string libraries, etcetera, modules and functions and anonymous functions. Alright, so for this one, I'm just gonna do it plain. I don't think I'm gonna bother writing a function for this one. Um So let's assign it to a variable and let's say scores is equal to and we will take this variable here. Perfect. Um And then what do we need to do? Return the highest score. All right, let me say that for you guys. Let's look at our map library. Okay, So the first thing we need to do is to make sure we're just getting these numbers here. So let's see if we can figure out a values function value. That's just values. Map returns all values from the map. Perfect. So we'll go ahead and do the new scores. It's equal to Yes. Yeah. Mhm. Of course, we will use the pipe operator and we'll do map dot values and let's inspect that. Yeah. Yeah, the new scores. Okay, let's open the terminal here and then we'll do a little homework. Oh, this is the issue with the char lists like last time. So let's do. so instead of this will do they don't inspect. And then we'll do char lists uh came up are we came across this issue in a previous lesson. I remember which one it was about how um lists of integers will return or just um char lists. And so they'll print as the characters as opposed to printing as a list of integers. So that should fix it. Yes, of course the variables unused. So we don't need that. Perfect. So we got that one there. And so now what we need to do is return the highest score. So we want some sort of max function if we can um Which let's check out the library for that. See here. Mm hmm. Max. And perfect. It takes it a list and so we can add in the in um rock max And then you should get 96. Beautiful. Um There's probably more concise way to do this. Um but I'm just gonna stick with doing it for now. So we can really see the using all the operators here. Alright, so number two already function of formatting strings. By doing the following, remove trailing ending spaces, replaces multiple spaces with a single space, capitalizes the first letter of the string and adds a period at the end. If there's no period. Okay. That's a lot of things. So let's start by writing a module here. So we'll do a death module and we'll call it formatting. Um And let's say def strings. Mm hmm. Uh what's called? Format will love this evening. But Oh well. Alright. So the first thing we're gonna want to do is remove trailing and end spaces there. Um I know that there is a trim function here so we should just be able to just string trim. Um but what we really want to do is to take the string and then use the pipe operator to push it over to that. Um And then down here we will do I. O dot expect. And let's make a string that has a bunch of the conditional zones here. So we'll say formatting dot format Australia is going to say hello? A bunch of spaces. How are you? Don't do that? This one should trim the ends. Maybe we'll put a couple of the beginning to the trailing spaces on both sides. So. Perfect. That worked. Alright. Um the next thing is replaced multiple spaces with a single space. Alright, so the easiest way to do this would be to just grab the values like the words. So let's do split string dot split. Yeah. Perfect. Now we have it in lift there. Um that's A and B. Done capitalize the first letter of the string. Okay, so we need to make it a string again, miss that step. Is there a joint function in strings. Okay. No, so let's check out in um alphabet join. Perfect. So we will do, you know, don't join and we're going to join it for every we want a space between every word. That should work. Yes. Hello? How are you? Perfect. All right now we want to capitalize the first letter of the string. Um which is gonna be jesus capitalize, I'm pretty sure that's just the first one. Yes. Simple to string. Mhm creator here. Perfect. Let's check this again. Awesome. And then now the last thing we want to do is add a period at the end if there's no period. So what we wanna do is check first of all, there's a period And if there isn't, we want to add one. So I'm going to assign this to a new variable that is new. String is equal. Tales look in a format that's a little bit nicer so that it's not so awful to look at and then we'll give you an if statement here. So we'll say if hmm we need to go to my strings here. Uh huh mm ends with hmm I wonder if it'll pick up the period separately though. Let's find out. So string that ends with Mhm And then we'll put in it's gonna be the string right scroll down string suffix. Okay, so a new string and then it's gonna be a period. Mhm. You Hello? So right now we should not get anything different. Yeah. No, because we're not we're not returning anything. Um let's add in a period here. Perfect. So it is picking up just that period. Um We should be able to add the period anywhere and still haven't worked. Yeah. Perfect. So then otherwise do nothing. Um I don't know how to do like this. Not yet. I don't like getting jobs or do it in the bank but I don't know how to do that. So I'm just gonna check for it and then if it doesn't have it, I'm gonna add it on. So now we need to append period. Um let me see here sort of pent. Now replace suffix. Okay, so we'll do spring dot replace suffix. I'm gonna assume it doesn't have a suffix and we'll be replacing it with a period. Let's get rid of this. This cervix police or folks is undefined or private. Oh, I need to add the new strength. Hello, How are you? Perfect. That should meet all of our criteria here, remove trailing and spaces, replace multiple spaces with single space, capitalize the first letter of the string, add period at the end, awesome and then write a function that finds the most recurring letter in a given string. Alright, so let's move this up but we're going to have the same module. Um So let's find a new function which we'll call it um recurring and we're gonna be taking in straight in here. And what do you want to do recurring letter? So let's split up all the letters. Yeah, I think we're gonna split up all the letters into a. Okay so we will do string. Mhm. And got split and we'll split it just between the characters. Um and then we'll bring in new. I know that inspect formatting got occurring. And let's use these greetings. So we should get to ease. Will be Yeah, that should work. Okay, so here we should have grading split individually into a string. Alright, so we're getting the empty spaces here. So these are the options for our split string. Split pattern options of any true. Everything's removed from the resulting list. Perfect. So tram trip that should fix the extras greetings. Perfect. Alright. Now we're gonna want to check how many of each one occur. Um Let's go to the library and see if there's anything weakened. The size of the innumerable frequencies turns a map with key is unique elements of innumerable values as the count of five elements And three. Oh perfect. Okay. So let's do nina dot frequencies. You to G. two. Okay, so let's use a different word that only has one. We'll just use low, it only has one recurring just for the sake of this. Um E one H 1 l. 201. Alright, so now we need to take this and find the one that has the most. Let's see if we can find, let's make it a list. So it's grouped into two balls in the list. Let me see here. So let's see I do converts innumerable to list. Let's just try this and see how it goes. So, You know, I've got two lists. We get pupils here. So we want to return the tuple. Now, that is the max. So back to that max function. Let's see what we will do you? No, I'm not max. Okay, so this is returning the largest letter. We need the largest number and number turned the letter so that's not gonna work. What's max by return the maximum element of the number is calculated by. Oh, so we can turn an anonymous function in there. All right. So let's try this one. So let's say in a max by and then we'll give it anonymous function. What do we want to do? We wanted to look at tuple, wanted to ignore the first element and look at the second and then return the second. Eight. Look at the tuple or the first element. Look at the second and return the largest of the second. I'm pretty sure that's L. Two perfection. Now, if we did why? Um, so change it. Yeah. So that changes there. I hope the fact that we're doing this and then we will do. So let's find the ability to find index fetch. So free, let's do you know, I'm not fetch. Maybe we can usually pattern matching for this. So, well, let's try because we don't usually pattern matching yet. So let's say new string is equal to string If we did A. B is equal to new string and then return a. You can tell. Perfect. So we can do pattern matching. I think we can also, instead of doing the pattern matching there just still um we can do an enema dot fetch then dec zero, not for type of tuple. Um mm hmm. So let's try. Okay. Okay, mm hmm. Mhm. Alright. I think we're just going to stick to doing it to the pattern matching because it's a little more tricky to grab a specific value with. And I think we hit all of them pipe operator pattern matching. All the libraries, modules and functions and anonymous functions. Perfect. So that is working through that homework um and stay tuned for some more lessons.
279 Views 0 Likes 1 Comments

Let's go over the homework for the |> operator tutorial, where we combine a lot of what we've learned already!

##### Comment
@amy 10 months ago

So I realized that I should have avoided using `if` as we haven’t touched on it yet, and it’s also good practice in Elixir.

I have a new solution that where I avoid doing that:

`` defmodule Formatting do``

def format(string) do

``````new_string =
string
|> String.trim()
|> String.split()
|> Enum.join(" ")
|> String.capitalize()

ends_with(new_string, String.last(new_string))``````

end

def ends_with(string, last) when last == “.”, do: string

def ends_with(string, _last), do: String.replace_suffix(string, “”, “.”)

# 3. Write a function that finds the most recurring letter in a given in a string.

def recurring(string) do

``````new_string =
string
|> String.split("", trim: true)
|> Enum.frequencies()
|> Enum.to_list()
|> Enum.max_by(fn {_, x} -> x end)

{a, _b} = new_string
a``````

end end

@amy 10 months ago

That formatted very terribly… let’s try again!

`````` defmodule Formatting do
def format(string) do
new_string =
string
|> String.trim()
|> String.split()
|> Enum.join(" ")
|> String.capitalize()

ends_with(new_string, String.last(new_string))
end

def ends_with(string, last) when last == ".", do: string

def ends_with(string, _last), do: String.replace_suffix(string, "", ".")

def recurring(string) do
new_string =
string
|> String.split("", trim: true)
|> Enum.frequencies()
|> Enum.to_list()
|> Enum.max_by(fn {_, x} -> x end)

{a, _b} = new_string
a``````

end end