Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Ensure the translated C# code behaves exactly like the original Ruby snippet. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| using System;
using System.Collections.Generic;
public class Program
{
public static IEnumerable<int[]> Combinations(int m, int n)
{
int[] result = new int[m];
Stack<int> stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0)
{
int index = stack.Count - 1;
int value = stack.Pop();
while (value < n)
{
result[index++] = ++value;
stack.Push(value);
if (index == m)
{
yield return result;
break;
}
}
}
}
static void Main()
{
foreach (int[] c in Combinations(3, 5))
{
Console.WriteLine(string.Join(",", c));
Console.WriteLine();
}
}
}
|
Convert the following code from Ruby to C++, ensuring the logic remains intact. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| #include <algorithm>
#include <iostream>
#include <string>
void comb(int N, int K)
{
std::string bitmask(K, 1);
bitmask.resize(N, 0);
do {
for (int i = 0; i < N; ++i)
{
if (bitmask[i]) std::cout << " " << i;
}
std::cout << std::endl;
} while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
int main()
{
comb(5, 3);
}
|
Change the programming language of this snippet from Ruby to Java without modifying what it does. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| import java.util.Collections;
import java.util.LinkedList;
public class Comb{
public static void main(String[] args){
System.out.println(comb(3,5));
}
public static String bitprint(int u){
String s= "";
for(int n= 0;u > 0;++n, u>>= 1)
if((u & 1) > 0) s+= n + " ";
return s;
}
public static int bitcount(int u){
int n;
for(n= 0;u > 0;++n, u&= (u - 1));
return n;
}
public static LinkedList<String> comb(int c, int n){
LinkedList<String> s= new LinkedList<String>();
for(int u= 0;u < 1 << n;u++)
if(bitcount(u) == c) s.push(bitprint(u));
Collections.sort(s);
return s;
}
}
|
Generate an equivalent Python version of this Ruby code. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| >>> from itertools import combinations
>>> list(combinations(range(5),3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
|
Produce a language-to-language conversion: from Ruby to VB, same semantics. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| input "Enter n comb m. ", n
input m
outstr$ = ""
call iterate (outstr$, 0, m-1, n-1)
end
subroutine iterate (curr$, start, stp, depth)
for i = start to stp
if depth = 0 then print curr$ + " " + string(i)
call iterate (curr$ + " " + string(i), i+1, stp, depth-1)
next i
end subroutine
|
Port the following code from Ruby to Go with equivalent syntax and logic. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| package main
import (
"fmt"
)
func main() {
comb(5, 3, func(c []int) {
fmt.Println(c)
})
}
func comb(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
|
Write the same algorithm in C as shown in this Scala implementation. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| #include <stdio.h>
typedef unsigned long marker;
marker one = 1;
void comb(int pool, int need, marker chosen, int at)
{
if (pool < need + at) return;
if (!need) {
for (at = 0; at < pool; at++)
if (chosen & (one << at)) printf("%d ", at);
printf("\n");
return;
}
comb(pool, need - 1, chosen | (one << at), at + 1);
comb(pool, need, chosen, at + 1);
}
int main()
{
comb(5, 3, 0, 0);
return 0;
}
|
Maintain the same structure and functionality when rewriting this code in C#. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| using System;
using System.Collections.Generic;
public class Program
{
public static IEnumerable<int[]> Combinations(int m, int n)
{
int[] result = new int[m];
Stack<int> stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0)
{
int index = stack.Count - 1;
int value = stack.Pop();
while (value < n)
{
result[index++] = ++value;
stack.Push(value);
if (index == m)
{
yield return result;
break;
}
}
}
}
static void Main()
{
foreach (int[] c in Combinations(3, 5))
{
Console.WriteLine(string.Join(",", c));
Console.WriteLine();
}
}
}
|
Translate the given Scala code snippet into C++ without altering its behavior. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| #include <algorithm>
#include <iostream>
#include <string>
void comb(int N, int K)
{
std::string bitmask(K, 1);
bitmask.resize(N, 0);
do {
for (int i = 0; i < N; ++i)
{
if (bitmask[i]) std::cout << " " << i;
}
std::cout << std::endl;
} while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
int main()
{
comb(5, 3);
}
|
Produce a language-to-language conversion: from Scala to Java, same semantics. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| import java.util.Collections;
import java.util.LinkedList;
public class Comb{
public static void main(String[] args){
System.out.println(comb(3,5));
}
public static String bitprint(int u){
String s= "";
for(int n= 0;u > 0;++n, u>>= 1)
if((u & 1) > 0) s+= n + " ";
return s;
}
public static int bitcount(int u){
int n;
for(n= 0;u > 0;++n, u&= (u - 1));
return n;
}
public static LinkedList<String> comb(int c, int n){
LinkedList<String> s= new LinkedList<String>();
for(int u= 0;u < 1 << n;u++)
if(bitcount(u) == c) s.push(bitprint(u));
Collections.sort(s);
return s;
}
}
|
Transform the following Scala implementation into Python, maintaining the same output and logic. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| >>> from itertools import combinations
>>> list(combinations(range(5),3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
|
Convert this Scala snippet to VB and keep its semantics consistent. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| input "Enter n comb m. ", n
input m
outstr$ = ""
call iterate (outstr$, 0, m-1, n-1)
end
subroutine iterate (curr$, start, stp, depth)
for i = start to stp
if depth = 0 then print curr$ + " " + string(i)
call iterate (curr$ + " " + string(i), i+1, stp, depth-1)
next i
end subroutine
|
Please provide an equivalent version of this Scala code in Go. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| package main
import (
"fmt"
)
func main() {
comb(5, 3, func(c []int) {
fmt.Println(c)
})
}
func comb(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
|
Write the same code in C as shown below in Swift. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| #include <stdio.h>
typedef unsigned long marker;
marker one = 1;
void comb(int pool, int need, marker chosen, int at)
{
if (pool < need + at) return;
if (!need) {
for (at = 0; at < pool; at++)
if (chosen & (one << at)) printf("%d ", at);
printf("\n");
return;
}
comb(pool, need - 1, chosen | (one << at), at + 1);
comb(pool, need, chosen, at + 1);
}
int main()
{
comb(5, 3, 0, 0);
return 0;
}
|
Convert this Swift snippet to C# and keep its semantics consistent. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| using System;
using System.Collections.Generic;
public class Program
{
public static IEnumerable<int[]> Combinations(int m, int n)
{
int[] result = new int[m];
Stack<int> stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0)
{
int index = stack.Count - 1;
int value = stack.Pop();
while (value < n)
{
result[index++] = ++value;
stack.Push(value);
if (index == m)
{
yield return result;
break;
}
}
}
}
static void Main()
{
foreach (int[] c in Combinations(3, 5))
{
Console.WriteLine(string.Join(",", c));
Console.WriteLine();
}
}
}
|
Port the provided Swift code into C++ while preserving the original functionality. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| #include <algorithm>
#include <iostream>
#include <string>
void comb(int N, int K)
{
std::string bitmask(K, 1);
bitmask.resize(N, 0);
do {
for (int i = 0; i < N; ++i)
{
if (bitmask[i]) std::cout << " " << i;
}
std::cout << std::endl;
} while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
int main()
{
comb(5, 3);
}
|
Keep all operations the same but rewrite the snippet in Java. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| import java.util.Collections;
import java.util.LinkedList;
public class Comb{
public static void main(String[] args){
System.out.println(comb(3,5));
}
public static String bitprint(int u){
String s= "";
for(int n= 0;u > 0;++n, u>>= 1)
if((u & 1) > 0) s+= n + " ";
return s;
}
public static int bitcount(int u){
int n;
for(n= 0;u > 0;++n, u&= (u - 1));
return n;
}
public static LinkedList<String> comb(int c, int n){
LinkedList<String> s= new LinkedList<String>();
for(int u= 0;u < 1 << n;u++)
if(bitcount(u) == c) s.push(bitprint(u));
Collections.sort(s);
return s;
}
}
|
Generate an equivalent Python version of this Swift code. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| >>> from itertools import combinations
>>> list(combinations(range(5),3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
|
Write the same code in VB as shown below in Swift. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| input "Enter n comb m. ", n
input m
outstr$ = ""
call iterate (outstr$, 0, m-1, n-1)
end
subroutine iterate (curr$, start, stp, depth)
for i = start to stp
if depth = 0 then print curr$ + " " + string(i)
call iterate (curr$ + " " + string(i), i+1, stp, depth-1)
next i
end subroutine
|
Write a version of this Swift function in Go with identical behavior. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| package main
import (
"fmt"
)
func main() {
comb(5, 3, func(c []int) {
fmt.Println(c)
})
}
func comb(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
|
Convert this Tcl block to C, preserving its control flow and logic. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| #include <stdio.h>
typedef unsigned long marker;
marker one = 1;
void comb(int pool, int need, marker chosen, int at)
{
if (pool < need + at) return;
if (!need) {
for (at = 0; at < pool; at++)
if (chosen & (one << at)) printf("%d ", at);
printf("\n");
return;
}
comb(pool, need - 1, chosen | (one << at), at + 1);
comb(pool, need, chosen, at + 1);
}
int main()
{
comb(5, 3, 0, 0);
return 0;
}
|
Rewrite this program in C# while keeping its functionality equivalent to the Tcl version. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| using System;
using System.Collections.Generic;
public class Program
{
public static IEnumerable<int[]> Combinations(int m, int n)
{
int[] result = new int[m];
Stack<int> stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0)
{
int index = stack.Count - 1;
int value = stack.Pop();
while (value < n)
{
result[index++] = ++value;
stack.Push(value);
if (index == m)
{
yield return result;
break;
}
}
}
}
static void Main()
{
foreach (int[] c in Combinations(3, 5))
{
Console.WriteLine(string.Join(",", c));
Console.WriteLine();
}
}
}
|
Write a version of this Tcl function in C++ with identical behavior. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| #include <algorithm>
#include <iostream>
#include <string>
void comb(int N, int K)
{
std::string bitmask(K, 1);
bitmask.resize(N, 0);
do {
for (int i = 0; i < N; ++i)
{
if (bitmask[i]) std::cout << " " << i;
}
std::cout << std::endl;
} while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
int main()
{
comb(5, 3);
}
|
Port the provided Tcl code into Java while preserving the original functionality. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| import java.util.Collections;
import java.util.LinkedList;
public class Comb{
public static void main(String[] args){
System.out.println(comb(3,5));
}
public static String bitprint(int u){
String s= "";
for(int n= 0;u > 0;++n, u>>= 1)
if((u & 1) > 0) s+= n + " ";
return s;
}
public static int bitcount(int u){
int n;
for(n= 0;u > 0;++n, u&= (u - 1));
return n;
}
public static LinkedList<String> comb(int c, int n){
LinkedList<String> s= new LinkedList<String>();
for(int u= 0;u < 1 << n;u++)
if(bitcount(u) == c) s.push(bitprint(u));
Collections.sort(s);
return s;
}
}
|
Transform the following Tcl implementation into Python, maintaining the same output and logic. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| >>> from itertools import combinations
>>> list(combinations(range(5),3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
|
Change the following Tcl code into VB without altering its purpose. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| input "Enter n comb m. ", n
input m
outstr$ = ""
call iterate (outstr$, 0, m-1, n-1)
end
subroutine iterate (curr$, start, stp, depth)
for i = start to stp
if depth = 0 then print curr$ + " " + string(i)
call iterate (curr$ + " " + string(i), i+1, stp, depth-1)
next i
end subroutine
|
Translate the given Tcl code snippet into Go without altering its behavior. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| package main
import (
"fmt"
)
func main() {
comb(5, 3, func(c []int) {
fmt.Println(c)
})
}
func comb(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
|
Generate a PHP translation of this Rust snippet without changing its computational steps. | fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Ensure the translated PHP code behaves exactly like the original Ada snippet. | with Ada.Text_IO; use Ada.Text_IO;
procedure Test_Combinations is
generic
type Integers is range <>;
package Combinations is
type Combination is array (Positive range <>) of Integers;
procedure First (X : in out Combination);
procedure Next (X : in out Combination);
procedure Put (X : Combination);
end Combinations;
package body Combinations is
procedure First (X : in out Combination) is
begin
X (1) := Integers'First;
for I in 2..X'Last loop
X (I) := X (I - 1) + 1;
end loop;
end First;
procedure Next (X : in out Combination) is
begin
for I in reverse X'Range loop
if X (I) < Integers'Val (Integers'Pos (Integers'Last) - X'Last + I) then
X (I) := X (I) + 1;
for J in I + 1..X'Last loop
X (J) := X (J - 1) + 1;
end loop;
return;
end if;
end loop;
raise Constraint_Error;
end Next;
procedure Put (X : Combination) is
begin
for I in X'Range loop
Put (Integers'Image (X (I)));
end loop;
end Put;
end Combinations;
type Five is range 0..4;
package Fives is new Combinations (Five);
use Fives;
X : Combination (1..3);
begin
First (X);
loop
Put (X); New_Line;
Next (X);
end loop;
exception
when Constraint_Error =>
null;
end Test_Combinations;
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Translate the given AutoHotKey code snippet into PHP without altering its behavior. | MsgBox % Comb(1,1)
MsgBox % Comb(3,3)
MsgBox % Comb(3,2)
MsgBox % Comb(2,3)
MsgBox % Comb(5,3)
Comb(n,t) {Β
IfLess n,%t%, Return
Loop %t%
c%A_Index% := A_Index
i := t+1, c%i% := n+1
Loop {
Loop %t%
i := t+1-A_Index, c .= c%i% " "
c .= "`n" Β
j := 1, i := 2
Loop
If (c%j%+1 = c%i%)
c%j% := j, ++j, ++i
Else Break
If (j > t)
Return c
c%j% += 1
}
}
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Produce a language-to-language conversion: from AWK to PHP, same semantics. | BEGIN {
if (length(r) == 0) r = 3
if (length(n) == 0) n = 5
for (i=1; i <= r; i++) {
A[i] = i
if (i < r ) printf i OFS
else print i}
while (A[1] < n - r + 1) {
for (i = r; i >= 1; i--) {
if (A[i] < n - r + i) {
A[i]++
p = i
break}}
for (i = p + 1; i <= r; i++) A[i] = A[i - 1] + 1
for (i=1; i <= r; i++) {
if (i < r) printf A[i] OFS
else print A[i]}}
exit}
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Convert this BBC_Basic snippet to PHP and keep its semantics consistent. | INSTALL @lib$+"SORTLIB"
sort% = FN_sortinit(0,0)
M% = 3
N% = 5
C% = FNfact(N%)/(FNfact(M%)*FNfact(N%-M%))
DIM s$(C%)
PROCcomb(M%, N%, s$())
CALL sort%, s$(0)
FOR I% = 0 TO C%-1
PRINT s$(I%)
NEXT
END
DEF PROCcomb(C%, N%, s$())
LOCAL I%, U%
FOR U% = 0 TO 2^N%-1
IF FNbits(U%) = C% THEN
s$(I%) = FNlist(U%)
I% += 1
ENDIF
NEXT
ENDPROC
DEF FNbits(U%)
LOCAL N%
WHILE U%
N% += 1
U% = U% AND (U%-1)
ENDWHILE
= N%
DEF FNlist(U%)
LOCAL N%, s$
WHILE U%
IF U% AND 1 s$ += STR$(N%) + " "
N% += 1
U% = U% >> 1
ENDWHILE
= s$
DEF FNfact(N%)
IF N%<=1 THEN = 1 ELSE = N%*FNfact(N%-1)
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Convert this Clojure block to PHP, preserving its control flow and logic. | (defn combinations
"If m=1, generate a nested list of numbers [0,n)
If m>1, for each x in [0,n), and for each list in the recursion on [x+1,n), cons the two"
[m n]
(letfn [(comb-aux
[m start]
(if (= 1 m)
(for [x (range start n)]
(list x))
(for [x (range start n)
xs (comb-aux (dec m) (inc x))]
(cons x xs))))]
(comb-aux m 0)))
(defn print-combinations
[m n]
(doseq [line (combinations m n)]
(doseq [n line]
(printf "%s " n))
(printf "%n")))
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Generate an equivalent PHP version of this Common_Lisp code. | (defun map-combinations (m n fn)
"Call fn with each m combination of the integers from 0 to n-1 as a list. The list may be destroyed after fn returns."
(let ((combination (make-list m)))
(labels ((up-from (low)
(let ((start (1- low)))
(lambda () (incf start))))
(mc (curr left needed comb-tail)
(cond
((zerop needed)
(funcall fn combination))
((= left needed)
(map-into comb-tail (up-from curr))
(funcall fn combination))
(t
(setf (first comb-tail) curr)
(mc (1+ curr) (1- left) (1- needed) (rest comb-tail))
(mc (1+ curr) (1- left) needed comb-tail)))))
(mc 0 n m combination))))
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Write a version of this D function in PHP with identical behavior. | T[][] comb(T)(in T[] arr, in int k) pure nothrow {
if (k == 0) return [[]];
typeof(return) result;
foreach (immutable i, immutable x; arr)
foreach (suffix; arr[i + 1 .. $].comb(k - 1))
result ~= x ~ suffix;
return result;
}
void main() {
import std.stdio;
[0, 1, 2, 3].comb(2).writeln;
}
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Port the following code from Elixir to PHP with equivalent syntax and logic. | defmodule RC do
def comb(0, _), do: [[]]
def comb(_, []), do: []
def comb(m, [h|t]) do
(for l <- comb(m-1, t), do: [h|l]) ++ comb(m, t)
end
end
{m, n} = {3, 5}
list = for i <- 1..n, do: i
Enum.each(RC.comb(m, list), fn x -> IO.inspect x end)
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Please provide an equivalent version of this Erlang code in PHP. | -module(comb).
-compile(export_all).
comb(0,_) ->
[[]];
comb(_,[]) ->
[];
comb(N,[H|T]) ->
[[H|L] || L <- comb(N-1,T)]++comb(N,T).
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Rewrite this program in PHP while keeping its functionality equivalent to the F# version. | let choose m n =
let rec fC prefix m from = seq {
let rec loopFor f = seq {
match f with
| [] -> ()
| x::xs ->
yield (x, fC [] (m-1) xs)
yield! loopFor xs
}
if m = 0 then yield prefix
else
for (i, s) in loopFor from do
for x in s do
yield prefix@[i]@x
}
fC [] m [0..(n-1)]
[<EntryPoint>]
let main argv =
choose 3 5
|> Seq.iter (printfn "%A")
0
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Generate a PHP translation of this Factor snippet without changing its computational steps. | USING: math.combinatorics prettyprint ;
5 iota 3 all-combinations .
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Produce a language-to-language conversion: from Fortran to PHP, same semantics. | program Combinations
use iso_fortran_env
implicit none
type comb_result
integer, dimension(:), allocatable :: combs
end type comb_result
type(comb_result), dimension(:), pointer :: r
integer :: i, j
call comb(5, 3, r)
do i = 0, choose(5, 3) - 1
do j = 2, 0, -1
write(*, "(I4, ' ')", advance="no") r(i)%combs(j)
end do
deallocate(r(i)%combs)
write(*,*) ""
end do
deallocate(r)
contains
function choose(n, k, err)
integer :: choose
integer, intent(in) :: n, k
integer, optional, intent(out) :: err
integer :: imax, i, imin, ie
ie = 0
if ( (n < 0 ) .or. (k < 0 ) ) then
write(ERROR_UNIT, *) "negative in choose"
choose = 0
ie = 1
else
if ( n < k ) then
choose = 0
else if ( n == k ) then
choose = 1
else
imax = max(k, n-k)
imin = min(k, n-k)
choose = 1
do i = imax+1, n
choose = choose * i
end do
do i = 2, imin
choose = choose / i
end do
end if
end if
if ( present(err) ) err = ie
end function choose
subroutine comb(n, k, co)
integer, intent(in) :: n, k
type(comb_result), dimension(:), pointer, intent(out) :: co
integer :: i, j, s, ix, kx, hm, t
integer :: err
hm = choose(n, k, err)
if ( err /= 0 ) then
nullify(co)
return
end if
allocate(co(0:hm-1))
do i = 0, hm-1
allocate(co(i)%combs(0:k-1))
end do
do i = 0, hm-1
ix = i; kx = k
do s = 0, n-1
if ( kx == 0 ) exit
t = choose(n-(s+1), kx-1)
if ( ix < t ) then
co(i)%combs(kx-1) = s
kx = kx - 1
else
ix = ix - t
end if
end do
end do
end subroutine comb
end program Combinations
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Translate this program into PHP but keep the logic exactly as in Groovy. | def comb
comb = { m, list ->
def n = list.size()
m == 0 ?
[[]] :
(0..(n-m)).inject([]) { newlist, k ->
def sublist = (k+1 == n) ? [] : list[(k+1)..<n]
newlist += comb(m-1, sublist).collect { [list[k]] + it }
}
}
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Preserve the algorithm and functionality while converting the code from Haskell to PHP. | comb :: Int -> [a] -> [[a]]
comb 0 _ = [[]]
comb _ [] = []
comb m (x:xs) = map (x:) (comb (m-1) xs) ++ comb m xs
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Can you help me rewrite this code in PHP instead of Icon, keeping it the same logically? | procedure main()
return combinations(3,5,0)
end
procedure combinations(m,n,z)
/z := 1
write(m," combinations of ",n," integers starting from ",z)
every put(L := [], z to n - 1 + z by 1)
write("Intial list\n",list2string(L))
write("Combinations:")
every write(list2string(lcomb(L,m)))
end
procedure list2string(L)
every (s := "[") ||:= " " || (!L|"]")
return s
end
link lists
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Please provide an equivalent version of this J code in PHP. | require'stats'
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Translate this program into PHP but keep the logic exactly as in Julia. | using Combinatorics
n = 4
m = 3
for i in combinations(0:n,m)
println(i')
end
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Maintain the same structure and functionality when rewriting this code in PHP. | function map(f, a, ...) if a then return f(a), map(f, ...) end end
function incr(k) return function(a) return k > a and a or a+1 end end
function combs(m, n)
if m * n == 0 then return {{}} end
local ret, old = {}, combs(m-1, n-1)
for i = 1, n do
for k, v in ipairs(old) do ret[#ret+1] = {i, map(incr(i), unpack(v))} end
end
return ret
end
for k, v in ipairs(combs(3, 5)) do print(unpack(v)) end
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Produce a language-to-language conversion: from Mathematica to PHP, same semantics. | combinations[n_Integer, m_Integer]/;m>= 0:=Union[Sort /@ Permutations[Range[0, n - 1], {m}]]
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Write a version of this MATLAB function in PHP with identical behavior. | >> nchoosek((0:4),3)
ans =
0 1 2
0 1 3
0 1 4
0 2 3
0 2 4
0 3 4
1 2 3
1 2 4
1 3 4
2 3 4
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Preserve the algorithm and functionality while converting the code from Nim to PHP. | iterator comb(m, n: int): seq[int] =
var c = newSeq[int](n)
for i in 0 ..< n: c[i] = i
block outer:
while true:
yield c
var i = n - 1
inc c[i]
if c[i] <= m - 1: continue
while c[i] >= m - n + i:
dec i
if i < 0: break outer
inc c[i]
while i < n-1:
c[i+1] = c[i] + 1
inc i
for i in comb(5, 3):
echo i
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Write a version of this OCaml function in PHP with identical behavior. | let combinations m n =
let rec c = function
| (0,_) -> [[]]
| (_,0) -> []
| (p,q) -> List.append
(List.map (List.cons (n-q)) (c (p-1, q-1)))
(c (p , q-1))
in c (m , n)
let () =
let rec print_list = function
| [] -> print_newline ()
| hd :: tl -> print_int hd ; print_string " "; print_list tl
in List.iter print_list (combinations 3 5)
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Write the same algorithm in PHP as shown in this Pascal implementation. | Program Combinations;
const
m_max = 3;
n_max = 5;
var
combination: array [0..m_max] of integer;
procedure generate(m: integer);
var
n, i: integer;
begin
if (m > m_max) then
begin
for i := 1 to m_max do
write (combination[i], ' ');
writeln;
end
else
for n := 1 to n_max do
if ((m = 1) or (n > combination[m-1])) then
begin
combination[m] := n;
generate(m + 1);
end;
end;
begin
generate(1);
end.
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Write a version of this Perl function in PHP with identical behavior. | use ntheory qw/forcomb/;
forcomb { print "@_\n" } 5,3
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Rewrite this program in PHP while keeping its functionality equivalent to the PowerShell version. | $source = @'
using System;
using System.Collections.Generic;
namespace Powershell
{
public class CSharp
{
public static IEnumerable<int[]> Combinations(int m, int n)
{
int[] result = new int[m];
Stack<int> stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0) {
int index = stack.Count - 1;
int value = stack.Pop();
while (value < n) {
result[index++] = value++;
stack.Push(value);
if (index == m) {
yield return result;
break;
}
}
}
}
}
}
'@
Add-Type -TypeDefinition $source -Language CSharp
[Powershell.CSharp]::Combinations(3,5) | Format-Wide {$_} -Column 3 -Force
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Generate an equivalent PHP version of this R code. | print(combn(0:4, 3))
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Can you help me rewrite this code in PHP instead of Racket, keeping it the same logically? | (define sublists
(match-lambda**
[(0 _) '(())]
[(_ '()) '()]
[(m (cons x xs)) (append (map (curry cons x) (sublists (- m 1) xs))
(sublists m xs))]))
(define (combinations n m)
(sublists n (range m)))
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Write the same code in PHP as shown below in REXX. |
parse arg x y $ .
if x=='' | x=="," then x= 5
if y=='' | y=="," then y= 3; oy= y; y= abs(y)
if $=='' | $=="," then $='123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
"~!@#$%^&*()_+`{}|[]\:;<>?,./βββββΒ±β₯β€ββ"
if y>x then do; say y " can't be greater than " x; exit 1; end
say "ββββββββββββ" x ' things taken ' y " at a time:"
say "ββββββββββββ" combN(x,y) ' combinations.'
exit
combN: procedure expose $ oy; parse arg x,y; xp= x+1; xm= xp-y; !.= 0
if x=0 | y=0 then return 'no'
do i=1 for y; !.i= i
end
do j=1; L=
do d=1 for y; L= L substr($, !.d, 1)
end
if oy>0 then say L; !.y= !.y + 1
if !.y==xp then if .combN(y-1) then leave
end
return j
.combN: procedure expose !. y xm; parse arg d; if d==0 then return 1; p= !.d
do u=d to y; !.u= p+1; if !.u==xm+u then return .combN(u-1); p= !.u
end
return 0
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Port the following code from Ruby to PHP with equivalent syntax and logic. | def comb(m, n)
(0...n).to_a.each_combination(m) { |p| puts(p) }
end
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Port the following code from Scala to PHP with equivalent syntax and logic. | implicit def toComb(m: Int) = new AnyRef {
def comb(n: Int) = recurse(m, List.range(0, n))
private def recurse(m: Int, l: List[Int]): List[List[Int]] = (m, l) match {
case (0, _) => List(Nil)
case (_, Nil) => Nil
case _ => (recurse(m - 1, l.tail) map (l.head :: _)) ::: recurse(m, l.tail)
}
}
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Produce a functionally identical PHP code for the snippet given in Swift. | func addCombo(prevCombo: [Int], var pivotList: [Int]) -> [([Int], [Int])] {
return (0..<pivotList.count)
.map {
_ -> ([Int], [Int]) in
(prevCombo + [pivotList.removeAtIndex(0)], pivotList)
}
}
func combosOfLength(n: Int, m: Int) -> [[Int]] {
return [Int](1...m)
.reduce([([Int](), [Int](0..<n))]) {
(accum, _) in
accum.flatMap(addCombo)
}.map {
$0.0
}
}
println(combosOfLength(5, 3))
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Translate the given Tcl code snippet into PHP without altering its behavior. | proc comb {m n} {
set set [list]
for {set i 0} {$i < $n} {incr i} {lappend set $i}
return [combinations $set $m]
}
proc combinations {list size} {
if {$size == 0} {
return [list [list]]
}
set retval {}
for {set i 0} {($i + $size) <= [llength $list]} {incr i} {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr {$i + 1}] end]
foreach subset [combinations $remainingElements [expr {$size - 1}]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
comb 3 5 ;
| <?php
$a=array(1,2,3,4,5);
$k=3;
$n=5;
$c=array_splice($a, $k);
$b=array_splice($a, 0, $k);
$j=$k-1;
print_r($b);
while (1) {
$m=array_search($b[$j]+1,$c);
if ($m!==false) {
$c[$m]-=1;
$b[$j]=$b[$j]+1;
print_r($b);
}
if ($b[$k-1]==$n) {
$i=$k-1;
while ($i >= 0) {
if ($i == 0 && $b[$i] == $n-$k+1) break 2;
$m=array_search($b[$i]+1,$c);
if ($m!==false) {
$c[$m]=$c[$m]-1;
$b[$i]=$b[$i]+1;
$g=$i;
while ($g != $k-1) {
array_unshift ($c, $b[$g+1]);
$b[$g+1]=$b[$g]+1;
$g++;
}
$c=array_diff($c,$b);
print_r($b);
break;
}
$i--;
}
}
}
?>
|
Maintain the same structure and functionality when rewriting this code in Rust. | #include <stdio.h>
typedef unsigned long marker;
marker one = 1;
void comb(int pool, int need, marker chosen, int at)
{
if (pool < need + at) return;
if (!need) {
for (at = 0; at < pool; at++)
if (chosen & (one << at)) printf("%d ", at);
printf("\n");
return;
}
comb(pool, need - 1, chosen | (one << at), at + 1);
comb(pool, need, chosen, at + 1);
}
int main()
{
comb(5, 3, 0, 0);
return 0;
}
| fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
|
Generate an equivalent Rust version of this Java code. | import java.util.Collections;
import java.util.LinkedList;
public class Comb{
public static void main(String[] args){
System.out.println(comb(3,5));
}
public static String bitprint(int u){
String s= "";
for(int n= 0;u > 0;++n, u>>= 1)
if((u & 1) > 0) s+= n + " ";
return s;
}
public static int bitcount(int u){
int n;
for(n= 0;u > 0;++n, u&= (u - 1));
return n;
}
public static LinkedList<String> comb(int c, int n){
LinkedList<String> s= new LinkedList<String>();
for(int u= 0;u < 1 << n;u++)
if(bitcount(u) == c) s.push(bitprint(u));
Collections.sort(s);
return s;
}
}
| fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
|
Please provide an equivalent version of this Go code in Rust. | package main
import (
"fmt"
)
func main() {
comb(5, 3, func(c []int) {
fmt.Println(c)
})
}
func comb(n, m int, emit func([]int)) {
s := make([]int, m)
last := m - 1
var rc func(int, int)
rc = func(i, next int) {
for j := next; j < n; j++ {
s[i] = j
if i == last {
emit(s)
} else {
rc(i+1, j+1)
}
}
return
}
rc(0, 0)
}
| fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
|
Rewrite the snippet below in Python so it works the same as the original Rust code. | fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
| >>> from itertools import combinations
>>> list(combinations(range(5),3))
[(0, 1, 2), (0, 1, 3), (0, 1, 4), (0, 2, 3), (0, 2, 4), (0, 3, 4), (1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
|
Ensure the translated VB code behaves exactly like the original Rust snippet. | fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
| Option Explicit
Option Base 0
Private ArrResult
Sub test()
Main_Combine 5, 3
Dim j As Long, i As Long, temp As String
For i = LBound(ArrResult, 1) To UBound(ArrResult, 1)
temp = vbNullString
For j = LBound(ArrResult, 2) To UBound(ArrResult, 2)
temp = temp & " " & ArrResult(i, j)
Next
Debug.Print temp
Next
Erase ArrResult
End Sub
Private Sub Main_Combine(M As Long, N As Long)
Dim MyArr, i As Long
ReDim MyArr(M - 1)
If LBound(MyArr) > 0 Then ReDim MyArr(M)
For i = LBound(MyArr) To UBound(MyArr)
MyArr(i) = i
Next i
i = IIf(LBound(MyArr) > 0, N, N - 1)
ReDim ArrResult(i, LBound(MyArr))
Combine MyArr, N, LBound(MyArr), LBound(MyArr)
ReDim Preserve ArrResult(UBound(ArrResult, 1), UBound(ArrResult, 2) - 1)
ArrResult = Transposition(ArrResult)
End Sub
Private Sub Combine(MyArr As Variant, Nb As Long, Deb As Long, Ind As Long)
Dim i As Long, j As Long, N As Long
For i = Deb To UBound(MyArr, 1)
ArrResult(Ind, UBound(ArrResult, 2)) = MyArr(i)
N = IIf(LBound(ArrResult, 1) = 0, Nb - 1, Nb)
If Ind = N Then
ReDim Preserve ArrResult(UBound(ArrResult, 1), UBound(ArrResult, 2) + 1)
For j = LBound(ArrResult, 1) To UBound(ArrResult, 1)
ArrResult(j, UBound(ArrResult, 2)) = ArrResult(j, UBound(ArrResult, 2) - 1)
Next j
Else
Call Combine(MyArr, Nb, i + 1, Ind + 1)
End If
Next i
End Sub
Private Function Transposition(ByRef MyArr As Variant) As Variant
Dim T, i As Long, j As Long
ReDim T(LBound(MyArr, 2) To UBound(MyArr, 2), LBound(MyArr, 1) To UBound(MyArr, 1))
For i = LBound(MyArr, 1) To UBound(MyArr, 1)
For j = LBound(MyArr, 2) To UBound(MyArr, 2)
T(j, i) = MyArr(i, j)
Next j
Next i
Transposition = T
Erase T
End Function
|
Translate this program into Rust but keep the logic exactly as in C#. | using System;
using System.Collections.Generic;
public class Program
{
public static IEnumerable<int[]> Combinations(int m, int n)
{
int[] result = new int[m];
Stack<int> stack = new Stack<int>();
stack.Push(0);
while (stack.Count > 0)
{
int index = stack.Count - 1;
int value = stack.Pop();
while (value < n)
{
result[index++] = ++value;
stack.Push(value);
if (index == m)
{
yield return result;
break;
}
}
}
}
static void Main()
{
foreach (int[] c in Combinations(3, 5))
{
Console.WriteLine(string.Join(",", c));
Console.WriteLine();
}
}
}
| fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
|
Write the same algorithm in Rust as shown in this C++ implementation. | #include <algorithm>
#include <iostream>
#include <string>
void comb(int N, int K)
{
std::string bitmask(K, 1);
bitmask.resize(N, 0);
do {
for (int i = 0; i < N; ++i)
{
if (bitmask[i]) std::cout << " " << i;
}
std::cout << std::endl;
} while (std::prev_permutation(bitmask.begin(), bitmask.end()));
}
int main()
{
comb(5, 3);
}
| fn comb<T: std::fmt::Default>(arr: &[T], n: uint) {
let mut incl_arr: ~[bool] = std::vec::from_elem(arr.len(), false);
comb_intern(arr, n, incl_arr, 0);
}
fn comb_intern<T: std::fmt::Default>(arr: &[T], n: uint, incl_arr: &mut [bool], index: uint) {
if (arr.len() < n + index) { return; }
if (n == 0) {
let mut it = arr.iter().zip(incl_arr.iter()).filter_map(|(val, incl)|
if (*incl) { Some(val) } else { None }
);
for val in it { print!("{} ", *val); }
print("\n");
return;
}
incl_arr[index] = true;
comb_intern(arr, n-1, incl_arr, index+1);
incl_arr[index] = false;
comb_intern(arr, n, incl_arr, index+1);
}
fn main() {
let arr1 = ~[1, 2, 3, 4, 5];
comb(arr1, 3);
let arr2 = ~["A", "B", "C", "D", "E"];
comb(arr2, 3);
}
|
Rewrite this program in C# while keeping its functionality equivalent to the Ada version. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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 programming language of this snippet from Ada to C# without modifying what it does. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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;
}
}
|
Produce a functionally identical C code for the snippet given in Ada. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| #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;
}
|
Please provide an equivalent version of this Ada code in C. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| #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;
}
|
Please provide an equivalent version of this Ada code in C++. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| #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));
}
|
Produce a language-to-language conversion: from Ada to C++, same semantics. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| #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));
}
|
Produce a functionally identical Go code for the snippet given in Ada. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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)
}
}
|
Write the same algorithm in Go as shown in this Ada implementation. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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)
}
}
|
Preserve the algorithm and functionality while converting the code from Ada to Java. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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 Ada function in Java with identical behavior. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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));
}
}
|
Change the programming language of this snippet from Ada to Python without modifying what it does. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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)
|
Port the following code from Ada to Python with equivalent syntax and logic. | package binning is
type Nums_Array is array (Natural range <>) of Integer;
function Is_Sorted (Item : Nums_Array) return Boolean;
subtype Limits_Array is Nums_Array with
Dynamic_Predicate => Is_Sorted (Limits_Array);
function Bins (Limits : Limits_Array; Data : Nums_Array) return Nums_Array;
procedure Print (Limits : Limits_Array; Bin_Result : Nums_Array);
end binning;
| 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)
|
Port the provided AutoHotKey code into C while preserving the original functionality. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| #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;
}
|
Preserve the algorithm and functionality while converting the code from AutoHotKey to C. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| #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;
}
|
Produce a functionally identical C# code for the snippet given in AutoHotKey. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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 algorithm in C# as shown in this AutoHotKey implementation. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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 AutoHotKey. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| #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 provided AutoHotKey code into C++ while preserving the original functionality. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| #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 this program into Java but keep the logic exactly as in AutoHotKey. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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));
}
}
|
Change the following AutoHotKey code into Java without altering its purpose. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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));
}
}
|
Port the provided AutoHotKey code into Python while preserving the original functionality. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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)
|
Ensure the translated Python code behaves exactly like the original AutoHotKey snippet. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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)
|
Maintain the same structure and functionality when rewriting this code in Go. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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)
}
}
|
Preserve the algorithm and functionality while converting the code from AutoHotKey to Go. | Bin_given_limits(limits, data){
bin := [], counter := 0
for i, val in data {
if (limits[limits.count()] <= val)
bin["β", ++counter] := val
else for j, limit in limits
if (limits[j-1] <= val && val < limits[j])
bin[limit, ++counter] := val
}
for j, limit in limits {
output .= (prevlimit ? prevlimit : "-β") ", " limit "Β : " ((x:=bin[limit].Count())?x:0) "`n"
prevlimit := limit
}
return output .= (prevlimit ? prevlimit : "-β") ", βΒ : " ((x:=bin["β"].Count())?x:0) "`n"
}
| 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)
}
}
|
Rewrite this program in C while keeping its functionality equivalent to the Factor version. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| #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 Factor code into C without altering its purpose. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| #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;
}
|
Translate the given Factor code snippet into C# without altering its behavior. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| 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;
}
}
|
Produce a functionally identical C# code for the snippet given in Factor. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| 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 Factor. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| #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));
}
|
Keep all operations the same but rewrite the snippet in C++. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| #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 this program into Java but keep the logic exactly as in Factor. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| 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 Factor function in Java with identical behavior. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| 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 Factor. | USING: assocs formatting grouping io kernel math math.parser
math.statistics sequences sequences.extras sorting.extras ;
: bin ( data limits -- seq )
dup length 1 + [ 0 ] replicate -rot
[ bisect-right over [ 1 + ] change-nth ] curry each ;
: .bin ( {lo,hi} n i -- )
swap "%3d members in " printf zero? "(" "[" ? write
"%s, %s)\n" vprintf ;
: .bins ( data limits -- )
dup [ number>string ] map "-β" prefix "β" suffix 2 clump
-rot bin [ .bin ] 2each-index ;
"First example:" print
{
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
}
{ 23 37 43 53 67 83 } .bins nl
"Second example:" print
{
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
}
{ 14 18 249 312 389 392 513 591 634 720 } .bins
| 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)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.