Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Convert this REXX block to C#, preserving its control flow and logic. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
|
Translate this program into C# but keep the logic exactly as in REXX. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
|
Write the same code in C++ as shown below in REXX. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
|
Generate an equivalent C++ version of this REXX code. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
|
Preserve the algorithm and functionality while converting the code from REXX to Java. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
|
Convert this REXX block to Java, preserving its control flow and logic. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
|
Translate this program into Python but keep the logic exactly as in REXX. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Generate an equivalent Python version of this REXX code. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Keep all operations the same but rewrite the snippet in Go. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| 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)
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)
}
}
|
Convert this REXX snippet to Go and keep its semantics consistent. |
lims= 23 37 43 53 67 83
data= 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
call lims lims; call bins data
call show 'the 1st set of bin counts for the specified data:'
say; say; say
lims= 14 18 249 312 389 392 513 591 634 720
data= 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
call lims lims; call bins data
call show 'the 2nd set of bin counts for the specified data:'
exit 0
bins: parse arg nums; !.= 0; datum= words(nums); wc= length(datum)
do j=1 for datum; x= word(nums, j)
do k=0 for #
if x < @.k then do; !.k= !.k + 1; iterate j; end
end
!.k= !.k + 1
end
lims: parse arg limList; #= words(limList); wb= 0
do j=1 for #; _= j - 1; @._= word(limList, j); wb= max(wb, length(@._) )
end
show: parse arg t; say center(t, 51 ); $= left('', 9)
say center('', 51, "═")
jp= # - 1; ge= '≥'; le='<'; eq= ' count='
do j=0 for #; jm= j - 1; bin= right(@.j, wb)
if j==0 then say $ left('', length(ge) +3+wb+length(..) )le bin eq right(!.j, wc)
else say $ ge right(@.jm, wb) .. le bin eq right(!.j, wc)
if j==jp then say $ ge right(@.jp,wb) left('', 3+length(..)+wb) eq right(!.#, wc)
end
| 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)
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)
}
}
|
Ensure the translated C code behaves exactly like the original Ruby snippet. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| #include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int data1[] = {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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int data2[] = {
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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}
|
Change the following Ruby code into C without altering its purpose. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| #include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int data1[] = {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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int data2[] = {
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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}
|
Convert this Ruby snippet to C# and keep its semantics consistent. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
|
Transform the following Ruby implementation into C#, maintaining the same output and logic. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
|
Preserve the algorithm and functionality while converting the code from Ruby to C++. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
|
Convert this Ruby block to C++, preserving its control flow and logic. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
|
Port the following code from Ruby to Java with equivalent syntax and logic. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
|
Translate the given Ruby code snippet into Java without altering its behavior. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
|
Write a version of this Ruby function in Python with identical behavior. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Translate this program into Python but keep the logic exactly as in Ruby. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Write a version of this Ruby function in Go with identical behavior. | Test = Struct.new(:limits, :data)
tests = Test.new( [23, 37, 43, 53, 67, 83],
[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]),
Test.new( [14, 18, 249, 312, 389, 392, 513, 591, 634, 720],
[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])
def bin(limits, data)
data.map{|d| limits.bsearch{|limit| limit > d} }.tally
end
def present_bins(limits, bins)
ranges = ([nil]+limits+[nil]).each_cons(2).map{|low, high| Range.new(low, high, true) }
ranges.each{|range| puts "
end
tests.each do |test|
present_bins(test.limits, bin(test.limits, test.data))
puts
end
| 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)
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)
}
}
|
Change the following Tcl code into C without altering its purpose. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| #include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int data1[] = {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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int data2[] = {
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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}
|
Ensure the translated C code behaves exactly like the original Tcl snippet. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| #include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int data1[] = {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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int data2[] = {
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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}
|
Maintain the same structure and functionality when rewriting this code in C#. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
|
Rewrite the snippet below in C# so it works the same as the original Tcl code. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
|
Change the following Tcl code into C++ without altering its purpose. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
|
Convert this Tcl block to C++, preserving its control flow and logic. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
|
Translate the given Tcl code snippet into Java without altering its behavior. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
|
Can you help me rewrite this code in Java instead of Tcl, keeping it the same logically? | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
|
Preserve the algorithm and functionality while converting the code from Tcl to Python. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Generate an equivalent Python version of this Tcl code. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Write the same algorithm in Go as shown in this Tcl implementation. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| 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)
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)
}
}
|
Convert the following code from Tcl to Go, ensuring the logic remains intact. | namespace path {::tcl::mathop ::tcl::mathfunc}
proc lincr {_list index} {
upvar $_list list
lset list $index [+ [lindex $list $index] 1]
}
proc distribute_bins {binlims data} {
set bins [lrepeat [+ [llength $binlims] 1] 0]
foreach val $data {
lincr bins [+ [lsearch -exact -integer -sorted -bisect $binlims $val] 1]
}
return $bins
}
proc print_bins {binlims bins} {
set binlims [list -∞ {*}$binlims ∞]
for {set i 0} {$i < [llength $bins]} {incr i} {
puts "[lindex $binlims $i]..[lindex $binlims [+ $i 1]]: [lindex $bins $i]"
}
}
set binlims {23 37 43 53 67 83}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
puts ""
set binlims {14 18 249 312 389 392 513 591 634 720}
set data {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}
print_bins $binlims [distribute_bins $binlims $data]
| 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)
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)
}
}
|
Produce a functionally identical Rust code for the snippet given in C. | #include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int data1[] = {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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int data2[] = {
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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Rewrite this program in Rust while keeping its functionality equivalent to the C version. | #include <stdio.h>
#include <stdlib.h>
size_t upper_bound(const int* array, size_t n, int value) {
size_t start = 0;
while (n > 0) {
size_t step = n / 2;
size_t index = start + step;
if (value >= array[index]) {
start = index + 1;
n -= step + 1;
} else {
n = step;
}
}
return start;
}
int* bins(const int* limits, size_t nlimits, const int* data, size_t ndata) {
int* result = calloc(nlimits + 1, sizeof(int));
if (result == NULL)
return NULL;
for (size_t i = 0; i < ndata; ++i)
++result[upper_bound(limits, nlimits, data[i])];
return result;
}
void print_bins(const int* limits, size_t n, const int* bins) {
if (n == 0)
return;
printf(" < %3d: %2d\n", limits[0], bins[0]);
for (size_t i = 1; i < n; ++i)
printf(">= %3d and < %3d: %2d\n", limits[i - 1], limits[i], bins[i]);
printf(">= %3d : %2d\n", limits[n - 1], bins[n]);
}
int main() {
const int limits1[] = {23, 37, 43, 53, 67, 83};
const int data1[] = {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};
printf("Example 1:\n");
size_t n = sizeof(limits1) / sizeof(int);
int* b = bins(limits1, n, data1, sizeof(data1) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits1, n, b);
free(b);
const int limits2[] = {14, 18, 249, 312, 389, 392, 513, 591, 634, 720};
const int data2[] = {
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};
printf("\nExample 2:\n");
n = sizeof(limits2) / sizeof(int);
b = bins(limits2, n, data2, sizeof(data2) / sizeof(int));
if (b == NULL) {
fprintf(stderr, "Out of memory\n");
return EXIT_FAILURE;
}
print_bins(limits2, n, b);
free(b);
return EXIT_SUCCESS;
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Rewrite the snippet below in Rust so it works the same as the original C++ code. | #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Port the provided C# code into Rust while preserving the original functionality. | using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Generate a Rust translation of this C# snippet without changing its computational steps. | using System;
public class Program
{
static void Main()
{
PrintBins(new [] { 23, 37, 43, 53, 67, 83 },
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
);
Console.WriteLine();
PrintBins(new [] { 14, 18, 249, 312, 389, 392, 513, 591, 634, 720 },
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);
}
static void PrintBins(int[] limits, params int[] data)
{
int[] bins = Bins(limits, data);
Console.WriteLine($"-∞ .. {limits[0]} => {bins[0]}");
for (int i = 0; i < limits.Length-1; i++) {
Console.WriteLine($"{limits[i]} .. {limits[i+1]} => {bins[i+1]}");
}
Console.WriteLine($"{limits[^1]} .. ∞ => {bins[^1]}");
}
static int[] Bins(int[] limits, params int[] data)
{
Array.Sort(limits);
int[] bins = new int[limits.Length + 1];
foreach (int n in data) {
int i = Array.BinarySearch(limits, n);
i = i < 0 ? ~i : i+1;
bins[i]++;
}
return bins;
}
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Can you help me rewrite this code in Rust instead of Java, keeping it the same logically? | import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Port the following code from Go to Rust with equivalent syntax and logic. | 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)
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)
}
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Keep all operations the same but rewrite the snippet in Rust. | 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)
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)
}
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Convert the following code from Rust to Python, ensuring the logic remains intact. | fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Translate the given C++ code snippet into Rust without altering its behavior. | #include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
std::vector<int> bins(const std::vector<int>& limits,
const std::vector<int>& data) {
std::vector<int> result(limits.size() + 1, 0);
for (int n : data) {
auto i = std::upper_bound(limits.begin(), limits.end(), n);
++result[i - limits.begin()];
}
return result;
}
void print_bins(const std::vector<int>& limits, const std::vector<int>& bins) {
size_t n = limits.size();
if (n == 0)
return;
assert(n + 1 == bins.size());
std::cout << " < " << std::setw(3) << limits[0] << ": "
<< std::setw(2) << bins[0] << '\n';
for (size_t i = 1; i < n; ++i)
std::cout << ">= " << std::setw(3) << limits[i - 1] << " and < "
<< std::setw(3) << limits[i] << ": " << std::setw(2)
<< bins[i] << '\n';
std::cout << ">= " << std::setw(3) << limits[n - 1] << " : "
<< std::setw(2) << bins[n] << '\n';
}
int main() {
const std::vector<int> limits1{23, 37, 43, 53, 67, 83};
const std::vector<int> data1{
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};
std::cout << "Example 1:\n";
print_bins(limits1, bins(limits1, data1));
const std::vector<int> limits2{14, 18, 249, 312, 389,
392, 513, 591, 634, 720};
const std::vector<int> data2{
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};
std::cout << "\nExample 2:\n";
print_bins(limits2, bins(limits2, data2));
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Produce a functionally identical Rust code for the snippet given in Java. | import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Bins {
public static <T extends Comparable<? super T>> int[] bins(
List<? extends T> limits, Iterable<? extends T> data) {
int[] result = new int[limits.size() + 1];
for (T n : data) {
int i = Collections.binarySearch(limits, n);
if (i >= 0) {
i = i+1;
} else {
i = ~i;
}
result[i]++;
}
return result;
}
public static void printBins(List<?> limits, int[] bins) {
int n = limits.size();
if (n == 0) {
return;
}
assert n+1 == bins.length;
System.out.printf(" < %3s: %2d\n", limits.get(0), bins[0]);
for (int i = 1; i < n; i++) {
System.out.printf(">= %3s and < %3s: %2d\n", limits.get(i-1), limits.get(i), bins[i]);
}
System.out.printf(">= %3s : %2d\n", limits.get(n-1), bins[n]);
}
public static void main(String[] args) {
List<Integer> limits = Arrays.asList(23, 37, 43, 53, 67, 83);
List<Integer> data = Arrays.asList(
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);
System.out.println("Example 1:");
printBins(limits, bins(limits, data));
limits = Arrays.asList(14, 18, 249, 312, 389,
392, 513, 591, 634, 720);
data = Arrays.asList(
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);
System.out.println();
System.out.println("Example 2:");
printBins(limits, bins(limits, data));
}
}
| fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
|
Preserve the algorithm and functionality while converting the code from Rust to Python. | fn make_bins(limits: &Vec<usize>, data: &Vec<usize>) -> Vec<Vec<usize>> {
let mut bins: Vec<Vec<usize>> = Vec::with_capacity(limits.len() + 1);
for _ in 0..=limits.len() {bins.push(Vec::new());}
limits.iter().enumerate().for_each(|(idx, limit)| {
data.iter().for_each(|elem| {
if idx == 0 && elem < limit { bins[0].push(*elem); }
else if idx == limits.len()-1 && elem >= limit { bins[limits.len()].push(*elem); }
else if elem < limit && elem >= &limits[idx-1] { bins[idx].push(*elem); }
});
});
bins
}
fn print_bins(limits: &Vec<usize>, bins: &Vec<Vec<usize>>) {
for (idx, bin) in bins.iter().enumerate() {
if idx == 0 {
println!(" < {:3} := {:3}", limits[idx], bin.len());
} else if idx == limits.len() {
println!(">= {:3} := {:3}", limits[idx-1], bin.len());
}else {
println!(">= {:3} .. < {:3} := {:3}", limits[idx-1], limits[idx], bin.len());
}
};
}
fn main() {
let limits1 = vec![23, 37, 43, 53, 67, 83];
let data1 = vec![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];
let limits2 = vec![14, 18, 249, 312, 389, 392, 513, 591, 634, 720];
let data2 = vec![
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
];
println!("RC FIRST EXAMPLE");
let bins1 = make_bins(&limits1, &data1);
print_bins(&limits1, &bins1);
println!("\nRC SECOND EXAMPLE");
let bins2 = make_bins(&limits2, &data2);
print_bins(&limits2, &bins2);
}
| from bisect import bisect_right
def bin_it(limits: list, data: list) -> list:
"Bin data according to (ascending) limits."
bins = [0] * (len(limits) + 1)
for d in data:
bins[bisect_right(limits, d)] += 1
return bins
def bin_print(limits: list, bins: list) -> list:
print(f" < {limits[0]:3} := {bins[0]:3}")
for lo, hi, count in zip(limits, limits[1:], bins[1:]):
print(f">= {lo:3} .. < {hi:3} := {count:3}")
print(f">= {limits[-1]:3} := {bins[-1]:3}")
if __name__ == "__main__":
print("RC FIRST EXAMPLE\n")
limits = [23, 37, 43, 53, 67, 83]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
print("\nRC SECOND EXAMPLE\n")
limits = [14, 18, 249, 312, 389, 392, 513, 591, 634, 720]
data = [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]
bins = bin_it(limits, data)
bin_print(limits, bins)
|
Transform the following Ada implementation into C#, maintaining the same output and logic. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Translate the given Ada code snippet into C# without altering its behavior. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Convert this Ada snippet to C and keep its semantics consistent. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Convert this Ada block to C, preserving its control flow and logic. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Convert this Ada block to C++, preserving its control flow and logic. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Change the following Ada code into C++ without altering its purpose. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Change the following Ada code into Go without altering its purpose. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Write a version of this Ada function in Go with identical behavior. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Rewrite this program in Java while keeping its functionality equivalent to the Ada version. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Convert the following code from Ada to Java, ensuring the logic remains intact. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Produce a language-to-language conversion: from Ada to Python, same semantics. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Write the same algorithm in Python as shown in this Ada implementation. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Convert the following code from Ada to VB, ensuring the logic remains intact. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Translate this program into VB but keep the logic exactly as in Ada. | with Ada.Text_IO;
procedure Department_Numbers is
use Ada.Text_IO;
begin
Put_Line (" P S F");
for Police in 2 .. 6 loop
for Sanitation in 1 .. 7 loop
for Fire in 1 .. 7 loop
if
Police mod 2 = 0 and
Police + Sanitation + Fire = 12 and
Sanitation /= Police and
Sanitation /= Fire and
Police /= Fire
then
Put_Line (Police'Image & Sanitation'Image & Fire'Image);
end if;
end loop;
end loop;
end loop;
end Department_Numbers;
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Generate a C translation of this Arturo snippet without changing its computational steps. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Port the following code from Arturo to C with equivalent syntax and logic. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Ensure the translated C# code behaves exactly like the original Arturo snippet. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Produce a functionally identical C# code for the snippet given in Arturo. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Keep all operations the same but rewrite the snippet in C++. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Keep all operations the same but rewrite the snippet in C++. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Convert the following code from Arturo to Java, ensuring the logic remains intact. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Write the same algorithm in Java as shown in this Arturo implementation. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Generate a Python translation of this Arturo snippet without changing its computational steps. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Generate an equivalent Python version of this Arturo code. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Keep all operations the same but rewrite the snippet in VB. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Write the same algorithm in VB as shown in this Arturo implementation. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Keep all operations the same but rewrite the snippet in Go. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Produce a functionally identical Go code for the snippet given in Arturo. | loop 1..7 'x [
loop 1..7 'y [
loop 1..7 'z [
if all? @[
even? x
12 = sum @[x y z]
3 = size unique @[x y z]
] -> print [x y z]
]
]
]
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Convert the following code from AutoHotKey to C, ensuring the logic remains intact. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Port the provided AutoHotKey code into C while preserving the original functionality. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Write the same code in C# as shown below in AutoHotKey. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Convert this AutoHotKey block to C++, preserving its control flow and logic. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Transform the following AutoHotKey implementation into C++, maintaining the same output and logic. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Generate a Java translation of this AutoHotKey snippet without changing its computational steps. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Rewrite the snippet below in Java so it works the same as the original AutoHotKey code. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Convert the following code from AutoHotKey to Python, ensuring the logic remains intact. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Write the same code in Python as shown below in AutoHotKey. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Generate a VB translation of this AutoHotKey snippet without changing its computational steps. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Convert this AutoHotKey block to VB, preserving its control flow and logic. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Rewrite this program in Go while keeping its functionality equivalent to the AutoHotKey version. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Convert the following code from AutoHotKey to Go, ensuring the logic remains intact. | perm(elements, n, opt:="", Delim:="", str:="", res:="", j:=0, dup:="") {
res := IsObject(res) ? res : [], dup := IsObject(dup) ? dup : []
if (n > j)
Loop, parse, elements, % Delim
res := !(InStr(str, A_LoopField) && !(InStr(opt, "rep"))) ? perm(elements, n, opt, Delim, trim(str Delim A_LoopField, Delim), res, j+1, dup) : res
else if !(dup[x := perm_sort(str, Delim)] && (InStr(opt, "comb")))
dup[x] := 1, res.Insert(str)
return res, j++
}
perm_sort(str, Delim){
Loop, Parse, str, % Delim
res .= A_LoopField "`n"
Sort, res, D`n
return StrReplace(res, "`n", Delim)
}
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Port the following code from AWK to C with equivalent syntax and logic. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Change the programming language of this snippet from AWK to C without modifying what it does. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| #include<stdio.h>
int main()
{
int police,sanitation,fire;
printf("Police Sanitation Fire\n");
printf("----------------------------------");
for(police=2;police<=6;police+=2){
for(sanitation=1;sanitation<=7;sanitation++){
for(fire=1;fire<=7;fire++){
if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){
printf("\n%d\t\t%d\t\t%d",police,sanitation,fire);
}
}
}
}
return 0;
}
|
Generate an equivalent C# version of this AWK code. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Write the same algorithm in C# as shown in this AWK implementation. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| using System;
public class Program
{
public static void Main() {
for (int p = 2; p <= 7; p+=2) {
for (int s = 1; s <= 7; s++) {
int f = 12 - p - s;
if (s >= f) break;
if (f > 7) continue;
if (s == p || f == p) continue;
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}");
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}");
}
}
}
}
|
Convert this AWK snippet to C++ and keep its semantics consistent. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Port the following code from AWK to C++ with equivalent syntax and logic. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| #include <iostream>
#include <iomanip>
int main( int argc, char* argv[] ) {
int sol = 1;
std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n";
for( int f = 1; f < 8; f++ ) {
for( int p = 1; p < 8; p++ ) {
for( int s = 1; s < 8; s++ ) {
if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) {
std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 )
<< ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p
<< "\t\t" << std::setw( 6 ) << s << "\n";
}
}
}
}
return 0;
}
|
Can you help me rewrite this code in Java instead of AWK, keeping it the same logically? |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Convert this AWK snippet to Java and keep its semantics consistent. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| public class DepartmentNumbers {
public static void main(String[] args) {
System.out.println("Police Sanitation Fire");
System.out.println("------ ---------- ----");
int count = 0;
for (int i = 2; i <= 6; i += 2) {
for (int j = 1; j <= 7; ++j) {
if (j == i) continue;
for (int k = 1; k <= 7; ++k) {
if (k == i || k == j) continue;
if (i + j + k != 12) continue;
System.out.printf(" %d %d %d\n", i, j, k);
count++;
}
}
}
System.out.printf("\n%d valid combinations", count);
}
}
|
Translate this program into Python but keep the logic exactly as in AWK. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Translate this program into Python but keep the logic exactly as in AWK. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| from itertools import permutations
def solve():
c, p, f, s = "\\,Police,Fire,Sanitation".split(',')
print(f"{c:>3} {p:^6} {f:^4} {s:^10}")
c = 1
for p, f, s in permutations(range(1, 8), r=3):
if p + s + f == 12 and p % 2 == 0:
print(f"{c:>3}: {p:^6} {f:^4} {s:^10}")
c += 1
if __name__ == '__main__':
solve()
|
Generate a VB translation of this AWK snippet without changing its computational steps. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Ensure the translated VB code behaves exactly like the original AWK snippet. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| Module Module1
Sub Main()
For p = 2 To 7 Step 2
For s = 1 To 7
Dim f = 12 - p - s
If s >= f Then
Exit For
End If
If f > 7 Then
Continue For
End If
If s = p OrElse f = p Then
Continue For
End If
Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}")
Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}")
Next
Next
End Sub
End Module
|
Maintain the same structure and functionality when rewriting this code in Go. |
BEGIN {
print("
for (fire=1; fire<=7; fire++) {
for (police=1; police<=7; police++) {
for (sanitation=1; sanitation<=7; sanitation++) {
if (rules() ~ /^1+$/) {
printf("%2d %2d %2d %2d\n",++count,fire,police,sanitation)
}
}
}
}
exit(0)
}
function rules( stmt1,stmt2,stmt3) {
stmt1 = fire != police && fire != sanitation && police != sanitation
stmt2 = fire + police + sanitation == 12
stmt3 = police % 2 == 0
return(stmt1 stmt2 stmt3)
}
| package main
import "fmt"
func main() {
fmt.Println("Police Sanitation Fire")
fmt.Println("------ ---------- ----")
count := 0
for i := 2; i < 7; i += 2 {
for j := 1; j < 8; j++ {
if j == i { continue }
for k := 1; k < 8; k++ {
if k == i || k == j { continue }
if i + j + k != 12 { continue }
fmt.Printf(" %d %d %d\n", i, j, k)
count++
}
}
}
fmt.Printf("\n%d valid combinations\n", count)
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.