Just because Exercism doesn’t offer your favorite language as an official track, it doesn’t mean we can’t play at all. Post some solutions to the weekly challenges in the language of your choice!

  • AndyOPM
    link
    fedilink
    arrow-up
    3
    ·
    8 months ago

    Here are a bunch in Factor, taking the easy way when the solution is already in the standard library:

    Leap
    USING: calendar ;
    
    ALIAS: leap? leap-year?
    
    Reverse String
    USING: sequences ;
    
    ALIAS: reverse-string reverse
    
    Raindrops
    USING: kernel math.functions math.parser sequences ;
    
    : raindrops ( n -- sound )
      { 3 5 7 } [ dupd divisor? ] map
      [ { "Pling" "Plang" "Plong" } nth "" ? ] map-index
      concat
      [ number>string ] [ nip ] if-empty
    ;
    
    Roman Numerals
    USING: roman ;
    
    ALIAS: roman-numerals >ROMAN
    
    Protein Translation
    USING: combinators grouping kernel sequences sequences.extras sets ;
    
    : RNA>proteins ( RNA -- proteins )
      3 group
      [ { "UAA" "UAG" "UGA" } in? ] cut-when drop
      [
        {
          { [ dup "AUG" =                         ] [ "Methionine"    ] }
          { [ dup "UGG" =                         ] [ "Tryptophan"    ] }
          { [ dup { "UUU" "UUC"             } in? ] [ "Phenylalanine" ] }
          { [ dup { "UUA" "UUG"             } in? ] [ "Leucine"       ] }
          { [ dup { "UAU" "UAC"             } in? ] [ "Tyrosine"      ] }
          { [ dup { "UGU" "UGC"             } in? ] [ "Cysteine"      ] }
          { [ dup { "UCU" "UCC" "UCA" "UCG" } in? ] [ "Serine"        ] }
        } cond nip
      ] map
    ;
    
    Acronym
    USING: sequences sequences.extras splitting unicode ;
    
    : >TLA ( phrase -- TLA )
      " -" split
      [ [ Letter? ] filter ] map-harvest
      [ 1 head >upper ] map-concat
    ;
    
    Allergies
    USING: kernel math sequences sets ;
    
    CONSTANT: scores
      { "eggs" "peanuts" "shellfish" "strawberries" "tomatoes" "chocolate" "pollen" "cats" }
    
    : (allergy-test) ( allergens remainder -- allergens' remainder' )
      dup log2
      [ scores ?nth '[ _ suffix! ] dip ]
      [ 2^ - ] bi
    ;
    
    : allergy-test ( allergen total -- allergic? allergens )
      V{ } clone swap
      [ (allergy-test) ] until-zero sift
      dup [ in? ] dip
    ;
    
    • AndyOPM
      link
      fedilink
      arrow-up
      1
      ·
      8 months ago
      Raindrops, again
      USING: assocs kernel math.functions math.parser sequences sequences.extras ;
      
      : raindrops ( n -- sound )
        { 3 5 7 } [ dupd divisor? ] find-all keys
        { "Pling" "Plang" "Plong" } nths concat
        [ number>string ] [ nip ] if-empty ;