9 ms·
A World Without Strings Is Chaos
- m4r35n357 7y agoPhysicists will be disappointed ;)
- perl4ever 7y agoI was thinking I really need to research how to efficiently deal with strings in a certain language that doesn't allow them to be accessed as arrays of characters. Because then I could get started on porting some C code to it.
- swsieber 7y agoHa ha. Rust? I suppose it depends on the context of what you're trying to do. Edit: if it is Rust most of the time you'll want to use character iterators/map/filter/etc. Then benchmark.
- trevyn 7y agoThe Rust docs have good sections on this: https://doc.rust-lang.org/book/ch08-02-strings.html https://doc.rust-lang.org/book/ch08-02-strings.html https://doc.rust-lang.org/std/primitive.char.html https://doc.rust-lang.org/std/primitive.char.html TLDR you can straightforwardly access as bytes, or iterate as "Unicode scalar values" as stored in the 4-byte "char" type. (But a "Unicode scalar value" is not exactly equal to the colloquial meaning of "character".) However: If you care about valid UTF-8 inputs not breaking the function or intent of your code, it's probably time to understand the problems Unicode is actually trying to address, and apply those considerations to what you're trying to do. :)
- steveklabnik 7y agoIf you’re talking about Rust, https://doc.rust-lang.org/stable/std/string/struct.String.html#method.as_bytes https://doc.rust-lang.org/stable/std/string/struct.String.ht... and related methods will give you byte slices. If not Rust, I’m sure the language has some sort of API!
- Thiez 7y agoI wonder how many of the solutions would still work for arbitrary unicode strings. It looks like the author assumes only ascii exists. Perhaps the interns would be better off if they were taught that "reverse a string" is a fundamentally dangerous (and pointless) operation in almost any real-life context. Strings are way more complex than people give them credit for.
- deleted 7y ago[deleted]
- togs 7y agoIt’s not clear to me why reversing a string is dangerous or pointless.
- bastawhiz 7y agoAn array of characters reversed loses the original meaning of the characters. Other than looking for palindromes, there's almost nothing in the way of practical uses of a reversed string that can't be accomplished by subscripting the string and iterating from the end to the beginning.
- PeCaN 7y agoIterating from back to front doesn't work either; you probably (depending on what you're doing) still have to segment into grapheme clusters—which is stateful as of unicode 9, so you have to start from the beginning of the string. And then god forbid you get U+202C POP DIRECTIONAL FORMATTING CODE....
- catlifeonmars 7y agoI found this little gem on Software engineering StackExchange: https://softwareengineering.stackexchange.com/questions/24691/what-do-you-use-string-reversal-for https://softwareengineering.stackexchange.com/questions/2469... Suffice it to say, there are many interesting uses for string reversal :)
- Tokkemon 7y agoThat is an absolutely delightful Mouse Hunt reference I wasn't expecting today.
- rambojazz 7y agoCan somebody explain the title to me? This is just a collection of puzzles.
- MaxBarraclough 7y agoIt's a reference to a quote from the 1997 film Mouse Hunt. https://en.wikiquote.org/wiki/MouseHunt_(film)#Lars_Smuntz https://en.wikiquote.org/wiki/MouseHunt_(film)#Lars_Smuntz
- enriquto 7y agoI hate string processing. Is there any widely used programming language that does not support strings? I mean, you can do all of math, scientific computing, and graphics without strings. Forcing your language to support strings will inevitably impose compromises that make the language worse when you do not need them. I want such a language, unencumbered by the intrinsic uglyness of strings.
- tsimionescu 7y agowell, C++ doesn't really have too much support for strings, except the "" syntax for constant ASCII strings. Everything else is library based, and lots of people apparently don't use std::string and friends. However, I can't really think of many programming languages where string and string processing affect the syntax or semantics of any other aspect of the language (there are TCL, Perl and most shell languages, but definitely not C, C++, Java, C#, any flavor of LISP, Haskell, *ML and most others) That said, I don't really think you can use a programming system that doesn't,support strings:at some point you have to communicate something to a human, and at that point you need text. And traditionally, math is one of the places where people invented all sorts of creative writing systems. Also, you have symbolic math where even the core of the solver needs at least some basic string support (e.g. Solve for x "x + c = 0" => solution is -c.
- bryal 7y agoFuthark has only the most basic string support -- string literal syntax, which which is just sugar for a utf-8 byte array. It's a functional array language that generates fast GPU code, and as a GPU program is essentially a pure function (no input/output), string handling is very much a secondary concern. Futhark is used exactly for math, scientific computing, and graphics, and not for strings.
- jodrellblank 7y agoDyalog APL: 0 - Multiplicity (count character) 'h'(+/=)'fhqwhgads' 1 - Trapeze Part (palindrome) (⊢≡⌽)'racecar' 2 - chars which appear more than once (not too happy with my Key ⌸ ones) {k←{⍺,⍨1<≢⍵}⌸⍵ ⋄ k[;1]/k[;2]} 'applause' 3 - reordered letters 'teapot'{⍺[⍋⍺]≡⍵[⍋⍵]}'toptea' ≡/(⊂∘⍋⌷⊢)¨'teapot' 'toptea' 4 - chars which appear once {k←{⍺,⍨1=≢⍵}⌸⍵ ⋄ k[;1]/k[;2]} 'somewhat heterogenous' 5 - musical chars 'barfoo' {∨/⍺≡⍤1⊢⍵⌽⍤0 1⍨⍳≢⍵} 'foobar' 6 - sort strings by length, ascending {⍵[⍋≢¨⍵]} 'books' 'apple' 'peanut' 'aardvark' 'melon' 'pie' ((⊂∘⍋(≢¨⊢))⌷⊢) 'books' 'apple' 'peanut' 'aardvark' 'melon' 'pie' 7 - Most frequent character {k←{⍺,≢⍵}⌸⍵ ⋄ ⊃k[;1][⍋k[;2]]} 'abdbbac' 8 - reverse words {⊃{⍺,' ',⍵}/⌽¨' '((~=)⊆⊢)⍵} 'a few words in a sentence' 9 - compress 1 0 0 1 0 1/'foobar' 10 - expand 1 0 0 1 0 1 {'_'@(⍸~⍺)⍺\⍵} 'fbr' 11 - Consonants {'_'@(⍸'AEIOUYaeiouy'∊⍨⍵)⊢⍵} 'FLAPJACKS' 12 - Consonants Rdx {⍵/⍨~'AEIOUYaeiouy'∊⍨⍵} 'FLAPJACKS' 13 - Word replace ⍸'a few words in a sentence' {idx←⍸⍵⍷⍺ ⋄ idx,←idx+¨⍳¯1+≢⍵ ⋄ 'x'@idx⊢⍺} 'words' 14 - Permutations ? a recursive one shouldn't be so hard, but.. {0=≢⍵:'' ⋄ ... } 'xyz' non-recursive, much harder: https://code.jsoftware.com/wiki/Doc/Articles/Play202
- xelxebar 7y agoNice! Here are some J solutions. Unfortunately, I only have time for a few, right now. Hopefully, I can add some more later: 'mississippi' +/@:= 's' NB. 0 - Multiplicity (-:|.) 'racecar' NB. 1 - Trapeze Part (~. #~ 1 < +/@|:@=) 'applause' NB. 2 - Duplicity 'teapot' -:&({~/:) 'toptea' NB. 3 - Sort Yourself Out (~. #~ 1 = +/@|:@=) 'foo bar' NB. 4 - Precious Snowflakes
- xelxebar 7y agoJust for posterity, here are the rest I came up with: 'foobar' (e. ] A.~ +/@(!@i.@- *&|: >:@i.@- $&> i.)@#) 'barfoo' NB. 5 - Musical Chars (/: #&>) 'books';'apple';'peanut';'aardvark';'melon';'pie' NB. 6 - Size Matters (~. #~ [: (= >./) +/@|:@=) 'abdbbac' NB. 7 - Popularity Contest |.&.>&.;: 'a few words in a sentence' NB. 8 - esreveR A ecnetneS 'foobar' #~ 1 0 0 1 0 1 NB. 9 - Compression Session 'fbr' [`(I.@])`($&'_'@#@])} 1 0 0 1 0 1 NB. 10 - Expansion Mansion '_'&(I.@e.&'AIUEOaiueo'@]}) 'FLAPJACKS' NB. 11 - C_ns_n_nts -.&'AIUEOaiueo' 'FLAPJACKS' NB. 12 - Cnsnnts Rdx 'one fish two fish' [`($&'X'@#&.>@[)@.e."0 _&.;: 'fish' NB. 13 - TITLE REDACTED (A.~ i.@!@#) 'xyz' NB. 14 - It's More Fun to Permute Note that 14 is trivially non-recursive. I am fairly happy with these solutions, especially 5 and 13 which took the most thought.
- kazinator 7y agoThis is the TXR Lisp interactive listener of TXR 233. Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet. 1> (countq #\h "fhqwhgads") 2 2> [[callf equal identity reverse] "palindrome"] nil 3> [[callf equal identity reverse] "racecar"] t 4> [(opip (mappend [iff (op > (countq @1 @@1) 1) list] @1) uniq) "applause"] "ap" 5> [(opip (mappend [iff (op > (countq @1 @@1) 1) list] @1) uniq) "foo"] "o" 6> [(opip (mappend [iff (op > (countq @1 @@1) 1) list] @1) uniq) "baz"] "" 7> [[mapf equal sort sort] "teapot" "toptea"] t 8> [[mapf equal identity sort] "apple" "elap"] nil 9> [(op mappend [iff (op eql (countq @1 @@1) 1) list] @1) "somewhat heterogeneous"] "mwa rgnu" 10> [(do and (= (len @1) (len @2)) (search-str `@2@2` @1)) "foobar" "barfoo"] 3 11> [(do and (= (len @1) (len @2)) (search-str `@2@2` @1)) "fboaro" "foobar"] nil 12> [sort '#"books apple peanut aardvark melon pie" : len] ("pie" "books" "apple" "melon" "peanut" "aardvark") Yawn ... 13> (perm "xyz") ;; non-recursive, lazy, written in C. ("xyz" "xzy" "yxz" "yzx" "zxy" "zyx")
- kazinator 7y agoErratum: 8> [[mapf equal identity sort] "apple" "elap"] nil Should be: ... sort sort ...