How to make function parameters mutable in mojo?

from testing import assert_equal

def hammingWeight(n: Int) -> Int:
    res = 0
    while n:
        res += 1 if (n & 1) == 1 else 0
        n >>= 1
    return res

def test_lc191():
    for inp, exp in [(11, 3), (128, 1), (2147483645, 30)]: assert_equal(hammingWeight(inp), exp)

How to make function parameter n mutable here ?

The question is whether you want n by reference or as a copy.

By reference, mut n.

As a copy, owned n.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.