Reverse-only-alphabetical-order
Today's AlgoDaily problem requires you to reverse the alphanumeric characters in a string while keeping the positions of other characters fixed. My solution is here: https://github.com/mariakesa/Algorithms/blob/master/AlgoDaily/AlgorithmsPractice.ipynb
It's kind of hacky, but it gets the job done:-D I use high-level functions (string reversal [::-1] and .insert() method) and I'm not sure whether that would fly in an interview or you would have to implement something more basic.
The idea is to reverse the string, go and catch the characters and store them in a list and then do insertions into that list with the non-alphanumeric characters using their original indices.
I talked to my friend and he was thinking of a solution where you pre-allocate the array. So it got me thinking, is a python list an array data structure? If so insertions are very expensive, because you have to recopy the contents of the entire array so you get O(n)! But it turns out that a python list isn't really a classical CS array, see this https://www.geeksforgeeks.org/difference-between-list-and-array-in-python/ It allocates more memory so that insertion and addition operations would be faster. Interesting!
I came across this quote in a book:
"If you are immune to boredom, there is literally nothing you cannot accomplish.
—David Foster Wallace"
So it is with me and these daily exercises. You may think it's more exciting and sexy to do Deep Learning, but you can learn a lot from daily practice on these sorts of problems. I was implementing a Bayesian machine learning algorithm today for 6 hours from scratch. You really have to break down a complex model into sub-problems and sometimes these sub-problems are not any more difficult than these kinds of exercises. I highly recommend Andrew Ng's deep learning courses on Coursera, because they have programming exercises with templates for coding up your implementations. They're always step-by-step, function-by-function. So it's good to practice these things!
Kommentaarid
Postita kommentaar