code: def _read_and_preprocess_apps(target_path: str) -> List[CodeInstance]:
      """Read APPS dataset.
  
      Adapted from
          https://github.com/lxuechen/apps/blob/main/train/dataset_apps/APPSBaseDataset.py
      """
      SINGLE_STR_LIMIT = 150000  # From original codebase.
  
      instances = []
      for split_tag in (TRAIN_SPLIT, TEST_SPLIT):
          split_dir = os.path.join(target_path, split_tag)
  
          num_problems = 0
          skipped_problems = []
          for problem_name in apps_listdir_with_pinned_order(
              target_path, split_tag
          ):
              problem_dir = os.path.join(split_dir, problem_name)
  
              question_fname = os.path.join(problem_dir, "question.txt")
              sols_fname = os.path.join(problem_dir, "solutions.json")
              tests_fname = os.path.join(
                  problem_dir, "input_output.json"
              )
  
              # All instances must have the question description.
              if not os.path.isfile(question_fname):
                  skipped_problems.append(problem_name)
                  continue
              else:
                  # Train instances must have solution code.
                  if split_tag in ("train",):
                      if not os.path.isfile(sols_fname):
                          skipped_problems.append(problem_name)
                          continue
                  # Test instances can ignore solution code, but must have test cases.
                  elif split_tag in ("test",):
                      if not os.path.exists(
                          tests_fname
                      ) or not os.path.isfile(tests_fname):
                          skipped_problems.append(problem_name)
                          continue
  
              # Answer type.
              starter_code_fname = os.path.join(
                  problem_dir, "starter_code.py"
              )
              if os.path.exists(starter_code_fname):
                  answer_type = "\nUse Call-Based format\n"
              else:
                  answer_type = "\nUse Standard Input format\n"
  
              # Starter code.
              if os.path.isfile(starter_code_fname):
                  with open(starter_code_fname, "r") as f:
                      starter_code = f.read()
              else:
                  starter_code = ""
  
              # Question description.
              with open(question_fname, "r") as f:
                  question = f.read()
  
              # Solutions. Multiple of them!
              if os.path.isfile(sols_fname):
                  with open(sols_fname, "r") as f:
                      sols_str_list = json.load(f)
                      solutions = [
                          _reindent_code(sol_str)
                          for sol_str in sols_str_list
                      ]
              else:
                  solutions = []
  
              # Tests.
              if os.path.exists(tests_fname):
                  with open(tests_fname, "r") as f:
                      # Some files may contain the key `fn_name`, which indicates it's
                      # call-based instance. Annoying!
  
                      # Call-based instances check function input/outputs; for other instances
                      # I/O is handled through stdin and stdout streams.
                      data: Dict = json.load(f)
              else:
                  data = dict()
              data["root"] = problem_dir
  
              # Truncate for training, following original codebase.
              question = question[:SINGLE_STR_LIMIT]
              starter_code = starter_code[:SINGLE_STR_LIMIT]
              solutions = [sol[:SINGLE_STR_LIMIT] for sol in solutions]
              if len(solutions) == 0:
                  solutions = [""]
  
              # Create overall prompt.
              prompt = _make_input_for_apps(
                  question=question,
                  starter_code=starter_code,
                  answer_type=answer_type,
              )
              instance = CodeInstance(
                  input=Input(text=prompt),
                  references=[
                      CodeReference(
                          output=Output(text=solution),
                          tags=[CORRECT_TAG],
                          test_cases=data,
                      )
                      for solution in solutions
                  ],
                  split=split_tag,
                  metadata=data,
              )
              instances.append(instance)
              num_problems += 1
          # Should not skip any cases; just defensive.
          hlog(
              f"Split {split_tag}, "
              f"skipped {len(skipped_problems)}/{num_problems} problems with no description or solution. "
              f"Their ids are: {skipped_problems}"
          )
      return instances
  
imports: import io
  import json
  import os
  from typing import List, Dict, Iterable, Optional, cast
  from helm.common.general import ensure_file_downloaded
  from helm.common.hierarchical_logger import hlog
  from .code_scenario_helper import run
  from .code_scenario_apps_pinned_file_order import apps_listdir_with_pinned_order
  from .scenario import Scenario, Instance, Reference, TRAIN_SPLIT, VALID_SPLIT, TEST_SPLIT, CORRECT_TAG, Input, Output
label_desc: Use image node_id 4 for calling a global function with example usage: _read_and_preprocess_apps(target_path) and returns: instances