code golf

In a language of your choice, write a function to reverse a string in the fewest number of characters possible.
If your language has a built-in 'reverse' function, you cannot use it.

Anus Alien Shirt $21.68

Thalidomide Vintage Ad Shirt $22.14

Anus Alien Shirt $21.68

  1. 2 weeks ago
    Anonymous

    //JavaScript
    let reverse = s =>[...s].reduce((a,v)=>v+a,'');

    • 2 weeks ago
      Anonymous

      #include <string.h>
      char*r(char*s){char*v=strdup(s)+strlen(s);while(*s)*--v=*s++;return v;}

  2. 2 weeks ago
    Anonymous

    std::string reverse(std::string str) {
    std::string reverse_str;
    while(!str.empty()) {
    reverse_str.push_back(str.back());
    str.pop_back();
    }
    return reverse_str;
    }

  3. 2 weeks ago
    Anonymous

    function gay ($string) {
    return implode("", array_reverse(str_split($string*~~;
    }

    • 2 weeks ago
      Anonymous

      no array_reverse

      • 2 weeks ago
        Anonymous

        whymst? i thought the OP wanted to prevent shit like
        return strrev($string)

        • 2 weeks ago
          Anonymous

          because then every answer would be:
          convert string to array
          reverse array
          join array

          which is boring and not at all a challenge to optimize

  4. 2 weeks ago
    Anonymous

    lambda a:return reversed(a)

    • 2 weeks ago
      Anonymous

      triple-moron

      • 2 weeks ago
        Anonymous

        Not using language builtins is not using the language moron

        • 2 weeks ago
          Anonymous

          >using language builtins
          you're missing the point, and you're still a moron for thinking you need to use "return" with lambdas

    • 2 weeks ago
      Anonymous

      lambda a: returned a[::-1]

  5. 2 weeks ago
    Anonymous

    `,[>,]<[.<]`

    • 2 weeks ago
      Anonymous

      wtf is that it looks like 3 birds talking to each other

      • 2 weeks ago
        Anonymous

        One bird listens to the input, the next bird reverses it and the final bird prints the result

  6. 2 weeks ago
    Anonymous

    U+202E

    • 2 weeks ago
      Anonymous

      clever

    • 2 weeks ago
      Anonymous

      Today I learned something

  7. 2 weeks ago
    Anonymous

    in The Rust Programming Language this is just:
    s.chars().rev().collect::<String>();

    • 2 weeks ago
      Anonymous

      >If your language has a built-in 'reverse' function, you cannot use it.
      So this is the power of The Rust Programming Language...

      • 2 weeks ago
        Anonymous

        is being illiterate a prerequisite to using Rust?

        fn reverse(s: &str, rev: &mut String) {
        if s.is_empty() {
        return;
        }
        reverse(&s[1..], rev);
        rev.push(s.chars().next().unwrap());
        }

        • 2 weeks ago
          Anonymous

          >returns value by parameter passing
          >has an undocumented precondition of rev being empty
          I thought we already got rid of this shit. Why wouldn't you make it just return String?

          • 2 weeks ago
            Anonymous

            that would allocate a new string each time

          • 2 weeks ago
            Anonymous

            If you cared about performance, your first stop should be TCO and reserve instead of recycling Strings.

          • 2 weeks ago
            Anonymous

            can't be optimized if i have something after the recursion, if i wanted to do it iteratively i would have used a loop, but i recursion is cooler. but still even if it's not the fastest implementation it doesn't mean i have to give up and make it slower on purpose

          • 2 weeks ago
            Anonymous

            >can't be optimized if i have something after the recursion
            Yes, so you should rethink your approach so that recursion is last.
            You made it slower unwittingly instead of on purpose.

          • 2 weeks ago
            Anonymous

            if the recursion is last it's just a for loop i said...at that point i would use a for loop, and i still don't see how returning a string would be better

        • 2 weeks ago
          Anonymous

          holy shit this language fricking sucks lol

          • 2 weeks ago
            Anonymous

            that function is pretty much c/c++ with minor differences, what's so ugly about it?

          • 2 weeks ago
            Anonymous

            >2 parameters for some reason
            >.unwrap().unwrap().unwrap().next().penis().next()
            >bunch of random nonsense glyphs not used in any other language
            its an ugly nonsensical special snowflake language for people who will never have a job or be productive

          • 2 weeks ago
            Anonymous

            you like this more?
            fn reverse(s: &str, rev: &mut String) {
            if let Some(c) = s.chars().next() {
            reverse(&s[1..], rev);
            rev.push(c);
            }
            }

          • 2 weeks ago
            Anonymous

            of random nonsense glyphs not used in any other language
            which ones? [1..] is pretty self explanatory

          • 2 weeks ago
            Anonymous

            >>2 parameters for some reason

            that would allocate a new string each time

          • 2 weeks ago
            Anonymous

            not ugly, just could have been shorter maybe, depending on the rules we are going by. I assumed I couldn't use a standard swap macro or assume a custom for expansion

          • 2 weeks ago
            Anonymous

            Its very ugly

          • 2 weeks ago
            Anonymous

            everything looks ugly without whitespace. But it's basically the same as how the corresponding c code would look except for the dereferences.

          • 2 weeks ago
            Anonymous

            It only looks similar to C if you know nothing about programming

          • 2 weeks ago
            Anonymous

            are you actually moronic

          • 2 weeks ago
            Anonymous

            >unwrap().unwrap().unwrap().unwrap()

            >enforcing error handling... LE BAD
            history doomed repeat blah blah blah

          • 2 weeks ago
            Anonymous

            rustroony missing the point as usual

          • 2 weeks ago
            Anonymous

            not an argument

          • 2 weeks ago
            Anonymous

            It is bad. Instead of putting bumper guards around bad programmers you should fire them and hire a white male

          • 2 weeks ago
            Anonymous

            >just git gud, be white ma-
            SEGMENTATION FAULT CORE DUMPED

        • 2 weeks ago
          Anonymous

          >unwrap().unwrap().unwrap().unwrap()

          • 2 weeks ago
            Anonymous

            you like this more?
            fn reverse(s: &str, rev: &mut String) {
            if let Some(c) = s.chars().next() {
            reverse(&s[1..], rev);
            rev.push(c);
            }
            }

        • 2 weeks ago
          Anonymous

          >write a function to reverse a string in the fewest number of characters possible.
          If
          >if s is_empty...
          fricking dumb troony kys

    • 2 weeks ago
      Anonymous

      is being illiterate a prerequisite to using Rust?

  8. 2 weeks ago
    Anonymous

    r::(s:$S)->S{f:=*s[0];l:=*s[s.count];while f<l{t:=f.*;f.*=l.*;l.*=t}f+=1;l-=1;}
    could maybe do better but this is also a relatively efficient solution and the other thing I tried was only one char less, but I'm not sure it's correct.

    • 2 weeks ago
      Anonymous

      wait I'm moronic, drop the return value, since this mutated the passed string.

  9. 2 weeks ago
    Anonymous

    apl

    • 2 weeks ago
      Anonymous

      IQfy can't into unicode?
      character 0x233d

    • 2 weeks ago
      Anonymous

      IQfy can't into unicode?
      character 0x233d

      lol i was thinking "holy shit apl has some obscure invisible operator that reverses strings"

  10. 2 weeks ago
    Anonymous

    bump

    • 2 weeks ago
      Anonymous

      fugg.
      U+21CC in Uiua

  11. 2 weeks ago
    Anonymous

    how the frick can NONE of you read?!?!?!

  12. 2 weeks ago
    Anonymous

    for(i = str.len, i--){
    write(str[i])
    }

    • 2 weeks ago
      Anonymous

      oh that just displays the string in reverse
      this code actually reverses it:
      for(i = str.len, i--){
      str.temp[str.len - i] = str[i]
      }

  13. 2 weeks ago
    Anonymous

    picrel.

    Fiddle: https://dbfiddle.uk/2rF6W7a5

    • 2 weeks ago
      Anonymous

      I'm not clicking that virus link, jeet

    • 2 weeks ago
      Anonymous

      >manmade horrors just slightly outside your comprehension

  14. 2 weeks ago
    Anonymous

    ``
    (define reverse-string
    (lambda (str)
    (let loop ((l (string->list str))
    (col '(*~~
    (cond
    ((null? l) (list->string col))
    (else (loop (cdr l) (cons (car l) col*~~*~~)
    ``

  15. 2 weeks ago
    Anonymous

    def r(s):
    return s[::-1]

  16. 2 weeks ago
    Anonymous

    good thread

  17. 2 weeks ago
    Anonymous

    (define (reverse-string str)
    (list->string (reverse (string->list str*~~)

  18. 2 weeks ago
    Anonymous

    why make it hard when it can be easy? you just have to turn the monitor

  19. 2 weeks ago
    Anonymous

    reverse = foldl (flip (:)) []

Your email address will not be published. Required fields are marked *