FAQ
Hi,

Any way to concat char to a string?
http://play.golang.org/p/XqJINNoz9E

I was trying to learn go with *the word **reverse *problem BTW:

Given a string say "I am a human being" the output should reverse all
letters within each word but otherwise preserve everything.

Eg: output should be "I ma a namuh gnieb"
No build-in string reverse function or build-in stack data-structure should
be used.


Your go implementation is appreciated.

Thanks

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Rui Ueyama at Apr 24, 2014 at 10:17 pm
    There are many utility functions in strings package that are useful for
    string processing. My implementation of the reverse problem is
    http://play.golang.org/p/kYUwowKJex.

    On Thu, Apr 24, 2014 at 3:05 PM, Tong Sun wrote:

    Hi,

    Any way to concat char to a string?
    http://play.golang.org/p/XqJINNoz9E

    I was trying to learn go with *the word **reverse *problem BTW:

    Given a string say "I am a human being" the output should reverse all
    letters within each word but otherwise preserve everything.

    Eg: output should be "I ma a namuh gnieb"
    No build-in string reverse function or build-in stack data-structure
    should be used.


    Your go implementation is appreciated.

    Thanks

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • peterGo at Apr 25, 2014 at 12:35 am
    reverse
    http://play.golang.org/p/EMASeng_ae

    Peter

    On Thursday, April 24, 2014 6:05:37 PM UTC-4, Tong Sun wrote:

    Hi,

    Any way to concat char to a string?
    http://play.golang.org/p/XqJINNoz9E

    I was trying to learn go with *the word **reverse *problem BTW:

    Given a string say "I am a human being" the output should reverse all
    letters within each word but otherwise preserve everything.

    Eg: output should be "I ma a namuh gnieb"
    No build-in string reverse function or build-in stack data-structure
    should be used.


    Your go implementation is appreciated.

    Thanks
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Simon place at Apr 25, 2014 at 2:48 am
    general advise, use what you know.

    in this case you know the length of the result, so make a string that
    length then figure out when the letters go, really you would only need
    concatenation if you have a stream AND you couldn't/didn't want too, buffer
    it all.
    On Thursday, 24 April 2014 23:05:37 UTC+1, Tong Sun wrote:

    Hi,

    Any way to concat char to a string?
    http://play.golang.org/p/XqJINNoz9E

    I was trying to learn go with *the word **reverse *problem BTW:

    Given a string say "I am a human being" the output should reverse all
    letters within each word but otherwise preserve everything.

    Eg: output should be "I ma a namuh gnieb"
    No build-in string reverse function or build-in stack data-structure
    should be used.


    Your go implementation is appreciated.

    Thanks
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Fredistic at Apr 25, 2014 at 5:45 am
    I thought I'd indulge in some premature optimization by using neither split
    nor join.
    It turns out to be more lines of code, but just might be "more efficient".

    Also, some more test data. Including Unicode strings, which mess up the
    display.

    http://play.golang.org/p/7oUDPvq8P-
    THE PLAYGROUND FAILS, IT TRIPS UP ON MY UNICODE STRINGS.
    I suppose I could play around with RTL and LTR marks, if I was really
    diligent...

    So I've attached the file, wordrev.go
    The output of a run is:
    ---------------------------------------------
    (I am a human being)
    (I ma a namuh gnieb) 5 words.

    (אתה מדבר אנגלית)
    (התא רבדמ תילגנא)
      3 words.

    (حياة طويلة إلى أمريكا)
    (ةايح ةليوط ىلإ اكيرمأ)
    4 words.

    (antidisestablishmentarianism)
    (msinairatnemhsilbatsesiditna) 1 words.

    (x)
    (x) 1 words.

    ()
    () 0 words.
    ----------------------------------------------------

      RickyS

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Tong Sun at Apr 25, 2014 at 1:43 pm

    On Fri, Apr 25, 2014 at 1:45 AM, wrote:

    I thought I'd indulge in some premature optimization by using neither
    split nor join.
    It turns out to be more lines of code, but just might be "more efficient".
    and "more correct" too -- note the *reverse words but nothing else *requirement
    in OP. I.e., "the output should reverse all letters within each word but
    otherwise preserve everything".

    E.g., consider the test cases of punctuation, or more spaces.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Carlos Castillo at Apr 25, 2014 at 6:10 am
    If you want to manipulate individual characters or bytes in a string,
    either for re-ordering or concatenation, you should probably convert them
    to slices first. Otherwise most operations you perform that generates a new
    string (notable exception: sub-string) will allocate memory and copy the
    relevant data so you will be doing extra work, but more importantly putting
    more pressure on the memory system / garbage collector.

    You can convert any string to a []byte to manipulate individual bytes, or
    you can convert a string to a []rune to manipulate text in a unicode safe
    manner.

    Here's runes: http://play.golang.org/p/zvXq4OY04K

    Strings.Split *does not* allocate new memory for the substrings, nor does
    it copy data, but Strings.Join does. Therefore there are technically more
    efficient solutions, but I like the symmetry and simplicity of the
    "backwords" function. The big benefits of the approach is that:
      - It respects unicode characters (manipulating []bytes will likely trash
    utf-8 strings that have non-ascii characters)
      - The reversal code doesn't allocate, it does all its work on the original
    runes
      - You have a nice framework for reducing the number of allocations even
    further, for example, you could convert the entire string to []rune of the
    start, and have only two allocation / copy operations in total: one from
    string to []rune and one back, you just have to re-write the tokenizing
    function, here's an example in backwords2, and it reuses the reverse
    function un-altered: http://play.golang.org/p/D8A7Fi0MPv
    On Thursday, April 24, 2014 3:05:37 PM UTC-7, Tong Sun wrote:

    Hi,

    Any way to concat char to a string?
    http://play.golang.org/p/XqJINNoz9E

    I was trying to learn go with *the word **reverse *problem BTW:

    Given a string say "I am a human being" the output should reverse all
    letters within each word but otherwise preserve everything.

    Eg: output should be "I ma a namuh gnieb"
    No build-in string reverse function or build-in stack data-structure
    should be used.


    Your go implementation is appreciated.

    Thanks
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedApr 24, '14 at 10:05p
activeApr 25, '14 at 1:43p
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase