Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Created August 14, 2023 14:48
Show Gist options
  • Save keturiosakys/24a4981046c0fc4e95c7817dd9e98b48 to your computer and use it in GitHub Desktop.
Save keturiosakys/24a4981046c0fc4e95c7817dd9e98b48 to your computer and use it in GitHub Desktop.
You have a faulty keyboard. Whenever you type a vowel on it (a,e,i,o,u,y), it reverses the string that you have written, instead of typing the character. Typing other characters works as expected. Given a string, return what will be on the screen after typing with your faulty keyboard.
open Base
(*You have a faulty keyboard. Whenever you type a vowel on it (a,e,i,o,u,y), it reverses
the string that you have written, instead of typing the character. Typing other
characters works as expected. Given a string, return what will be on the screen after
typing with your faulty keyboard.*)
let faulty_keyboard str =
let rec aux acc = function
| [] -> acc
| el :: tl
when Char.equal el 'a'
|| Char.equal el 'e'
|| Char.equal el 'i'
|| Char.equal el 'o'
|| Char.equal el 'u'
|| Char.equal el 'y' -> aux (List.rev acc) tl
| el :: tl -> aux (el :: acc) tl
in
String.to_list str |> aux [] |> List.rev |> String.of_char_list
;;
let%test_module "faulty keyboard" =
(module struct
let%test_unit "test 1" = [%test_eq: string] (faulty_keyboard "string") "rtsng"
let%test_unit "test 2" =
[%test_eq: string] (faulty_keyboard "hello world!") "w hllrld!"
;;
end)
;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment