Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Dyalog 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


Nice! 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


Just 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: