title: Write A C Program To Find The Parity Of An Unsigned Integer
signature: function write_a_c_program_to_find_the_parity_of_an_unsigned_integer(n)
source_py: def write_a_c_program_to_find_the_parity_of_an_unsigned_integer(n):
      parity = 0
      while n:
          parity = (~ parity)
          n = (n & (n - 1))
      return parity
source_cpp: bool write_a_c_program_to_find_the_parity_of_an_unsigned_integer(unsigned int n) {
    bool parity = 0;
    while ( n ) {
      parity = ! parity;
      n = n & ( n - 1 );
    }
    return parity;
  }
question_info.test_code: 
  
  -- SOLUTION CODE
  -- ============================================
  PLACEHOLDER_CODE_BODY
  
  -- TESTING CODE 
  -- ============================================
  function isCorrect(actual, expected)
      if type(expected) ~= type(actual) then
          return false
      end
      
      if type(expected) == 'table' then
          for k,v_expected in pairs(expected) do
              
              if actual[k] == nil then
                  return false
              end
              local v_actual = actual[k]
              if not isCorrect(v_actual, v_expected) then
                  return false
              end
              actual[k]=nil
          end
      
          if next(actual) then 
              return false
          end
          return true
      else
          return actual == expected
      end
  end
  
  function validateSolution(actual, expected)
      return isCorrect(actual,expected)
  end
  
  function driver(n, expected)
      local exec_result;
      local status, error_msg = pcall(function () 
          exec_result=PLACEHOLDER_FN_NAME(n) 
      end)
      if status then
          if validateSolution(exec_result,expected) then
              return "PASSED"
          end
          return "FAILED"
      else
          return string.format("ERROR=%s",string.gsub(error_msg,".*:[%d]+: ",""))
      end
  end
      
  function main()
      result = driver(63, 0)
      print(string.format("TEST-0...%s",result))
  
      result = driver(64, -1)
      print(string.format("TEST-1...%s",result))
  
      result = driver(85, 0)
      print(string.format("TEST-2...%s",result))
  
      result = driver(36, 0)
      print(string.format("TEST-3...%s",result))
  
      result = driver(20, 0)
      print(string.format("TEST-4...%s",result))
  
      result = driver(63, 0)
      print(string.format("TEST-5...%s",result))
  
      result = driver(42, -1)
      print(string.format("TEST-6...%s",result))
  
      result = driver(19, -1)
      print(string.format("TEST-7...%s",result))
  
      result = driver(62, -1)
      print(string.format("TEST-8...%s",result))
  
      result = driver(97, -1)
      print(string.format("TEST-9...%s",result))
  
  end
  
  main()
question_info.entry_cls_name: Solution
question_info.test_list: [{"idx": 0, "outputs": 0, "inputs": {"n": 63}}, {"idx": 1, "outputs": -1, "inputs": {"n": 64}}, {"idx": 2, "outputs": 0, "inputs": {"n": 85}}, {"idx": 3, "outputs": 0, "inputs": {"n": 36}}, {"idx": 4, "outputs": 0, "inputs": {"n": 20}}, {"idx": 5, "outputs": 0, "inputs": {"n": 63}}, {"idx": 6, "outputs": -1, "inputs": {"n": 42}}, {"idx": 7, "outputs": -1, "inputs": {"n": 19}}, {"idx": 8, "outputs": -1, "inputs": {"n": 62}}, {"idx": 9, "outputs": -1, "inputs": {"n": 97}}]
question_info.extension: lua
question_info.entry_fn_name: write_a_c_program_to_find_the_parity_of_an_unsigned_integer
question_info.test_case_ids: 0 1 2 3 4 5 6 7 8 9
question_info.commands: lua __FILENAME__