Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Can you help me rewrite this code in C instead of Scala, keeping it the same logically?
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
#include <stdio.h> #include <stdlib.h> #include <string.h> void process(int lineNum, char buffer[]) { char days[7][64]; int i = 0, d = 0, j = 0; while (buffer[i] != 0) { if (buffer[i] == ' ') { days[d][j] = '\0'; ++d; j = 0; } else if (buffer[i] == '\n' || buffer[i] == '\r') { days[d][j] = '\0'; ++d; break; } else { days[d][j] = buffer[i]; ++j; } if (d >= 7) { printf("There aren't 7 days in line %d\n", lineNum); return; } ++i; } if (buffer[i] == '\0') { days[d][j] = '\0'; ++d; } if (d < 7) { printf("There aren't 7 days in line %d\n", lineNum); return; } else { int len = 0; for (len = 1; len < 64; ++len) { int d1; for (d1 = 0; d1 < 7; ++d1) { int d2; for (d2 = d1 + 1; d2 < 7; ++d2) { int unique = 0; for (i = 0; i < len; ++i) { if (days[d1][i] != days[d2][i]) { unique = 1; break; } } if (!unique) { goto next_length; } } } printf("%2d ", len); for (i = 0; i < 7; ++i) { printf(" %s", days[i]); } printf("\n"); return; next_length: {} } } printf("Failed to find uniqueness within the bounds."); } int main() { char buffer[1024]; int lineNum = 1, len; FILE *fp; fp = fopen("days_of_week.txt", "r"); while (1) { memset(buffer, 0, sizeof(buffer)); fgets(buffer, sizeof(buffer), fp); len = strlen(buffer); if (len == 0 || buffer[len - 1] == '\0') { break; } process(lineNum++, buffer); } fclose(fp); return 0; }
Can you help me rewrite this code in C# instead of Scala, keeping it the same logically?
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
using System; using System.Collections.Generic; namespace Abbreviations { class Program { static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines("days_of_week.txt"); int i = 0; foreach (string line in lines) { i++; if (line.Length > 0) { var days = line.Split(); if (days.Length != 7) { throw new Exception("There aren't 7 days in line " + i); } Dictionary<string, int> temp = new Dictionary<string, int>(); foreach (string day in days) { if (temp.ContainsKey(day)) { Console.WriteLine(" ∞ {0}", line); continue; } temp.Add(day, 1); } int len = 1; while (true) { temp.Clear(); foreach(string day in days) { string key; if (len < day.Length) { key = day.Substring(0, len); } else { key = day; } if (temp.ContainsKey(key)) { break; } temp.Add(key, 1); } if (temp.Count == 7) { Console.WriteLine("{0,2:D} {1}", len, line); break; } len++; } } } } } }
Translate this program into C# but keep the logic exactly as in Scala.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
using System; using System.Collections.Generic; namespace Abbreviations { class Program { static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines("days_of_week.txt"); int i = 0; foreach (string line in lines) { i++; if (line.Length > 0) { var days = line.Split(); if (days.Length != 7) { throw new Exception("There aren't 7 days in line " + i); } Dictionary<string, int> temp = new Dictionary<string, int>(); foreach (string day in days) { if (temp.ContainsKey(day)) { Console.WriteLine(" ∞ {0}", line); continue; } temp.Add(day, 1); } int len = 1; while (true) { temp.Clear(); foreach(string day in days) { string key; if (len < day.Length) { key = day.Substring(0, len); } else { key = day; } if (temp.ContainsKey(key)) { break; } temp.Add(key, 1); } if (temp.Count == 7) { Console.WriteLine("{0,2:D} {1}", len, line); break; } len++; } } } } } }
Convert this Scala snippet to C++ and keep its semantics consistent.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
#include <iomanip> #include <iostream> #include <fstream> #include <map> #include <sstream> #include <string> #include <vector> std::vector<std::string> split(const std::string& str, char delimiter) { std::vector<std::string> tokens; std::string token; std::istringstream tokenStream(str); while (std::getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } int main() { using namespace std; string line; int i = 0; ifstream in("days_of_week.txt"); if (in.is_open()) { while (getline(in, line)) { i++; if (line.empty()) { continue; } auto days = split(line, ' '); if (days.size() != 7) { throw std::runtime_error("There aren't 7 days in line " + i); } map<string, int> temp; for (auto& day : days) { if (temp.find(day) != temp.end()) { cerr << " ∞ " << line << '\n'; continue; } temp[day] = 1; } int len = 1; while (true) { temp.clear(); for (auto& day : days) { string key = day.substr(0, len); if (temp.find(key) != temp.end()) { break; } temp[key] = 1; } if (temp.size() == 7) { cout << setw(2) << len << " " << line << '\n'; break; } len++; } } } return 0; }
Can you help me rewrite this code in C++ instead of Scala, keeping it the same logically?
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
#include <iomanip> #include <iostream> #include <fstream> #include <map> #include <sstream> #include <string> #include <vector> std::vector<std::string> split(const std::string& str, char delimiter) { std::vector<std::string> tokens; std::string token; std::istringstream tokenStream(str); while (std::getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } int main() { using namespace std; string line; int i = 0; ifstream in("days_of_week.txt"); if (in.is_open()) { while (getline(in, line)) { i++; if (line.empty()) { continue; } auto days = split(line, ' '); if (days.size() != 7) { throw std::runtime_error("There aren't 7 days in line " + i); } map<string, int> temp; for (auto& day : days) { if (temp.find(day) != temp.end()) { cerr << " ∞ " << line << '\n'; continue; } temp[day] = 1; } int len = 1; while (true) { temp.clear(); for (auto& day : days) { string key = day.substr(0, len); if (temp.find(key) != temp.end()) { break; } temp[key] = 1; } if (temp.size() == 7) { cout << setw(2) << len << " " << line << '\n'; break; } len++; } } } return 0; }
Convert this Scala snippet to Java and keep its semantics consistent.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; public class Abbreviations { public static void main(String[] args) throws IOException { Path path = Paths.get("days_of_week.txt"); List<String> readAllLines = Files.readAllLines(path); for (int i = 0; i < readAllLines.size(); i++) { String line = readAllLines.get(i); if (line.length() == 0) continue; String[] days = line.split(" "); if (days.length != 7) throw new RuntimeException("There aren't 7 days on line " + (i + 1)); Map<String, Integer> temp = new HashMap<>(); for (String day : days) { Integer count = temp.getOrDefault(day, 0); temp.put(day, count + 1); } if (temp.size() < 7) { System.out.print(" ∞ "); System.out.println(line); continue; } int len = 1; while (true) { temp.clear(); for (String day : days) { String sd; if (len >= day.length()) { sd = day; } else { sd = day.substring(0, len); } Integer count = temp.getOrDefault(sd, 0); temp.put(sd, count + 1); } if (temp.size() == 7) { System.out.printf("%2d %s\n", len, line); break; } len++; } } } }
Port the provided Scala code into Java while preserving the original functionality.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; public class Abbreviations { public static void main(String[] args) throws IOException { Path path = Paths.get("days_of_week.txt"); List<String> readAllLines = Files.readAllLines(path); for (int i = 0; i < readAllLines.size(); i++) { String line = readAllLines.get(i); if (line.length() == 0) continue; String[] days = line.split(" "); if (days.length != 7) throw new RuntimeException("There aren't 7 days on line " + (i + 1)); Map<String, Integer> temp = new HashMap<>(); for (String day : days) { Integer count = temp.getOrDefault(day, 0); temp.put(day, count + 1); } if (temp.size() < 7) { System.out.print(" ∞ "); System.out.println(line); continue; } int len = 1; while (true) { temp.clear(); for (String day : days) { String sd; if (len >= day.length()) { sd = day; } else { sd = day.substring(0, len); } Integer count = temp.getOrDefault(sd, 0); temp.put(sd, count + 1); } if (temp.size() == 7) { System.out.printf("%2d %s\n", len, line); break; } len++; } } } }
Preserve the algorithm and functionality while converting the code from Scala to Python.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
def shortest_abbreviation_length(line, list_size): words = line.split() word_count = len(words) if word_count != list_size: raise ValueError(f'Not enough entries, expected {list_size} found {word_count}') abbreviation_length = 1 abbreviations = set() while(True): abbreviations = {word[:abbreviation_length] for word in words} if len(abbreviations) == list_size: return abbreviation_length abbreviation_length += 1 abbreviations.clear() def automatic_abbreviations(filename, words_per_line): with open(filename) as file: for line in file: line = line.rstrip() if len(line) > 0: length = shortest_abbreviation_length(line, words_per_line) print(f'{length:2} {line}') else: print() automatic_abbreviations('daysOfWeek.txt', 7)
Transform the following Scala implementation into Python, maintaining the same output and logic.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
def shortest_abbreviation_length(line, list_size): words = line.split() word_count = len(words) if word_count != list_size: raise ValueError(f'Not enough entries, expected {list_size} found {word_count}') abbreviation_length = 1 abbreviations = set() while(True): abbreviations = {word[:abbreviation_length] for word in words} if len(abbreviations) == list_size: return abbreviation_length abbreviation_length += 1 abbreviations.clear() def automatic_abbreviations(filename, words_per_line): with open(filename) as file: for line in file: line = line.rstrip() if len(line) > 0: length = shortest_abbreviation_length(line, words_per_line) print(f'{length:2} {line}') else: print() automatic_abbreviations('daysOfWeek.txt', 7)
Convert this Scala snippet to VB and keep its semantics consistent.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
Function MinimalLenght(strLine As String) As Integer Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer myVar = Split(strLine, " ") Count = 0 Do Set myColl = New Collection Count = Count + 1 On Error Resume Next Do myColl.Add Left$(myVar(I), Count), Left$(myVar(I), Count) I = I + 1 Loop While Err.Number = 0 And I <= UBound(myVar) Flag = Err.Number = 0 On Error GoTo 0 I = 0 Set myColl = Nothing Loop While Not Flag MinimalLenght = Count End Function
Preserve the algorithm and functionality while converting the code from Scala to VB.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
Function MinimalLenght(strLine As String) As Integer Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer myVar = Split(strLine, " ") Count = 0 Do Set myColl = New Collection Count = Count + 1 On Error Resume Next Do myColl.Add Left$(myVar(I), Count), Left$(myVar(I), Count) I = I + 1 Loop While Err.Number = 0 And I <= UBound(myVar) Flag = Err.Number = 0 On Error GoTo 0 I = 0 Set myColl = Nothing Loop While Not Flag MinimalLenght = Count End Function
Please provide an equivalent version of this Scala code in Go.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
package main import( "bufio" "fmt" "os" "strings" ) func distinctStrings(strs []string) []string { len := len(strs) set := make(map[string]bool, len) distinct := make([]string, 0, len) for _, str := range strs { if !set[str] { distinct = append(distinct, str) set[str] = true } } return distinct } func takeRunes(s string, n int) string { i := 0 for j := range s { if i == n { return s[:j] } i++ } return s } func main() { file, err := os.Open("days_of_week.txt") if err != nil { fmt.Println("Unable to open file.") return } defer file.Close() reader := bufio.NewReader(file) lineCount := 0 for { line, err := reader.ReadString('\n') if err != nil { return } line = strings.TrimSpace(line) lineCount++ if line == "" { fmt.Println() continue } days := strings.Fields(line) daysLen := len(days) if (len(days) != 7) { fmt.Println("There aren't 7 days in line", lineCount) return } if len(distinctStrings(days)) != 7 { fmt.Println(" ∞ ", line) continue } for abbrevLen := 1; ; abbrevLen++ { abbrevs := make([]string, daysLen) for i := 0; i < daysLen; i++ { abbrevs[i] = takeRunes(days[i], abbrevLen) } if len(distinctStrings(abbrevs)) == 7 { fmt.Printf("%2d %s\n", abbrevLen, line) break } } } }
Transform the following Scala implementation into Go, maintaining the same output and logic.
import java.io.File val r = Regex("[ ]+") fun main(args: Array<String>) { val lines = File("days_of_week.txt").readLines() for ((i, line) in lines.withIndex()) { if (line.trim().isEmpty()) { println() continue } val days = line.trim().split(r) if (days.size != 7) throw RuntimeException("There aren't 7 days in line ${i + 1}") if (days.distinct().size < 7) { println(" ∞ $line") continue } var len = 1 while (true) { if (days.map { it.take(len) }.distinct().size == 7) { println("${"%2d".format(len)} $line") break } len++ } } }
package main import( "bufio" "fmt" "os" "strings" ) func distinctStrings(strs []string) []string { len := len(strs) set := make(map[string]bool, len) distinct := make([]string, 0, len) for _, str := range strs { if !set[str] { distinct = append(distinct, str) set[str] = true } } return distinct } func takeRunes(s string, n int) string { i := 0 for j := range s { if i == n { return s[:j] } i++ } return s } func main() { file, err := os.Open("days_of_week.txt") if err != nil { fmt.Println("Unable to open file.") return } defer file.Close() reader := bufio.NewReader(file) lineCount := 0 for { line, err := reader.ReadString('\n') if err != nil { return } line = strings.TrimSpace(line) lineCount++ if line == "" { fmt.Println() continue } days := strings.Fields(line) daysLen := len(days) if (len(days) != 7) { fmt.Println("There aren't 7 days in line", lineCount) return } if len(distinctStrings(days)) != 7 { fmt.Println(" ∞ ", line) continue } for abbrevLen := 1; ; abbrevLen++ { abbrevs := make([]string, daysLen) for i := 0; i < daysLen; i++ { abbrevs[i] = takeRunes(days[i], abbrevLen) } if len(distinctStrings(abbrevs)) == 7 { fmt.Printf("%2d %s\n", abbrevLen, line) break } } } }
Generate a C translation of this Tcl snippet without changing its computational steps.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
#include <stdio.h> #include <stdlib.h> #include <string.h> void process(int lineNum, char buffer[]) { char days[7][64]; int i = 0, d = 0, j = 0; while (buffer[i] != 0) { if (buffer[i] == ' ') { days[d][j] = '\0'; ++d; j = 0; } else if (buffer[i] == '\n' || buffer[i] == '\r') { days[d][j] = '\0'; ++d; break; } else { days[d][j] = buffer[i]; ++j; } if (d >= 7) { printf("There aren't 7 days in line %d\n", lineNum); return; } ++i; } if (buffer[i] == '\0') { days[d][j] = '\0'; ++d; } if (d < 7) { printf("There aren't 7 days in line %d\n", lineNum); return; } else { int len = 0; for (len = 1; len < 64; ++len) { int d1; for (d1 = 0; d1 < 7; ++d1) { int d2; for (d2 = d1 + 1; d2 < 7; ++d2) { int unique = 0; for (i = 0; i < len; ++i) { if (days[d1][i] != days[d2][i]) { unique = 1; break; } } if (!unique) { goto next_length; } } } printf("%2d ", len); for (i = 0; i < 7; ++i) { printf(" %s", days[i]); } printf("\n"); return; next_length: {} } } printf("Failed to find uniqueness within the bounds."); } int main() { char buffer[1024]; int lineNum = 1, len; FILE *fp; fp = fopen("days_of_week.txt", "r"); while (1) { memset(buffer, 0, sizeof(buffer)); fgets(buffer, sizeof(buffer), fp); len = strlen(buffer); if (len == 0 || buffer[len - 1] == '\0') { break; } process(lineNum++, buffer); } fclose(fp); return 0; }
Ensure the translated C code behaves exactly like the original Tcl snippet.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
#include <stdio.h> #include <stdlib.h> #include <string.h> void process(int lineNum, char buffer[]) { char days[7][64]; int i = 0, d = 0, j = 0; while (buffer[i] != 0) { if (buffer[i] == ' ') { days[d][j] = '\0'; ++d; j = 0; } else if (buffer[i] == '\n' || buffer[i] == '\r') { days[d][j] = '\0'; ++d; break; } else { days[d][j] = buffer[i]; ++j; } if (d >= 7) { printf("There aren't 7 days in line %d\n", lineNum); return; } ++i; } if (buffer[i] == '\0') { days[d][j] = '\0'; ++d; } if (d < 7) { printf("There aren't 7 days in line %d\n", lineNum); return; } else { int len = 0; for (len = 1; len < 64; ++len) { int d1; for (d1 = 0; d1 < 7; ++d1) { int d2; for (d2 = d1 + 1; d2 < 7; ++d2) { int unique = 0; for (i = 0; i < len; ++i) { if (days[d1][i] != days[d2][i]) { unique = 1; break; } } if (!unique) { goto next_length; } } } printf("%2d ", len); for (i = 0; i < 7; ++i) { printf(" %s", days[i]); } printf("\n"); return; next_length: {} } } printf("Failed to find uniqueness within the bounds."); } int main() { char buffer[1024]; int lineNum = 1, len; FILE *fp; fp = fopen("days_of_week.txt", "r"); while (1) { memset(buffer, 0, sizeof(buffer)); fgets(buffer, sizeof(buffer), fp); len = strlen(buffer); if (len == 0 || buffer[len - 1] == '\0') { break; } process(lineNum++, buffer); } fclose(fp); return 0; }
Convert this Tcl block to C#, preserving its control flow and logic.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
using System; using System.Collections.Generic; namespace Abbreviations { class Program { static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines("days_of_week.txt"); int i = 0; foreach (string line in lines) { i++; if (line.Length > 0) { var days = line.Split(); if (days.Length != 7) { throw new Exception("There aren't 7 days in line " + i); } Dictionary<string, int> temp = new Dictionary<string, int>(); foreach (string day in days) { if (temp.ContainsKey(day)) { Console.WriteLine(" ∞ {0}", line); continue; } temp.Add(day, 1); } int len = 1; while (true) { temp.Clear(); foreach(string day in days) { string key; if (len < day.Length) { key = day.Substring(0, len); } else { key = day; } if (temp.ContainsKey(key)) { break; } temp.Add(key, 1); } if (temp.Count == 7) { Console.WriteLine("{0,2:D} {1}", len, line); break; } len++; } } } } } }
Translate the given Tcl code snippet into C# without altering its behavior.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
using System; using System.Collections.Generic; namespace Abbreviations { class Program { static void Main(string[] args) { string[] lines = System.IO.File.ReadAllLines("days_of_week.txt"); int i = 0; foreach (string line in lines) { i++; if (line.Length > 0) { var days = line.Split(); if (days.Length != 7) { throw new Exception("There aren't 7 days in line " + i); } Dictionary<string, int> temp = new Dictionary<string, int>(); foreach (string day in days) { if (temp.ContainsKey(day)) { Console.WriteLine(" ∞ {0}", line); continue; } temp.Add(day, 1); } int len = 1; while (true) { temp.Clear(); foreach(string day in days) { string key; if (len < day.Length) { key = day.Substring(0, len); } else { key = day; } if (temp.ContainsKey(key)) { break; } temp.Add(key, 1); } if (temp.Count == 7) { Console.WriteLine("{0,2:D} {1}", len, line); break; } len++; } } } } } }
Ensure the translated C++ code behaves exactly like the original Tcl snippet.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
#include <iomanip> #include <iostream> #include <fstream> #include <map> #include <sstream> #include <string> #include <vector> std::vector<std::string> split(const std::string& str, char delimiter) { std::vector<std::string> tokens; std::string token; std::istringstream tokenStream(str); while (std::getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } int main() { using namespace std; string line; int i = 0; ifstream in("days_of_week.txt"); if (in.is_open()) { while (getline(in, line)) { i++; if (line.empty()) { continue; } auto days = split(line, ' '); if (days.size() != 7) { throw std::runtime_error("There aren't 7 days in line " + i); } map<string, int> temp; for (auto& day : days) { if (temp.find(day) != temp.end()) { cerr << " ∞ " << line << '\n'; continue; } temp[day] = 1; } int len = 1; while (true) { temp.clear(); for (auto& day : days) { string key = day.substr(0, len); if (temp.find(key) != temp.end()) { break; } temp[key] = 1; } if (temp.size() == 7) { cout << setw(2) << len << " " << line << '\n'; break; } len++; } } } return 0; }
Produce a functionally identical C++ code for the snippet given in Tcl.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
#include <iomanip> #include <iostream> #include <fstream> #include <map> #include <sstream> #include <string> #include <vector> std::vector<std::string> split(const std::string& str, char delimiter) { std::vector<std::string> tokens; std::string token; std::istringstream tokenStream(str); while (std::getline(tokenStream, token, delimiter)) { tokens.push_back(token); } return tokens; } int main() { using namespace std; string line; int i = 0; ifstream in("days_of_week.txt"); if (in.is_open()) { while (getline(in, line)) { i++; if (line.empty()) { continue; } auto days = split(line, ' '); if (days.size() != 7) { throw std::runtime_error("There aren't 7 days in line " + i); } map<string, int> temp; for (auto& day : days) { if (temp.find(day) != temp.end()) { cerr << " ∞ " << line << '\n'; continue; } temp[day] = 1; } int len = 1; while (true) { temp.clear(); for (auto& day : days) { string key = day.substr(0, len); if (temp.find(key) != temp.end()) { break; } temp[key] = 1; } if (temp.size() == 7) { cout << setw(2) << len << " " << line << '\n'; break; } len++; } } } return 0; }
Please provide an equivalent version of this Tcl code in Java.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; public class Abbreviations { public static void main(String[] args) throws IOException { Path path = Paths.get("days_of_week.txt"); List<String> readAllLines = Files.readAllLines(path); for (int i = 0; i < readAllLines.size(); i++) { String line = readAllLines.get(i); if (line.length() == 0) continue; String[] days = line.split(" "); if (days.length != 7) throw new RuntimeException("There aren't 7 days on line " + (i + 1)); Map<String, Integer> temp = new HashMap<>(); for (String day : days) { Integer count = temp.getOrDefault(day, 0); temp.put(day, count + 1); } if (temp.size() < 7) { System.out.print(" ∞ "); System.out.println(line); continue; } int len = 1; while (true) { temp.clear(); for (String day : days) { String sd; if (len >= day.length()) { sd = day; } else { sd = day.substring(0, len); } Integer count = temp.getOrDefault(sd, 0); temp.put(sd, count + 1); } if (temp.size() == 7) { System.out.printf("%2d %s\n", len, line); break; } len++; } } } }
Generate an equivalent Java version of this Tcl code.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.List; import java.util.Map; public class Abbreviations { public static void main(String[] args) throws IOException { Path path = Paths.get("days_of_week.txt"); List<String> readAllLines = Files.readAllLines(path); for (int i = 0; i < readAllLines.size(); i++) { String line = readAllLines.get(i); if (line.length() == 0) continue; String[] days = line.split(" "); if (days.length != 7) throw new RuntimeException("There aren't 7 days on line " + (i + 1)); Map<String, Integer> temp = new HashMap<>(); for (String day : days) { Integer count = temp.getOrDefault(day, 0); temp.put(day, count + 1); } if (temp.size() < 7) { System.out.print(" ∞ "); System.out.println(line); continue; } int len = 1; while (true) { temp.clear(); for (String day : days) { String sd; if (len >= day.length()) { sd = day; } else { sd = day.substring(0, len); } Integer count = temp.getOrDefault(sd, 0); temp.put(sd, count + 1); } if (temp.size() == 7) { System.out.printf("%2d %s\n", len, line); break; } len++; } } } }
Rewrite the snippet below in Python so it works the same as the original Tcl code.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
def shortest_abbreviation_length(line, list_size): words = line.split() word_count = len(words) if word_count != list_size: raise ValueError(f'Not enough entries, expected {list_size} found {word_count}') abbreviation_length = 1 abbreviations = set() while(True): abbreviations = {word[:abbreviation_length] for word in words} if len(abbreviations) == list_size: return abbreviation_length abbreviation_length += 1 abbreviations.clear() def automatic_abbreviations(filename, words_per_line): with open(filename) as file: for line in file: line = line.rstrip() if len(line) > 0: length = shortest_abbreviation_length(line, words_per_line) print(f'{length:2} {line}') else: print() automatic_abbreviations('daysOfWeek.txt', 7)
Write the same code in Python as shown below in Tcl.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
def shortest_abbreviation_length(line, list_size): words = line.split() word_count = len(words) if word_count != list_size: raise ValueError(f'Not enough entries, expected {list_size} found {word_count}') abbreviation_length = 1 abbreviations = set() while(True): abbreviations = {word[:abbreviation_length] for word in words} if len(abbreviations) == list_size: return abbreviation_length abbreviation_length += 1 abbreviations.clear() def automatic_abbreviations(filename, words_per_line): with open(filename) as file: for line in file: line = line.rstrip() if len(line) > 0: length = shortest_abbreviation_length(line, words_per_line) print(f'{length:2} {line}') else: print() automatic_abbreviations('daysOfWeek.txt', 7)
Can you help me rewrite this code in VB instead of Tcl, keeping it the same logically?
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
Function MinimalLenght(strLine As String) As Integer Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer myVar = Split(strLine, " ") Count = 0 Do Set myColl = New Collection Count = Count + 1 On Error Resume Next Do myColl.Add Left$(myVar(I), Count), Left$(myVar(I), Count) I = I + 1 Loop While Err.Number = 0 And I <= UBound(myVar) Flag = Err.Number = 0 On Error GoTo 0 I = 0 Set myColl = Nothing Loop While Not Flag MinimalLenght = Count End Function
Ensure the translated VB code behaves exactly like the original Tcl snippet.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
Function MinimalLenght(strLine As String) As Integer Dim myVar As Variant, I As Integer, Flag As Boolean, myColl As Collection, Count As Integer myVar = Split(strLine, " ") Count = 0 Do Set myColl = New Collection Count = Count + 1 On Error Resume Next Do myColl.Add Left$(myVar(I), Count), Left$(myVar(I), Count) I = I + 1 Loop While Err.Number = 0 And I <= UBound(myVar) Flag = Err.Number = 0 On Error GoTo 0 I = 0 Set myColl = Nothing Loop While Not Flag MinimalLenght = Count End Function
Rewrite the snippet below in Go so it works the same as the original Tcl code.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
package main import( "bufio" "fmt" "os" "strings" ) func distinctStrings(strs []string) []string { len := len(strs) set := make(map[string]bool, len) distinct := make([]string, 0, len) for _, str := range strs { if !set[str] { distinct = append(distinct, str) set[str] = true } } return distinct } func takeRunes(s string, n int) string { i := 0 for j := range s { if i == n { return s[:j] } i++ } return s } func main() { file, err := os.Open("days_of_week.txt") if err != nil { fmt.Println("Unable to open file.") return } defer file.Close() reader := bufio.NewReader(file) lineCount := 0 for { line, err := reader.ReadString('\n') if err != nil { return } line = strings.TrimSpace(line) lineCount++ if line == "" { fmt.Println() continue } days := strings.Fields(line) daysLen := len(days) if (len(days) != 7) { fmt.Println("There aren't 7 days in line", lineCount) return } if len(distinctStrings(days)) != 7 { fmt.Println(" ∞ ", line) continue } for abbrevLen := 1; ; abbrevLen++ { abbrevs := make([]string, daysLen) for i := 0; i < daysLen; i++ { abbrevs[i] = takeRunes(days[i], abbrevLen) } if len(distinctStrings(abbrevs)) == 7 { fmt.Printf("%2d %s\n", abbrevLen, line) break } } } }
Transform the following Tcl implementation into Go, maintaining the same output and logic.
set f [open abbreviations_automatic_weekdays.txt] set lines [split [read -nonewline $f] \n] close $f foreach days $lines { if {[string length $days] == 0} continue if {[llength $days] != 7} { throw ERROR {not 7 days in a line} } if {[llength [lsort -unique $days]] != 7} { throw ERROR {not all 7 days in a line are distinct} } for {set i 0} {1} {incr i} { if {[llength [lsort -unique [lmap x $days {string range $x 0 $i}]]] == 7} break } incr i puts "$i $days" }
package main import( "bufio" "fmt" "os" "strings" ) func distinctStrings(strs []string) []string { len := len(strs) set := make(map[string]bool, len) distinct := make([]string, 0, len) for _, str := range strs { if !set[str] { distinct = append(distinct, str) set[str] = true } } return distinct } func takeRunes(s string, n int) string { i := 0 for j := range s { if i == n { return s[:j] } i++ } return s } func main() { file, err := os.Open("days_of_week.txt") if err != nil { fmt.Println("Unable to open file.") return } defer file.Close() reader := bufio.NewReader(file) lineCount := 0 for { line, err := reader.ReadString('\n') if err != nil { return } line = strings.TrimSpace(line) lineCount++ if line == "" { fmt.Println() continue } days := strings.Fields(line) daysLen := len(days) if (len(days) != 7) { fmt.Println("There aren't 7 days in line", lineCount) return } if len(distinctStrings(days)) != 7 { fmt.Println(" ∞ ", line) continue } for abbrevLen := 1; ; abbrevLen++ { abbrevs := make([]string, daysLen) for i := 0; i < daysLen; i++ { abbrevs[i] = takeRunes(days[i], abbrevLen) } if len(distinctStrings(abbrevs)) == 7 { fmt.Printf("%2d %s\n", abbrevLen, line) break } } } }
Maintain the same structure and functionality when rewriting this code in C#.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Keep all operations the same but rewrite the snippet in C#.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Keep all operations the same but rewrite the snippet in C.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Rewrite this program in C while keeping its functionality equivalent to the Ada version.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Translate this program into C++ but keep the logic exactly as in Ada.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Generate an equivalent C++ version of this Ada code.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Please provide an equivalent version of this Ada code in Go.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Convert the following code from Ada to Go, ensuring the logic remains intact.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Keep all operations the same but rewrite the snippet in Java.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Write the same code in Java as shown below in Ada.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Rewrite the snippet below in Python so it works the same as the original Ada code.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Rewrite the snippet below in Python so it works the same as the original Ada code.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Port the following code from Ada to VB with equivalent syntax and logic.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Write a version of this Ada function in VB with identical behavior.
with Ada.Text_IO; procedure Cantor_Set is subtype Level_Range is Integer range 1 .. 5; Image : array (Level_Range) of String (1 .. 81) := (others => (others => ' ')); procedure Cantor (Level : Natural; Length : Natural; Start : Natural) is begin if Level in Level_Range then Image (Level) (Start .. Start + Length - 1) := (others => '*'); Cantor (Level + 1, Length / 3, Start); Cantor (Level + 1, Length / 3, Start + 2 * Length / 3); end if; end Cantor; begin Cantor (Level => Level_Range'First, Length => 81, Start => 1); for L in Level_Range loop Ada.Text_IO.Put_Line (Image (L)); end loop; end Cantor_Set;
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Port the following code from Arturo to C with equivalent syntax and logic.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Convert this Arturo block to C, preserving its control flow and logic.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Write the same algorithm in C# as shown in this Arturo implementation.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Write a version of this Arturo function in C# with identical behavior.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Write the same code in C++ as shown below in Arturo.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Ensure the translated C++ code behaves exactly like the original Arturo snippet.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Write a version of this Arturo function in Java with identical behavior.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Port the following code from Arturo to Java with equivalent syntax and logic.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Translate this program into Python but keep the logic exactly as in Arturo.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Ensure the translated Python code behaves exactly like the original Arturo snippet.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Write a version of this Arturo function in VB with identical behavior.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Convert this Arturo snippet to VB and keep its semantics consistent.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Generate an equivalent Go version of this Arturo code.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Port the following code from Arturo to Go with equivalent syntax and logic.
width: 81 height: 5 lines: array.of: height repeat `*` width cantor: function [start length idx].export:[lines][ seg: length / 3 if seg = 0 -> return null loop idx..dec height 'i [ loop (start + seg).. dec start + 2 * seg 'j -> set lines\[i] j ` ` ] cantor start seg idx+1 cantor start + 2 * seg seg idx+1 ] cantor 0 width 1 loop lines 'line -> print join line
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Generate an equivalent C version of this AWK code.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
#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 AWK function in C with identical behavior.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Port the following code from AWK to C# with equivalent syntax and logic.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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 provided AWK code into C# while preserving the original functionality.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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 AWK snippet to C++ and keep its semantics consistent.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Port the provided AWK code into C++ while preserving the original functionality.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
#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 AWK version.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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 AWK snippet without changing its computational steps.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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(); } } }
Produce a language-to-language conversion: from AWK to Python, same semantics.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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 programming language of this snippet from AWK to Python without modifying what it does.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Rewrite the snippet below in VB so it works the same as the original AWK code.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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 AWK code into VB while preserving the original functionality.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
Module Module1 Const WIDTH = 81 Const HEIGHT = 5 Dim lines(HEIGHT, WIDTH) As Char Sub Init() For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 lines(i, j) = "*" Next Next End Sub Sub Cantor(start As Integer, len As Integer, index As Integer) Dim seg As Integer = len / 3 If seg = 0 Then Return End If For i = index To HEIGHT - 1 For j = start + seg To start + seg * 2 - 1 lines(i, j) = " " Next Next Cantor(start, seg, index + 1) Cantor(start + seg * 2, seg, index + 1) End Sub Sub Main() Init() Cantor(0, WIDTH, 1) For i = 0 To HEIGHT - 1 For j = 0 To WIDTH - 1 Console.Write(lines(i, j)) Next Console.WriteLine() Next End Sub End Module
Produce a language-to-language conversion: from AWK to Go, same semantics.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
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[:])) } }
Translate this program into Go but keep the logic exactly as in AWK.
BEGIN { WIDTH = 81 HEIGHT = 5 for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { lines[i][j] = "*" } } cantor(0,WIDTH,1) for (i=0; i<HEIGHT; ++i) { for (j=0; j<WIDTH; ++j) { printf("%s",lines[i][j]) } printf("\n") } exit(0) } function cantor(start,leng,indx, i,j,seg) { seg = int(leng/3) if (seg == 0) { return } for (i=indx; i<HEIGHT; ++i) { for (j=start+seg; j<start+seg*2; ++j) { lines[i][j] = " " } } cantor(start,seg,indx+1) cantor(start+seg*2,seg,indx+1) }
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Ensure the translated C code behaves exactly like the original Common_Lisp snippet.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
#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 Common_Lisp code in C.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
#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 Common_Lisp function in C# with identical behavior.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
using System; namespace CantorSet { class Program { const int WIDTH = 81; const int HEIGHT = 5; private static char[,] lines = new char[HEIGHT, WIDTH]; static Program() { for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i, j] = '*'; } } } private static void Cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i, j] = ' '; } } Cantor(start, seg, index + 1); Cantor(start + seg * 2, seg, index + 1); } static void Main(string[] args) { Cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { Console.Write(lines[i,j]); } Console.WriteLine(); } } } }
Translate this program into C# but keep the logic exactly as in Common_Lisp.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
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 Common_Lisp code in C++.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
#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; }
Can you help me rewrite this code in C++ instead of Common_Lisp, keeping it the same logically?
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
#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 Common_Lisp code snippet into Java without altering its behavior.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
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(); } } }
Maintain the same structure and functionality when rewriting this code in Java.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
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 Python as shown in this Common_Lisp implementation.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
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 Common_Lisp snippet without changing its computational steps.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Maintain the same structure and functionality when rewriting this code in VB.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
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
Maintain the same structure and functionality when rewriting this code in VB.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
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 Common_Lisp implementation into Go, maintaining the same output and logic.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Port the provided Common_Lisp code into Go while preserving the original functionality.
CANTOR =LAMBDA(n, APPLYN(n)( LAMBDA(grid, APPENDROWS(grid)( CANTOROW( LASTROW(grid) ) ) ) )({0,1}) ) CANTOROW =LAMBDA(xys, LET( nCols, COLUMNS(xys), IF(2 > nCols, xys, IF(3 < nCols, APPENDCOLS( CANTORSLICES(TAKECOLS(2)(xys)) )( CANTOROW(DROPCOLS(2)(xys)) ), CANTORSLICES(TAKECOLS(2)(xys)) ) ) ) ) CANTORSLICES =LAMBDA(ab, LET( a, INDEX(ab, 1), b, INDEX(ab, 2), third, (b - a) / 3, CHOOSE({1,2,3,4}, a, a + third, b - third, b) ) ) SHOWCANTOR =LAMBDA(grid, LET( leaves, LASTROW(grid), leafWidth, INDEX(leaves, 1, 2) - INDEX(leaves, 1, 1), leafCount, 1 / leafWidth, SHOWCANTROWS(leafCount)(grid) ) ) SHOWCANTROWS =LAMBDA(leafCount, LAMBDA(grid, LET( xs, FILTERP( LAMBDA(x, NOT(ISNA(x))) )( HEADCOL(grid) ), runLengths, LAMBDA(x, CEILING.MATH(leafCount * x))( SUBTRACT(TAILROW(xs))(INITROW(xs) ) ), iCols, SEQUENCE(1, COLUMNS(runLengths)), CONCAT( REPT( IF(ISEVEN(iCols), " ", "β–ˆ"), runLengths ) ) & IF(1 < ROWS(grid), CHAR(10) & SHOWCANTROWS(leafCount)( TAILCOL(grid) ), "" ) ) ) )
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Please provide an equivalent version of this D code in C.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
#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.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
#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 D snippet without changing its computational steps.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
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 D to C#, same semantics.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
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(); } } } }
Preserve the algorithm and functionality while converting the code from D to C++.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
#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 D snippet to C++ and keep its semantics consistent.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
#include <iostream> const int WIDTH = 81; const int HEIGHT = 5; char lines[WIDTH*HEIGHT]; void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { int pos = i * WIDTH + j; lines[pos] = ' '; } } cantor(start, seg, index + 1); cantor(start + 2 * seg, seg, index + 1); } int main() { for (int i = 0; i < WIDTH*HEIGHT; i++) { lines[i] = '*'; } cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT*WIDTH; i += WIDTH) { printf("%.*s\n", WIDTH, lines + i); } return 0; }
Write a version of this D function in Java with identical behavior.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
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 D snippet to Java and keep its semantics consistent.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
public class App { private static final int WIDTH = 81; private static final int HEIGHT = 5; private static char[][] lines; static { lines = new char[HEIGHT][WIDTH]; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { lines[i][j] = '*'; } } } private static void cantor(int start, int len, int index) { int seg = len / 3; if (seg == 0) return; for (int i = index; i < HEIGHT; i++) { for (int j = start + seg; j < start + seg * 2; j++) { lines[i][j] = ' '; } } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } public static void main(String[] args) { cantor(0, WIDTH, 1); for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < WIDTH; j++) { System.out.print(lines[i][j]); } System.out.println(); } } }
Rewrite the snippet below in Python so it works the same as the original D code.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Rewrite the snippet below in Python so it works the same as the original D code.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
WIDTH = 81 HEIGHT = 5 lines=[] def cantor(start, len, index): seg = len / 3 if seg == 0: return None for it in xrange(HEIGHT-index): i = index + it for jt in xrange(seg): j = start + seg + jt pos = i * WIDTH + j lines[pos] = ' ' cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) return None lines = ['*'] * (WIDTH*HEIGHT) cantor(0, WIDTH, 1) for i in xrange(HEIGHT): beg = WIDTH * i print ''.join(lines[beg : beg+WIDTH])
Maintain the same structure and functionality when rewriting this code in VB.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
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 D code into VB without altering its purpose.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
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
Maintain the same structure and functionality when rewriting this code in Go.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
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 D code into Go without altering its purpose.
import std.stdio; enum WIDTH = 81; enum HEIGHT = 5; char[WIDTH*HEIGHT] lines; 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+seg*2, seg, index+1); } void main() { lines[] = '*'; cantor(0, WIDTH, 1); for (int i=0; i<HEIGHT; i++) { int beg = WIDTH * i; writeln(lines[beg..beg+WIDTH]); } }
package main import "fmt" const ( width = 81 height = 5 ) var lines [height][width]byte func init() { for i := 0; i < height; i++ { for j := 0; j < width; j++ { lines[i][j] = '*' } } } func cantor(start, len, index int) { seg := len / 3 if seg == 0 { return } for i := index; i < height; i++ { for j := start + seg; j < start + 2 * seg; j++ { lines[i][j] = ' ' } } cantor(start, seg, index + 1) cantor(start + seg * 2, seg, index + 1) } func main() { cantor(0, width, 1) for _, line := range lines { fmt.Println(string(line[:])) } }
Generate a C translation of this 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.
#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 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.
#include <stdio.h> #define WIDTH 81 #define HEIGHT 5 char lines[HEIGHT][WIDTH]; void init() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) lines[i][j] = '*'; } } void cantor(int start, int len, int index) { int i, j, seg = len / 3; if (seg == 0) return; for (i = index; i < HEIGHT; ++i) { for (j = start + seg; j < start + seg * 2; ++j) lines[i][j] = ' '; } cantor(start, seg, index + 1); cantor(start + seg * 2, seg, index + 1); } void print() { int i, j; for (i = 0; i < HEIGHT; ++i) { for (j = 0; j < WIDTH; ++j) printf("%c", lines[i][j]); printf("\n"); } } int main() { init(); cantor(0, WIDTH, 1); print(); return 0; }
Port the provided Delphi code into C# while preserving the original functionality.
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(); } } } }