function submit(part)
  addpath('./lib');

  conf.assignmentKey = '1RFc-gNfEeapUhL5oS3IIQ';
  conf.itemName = 'Markov Networks for OCR';

  conf.partArrays = { ...
    { ...
      'Ga9CX', ...
      { 'ComputeSingletonFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'Y6ud3', ...
      { 'ComputeSingletonFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'YX6FP', ...
      { 'ComputePairwiseFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'sVpuc', ...
      { 'ComputePairwiseFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'ZzAEz', ...
      { 'ComputeTripletFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'jF5vU', ...
      { 'ComputeTripletFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'IQZRx', ...
      { 'ComputeSimilarityFactor.m' }, ...
      '', ...
    }, ...
    { ...
      'bWL2q', ...
      { 'ComputeSimilarityFactor.m' }, ...
      '', ...
    }, ...
    { ...
      'TfTAH', ...
      { 'ComputeAllSimilarityFactors.m' }, ...
      '', ...
    }, ...
    { ...
      '44rjP', ...
      { 'ComputeAllSimilarityFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'eGTyV', ...
      { 'ChooseTopSimilarityFactors.m' }, ...
      '', ...
    }, ...
    { ...
      'VfH4h', ...
      { 'ChooseTopSimilarityFactors.m' }, ...
      '', ...
    }, ...
  };

  conf.output = @output;
  submitWithConfiguration(conf);

end

% specifies which parts are test parts
function result = isTest(partIdx)
  if (mod(partIdx, 2) == 0)
      result = true;
  else
      result = false;
  end
end



function out = output(partIdx, auxstring)

load PA3Models.mat;
if (isTest(partIdx))
    load PA3TestCases.mat;
else
    load PA3SampleCases.mat;
end

if partIdx == 1
    images = Part1SampleImagesInput;
    factors = ComputeSingletonFactors(images, imageModel);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 2
    images = Part1TestImagesInput;
    factors = ComputeSingletonFactors(images, imageModel);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 3
    images = Part2SampleImagesInput;
    factors = ComputePairwiseFactors(images, pairwiseModel, imageModel.K);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 4
    images = Part2TestImagesInput;
    factors = ComputePairwiseFactors(images, pairwiseModel, imageModel.K);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 5
    images = Part3SampleImagesInput;
    factors = ComputeTripletFactors(images, tripletList, imageModel.K);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors, 2);
elseif partIdx == 6
    images = Part3TestImagesInput;
    factors = ComputeTripletFactors(images, tripletList, imageModel.K);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors, 2);
elseif partIdx == 7
    images = Part4SampleImagesInput;
    factor = ComputeSimilarityFactor(images, imageModel.K, 1, 2);
    factor = SortAllFactors(factor);
    out = SerializeFactorsFgGrading(factor);
elseif partIdx == 8
    images = Part4TestImagesInput;
    factor = ComputeSimilarityFactor(images, imageModel.K, 3, 4);
    factor = SortAllFactors(factor);
    out = SerializeFactorsFgGrading(factor);
elseif partIdx == 9
    images = Part5SampleImagesInput;
    factors = ComputeAllSimilarityFactors(images, imageModel.K);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 10
    images = Part5TestImagesInput;
    factors = ComputeAllSimilarityFactors(images, imageModel.K);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 11
    allFactors = Part6SampleFactorsInput;
    factors = ChooseTopSimilarityFactors(allFactors, 2);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
elseif partIdx == 12
    allFactors = Part6TestFactorsInput;
    factors = ChooseTopSimilarityFactors(allFactors, 2);
    factors = SortAllFactors(factors);
    out = SerializeFactorsFgGrading(factors);
end

end

function f = SortAllFactors(factors)

for i = 1:length(factors)
    factors(i) = SortFactorVars(factors(i));
end

varMat = vertcat(factors(:).var);
[unused, order] = sortrows(varMat);

f = factors(order);

end

function G = SortFactorVars(F)

[sortedVars, order] = sort(F.var);
G.var = sortedVars;

G.card = F.card(order);
G.val = zeros(numel(F.val), 1);

assignmentsInF = IndexToAssignment(1:numel(F.val), F.card);
assignmentsInG = assignmentsInF(:,order);
G.val(AssignmentToIndex(assignmentsInG, G.card)) = F.val;

end

function str = SerializeWordList (words)

str = [];
for i = 1:length(words)
    str = [str num2str(words{i}(:)') '  ']; %#ok
end

end