The Climax - #Day 10
The past few days of this challenge has been a wild journey which I could say that I am somewhat thankful for. First of all, I got to explore concepts that I have never used before like threading and bit manipulation. And also, I am not as fazed or easily dispossessed by the challenges as I once was.
Putting in mind that these past 9 days have been my first experience with algorithmic problems, I would say that this challenge was a success.
Let's see what today has in store for us, shall we?
The Challenge
Honestly, I was a little bit concerned with this question when I saw the significant number of dislikes and rejected submissions on this challenge.
Almost twice the number of dislikes!!!! About 5 times the number of rejected posts for the number of accepted ones
This troubling numbers made me very cautious going into the problem at hand.
Approach 1 - Inbuilt function
The first thing I did was to first research possible test cases that could fail the test like inf
, e9
, 74e34e21
, --3
, 99e2.5
e.t.c. The reason I did this was because I had come to notice that in the challenges (especially the hard ones), the example test cases are sometimes inadequate to test the efficiency of the program.
From there I came up with an algorithm that was so simple that I doubted whether it worked, making me add more test cases. The algorithm was:
- Convert the string to a number
- Check whether the conversion as a number is equivalent to its numeric meaning as a string
- Return
true
orfalse
based on what is gotten.
I initially used the intval()
function to perform the conversion but I realized that it didn't work when I tested it out. Upon more thought, I came up with an even simpler algorithm
- Check whether the string is valid numerically
- Return
true
orfalse
based on the result
This was easily done using the is_numeric()
function and out of fear, I used even more test cases, as my code was far too short (putting in mind this was tagged as hard) and I submitted the code.
Approach 2 - Regular Expressions
Honestly, I could have easily ended it there since the code was working but I felt that doing that was bypassing the purpose of the challenge. Hence, I decided to change my approach. I went back to the challenge prompt.
Looking closely at the question you notice the words "in order" for what makes the number a valid number. This led to the question, how do you confirm an order or pattern in a string. The answer - Regular Expressions
Regular Expressions or Regex (for short) are basically sequence(s) of characters which are used to define a pattern that can be used to search through a string.
Using this, I created a particular pattern that followed a certain order as follows:
- The string starts with a single
+
or-
. Basically, if the sign occurs more than once, the string will not match the pattern. - One or more digits starting the string OR one or more digits after the sign. OR string starts with a decimal point.
- Decimal point after the digits OR digits after the decimal point.
- The symbol
e
orE
after the digits. - Digits after the symbol ending the string.
After creating a regex in that order, using the preg_match()
function, I confirmed whether the string matches the defined pattern and return true
or false
based on the pattern.
I tested it with the test cases I made and then submitted.
Subscribe to my newsletter
Read articles from Daniel Abayomi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by