Avatar of NunoSempere

NunoSempere's solution

to Acronym in the OCaml Track

Published at Dec 29 2019 · 1 comment
Instructions
Test suite
Solution

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Getting Started

  1. Install the Exercism CLI.

  2. Install OCaml.

  3. For library documentation, follow Useful OCaml resources.

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:

make

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue or submit a PR. We welcome new contributors!

Source

Julien Vanier https://github.com/monkbroc

test.ml

(* acronym - 1.7.0 *)
open OUnit2
open Acronym

let ae exp got _test_ctxt =
  assert_equal exp got ~printer:(fun x -> x )

let tests = [
  "basic" >::
  ae "PNG" (acronym "Portable Network Graphics");
  "lowercase words" >::
  ae "ROR" (acronym "Ruby on Rails");
  "punctuation" >::
  ae "FIFO" (acronym "First In, First Out");
  "all caps word" >::
  ae "GIMP" (acronym "GNU Image Manipulation Program");
  "punctuation without whitespace" >::
  ae "CMOS" (acronym "Complementary metal-oxide semiconductor");
  "very long abbreviation" >::
  ae "ROTFLSHTMDCOALM" (acronym "Rolling On The Floor Laughing So Hard That My Dogs Came Over And Licked Me");
  "consecutive delimiters" >::
  ae "SIMUFTA" (acronym "Something - I made up from thin air");
  "apostrophes" >::
  ae "HC" (acronym "Halley's Comet");
  "underscore emphasis" >::
  ae "TRNT" (acronym "The Road _Not_ Taken");
]

let () =
  run_test_tt_main ("acronym tests" >::: tests)
let acronym string = 
    let explode s = List.init (String.length s) (String.get s) in
    let rec flushIrrelevantCharacters irrelevantCharacters listOfCharacters = (
        match listOfCharacters with
        | [] -> []
        | x :: xs -> if  List.mem x (explode irrelevantCharacters) (* Char.equal *) 
            then flushIrrelevantCharacters irrelevantCharacters xs
            else x :: flushIrrelevantCharacters irrelevantCharacters xs
        ) in     
    let listOfCharacters = string |> explode |> (flushIrrelevantCharacters ",._';") in
    let isAfterSpaceOrBar n i character  = 
      match character with
      | ' ' | '-' -> if i<=(n-2) then (Some (i+1)) else None
      | _ -> None in
    let n = List.length listOfCharacters in 
    let is_not_none x = 
      match x with
      | None -> false
      | _ -> true
      in
    let extract x = 
      match x with 
      | Some x -> x
      | None -> 0 in
    listOfCharacters |> (List.mapi (isAfterSpaceOrBar n)) |> List.filter is_not_none |> List.map extract |> List.map (List.nth listOfCharacters) |> List.cons (List.hd listOfCharacters) |> (flushIrrelevantCharacters ",._-'; ") |> List.map (String.make 1) |> (String.concat "") |> String.uppercase_ascii

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of NunoSempere

I am seeing other solutions, and they're so elegant; I didn't get the idea to use

String.split_on_chars ~on:[' '; '-']
(edited 59 days ago)

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?