Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Maintain the same structure and functionality when rewriting this code in C++.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Produce a language-to-language conversion: from Lua to C++, same semantics.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Rewrite this program in Java while keeping its functionality equivalent to the Lua version.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Convert this Lua snippet to Java and keep its semantics consistent.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Translate this program into Python but keep the logic exactly as in Lua.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Change the programming language of this snippet from Lua to Python without modifying what it does.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Write the same code in VB as shown below in Lua.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Translate this program into VB but keep the logic exactly as in Lua.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Transform the following Lua implementation into Go, maintaining the same output and logic.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Produce a language-to-language conversion: from Lua to Go, same semantics.
print( "Fire", "Police", "Sanitation" ) sol = 0 for f = 1, 7 do for p = 1, 7 do for s = 1, 7 do if s + p + f == 12 and p % 2 == 0 and f ~= p and f ~= s and p ~= s then print( f, p, s ); sol = sol + 1 end end end end print( string.format( "\n%d solutions found", sol ) )
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Port the provided Mathematica code into C while preserving the original functionality.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Write the same algorithm in C as shown in this Mathematica implementation.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Rewrite the snippet below in C# so it works the same as the original Mathematica code.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Write the same algorithm in C# as shown in this Mathematica implementation.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Port the following code from Mathematica to C++ with equivalent syntax and logic.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Rewrite this program in C++ while keeping its functionality equivalent to the Mathematica version.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Generate an equivalent Java version of this Mathematica code.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Translate this program into Java but keep the logic exactly as in Mathematica.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Write a version of this Mathematica function in Python with identical behavior.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Rewrite this program in Python while keeping its functionality equivalent to the Mathematica version.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Translate this program into VB but keep the logic exactly as in Mathematica.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Preserve the algorithm and functionality while converting the code from Mathematica to VB.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Transform the following Mathematica implementation into Go, maintaining the same output and logic.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Generate a Go translation of this Mathematica snippet without changing its computational steps.
Select[Permutations[Range[7], {3}], Total[#] == 12 && EvenQ[First[#]] &]
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Write the same algorithm in C as shown in this Nim implementation.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Transform the following Nim implementation into C, maintaining the same output and logic.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Write the same code in C# as shown below in Nim.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Keep all operations the same but rewrite the snippet in C#.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Generate an equivalent C++ version of this Nim code.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Convert the following code from Nim to C++, ensuring the logic remains intact.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Write the same algorithm in Java as shown in this Nim implementation.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Can you help me rewrite this code in Java instead of Nim, keeping it the same logically?
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Produce a functionally identical Python code for the snippet given in Nim.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Port the following code from Nim to Python with equivalent syntax and logic.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Transform the following Nim implementation into VB, maintaining the same output and logic.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Convert this Nim snippet to VB and keep its semantics consistent.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Port the following code from Nim to Go with equivalent syntax and logic.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Port the provided Nim code into Go while preserving the original functionality.
type Solution = tuple[p, s, f: int] iterator solutions(max, total: Positive): Solution = for p in countup(2, max, 2): for s in 1..max: if s == p: continue let f = total - p - s if f notin [p, s] and f in 1..max: yield (p, s, f) echo "P S F" for sol in solutions(7, 12): echo sol.p, " ", sol.s, " ", sol.f
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Maintain the same structure and functionality when rewriting this code in C.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Change the programming language of this snippet from OCaml to C without modifying what it does.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Port the following code from OCaml to C# with equivalent syntax and logic.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Produce a functionally identical C# code for the snippet given in OCaml.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Produce a functionally identical C++ code for the snippet given in OCaml.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Translate the given OCaml code snippet into C++ without altering its behavior.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Transform the following OCaml implementation into Java, maintaining the same output and logic.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Translate the given OCaml code snippet into Java without altering its behavior.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Generate a Python translation of this OCaml snippet without changing its computational steps.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Write a version of this OCaml function in Python with identical behavior.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Rewrite this program in VB while keeping its functionality equivalent to the OCaml version.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Transform the following OCaml implementation into Go, maintaining the same output and logic.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Rewrite this program in Go while keeping its functionality equivalent to the OCaml version.
type sfp = {s : int; f : int; p : int} let print_sfp e = Printf.printf "%d %d %d\n" e.s e.f e.p let print_sfp_list l = l |> List.iter print_sfp let sum l = List.fold_left (+) 0 l let element_in_list e l = l |> List.find_map (fun x -> if x == e then Some(e) else None) <> None let uniq l = let rec uniq_helper acc l = match l with | [] -> acc | h::t -> if element_in_list h t then uniq_helper acc t else uniq_helper (h::acc) t in uniq_helper [] l |> List.rev let is_uniq l = uniq l = l let department_numbers sl fl pl = sl |> List.fold_left (fun aa s -> fl |> List.fold_left (fun fa f -> pl |> List.fold_left (fun pa p -> if sum [s;f;p] == 12 && is_uniq [s;f;p] then {s = s; f = f; p = p} :: pa else pa) [] |> List.append fa) [] |> List.append aa) [] let _ = let s = [1;2;3;4;5;6;7] in let f = [1;2;3;4;5;6;7] in let p = [2;4;6] in let result = department_numbers s f p in print_endline "S F P"; print_sfp_list result;
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Write a version of this Perl function in C with identical behavior.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Change the programming language of this snippet from Perl to C without modifying what it does.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Rewrite the snippet below in C# so it works the same as the original Perl code.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Generate an equivalent C# version of this Perl code.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Can you help me rewrite this code in C++ instead of Perl, keeping it the same logically?
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Write the same algorithm in C++ as shown in this Perl implementation.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Port the following code from Perl to Java with equivalent syntax and logic.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Convert the following code from Perl to Java, ensuring the logic remains intact.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Rewrite the snippet below in Python so it works the same as the original Perl code.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Convert the following code from Perl to Python, ensuring the logic remains intact.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Rewrite the snippet below in VB so it works the same as the original Perl code.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Port the following code from Perl to VB with equivalent syntax and logic.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Write the same algorithm in Go as shown in this Perl implementation.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Maintain the same structure and functionality when rewriting this code in Go.
my @even_numbers; for (1..7) { if ( $_ % 2 == 0) { push @even_numbers, $_; } } print "Police\tFire\tSanitation\n"; foreach my $police_number (@even_numbers) { for my $fire_number (1..7) { for my $sanitation_number (1..7) { if ( $police_number + $fire_number + $sanitation_number == 12 && $police_number != $fire_number && $fire_number != $sanitation_number && $sanitation_number != $police_number) { print "$police_number\t$fire_number\t$sanitation_number\n"; } } } }
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Translate the given Racket code snippet into C without altering its behavior.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Generate an equivalent C version of this Racket code.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Change the following Racket code into C# without altering its purpose.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Can you help me rewrite this code in C# instead of Racket, keeping it the same logically?
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Maintain the same structure and functionality when rewriting this code in C++.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Maintain the same structure and functionality when rewriting this code in C++.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Translate the given Racket code snippet into Java without altering its behavior.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Rewrite the snippet below in Java so it works the same as the original Racket code.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Rewrite the snippet below in Python so it works the same as the original Racket code.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Convert the following code from Racket to Python, ensuring the logic remains intact.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Write the same code in VB as shown below in Racket.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Generate a VB translation of this Racket snippet without changing its computational steps.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Port the provided Racket code into Go while preserving the original functionality.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Translate this program into Go but keep the logic exactly as in Racket.
#lang racket (cons '(police fire sanitation) (filter (Ξ» (pfs) (and (not (check-duplicates pfs)) (= 12 (apply + pfs)) pfs)) (cartesian-product (range 2 8 2) (range 1 8) (range 1 8))))
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Write the same code in C as shown below in COBOL.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Change the programming language of this snippet from COBOL to C without modifying what it does.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Port the provided COBOL code into C# while preserving the original functionality.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Change the programming language of this snippet from COBOL to C# without modifying what it does.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Change the programming language of this snippet from COBOL to C++ without modifying what it does.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Generate an equivalent C++ version of this COBOL code.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Port the following code from COBOL to Java with equivalent syntax and logic.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Keep all operations the same but rewrite the snippet in Java.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }
Maintain the same structure and functionality when rewriting this code in Python.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Change the following COBOL code into Python without altering its purpose.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
from itertools import permutations def solve(): c, p, f, s = "\\,Police,Fire,Sanitation".split(',') print(f"{c:>3} {p:^6} {f:^4} {s:^10}") c = 1 for p, f, s in permutations(range(1, 8), r=3): if p + s + f == 12 and p % 2 == 0: print(f"{c:>3}: {p:^6} {f:^4} {s:^10}") c += 1 if __name__ == '__main__': solve()
Ensure the translated VB code behaves exactly like the original COBOL snippet.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Write the same code in VB as shown below in COBOL.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
Module Module1 Sub Main() For p = 2 To 7 Step 2 For s = 1 To 7 Dim f = 12 - p - s If s >= f Then Exit For End If If f > 7 Then Continue For End If If s = p OrElse f = p Then Continue For End If Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}") Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}") Next Next End Sub End Module
Rewrite this program in Go while keeping its functionality equivalent to the COBOL version.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Write the same code in Go as shown below in COBOL.
IDENTIFICATION DIVISION. PROGRAM-ID. DEPARTMENT-NUMBERS. DATA DIVISION. WORKING-STORAGE SECTION. 01 BANNER PIC X(24) VALUE "POLICE SANITATION FIRE". 01 COMBINATION. 03 FILLER PIC X(5) VALUE SPACES. 03 POLICE PIC 9. 03 FILLER PIC X(11) VALUE SPACES. 03 SANITATION PIC 9. 03 FILLER PIC X(5) VALUE SPACES. 03 FIRE PIC 9. 01 TOTAL PIC 99. PROCEDURE DIVISION. BEGIN. DISPLAY BANNER. PERFORM POLICE-LOOP VARYING POLICE FROM 2 BY 2 UNTIL POLICE IS GREATER THAN 6. STOP RUN. POLICE-LOOP. PERFORM SANITATION-LOOP VARYING SANITATION FROM 1 BY 1 UNTIL SANITATION IS GREATER THAN 7. SANITATION-LOOP. PERFORM FIRE-LOOP VARYING FIRE FROM 1 BY 1 UNTIL FIRE IS GREATER THAN 7. FIRE-LOOP. ADD POLICE, SANITATION, FIRE GIVING TOTAL. IF POLICE IS NOT EQUAL TO SANITATION AND POLICE IS NOT EQUAL TO FIRE AND SANITATION IS NOT EQUAL TO FIRE AND TOTAL IS EQUAL TO 12, DISPLAY COMBINATION.
package main import "fmt" func main() { fmt.Println("Police Sanitation Fire") fmt.Println("------ ---------- ----") count := 0 for i := 2; i < 7; i += 2 { for j := 1; j < 8; j++ { if j == i { continue } for k := 1; k < 8; k++ { if k == i || k == j { continue } if i + j + k != 12 { continue } fmt.Printf(" %d %d %d\n", i, j, k) count++ } } } fmt.Printf("\n%d valid combinations\n", count) }
Please provide an equivalent version of this REXX code in C.
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Please provide an equivalent version of this REXX code in C.
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
#include<stdio.h> int main() { int police,sanitation,fire; printf("Police Sanitation Fire\n"); printf("----------------------------------"); for(police=2;police<=6;police+=2){ for(sanitation=1;sanitation<=7;sanitation++){ for(fire=1;fire<=7;fire++){ if(police!=sanitation && sanitation!=fire && fire!=police && police+fire+sanitation==12){ printf("\n%d\t\t%d\t\t%d",police,sanitation,fire); } } } } return 0; }
Generate a C# translation of this REXX snippet without changing its computational steps.
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Keep all operations the same but rewrite the snippet in C#.
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
using System; public class Program { public static void Main() { for (int p = 2; p <= 7; p+=2) { for (int s = 1; s <= 7; s++) { int f = 12 - p - s; if (s >= f) break; if (f > 7) continue; if (s == p || f == p) continue; Console.WriteLine($"Police:{p}, Sanitation:{s}, Fire:{f}"); Console.WriteLine($"Police:{p}, Sanitation:{f}, Fire:{s}"); } } } }
Can you help me rewrite this code in C++ instead of REXX, keeping it the same logically?
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Transform the following REXX implementation into C++, maintaining the same output and logic.
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
#include <iostream> #include <iomanip> int main( int argc, char* argv[] ) { int sol = 1; std::cout << "\t\tFIRE\t\tPOLICE\t\tSANITATION\n"; for( int f = 1; f < 8; f++ ) { for( int p = 1; p < 8; p++ ) { for( int s = 1; s < 8; s++ ) { if( f != p && f != s && p != s && !( p & 1 ) && ( f + s + p == 12 ) ) { std::cout << "SOLUTION #" << std::setw( 2 ) << sol++ << std::setw( 2 ) << ":\t" << std::setw( 2 ) << f << "\t\t " << std::setw( 3 ) << p << "\t\t" << std::setw( 6 ) << s << "\n"; } } } } return 0; }
Generate an equivalent Java version of this REXX code.
say 'police sanitation fire' say '══════ ══════════ ════' #=0 do p=1 for 7; if p//2 then iterate do s=1 for 7; if s==p then iterate do f=1 for 7; if f==s then iterate if p + s + f \== 12 then iterate #= # + 1 say center(p,6) center(s,10) center(f,4) end end end say '══════ ══════════ ════' say say # ' solutions found.'
public class DepartmentNumbers { public static void main(String[] args) { System.out.println("Police Sanitation Fire"); System.out.println("------ ---------- ----"); int count = 0; for (int i = 2; i <= 6; i += 2) { for (int j = 1; j <= 7; ++j) { if (j == i) continue; for (int k = 1; k <= 7; ++k) { if (k == i || k == j) continue; if (i + j + k != 12) continue; System.out.printf(" %d %d %d\n", i, j, k); count++; } } } System.out.printf("\n%d valid combinations", count); } }