Created
June 17, 2021 11:14
-
-
Save mei-li/383243d3bbf313466c5aded33d82ced7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Filter a list is JS is an attribute of an array (array.filter) and takes one argument, a function to apply | |
to each list element if the return value of the function is True then the element is in the result list, otherwise | |
it is discarded | |
Example for filter | |
arr = ['some text', 'a', 'some more text', 'the', 'I go to play football'] | |
Let's say we want to filter out, stopwords (words with 3 or less characters). So single word entries with 3 or less characters. | |
So for each element of the list the check to apply would be something like: there are only letters (no spaces or other characters) | |
and the lenght is less or equal to 3 characters, then the result should be False to be fitered out. | |
Less google how to check for alphanumeric is JS. Googling: "is alphanumber JS" first reult | |
https://stackoverflow.com/questions/4434076/best-way-to-alphanumeric-check-in-javascript | |
so | |
input_string.match(/^[0-9a-z]+$/) | |
ISAPLHA AND LESSOR3 -> skip this item | |
So take the item is the reverse | |
NOT (ISAPLHA AND LESSOR3) | |
which can be also written as | |
NOT ISALPHA OR NOT LESSOR3 (this is called Morgan law and it is standard in boolean - https://en.wikipedia.org/wiki/De_Morgan%27s_laws - that is the most relevant video I found - https://www.youtube.com/watch?v=AGyjo2DLxjM) | |
NOT ISALPHA OR GREATER3 | |
so in code | |
arr.filter(x => !x.match(/^[0-9a-z]+$/) || x.length > 3) | |
If the morgan law and in general boolean is not clear. I am happy to prepare a session to clarify them hopefully once and for all. | |
Exercises | |
A. With 2 lists/arrays. One with places one as visited and list/array of Greek islands, | |
find the greek islands one has visited. | |
visited = ['Dublin', 'Athens', 'Mykonos', 'Samos', 'Berlin', 'Rodos', 'Naxos', 'Patra', 'Corfu'] | |
islands = ['Corfu', 'Ithaki', 'Crete', 'Samos', 'Samothraki', 'Naxos', 'Paros', 'Sifnos', 'Mykonos', 'Rodos', 'Tilos'] | |
B. You have a list of possible plans for today. | |
plans = [['coffee', 'wash bathroom', 'podcast', 'repair bike'], ['repair bike', 'podcast', 'study filter'], ['coffee', 'wash bathroom'], ['study filter', 'lunch with friend'], ['coffee', 'lunch with friend', 'podcast', 'study filter'], ['take day off']] | |
you are too busy, you can maximum do 2 things today. Filter the list to return only plans with maximum 2 things to do. | |
C. You have a list of texts and a list of names. You want to have only the texts that mention some name from the list. | |
texts = ['I spend some time playing with Maria', 'Why are we here?', 'That is not Berlin', 'We are on earth'] | |
names=['Berlin', 'Maria', 'London'] | |
you might want to make a helper function to use in the filter parameter. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The video on De Morgan's Law is really nice.
This is a nice resource to practice Boolean logic. I did these exercises when I was working with Leipzig.