The Climax - #Day 10

Daniel AbayomiDaniel Abayomi
3 min read

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

10_question.png Honestly, I was a little bit concerned with this question when I saw the significant number of dislikes and rejected submissions on this challenge.

10_stats_2.png Almost twice the number of dislikes!!!! 10_stats.png 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:

  1. Convert the string to a number
  2. Check whether the conversion as a number is equivalent to its numeric meaning as a string
  3. Return true or false 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

  1. Check whether the string is valid numerically
  2. Return true or false 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.

10_accepted_1.png

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:

  1. The string starts with a single + or -. Basically, if the sign occurs more than once, the string will not match the pattern.
  2. One or more digits starting the string OR one or more digits after the sign. OR string starts with a decimal point.
  3. Decimal point after the digits OR digits after the decimal point.
  4. The symbol e or E after the digits.
  5. 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.

10_accepted_2.png

0
Subscribe to my newsletter

Read articles from Daniel Abayomi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Daniel Abayomi
Daniel Abayomi