Today's challenge is to write a function that when given a string returns the string with the characters in the reverse order.
E.g.
reverseString('hello');
//output: 'olleh'
But instead of trying to make a nice shiny function, write something bad. Throw space and time optimisations out the window. Double down on cyclomatic complexity. Make the compiler sad.
Tests
reverseString('Use Markdown to write and format posts.') == '.stsop tamrof dna etirw ot nwodkraM esU';
reverseString('You can use Liquid tags to add rich content such as Tweets, YouTube videos, etc.') == '.cte ,soediv ebuTuoY ,steewT sa hcus tnetnoc hcir dda ot sgat diuqiL esu nac uoY';
reverseString('In addition to images for the post\'s content, you can also drag and drop a cover image') == 'egami revoc a pord dna gard osla nac uoy ,tnetnoc s\'tsop eht rof segami ot noitidda nI';
reverseString('') == '';
Latest comments (18)
Go - convert the string to a slice of runes, swap the runes in the slice via some bit manipulation, then convert the slice back to string and return it.
Remember, to swap the values of two integers a and b...
a = a XOR b
b = a XOR b
a = a XOR b
Update: it occurred to me that the solution above may be odd, but it's not that inefficient. We can do better. So... take 2:
We split the string into a slice of ordered rune-index pairs, reverse the slice by sorting based on the indices, then get the resulting string from the reordered slice. It's pretty inefficient to begin with, and choosing bubblesort as our sorting algorithm gets us some O(N^2) fun.
Ok, so this monstrosity uses the built-in
substr
method. But instead of using that to determine the string, it builds a whole bund of PHP code to execute within the shell, calls that via exec, and returns the string.Awfully bad practice, inefficient, and dangerous. It's the type of code which gives PHP a bad name!
OOoooooooooooo, I love it! Bit late to this party (we'll see if I can get a terrible answer in later), but I hope you make a habit of posting these! My kind of challenge. :D
We need a computer network.
I am too lazy to implement the reverse function so I made cursed version instead.
so easy
this works for any amount of chars xd
Recursive reverse.
function reverse(s) {
if(!s) return “”;
return reverse(s.substring(1)) + s[0];
}
WEDIDIT 🎉
❤️ this challenge
This solution is inspired by my_first_calculator.py. It works for all lowercase alphabetic strings of length 3. The generated (non-
eval
ed) version is 17,582 lines of code.When I open the "my_first_calculator.py" file it took so long, I thought something was wront with the git site ou my internet was down. Then I saw the file and I understood
include
using namespace std;
int main(){
cout << "Syntax error";
return 0;
}
Just shuffle the characters and see if it reversed the string! We'll get the reverse eventually.... (prepare to wait a long time for strings longer than 8 characters)
Perfect!
I'm loving this. Here my attempt:
This is my favorite so far. The reversed signatures are a nice touch
Thanks! Felt very meta some how.
Why not use Python's [::-1], but entirely in the browser? We can load the Pyodide runtime so we can run Python in the browser through WebAssembly. We'll have to eval as no ES module is shipped, but surely that's not a problem. Also the runtime is 20MB, but it's worth it!
Copy it into your browser console and be amazed..
My humble submission: