Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Created July 17, 2023 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keturiosakys/79817c68bdb70c3ffc43383928767e4e to your computer and use it in GitHub Desktop.
Save keturiosakys/79817c68bdb70c3ffc43383928767e4e to your computer and use it in GitHub Desktop.
Given a string, separate it into groups of non-space equal characters, sorted.
open Base
open Stdio
let explode_string str =
String.to_list str
|> List.filter ~f:(fun c -> not (phys_equal c ' '))
|> List.sort_and_group ~compare:(fun a b -> Char.compare a b)
|> List.map ~f:(fun l -> String.of_char_list l)
let%expect_test "explode_string 1" =
let res = explode_string "Ahh, abracadabra!" in
List.iter res ~f:(fun str -> print_string str);
[%expect {|!,Aaaaaabbcdhhrr|}]
let%expect_test "explode_string 2" =
let res = explode_string "\\o/\\o/" in
List.iter res ~f:(fun str -> print_string str);
[%expect {|//\\oo|}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment