So...here's what happened:
I was busy on father's day ironing a shirt that I would wear
for the later scene in this sketch but then I realized over my shoulder a
Craigslist ad it was in the wild so I had to go and get a closer look oh my
god wait there's more
wait
but how can anyone read that? it's both numbers and the written numbers
I knew what I must do. Hey Data Leapers two minutes of Python a day can make you
an amazing data scientist or an engineer and today we have a practical problem my
dog needs that dog hoodie in order to become a tech boy but that Craigslist
seller they put their number both in written number English and the numbers
themselves I guess to avoid BOTS being able to automatically detect and call
them with weird crank calls or something but I can fix this by writing a bot I'll
write some script in Python in order to be able to read that number into purely
numerix we'll do this in four ways one we'll do it with vanilla string
concatenation then we'll do it with a built-in Python phone we'll do this in
four ways one we'll do it with vanilla string concatenation then we'll do it
with a built-in Python function called dot replace third we'll be able to do
this with a list comprehension and finally we'll do this with recursion to
make sure they only have two minutes per method I'll be having a countdown for
each of the methods right there in the corner and please make sure to tell me
which of these methods you think is the most useful in the comments below
finally if any of this is useful to you please hit subscribe for more weekly
content just like this where I'll be outlining more tips and tricks about how
Python makes the world a little smaller and puts hoodies on dogs well let's get
started all right let's start that timer no time to waste let's start with our
phone number put into a string a variable called tricky phone number what
we need is a method function built into Python called dot split this is gonna
take into a string and then turn it out into a list broken up by space an empty
space right there luckily we have that exact same two limiter so if we apply
this function to the tricky phone number variable then we'll be able to output
list of each element both the numbers in written English and the numerix
themselves let's start off with a new variable called output it's gonna be an
empty string and we're going to slowly concatenate each number inside tricky
phone number what we're gonna do is want to split it first and then start a for
loop and test with a logic gate whether or not the number is digit this is
another built-in Python function that allows us to test whether or not this is
a not alphanumeric let me just change that if it's a purely numeric variable
perfect so that means it's just ones twos and threes and not the written one
two three all right on to the concatenation we concatenate the number
if it already is a numeric but if it isn't we need some help let me summon
the power of dictionaries okay so this is a dictionary that map's the written
out number in English to the numeric it's tough we bring the number into the
dictionary and right here I made a mistake it needs to be lower because
some of the numbers have capital letters in there and there you go all within two
minutes that's our first method now we start the timer or good you're probably
asking how the second method is able to change a variable in place well this
time we're not concatenate again strings we're going to be using the dot replace
function so the same for loop is gonna be built here and this time we're
checking if it's or not a digit here we just add not in front of the if
statement this way we'll be able to take the tricky phone number and and assign
it to another tricky phone number this time we're using the dot replace
function now the down replace takes any input of something that's trying to find
and then it also takes in what you want to replace it with this time we're
replacing the number and we're replacing it with the dictionary comes in again
with whatever that number value should be in numerics
and now let's see if that worked bingo bunker that one was pretty fast John for
the third method new timer it is a wise man once said we have to put together
what we've once broke apart if dot split changes a string into a list a dot join
changes a list back into a string the empty string becomes the glue that puts
together a list now what kind of list shall we put in here let's just split it
apart to see what it does it splits upon the space and then uses an empty string
in order to glue it back together this is the same input but without all those
nasty spaces getting in the way well this is where a list comprehension
comes in you see if we do number for a number in shrinky phone number dot split
this isn't going to change much in fact it's gonna be the exact same output what
we actually need is to change a few things depending on whether or not the
number is it numeric or it's not so let's look at if the number is digit and
if it isn't a digit once again the dictionary comes in put that number
remember to lower put it into the dictionary as a key and out comes an
error I forgot to define the dictionary of course rookie mistake but there it is
that's our third method of getting that dog hoodie bring ups the final time if
know time is short I think it's worthwhile to go over the theory of
recursion recursion is an algorithm that has a series of base conditions so maybe
one here and maybe one here but it bounces off the idea that sometimes
there's an alternate condition that could be met and this is the way that
we'll be able to clean this numeric let's define a function this is our
first function of the day hoora when we define this function we start with the
base condition let's start with the idea that what if the input number is a
single length number that four it must be a numeric
already if that's the case then just like the previous examples we just want
to return the number itself now there is a edge case where the input number could
actually be a list of a single string in that case we just need to make sure if
it's a type string if it's not then index the first element there's only one
element after all if it is just a string then we take it by itself next the
second base condition is whether it is something that can be put into the
dictionary if we test for the type of just it's a string therefore it's not a
list of strings we could then put it into our dictionary don't forget to
lower it finally we have our alternate condition this alternate condition just
puts it off like a procrastinator function to itself again that's the
recursive part it's going to take the very first element of this list doesn't
matter what it is because one of the two top basis conditions will take care of
it then it's going to concatenate it to its rest of the list starting from the
first index onward let's see if this works
don't forget display ah very nice now that I know how to dial this phone
number many not for BOTS I should be able to get that dog hoodie in no time
here I go meme inserted into the dialer Tron hi is this Craig Craigslist I'm
here for the dog hoodie am i a bot no I'm not a bot I called your phone number
like a human can I come inside oh I'm outside your house yeah oh I traced your
number with Python anyway can I can I come inside for the dog hoodie oh so I
supposed to bring the dog yeah okay bring it bring it over and I'll give it
to you okay I'll be back in a second
once again the day is saved by two minutes of Python thank you so much for
making it all the way to the end card I will have plenty of content up for you
this is my first video I really wanted to always make a youtube channel and now
I know that this is the kind of content that I personally would be interested in
but now I'm no longer an audience of just one I have all of you guys to tell
me what you might be interested in learning what kind of skits or funny
ways I can explain data science and how exactly I did a scientist in Silicon
Valley does his job or learns more from the day to day just drop a comment down
below please hit that subscribe button for a lot of other content just like
this coming up real soon I'm gonna try my best to make sure that I have more
than one video a week but definitely you will expect one video every Monday see
you guys again peace