How to resolve the algorithm Bin given limits step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Bin given limits step by step in the Go programming language

Table of Contents

Problem Statement

You are given a list of n ascending, unique numbers which are to form limits for n+1 bins which count how many of a large set of input numbers fall in the range of each bin. (Assuming zero-based indexing) The task is to create a function that given the ascending limits and a stream/ list of numbers, will return the bins; together with another function that given the same list of limits and the binning will print the limit of each bin together with the count of items that fell in the range. Assume the numbers to bin are too large to practically sort. Part 1: Bin using the following limits the given input data Part 2: Bin using the following limits the given input data Show output here, on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bin given limits step by step in the Go programming language

This Go program calculates and displays the frequency distribution of a set of data into bins based on predefined limits. It uses binary search to efficiently determine the bin for each data point. Here's a detailed breakdown of the code:

  1. Package and Imports:

    • The program starts with the package main statement, indicating that it's the entry point for an executable.
    • It imports the fmt package for input and output operations and the sort package for sorting and binary search.
  2. getBins Function:

    • This function takes two slices of integers: limits (the bin boundaries) and data (the data values we want to distribute into bins).
    • It creates a slice bins to store the frequency counts for each bin.
    • It iterates through each data point d in data.
    • For each d, it uses sort.SearchInts(limits, d) to perform binary search on the limits slice. This efficiently finds the index index where d belongs in the sorted limits.
    • If d is equal to the limit at index, it increments the count for the next bin. Otherwise, it increments the count for the current bin.
    • The function returns the bins slice containing the frequency counts.
  3. printBins Function:

    • This function takes two slices of integers: limits and bins.
    • It prints the bins and their frequency counts in a table format.
  4. main Function:

    • Defines two slices of integers: limitsList and dataList. Each sub-slice represents a dataset for which we want to calculate the frequency distribution.
    • Iterates through each dataset, calculates the bins using the getBins function, and prints the results using the printBins function.
  5. Example Output:

    • The program calculates and prints the frequency distribution of the data for each dataset defined in limitsList and dataList. For example, the output for the first dataset might look like this:
    Example 1
    
                 <  23 =  2
    >=  23 and <  37 =  6
    >=  37 and <  43 =  7
    >=  43 and <  53 = 12
    >=  53 and <  67 = 16
    >=  67 and <  83 =  6
    >=  83           =  5
    

Source code in the go programming language

package main

import (
    "fmt"
    "sort"
)

func getBins(limits, data []int) []int {
    n := len(limits)
    bins := make([]int, n+1)
    for _, d := range data {
        index := sort.SearchInts(limits, d) // uses binary search
        if index < len(limits) && d == limits[index] {
            index++
        }
        bins[index]++
    }
    return bins
}

func printBins(limits, bins []int) {
    n := len(limits)
    fmt.Printf("           < %3d = %2d\n", limits[0], bins[0])
    for i := 1; i < n; i++ {
        fmt.Printf(">= %3d and < %3d = %2d\n", limits[i-1], limits[i], bins[i])
    }
    fmt.Printf(">= %3d           = %2d\n", limits[n-1], bins[n])
    fmt.Println()
}

func main() {
    limitsList := [][]int{
        {23, 37, 43, 53, 67, 83},
        {14, 18, 249, 312, 389, 392, 513, 591, 634, 720},
    }

    dataList := [][]int{
        {
            95, 21, 94, 12, 99, 4, 70, 75, 83, 93, 52, 80, 57, 5, 53, 86, 65, 17, 92, 83, 71, 61, 54, 58, 47,
            16,  8,  9, 32, 84, 7, 87, 46, 19, 30, 37, 96, 6, 98, 40, 79, 97, 45, 64, 60, 29, 49, 36, 43, 55,
        },
        {
            445, 814, 519, 697, 700, 130, 255, 889, 481, 122, 932,  77, 323, 525, 570, 219, 367, 523, 442, 933,
            416, 589, 930, 373, 202, 253, 775,  47, 731, 685, 293, 126, 133, 450, 545, 100, 741, 583, 763, 306,
            655, 267, 248, 477, 549, 238,  62, 678,  98, 534, 622, 907, 406, 714, 184, 391, 913,  42, 560, 247,
            346, 860,  56, 138, 546,  38, 985, 948,  58, 213, 799, 319, 390, 634, 458, 945, 733, 507, 916, 123,
            345, 110, 720, 917, 313, 845, 426,   9, 457, 628, 410, 723, 354, 895, 881, 953, 677, 137, 397,  97,
            854, 740,  83, 216, 421,  94, 517, 479, 292, 963, 376, 981, 480,  39, 257, 272, 157,   5, 316, 395,
            787, 942, 456, 242, 759, 898, 576,  67, 298, 425, 894, 435, 831, 241, 989, 614, 987, 770, 384, 692,
            698, 765, 331, 487, 251, 600, 879, 342, 982, 527, 736, 795, 585,  40,  54, 901, 408, 359, 577, 237,
            605, 847, 353, 968, 832, 205, 838, 427, 876, 959, 686, 646, 835, 127, 621, 892, 443, 198, 988, 791,
            466,  23, 707, 467,  33, 670, 921, 180, 991, 396, 160, 436, 717, 918,   8, 374, 101, 684, 727, 749,
        },
    }

    for i := 0; i < len(limitsList); i++ {
        fmt.Println("Example", i+1, "\b\n")
        bins := getBins(limitsList[i], dataList[i])
        printBins(limitsList[i], bins)
    }
}


  

You may also check:How to resolve the algorithm Exponentiation operator step by step in the Lua programming language
You may also check:How to resolve the algorithm Walk a directory/Non-recursively step by step in the Gambas programming language
You may also check:How to resolve the algorithm Temperature conversion step by step in the Euphoria programming language
You may also check:How to resolve the algorithm Sierpinski square curve step by step in the Nim programming language
You may also check:How to resolve the algorithm Euler's identity step by step in the J programming language