본문 바로가기
Ocaml

[99 Problems in OCaml] 10번 문제

by seungh2 2021. 9. 19.

99 Problems in OCaml

https://ocaml.org/learn/tutorials/99problems.html

 

99 problems – OCaml

Your Help is Needed Many of the solutions below have been written by Victor Nicollet. Please contribute more solutions or improve the existing ones. 99 Problems (solved) in OCaml This section is inspired by Ninety-Nine Lisp Problems which in turn was based

ocaml.org

 

10번 문제

  • encode
    • encode list
    • list에 대해 run length encoding을 수행한다. 이때 (개수, data)를 pair로 하는 리스트를 반환한다.

 

내가 구현한 코드

개수를 세는 n을 인자로 갖고 정답을 누적하는 ans를 인자로 갖는 encode_ans라는 로컬 함수를 만들어서 해결하였다.

 

 

정답 코드

@를 사용하는 것 말고 ::와 List.rev를 사용한 방법이다.

 

test 결과

728x90

댓글