<commit_before>package com.sezgk.tractor.census;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.sezgk.tractor.util.Quicksort;

/**
 * Implementation of the tract grouping algorithm. This algorithm takes census tracts and groups them into congressional
 * districts.
 * 
 * @author <NAME>
 * @author <NAME> - variable number of district support.
 */
public class TractGroupingService
{
    /**
     * Takes in a list of census tracts and groups them into congressional districts.
     * 
     * @param tracts , the list of tracts to be grouped.
     * @param nDistricts , the number of districts to form.
     * @param seedCoordinate, the coordinate origin from which districts should begin being built.
     * @return a list of congressional districts, together containing all of the tracts.
     */
    public static List<CongressionalDistrict> createDistricts(List<CensusTract> tracts, int numDistricts, MapCoordinate seedCoordinate)
    {
        List<CongressionalDistrict> districts = new ArrayList<CongressionalDistrict>();
        Quicksort sorter = new Quicksort(tracts, seedCoordinate);
        tracts = sorter.getSortedCensusList();

        int statePopulation = calculateStatePopulation(tracts);
        int districtPop = 0;
        int districtCounter = 0;

        // Target population for each district
        int targetPop = statePopulation / numDistricts;

        // Loop for each district:
        // - add a district
        // - add tracts until the target population is reached. As each tract is added to a district,
        // it is removed from the list of tracts
        // - sort the list by distance from the first tract in the new district
        for (districtCounter = 0; districtCounter < numDistricts; districtCounter++)
        {
            districts.add(new CongressionalDistrict());
            while ((districtPop + tracts.get(0).getPopulation()) < targetPop && tracts.size() > 0)
            {
                CensusTract nextTract = tracts.get(0);
                districts.get(districtCounter).addTract(nextTract);
                districtPop += nextTract.getPopulation();
                tracts.remove(0);

            }
            tracts = new Quicksort(tracts, tracts.get(0).getPosition()).getSortedCensusList();
            districtPop = 0;
        }

        // Catch any missed tracts and add to the last district
        while (tracts.size() > 0)
        {
            CensusTract missedTract = tracts.get(0);
            districtPop += missedTract.getPopulation();
            districts.get(numDistricts - 1).addTract(tracts.get(0));
            tracts.remove(0);
        }

        /*System.out.println("Starting to fix");
        districts = fix(districts);
        for (int i=0; i<districts.size(); i++)
        {
        	System.out.println(districts.get(i).getDistrictPop());
        }*/
        return districts;<commit_msg>Changed the algorithm to build additional districts from the farthest tract.<commit_after>package com.sezgk.tractor.census;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.sezgk.tractor.util.Quicksort;

/**
 * Implementation of the tract grouping algorithm. This algorithm takes census tracts and groups them into congressional
 * districts.
 * 
 * @author <NAME>
 * @author <NAME> - variable number of district support.
 */
public class TractGroupingService
{
    /**
     * Takes in a list of census tracts and groups them into congressional districts.
     * 
     * @param tracts , the list of tracts to be grouped.
     * @param nDistricts , the number of districts to form.
     * @param seedCoordinate, the coordinate origin from which districts should begin being built.
     * @return a list of congressional districts, together containing all of the tracts.
     */
    public static List<CongressionalDistrict> createDistricts(List<CensusTract> tracts, int numDistricts, MapCoordinate seedCoordinate)
    {
        List<CongressionalDistrict> districts = new ArrayList<CongressionalDistrict>();
        Quicksort sorter = new Quicksort(tracts, seedCoordinate);
        tracts = sorter.getSortedCensusList();

        int statePopulation = calculateStatePopulation(tracts);
        int districtPop = 0;
        int districtCounter = 0;

        // Target population for each district
        int targetPop = statePopulation / numDistricts;

        // Loop for each district:
        // - add a district
        // - add tracts until the target population is reached. As each tract is added to a district,
        // it is removed from the list of tracts
        // - sort the list by distance from the first tract in the new district
        for (districtCounter = 0; districtCounter < numDistricts; districtCounter++)
        {
            districts.add(new CongressionalDistrict());
            while ((districtPop + tracts.get(0).getPopulation()) < targetPop && tracts.size() > 0)
            {
                CensusTract nextTract = tracts.get(0);
                districts.get(districtCounter).addTract(nextTract);
                districtPop += nextTract.getPopulation();
                tracts.remove(0);

            }
            tracts = new Quicksort(tracts, tracts.get(tracts.size()-1).getPosition()).getSortedCensusList();
            districtPop = 0;
        }

        // Catch any missed tracts and add to the last district
        while (tracts.size() > 0)
        {
            CensusTract missedTract = tracts.get(0);
            districtPop += missedTract.getPopulation();
            districts.get(numDistricts - 1).addTract(tracts.get(0));
            tracts.remove(0);
        }

        /*System.out.println("Starting to fix");
        districts = fix(districts);
        for (int i=0; i<districts.size(); i++)
        {
        	System.out.println(districts.get(i).getDistrictPop());
        }*/
        return districts;
{'fraction_non_alphanumeric': 0.09259571597233934, 'fraction_numerical': 0.005059875189745319, 'mean_word_length': 2.5172004744958483, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1087782', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1522, 'n_words': 481}