Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Convert this Delphi snippet to C# and keep its semantics consistent.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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(); } } } }
Change the following Delphi code into C++ without altering its purpose.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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; }
Produce a language-to-language conversion: from Delphi to C++, same semantics.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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 Delphi code in Java.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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(); } } }
Please provide an equivalent version of this Delphi code in Java.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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(); } } }
Convert this Delphi snippet to Python and keep its semantics consistent.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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])
Generate a Python translation of this Delphi snippet without changing its computational steps.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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 Delphi to VB with equivalent syntax and logic.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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
Keep all operations the same but rewrite the snippet in VB.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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 Delphi, keeping it the same logically?
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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[:])) } }
Convert the following code from Delphi to Go, ensuring the logic remains intact.
program Cantor_set; const WIDTH: Integer = 81; HEIGHT: Integer = 5; var Lines: TArray<TArray<Char>>; procedure Init; var i, j: Integer; begin SetLength(lines, HEIGHT, WIDTH); for i := 0 to HEIGHT - 1 do for j := 0 to WIDTH - 1 do lines[i, j] := '*'; end; procedure Cantor(start, len, index: Integer); var seg, i, j: Integer; begin seg := len div 3; if seg = 0 then Exit; for i := index to HEIGHT - 1 do for j := start + seg to start + seg * 2 - 1 do lines[i, j] := ' '; Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); end; var i, j: Integer; begin Init; Cantor(0, WIDTH, 1); for i := 0 to HEIGHT - 1 do begin for j := 0 to WIDTH - 1 do Write(lines[i, j]); Writeln; end; Readln; 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[:])) } }
Rewrite the snippet below in C so it works the same as the original Elixir code.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
#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; }
Transform the following Elixir implementation into C, maintaining the same output and logic.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
#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 Elixir code into C# without altering its purpose.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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 Elixir code in C++.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
#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 Elixir code snippet into C++ without altering its behavior.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
#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; }
Rewrite this program in Java while keeping its functionality equivalent to the Elixir version.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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 a Java translation of this Elixir snippet without changing its computational steps.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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 Elixir to Python without modifying what it does.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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])
Preserve the algorithm and functionality while converting the code from Elixir to VB.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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
Ensure the translated VB code behaves exactly like the original Elixir snippet.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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
Change the programming language of this snippet from Elixir to Go without modifying what it does.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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 Elixir implementation into Go, maintaining the same output and logic.
defmodule Cantor do @pos "β–ˆ" @neg " " def run(lines) do Enum.map(0..lines, fn line -> segment_size = 3 ** (lines - line - 1) chars = (3 ** line) Enum.map(0..chars, fn char -> char |> Integer.digits(3) |> Enum.any?(fn x -> x === 1 end) |> case do true -> @neg false -> @pos end end) |> Enum.reduce([], fn el, acc -> duplicate_char(acc, el, segment_size) end) |> Enum.join() |> String.trim_trailing() end) |> Enum.filter(fn line -> line !== "" end) end def duplicate_char(acc, el, segment_size) when segment_size >= 1, do: acc ++ [String.duplicate(el, segment_size)] def duplicate_char(acc, _el, segment_size) when segment_size < 1, do: acc end Cantor.run(5) |> IO.inspect()
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[:])) } }
Change the programming language of this snippet from Factor to C without modifying what it does.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
#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 Factor code into C without altering its purpose.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
#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 algorithm in C# as shown in this Factor implementation.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 Factor to C#, ensuring the logic remains intact.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 Factor code snippet into C++ without altering its behavior.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
#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; }
Ensure the translated C++ code behaves exactly like the original Factor snippet.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
#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 Factor snippet without changing its computational steps.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 Factor snippet.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 a Python translation of this Factor snippet without changing its computational steps.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 the same code in Python as shown below in Factor.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 Factor block to VB, preserving its control flow and logic.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 Factor to VB, ensuring the logic remains intact.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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
Transform the following Factor implementation into Go, maintaining the same output and logic.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 a version of this Factor function in Go with identical behavior.
USING: grouping.extras io kernel math sequences sequences.repeating ; IN: rosetta-code.cantor-set CONSTANT: width 81 CONSTANT: depth 5 : cantor ( n -- seq ) dup 0 = [ drop { 0 1 } ] [ 1 - cantor [ 3 / ] map dup [ 2/3 + ] map append ] if ; : gaps ( seq -- seq ) [ width * ] map [ - abs ] 2clump-map ; : print-cantor ( n -- ) cantor gaps [ even? "#" " " ? swap repeat ] map-index concat print ; depth <iota> [ print-cantor ] each
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 the following code from Forth to C, ensuring the logic remains intact.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
#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 Forth snippet without changing its computational steps.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
#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 a version of this Forth function in C# with identical behavior.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 Forth code.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 Forth code in C++.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
#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 Forth code snippet into C++ without altering its behavior.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
#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; }
Convert this Forth snippet to Java and keep its semantics consistent.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 Forth.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 this Forth snippet to Python and keep its semantics consistent.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 the same code in Python as shown below in Forth.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 VB instead of Forth, keeping it the same logically?
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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
Keep all operations the same but rewrite the snippet in VB.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 this Forth snippet to Go and keep its semantics consistent.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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 Forth block to Go, preserving its control flow and logic.
warnings off 4 : ** 1 swap 0 ?DO over * LOOP nip ; 3 swap ** constant width create string here width char # fill width allot : print string width type cr ; create length width , : reduce length dup @ 3 / swap ! ; : done? dup string - width >= ; : hole? dup c@ bl = ; : skip length @ + ; : whipe dup length @ bl fill skip ; : step hole? IF skip skip skip ELSE skip whipe skip THEN ; : split reduce string BEGIN step done? UNTIL drop ; : done? length @ 1 <= ; : step split print ; : go print BEGIN step done? UNTIL ; go bye
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[:])) } }
Produce a language-to-language conversion: from Groovy to C, same semantics.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
#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 Groovy block to C, preserving its control flow and logic.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
#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; }
Please provide an equivalent version of this Groovy code in C#.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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(); } } } }
Change the programming language of this snippet from Groovy to C# without modifying what it does.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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 functionally identical C++ code for the snippet given in Groovy.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
#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; }
Keep all operations the same but rewrite the snippet in C++.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
#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; }
Keep all operations the same but rewrite the snippet in Java.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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 Groovy implementation.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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 a Python translation of this Groovy snippet without changing its computational steps.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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])
Change the following Groovy code into Python without altering its purpose.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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
Change the following Groovy code into VB without altering its purpose.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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 code in Go as shown below in Groovy.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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[:])) } }
Produce a language-to-language conversion: from Groovy to Go, same semantics.
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 = (int) (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++) { System.out.print(lines[i][j]) } System.out.println() } } }
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[:])) } }
Produce a language-to-language conversion: from Haskell to C, same semantics.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
#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; }
Maintain the same structure and functionality when rewriting this code in C.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
#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 Haskell block to C#, preserving its control flow and logic.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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(); } } } }
Rewrite the snippet below in C# so it works the same as the original Haskell code.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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 Haskell to C++ with equivalent syntax and logic.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
#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; }
Convert this Haskell block to C++, preserving its control flow and logic.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
#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; }
Rewrite this program in Java while keeping its functionality equivalent to the Haskell version.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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 this Haskell snippet to Java and keep its semantics consistent.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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 this Haskell block to Python, preserving its control flow and logic.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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 Python translation of this Haskell snippet without changing its computational steps.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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 Haskell.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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
Port the following code from Haskell to VB with equivalent syntax and logic.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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
Transform the following Haskell implementation into Go, maintaining the same output and logic.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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[:])) } }
Preserve the algorithm and functionality while converting the code from Haskell to Go.
cantor :: [(Bool, Int)] -> [(Bool, Int)] cantor = concatMap go where go (bln, n) | bln && 1 < n = let m = quot n 3 in [(True, m), (False, m), (True, m)] | otherwise = [(bln, n)] main :: IO () main = putStrLn $ cantorLines 5 cantorLines :: Int -> String cantorLines n = unlines $ showCantor <$> take n (iterate cantor [(True, 3 ^ pred n)]) showCantor :: [(Bool, Int)] -> String showCantor = concatMap $ uncurry (flip replicate . c) where c True = '*' c False = ' '
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[:])) } }
Change the following J code into C without altering its purpose.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
#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 J snippet without changing its computational steps.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
#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 J code.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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(); } } } }
Transform the following J implementation into C#, maintaining the same output and logic.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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 a version of this J function in C++ with identical behavior.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
#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; }
Convert this J snippet to C++ and keep its semantics consistent.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
#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 J to Java without modifying what it does.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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(); } } }
Port the following code from J to Java with equivalent syntax and logic.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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 following J code into Python without altering its purpose.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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 Python but keep the logic exactly as in J.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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 J to VB, same semantics.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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
Port the provided J code into VB while preserving the original functionality.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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
Preserve the algorithm and functionality while converting the code from J to Go.
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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 Go instead of J, keeping it the same logically?
odometer =: [: (4 $. $.) $&1 cantor_dust =: monad define shape =. ,~ 3 ^ y a =. shape $ ' ' i =. odometer shape < (}:"1) 1j1 #"1 '#' (([: <"1 [: ;/"1 (#~ 1 e."1 [: (,/"2) 3 3&#:)) i)}a )
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 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
#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 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
#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; }
Ensure the translated C# code behaves exactly like the original Julia snippet.
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
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 Julia block to C#, preserving its control flow 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
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 Julia to C++, same semantics.
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
#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; }
Rewrite this program in C++ 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
#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; }
Keep all operations the same but rewrite the snippet in Java.
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(); } } }