Day 10: I4G 10 Days of Code Challenge

LeetCode Problem #65

The challenge for today wasn't necessarily so difficult. It was particularly challenging because there were a lot of rules to write codes for.

However, I discovered a way to shorten the entire program after many trials.

The Problem:

D 10 prob.png

THOUGHT PROCESS

I started by writing a program which took out all the edge cases individually. It was quite long and had a few bugs. While trying to clean up the bugs, I discovered that the entire program could be reduce to a few lines.

The idea is just to return True if an inputted number is valid. A float in python has all the characteristics of a valid number. Including the 'e' and its placement. This meant that I could return True if the float of the string exists, else the compiler will throw an error which will be caught by the except block, where it can return False.

The Solution:

D 10  soln.png

This was successful and a lot faster than the initial code.

Time complexity: O(n)

Space complexity: O(1)

Ciao!