Move History

Fork Selected
  • Mathematics
    Algorithms
    Logic
    Numbers
    Data Types
    Description

    Floyd's

    Code
    let is_happy h =
      let rec succ n = 
        match n with
        | 0 -> 0
        | _ -> let n' = n mod 10 in n' * n' + succ (n / 10)
      in
      let rec floyd's tortoise hare =
        match hare |> succ |> succ with
        | 1 -> true
        | hare' -> 
            let tortoise' = succ tortoise in 
            tortoise' <> hare' && floyd's tortoise' hare'
      in
      floyd's h h
    ;;
    Test Cases
    (* TODO: replace with your own tests, these are just how-to examples.
     * OUnit Spec example:
     * See http://ounit.forge.ocamlcore.org/api-ounit-2.0.0 for documentation
     *)
    
    module Tests = struct
        open OUnit
        let suite = [
            "Suite Name" >:::
                [
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 3) false
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 4) false
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 7) true
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 19) true
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 103) true
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 487) true
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 1663) true
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 1665) false
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 1000000000) true
                    );
                    "Test Name" >:: (fun _ ->
                        assert_equal (is_happy 10000000001) false
                    )
                ]
            ]
        ;;
    end
    
  • Code
    • def is_happy(h: int) -> bool:
    • """Returns `True` if `h` is happy, `False` otherwise."""
    • seen = set()
    • while h > 1 and h not in seen:
    • seen.add(h)
    • tot = 0
    • while h > 0:
    • tot += pow(h % 10, 2)
    • h //= 10
    • h = tot
    • return h == 1
    • let is_happy h =
    • let rec succ n =
    • match n with
    • | 0 -> 0
    • | _ -> let n' = n mod 10 in n' * n' + succ (n / 10)
    • in
    • let rec floyd's tortoise hare =
    • match hare |> succ |> succ with
    • | 1 -> true
    • | hare' ->
    • let tortoise' = succ tortoise in
    • tortoise' <> hare' && floyd's tortoise' hare'
    • in
    • floyd's h h
    • ;;
    Test Cases
    • import codewars_test as test
    • from solution import is_happy
    • (* TODO: replace with your own tests, these are just how-to examples.
    • * OUnit Spec example:
    • * See http://ounit.forge.ocamlcore.org/api-ounit-2.0.0 for documentation
    • *)
    • # test.assert_equals(actual, expected, [optional] message)
    • @test.describe("Example")
    • def test_group():
    • @test.it("test case")
    • def test_case():
    • test.assert_equals(is_happy(3), False)
    • test.assert_equals(is_happy(4), False)
    • test.assert_equals(is_happy(7), True)
    • test.assert_equals(is_happy(19), True)
    • test.assert_equals(is_happy(103), True)
    • test.assert_equals(is_happy(487), True)
    • test.assert_equals(is_happy(1663), True)
    • test.assert_equals(is_happy(1665), False)
    • test.assert_equals(is_happy(1000000000), True)
    • test.assert_equals(is_happy(10000000001), False)
    • # test.assert_equals(is_happy(), True)
    • module Tests = struct
    • open OUnit
    • let suite = [
    • "Suite Name" >:::
    • [
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 3) false
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 4) false
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 7) true
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 19) true
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 103) true
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 487) true
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 1663) true
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 1665) false
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 1000000000) true
    • );
    • "Test Name" >:: (fun _ ->
    • assert_equal (is_happy 10000000001) false
    • )
    • ]
    • ]
    • ;;
    • end