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).
Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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!
Julien Vanier https://github.com/monkbroc
(* 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)
open Base let acronym s = let is_word_character c = (Char.is_alpha c) || c == '\'' in let first_letter l = match (List.find l ~f:Char.is_alpha) with | None -> ' ' | Some c -> Char.uppercase c in String.to_list s |> List.map ~f:(fun c -> if (is_word_character c) then c else ' ') |> List.group ~break:(fun c1 c2 -> (is_word_character c1) && (c2 == ' ')) |> List.fold ~init: ~f:(fun acc l -> (first_letter l)::acc) |> List.rev |> String.of_char_list ;;
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.