Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Change the programming language of this snippet from Julia to Java without modifying what it does.
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Rewrite this program in Python while keeping its functionality equivalent to the Julia version.
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Rewrite the snippet below in Python so it works the same as the original Julia code.
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Translate this program into VB but keep the logic exactly as in Julia.
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Convert the following code from Julia to VB, ensuring the logic remains intact.
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Can you help me rewrite this code in Go instead of Julia, keeping it the same logically?
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Port the following code from Julia to Go with equivalent syntax and logic.
const width = 81 const height = 5 function cantor!(lines, start, len, idx) seg = div(len, 3) if seg > 0 for i in idx+1:height, j in start + seg + 1: start + seg * 2 lines[i, j] = ' ' end cantor!(lines, start, seg, idx + 1) cantor!(lines, start + 2 * seg, seg, idx + 1) end end lines = fill(UInt8(' cantor!(lines, 0, width, 1) for i in 1:height, j in 1:width print(Char(lines[i, j]), j == width ? "\n" : "") end
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Keep all operations the same but rewrite the snippet in C.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Change the following Lua code into C without altering its purpose.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Change the programming language of this snippet from Lua to C# without modifying what it does.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Translate this program into C# but keep the logic exactly as in Lua.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Convert the following code from Lua to C++, ensuring the logic remains intact.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Please provide an equivalent version of this Lua code in C++.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Port the following code from Lua to Java with equivalent syntax and logic.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Write a version of this Lua function in Java with identical behavior.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Rewrite the snippet below in Python so it works the same as the original Lua code.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Can you help me rewrite this code in Python instead of Lua, keeping it the same logically?
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Port the following code from Lua to VB with equivalent syntax and logic.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Translate the given Lua code snippet into VB without altering its behavior.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Translate this program into Go but keep the logic exactly as in Lua.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Ensure the translated Go code behaves exactly like the original Lua snippet.
local WIDTH = 81 local HEIGHT = 5 local lines = {} function cantor(start, length, index) local seg = math.floor(length / 3) if 0 == seg then return nil end for it=0, HEIGHT - index do i = index + it for jt=0, seg - 1 do j = start + seg + jt pos = WIDTH * i + j lines[pos] = ' ' end end cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return nil end for i=0, WIDTH * HEIGHT do lines[i] = '*' end cantor(0, WIDTH, 1) for i=0, HEIGHT-1 do beg = WIDTH * i for j=beg, beg+WIDTH-1 do if j <= WIDTH * HEIGHT then io.write(lines[j]) end end print() end
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Can you help me rewrite this code in C instead of Mathematica, keeping it the same logically?
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Convert this Mathematica snippet to C and keep its semantics consistent.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Convert this Mathematica block to C#, preserving its control flow and logic.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Produce a language-to-language conversion: from Mathematica to C#, same semantics.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Generate a C++ translation of this Mathematica snippet without changing its computational steps.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Translate the given Mathematica code snippet into C++ without altering its behavior.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Please provide an equivalent version of this Mathematica code in Java.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Ensure the translated Java code behaves exactly like the original Mathematica snippet.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Rewrite the snippet below in Python so it works the same as the original Mathematica code.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Port the following code from Mathematica to Python with equivalent syntax and logic.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Produce a language-to-language conversion: from Mathematica to VB, same semantics.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Write a version of this Mathematica function in VB with identical behavior.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Produce a language-to-language conversion: from Mathematica to Go, same semantics.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Generate an equivalent Go version of this Mathematica code.
Graphics[MeshPrimitives[CantorMesh[#],1]/.{x_}:>{x,-0.05#}&/@Range[5],ImageSize->600]
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Rewrite the snippet below in C so it works the same as the original Nim code.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Can you help me rewrite this code in C instead of Nim, keeping it the same logically?
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Write the same code in C# as shown below in Nim.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Generate a C# translation of this Nim snippet without changing its computational steps.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Convert this Nim block to C++, preserving its control flow and logic.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Preserve the algorithm and functionality while converting the code from Nim to C++.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Change the following Nim code into Java without altering its purpose.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Write the same algorithm in Java as shown in this Nim implementation.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Rewrite the snippet below in Python so it works the same as the original Nim code.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Write a version of this Nim function in Python with identical behavior.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Convert this Nim snippet to VB and keep its semantics consistent.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Produce a functionally identical VB code for the snippet given in Nim.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Translate this program into Go but keep the logic exactly as in Nim.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Write the same code in Go as shown below in Nim.
import strutils const Width = 81 Height = 5 var lines: array[Height, string] for line in lines.mitems: line = repeat('*', Width) proc cantor(start, length, index: Natural) = let seg = length div 3 if seg == 0: return for i in index..<Height: for j in (start + seg)..<(start + seg * 2): lines[i][j] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) cantor(0, Width, 1) for line in lines: echo line
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Transform the following Perl implementation into C, maintaining the same output and logic.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Rewrite this program in C while keeping its functionality equivalent to the Perl version.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Rewrite this program in C# while keeping its functionality equivalent to the Perl version.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Convert this Perl block to C#, preserving its control flow and logic.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Write the same code in C++ as shown below in Perl.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Translate this program into C++ but keep the logic exactly as in Perl.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Generate a Java translation of this Perl snippet without changing its computational steps.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Write a version of this Perl function in Java with identical behavior.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Change the programming language of this snippet from Perl to Python without modifying what it does.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Maintain the same structure and functionality when rewriting this code in Python.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Transform the following Perl implementation into VB, maintaining the same output and logic.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Produce a functionally identical VB code for the snippet given in Perl.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Produce a language-to-language conversion: from Perl to Go, same semantics.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Convert this Perl block to Go, preserving its control flow and logic.
use strict; use feature 'say'; sub cantor { our($height) = @_; my $width = 3 ** ($height - 1); our @lines = (' sub trim_middle_third { my($len, $start, $index) = @_; my $seg = int $len / 3 or return; for my $i ( $index .. $height - 1 ) { for my $j ( 0 .. $seg - 1 ) { substr $lines[$i], $start + $seg + $j, 1, ' '; } } trim_middle_third( $seg, $start + $_, $index + 1 ) for 0, $seg * 2; } trim_middle_third( $width, 0, 1 ); @lines; } say for cantor(5);
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Generate a C translation of this R snippet without changing its computational steps.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Port the following code from R to C with equivalent syntax and logic.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Translate this program into C# but keep the logic exactly as in R.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Generate an equivalent C# version of this R code.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Convert this R block to C++, preserving its control flow and logic.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Write a version of this R function in C++ with identical behavior.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Write the same code in Java as shown below in R.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Convert the following code from R to Java, ensuring the logic remains intact.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Please provide an equivalent version of this R code in Python.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Can you help me rewrite this code in Python instead of R, keeping it the same logically?
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Keep all operations the same but rewrite the snippet in VB.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Please provide an equivalent version of this R code in VB.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Write the same algorithm in Go as shown in this R implementation.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Write the same code in Go as shown below in R.
cantorSet <- function() { depth <- 6L cs <- vector('list', depth) cs[[1L]] <- c(0, 1) for(k in seq_len(depth)) { cs[[k + 1L]] <- unlist(sapply(seq_len(length(cs[[k]]) / 2L), function(j) { p <- cs[[k]][2L] / 3 h <- 2L * (j - 1L) c( cs[[k]][h + 1L] + c(0, p), cs[[k]][h + 2L] - c(p, 0) ) }, simplify = FALSE)) } cs } cantorSetGraph <- function() { cs <- cantorSet() u <- unlist(cs) df <- data.frame( x_start = u[seq_along(u) %% 2L == 1L], x_end = u[seq_along(u) %% 2L == 0L], depth = unlist(lapply(cs, function(e) { l <- length(e) n <- 0 while(l > 1) { n <- n + 1L l <- l / 2 } rep(n, length(e) / 2) })) ) require(ggplot2) g <- ggplot(df, aes_string(x = 'x_start', y = 'depth')) + geom_segment(aes_string(xend = 'x_end', yend = 'depth', size = 3)) + scale_y_continuous(trans = "reverse") + theme( axis.title = element_blank(), axis.line = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), legend.position = 'none', aspect.ratio = 1/5 ) list(graph = g, data = df, set = cs) }
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Port the provided Racket code into C while preserving the original functionality.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Rewrite this program in C while keeping its functionality equivalent to the Racket version.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Generate an equivalent C# version of this Racket code.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Please provide an equivalent version of this Racket code in C#.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Produce a language-to-language conversion: from Racket to C++, same semantics.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Produce a language-to-language conversion: from Racket to C++, same semantics.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Maintain the same structure and functionality when rewriting this code in Java.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Generate an equivalent Java version of this Racket code.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Preserve the algorithm and functionality while converting the code from Racket to Python.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Keep all operations the same but rewrite the snippet in Python.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Generate a VB translation of this Racket snippet without changing its computational steps.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Write the same algorithm in VB as shown in this Racket implementation.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Generate an equivalent Go version of this Racket code.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Generate a Go translation of this Racket snippet without changing its computational steps.
#lang racket/base (define current-width (make-parameter 81)) (define current-height (make-parameter 5)) (define (Cantor_set (w (current-width)) (h (current-height))) (define lines (build-list h (λ (_) (make-bytes w (char->integer #\#))))) (define (cantor start len index) (let* ((seg (quotient len 3)) (seg-start (+ start seg)) (seg-end (+ seg-start seg))) (unless (zero? seg) (for* ((i (in-range index h)) (j (in-range seg-start seg-end))) (bytes-set! (list-ref lines i) j (char->integer #\space))) (cantor start seg (add1 index)) (cantor seg-end seg (add1 index))))) (cantor 0 w 1) lines) (module+ main (for-each displayln (Cantor_set)))
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Please provide an equivalent version of this COBOL code in C.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Change the programming language of this snippet from COBOL to C without modifying what it does.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Generate a C# translation of this COBOL snippet without changing its computational steps.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Translate the given COBOL code snippet into C# without altering its behavior.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Port the following code from COBOL to C++ with equivalent syntax and logic.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Change the programming language of this snippet from COBOL to C++ without modifying what it does.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Preserve the algorithm and functionality while converting the code from COBOL to Java.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Translate this program into Java but keep the logic exactly as in COBOL.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Ensure the translated Python code behaves exactly like the original COBOL snippet.
IDENTIFICATION DIVISION. PROGRAM-ID. CANTOR. DATA DIVISION. WORKING-STORAGE SECTION. 01 SETTINGS. 03 NUM-LINES PIC 9 VALUE 5. 03 FILL-CHAR PIC X VALUE '#'. 01 VARIABLES. 03 CUR-LINE. 05 CHAR PIC X OCCURS 81 TIMES. 03 WIDTH PIC 99. 03 CUR-SIZE PIC 99. 03 POS PIC 99. 03 MAXPOS PIC 99. 03 NEXTPOS PIC 99. 03 I PIC 99. PROCEDURE DIVISION. BEGIN. COMPUTE WIDTH = 3 ** (NUM-LINES - 1). PERFORM INIT. MOVE WIDTH TO CUR-SIZE. DISPLAY CUR-LINE. PERFORM DO-LINE UNTIL CUR-SIZE IS EQUAL TO 1. STOP RUN. INIT. PERFORM INIT-CHAR VARYING I FROM 1 BY 1 UNTIL I IS GREATER THAN WIDTH. INIT-CHAR. MOVE FILL-CHAR TO CHAR(I). DO-LINE. DIVIDE 3 INTO CUR-SIZE. MOVE 1 TO POS. SUBTRACT CUR-SIZE FROM WIDTH GIVING MAXPOS. PERFORM BLANK-REGIONS UNTIL POS IS GREATER THAN MAXPOS. DISPLAY CUR-LINE. BLANK-REGIONS. ADD CUR-SIZE TO POS. PERFORM BLANK-CHAR CUR-SIZE TIMES. BLANK-CHAR. MOVE SPACE TO CHAR(POS). ADD 1 TO POS.
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])