Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
Actually, you could take the recursion along a bit further and make it a prototype of String, viz:
String.prototype.flip = function () { return (this.length === 1) ? this : this.substr(1).flip() + this.substr(0, 1); }; var original = "lewd i did live - evil did i dwel"; var reversed = original.flip();
Wouldn't solution #2 (and maybe some of the others) cause an O(n2 ) problem?
for (let character of str) { reverseString = character + reverseString; }
If str has N elements, and you do character + reverseString each time you get n*(n+1)/2 characters copied (ie. O(n2 )).
Whereas option#1 presumably uses only O(n) functions so it should be much faster as data grows and less stress on the GC.
I think it would be best to compare these with benchmarks (with large examples). Because I've found that the one place where code can get super slow is with successive concatenation in a for loop (as in option#2).
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
Array.prototype.flip = function () { for (var i = 0; i < this.length / 2; i++) { var tmp = this[this.length - i - 1]; this[this.length - i - 1] = this[i]; this[i] = tmp; } return this; } var original = "lewd i did live - evil did i dwel"; var reversed = [].flip.apply(original.split("")).join("");
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
This recursion thing is pretty cool. Here's another approach both as a String.prototype and as a standalone function:
String.prototype.flip2 = function() { if (1 >= this.length) return this; return ( this.substr(-1) + this.substr(1, this.length - 2).flip2() + this.substr(0, 1) ); }; function flip2(string) { if (1 >= string.length) return string; return ( string.substr(-1) + flip2(string.substr(1, string.length - 2)) + string.substr(0, 1) ); } var x = "lewd i did live - evil did i dwel".flip2(); var y = flip2("lewd i did live - evil did i dwel");
The approach is to take the first character and put it last, take the last character and put it first, and do that to the stuff in the middle.
It's been through the Closure Compiler, thus the 1 >= this.length.
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
I'm not sure what you'd call this solution, but it does reverse the string:
var times = function (n, fn /*, args*/) { var args = [].slice.call(arguments).slice(1); for (var i = 0; i < n; i++) { fn.call(this, i, args); } return args[2]; } var original = "lewd i did live - evil did i dwel"; var result = ""; var reversed = times(original.length, function (i, args) { args[2] = args[1].substr(i, 1) + args[2]; }, original, result);
That's the first time I've written code that uses .call(). Won't be the last time!
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
Going nuts here. More ideas, and for something as mundane as a reverse function. Monomania?
function flip3(string) { var result = Array(string.length); for (var i = string.length - 1, j = 0; i >= 0; i--, j++) { result[j] = string.charAt(i); } return result.join(""); } function flip4(string) { var result = Array(string.length).fill(1); result.map(function (item,index) { var rhs = string.length - 1 - index; result[index] = string.charAt(index); }); return result.join(""); }
Your mileage may vary on flip4's .fill() call. I'm having to use polyfills (long story) and if I don't fill the Array, .map() assumes that there's nothing there to work on.
Programmed Canon Canola calculators in 1977. Assorted platforms and languages ever since. Assisting with HOPL.info. I am NOT looking for work -- I've got more than enough to do.
Location
Perth, WA Australia
Education
A few diplomas.
Work
Software Engineer at [Daisy Digital](https://daisydigital.com.au/)
software developer with almost no qualifications, but a strong desire for learning. i believe that you can make it without qualifications if you push yourself. don't give up. the difference betw...
Actually, you could take the recursion along a bit further and make it a prototype of String, viz:
Someone'll beat me to a blog post, likely.
It's generally recommended that you should not extend native prototypes.
developers.google.com/web/updates/...
shakes fist at MooTools
Wouldn't solution #2 (and maybe some of the others) cause an O(n2 ) problem?
If str has N elements, and you do character + reverseString each time you get n*(n+1)/2 characters copied (ie. O(n2 )).
Whereas option#1 presumably uses only O(n) functions so it should be much faster as data grows and less stress on the GC.
I think it would be best to compare these with benchmarks (with large examples). Because I've found that the one place where code can get super slow is with successive concatenation in a for loop (as in option#2).
Thanks for the suggestion and the analysis. In subsequent posts, I try to compare the solutions based on their performance.
What about an old classic way:
This is a good improvement to the solution 2. It reduces the loop time by half. Thanks for sharing.
.split('')
can be shortened:Also good because split will fail badly for some utf8 things like emojis
So after doing .call(), let's do .apply(), viz
Okay, maybe it's time to do some real work now.
This recursion thing is pretty cool. Here's another approach both as a String.prototype and as a standalone function:
The approach is to take the first character and put it last, take the last character and put it first, and do that to the stuff in the middle.
It's been through the Closure Compiler, thus the
1 >= this.length
.I'm not sure what you'd call this solution, but it does reverse the string:
That's the first time I've written code that uses .call(). Won't be the last time!
Going nuts here. More ideas, and for something as mundane as a reverse function. Monomania?
Your mileage may vary on flip4's
.fill()
call. I'm having to use polyfills (long story) and if I don't fill the Array,.map()
assumes that there's nothing there to work on.I do have a couple of ideas yet to exorcise.
Short, sweet:
I'd forgotten about that third parameter into map. Short, sweet, superb!
first thing i thought of was
reduceRight
(works in the opposite direction toreduce
), with nosplit
orjoin
:)Nice idea for blog posts! It is helpful for people wanting to learn and helpful for you to practice the algos! 💯
Thank you, Martin.
certainly not very fast, but I'd like destructuring assignment and recursion :)
Another solution using for loop, but only iterates half the time.
Just don't use any emoji with skin color modifier though...
"🤷🏻♂️" split on '' becomes 7 "strings"
"🤷🏻♂️".split('').reverse().map(c => c.charCodeAt(0).toString(16))
// ["fe0f", "2642", "200d", "dffb", "d83c", "dd37", "d83e"]
That reversed looks... weird.
"️♂�🄷�"
already had this topic
What is this crazy black magic I'm looking at?
Would be great to see a performance comparison between then all.
Yes, I'll consider adding that in subsequent posts. Thanks for the suggestion.
You can find code snippets here. I did the same a couple of days ago
github.com/vyasriday/js-snippets/b...