Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Convert this Julia block to C++, preserving its control flow and logic. | function lookandsay(s::String)
rst = IOBuffer()
c = 1
for i in 1:length(s)
if i != length(s) && s[i] == s[i+1]
c += 1
else
print(rst, c, s[i])
c = 1
end
end
String(take!(rst))
end
function lookandsayseq(n::Integer)
rst = Vector{String}(undef, n)
rst[1] = "1"
for i in 2:n
rst[i] = lookandsay(rst[i-1])
end
rst
end
println(lookandsayseq(10))
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Generate a Java translation of this Julia snippet without changing its computational steps. | function lookandsay(s::String)
rst = IOBuffer()
c = 1
for i in 1:length(s)
if i != length(s) && s[i] == s[i+1]
c += 1
else
print(rst, c, s[i])
c = 1
end
end
String(take!(rst))
end
function lookandsayseq(n::Integer)
rst = Vector{String}(undef, n)
rst[1] = "1"
for i in 2:n
rst[i] = lookandsay(rst[i-1])
end
rst
end
println(lookandsayseq(10))
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Preserve the algorithm and functionality while converting the code from Julia to Python. | function lookandsay(s::String)
rst = IOBuffer()
c = 1
for i in 1:length(s)
if i != length(s) && s[i] == s[i+1]
c += 1
else
print(rst, c, s[i])
c = 1
end
end
String(take!(rst))
end
function lookandsayseq(n::Integer)
rst = Vector{String}(undef, n)
rst[1] = "1"
for i in 2:n
rst[i] = lookandsay(rst[i-1])
end
rst
end
println(lookandsayseq(10))
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Convert this Julia snippet to VB and keep its semantics consistent. | function lookandsay(s::String)
rst = IOBuffer()
c = 1
for i in 1:length(s)
if i != length(s) && s[i] == s[i+1]
c += 1
else
print(rst, c, s[i])
c = 1
end
end
String(take!(rst))
end
function lookandsayseq(n::Integer)
rst = Vector{String}(undef, n)
rst[1] = "1"
for i in 2:n
rst[i] = lookandsay(rst[i-1])
end
rst
end
println(lookandsayseq(10))
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Rewrite this program in Go while keeping its functionality equivalent to the Julia version. | function lookandsay(s::String)
rst = IOBuffer()
c = 1
for i in 1:length(s)
if i != length(s) && s[i] == s[i+1]
c += 1
else
print(rst, c, s[i])
c = 1
end
end
String(take!(rst))
end
function lookandsayseq(n::Integer)
rst = Vector{String}(undef, n)
rst[1] = "1"
for i in 2:n
rst[i] = lookandsay(rst[i-1])
end
rst
end
println(lookandsayseq(10))
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Produce a functionally identical C code for the snippet given in Lua. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Convert this Lua snippet to C# and keep its semantics consistent. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Port the provided Lua code into C++ while preserving the original functionality. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Port the following code from Lua to Java with equivalent syntax and logic. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Convert this Lua snippet to Python and keep its semantics consistent. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Write the same algorithm in VB as shown in this Lua implementation. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Translate the given Lua code snippet into Go without altering its behavior. | function lookAndSay S
put 0 into C
put char 1 of S into lastChar
repeat with i = 2 to length(S)
add 1 to C
if char i of S is lastChar then next repeat
put C & lastChar after R
put 0 into C
put char i of S into lastChar
end repeat
return R & C + 1 & lastChar
end lookAndSay
on demoLookAndSay
put 1 into x
repeat 10
put x & cr after message
put lookAndSay(x) into x
end repeat
put x after message
end demoLookAndSay
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Produce a functionally identical C code for the snippet given in Mathematica. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Port the following code from Mathematica to C++ with equivalent syntax and logic. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Translate this program into Java but keep the logic exactly as in Mathematica. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Port the provided Mathematica code into Python while preserving the original functionality. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Rewrite the snippet below in VB so it works the same as the original Mathematica code. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Port the provided Mathematica code into Go while preserving the original functionality. | LookAndSay[n_Integer?Positive]:= Reverse @@@ Tally /@ Split @ IntegerDigits @ n // Flatten // FromDigits
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Keep all operations the same but rewrite the snippet in C. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Write the same algorithm in C++ as shown in this Nim implementation. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Translate this program into Java but keep the logic exactly as in Nim. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Transform the following Nim implementation into Python, maintaining the same output and logic. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Rewrite this program in VB while keeping its functionality equivalent to the Nim version. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Preserve the algorithm and functionality while converting the code from Nim to Go. | iterator lookAndSay(n: int): string =
var current = "1"
yield current
for round in 2..n:
var ch = current[0]
var count = 1
var next = ""
for i in 1..current.high:
if current[i] == ch:
inc count
else:
next.add $count & ch
ch = current[i]
count = 1
current = next & $count & ch
yield current
for s in lookAndSay(12):
echo s
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Please provide an equivalent version of this OCaml code in C. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Port the following code from OCaml to C# with equivalent syntax and logic. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Keep all operations the same but rewrite the snippet in C++. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Change the programming language of this snippet from OCaml to Java without modifying what it does. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Write the same code in Python as shown below in OCaml. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Write the same algorithm in VB as shown in this OCaml implementation. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Preserve the algorithm and functionality while converting the code from OCaml to Go. | let rec seeAndSay = function
| [], nys -> List.rev nys
| x::xs, [] -> seeAndSay(xs, [x; 1])
| x::xs, y::n::nys when x=y -> seeAndSay(xs, y::1+n::nys)
| x::xs, nys -> seeAndSay(xs, x::1::nys)
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Transform the following Pascal implementation into C, maintaining the same output and logic. | program LookAndSayDemo(input, output);
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Transform the following Pascal implementation into C++, maintaining the same output and logic. | program LookAndSayDemo(input, output);
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Rewrite this program in Java while keeping its functionality equivalent to the Pascal version. | program LookAndSayDemo(input, output);
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Keep all operations the same but rewrite the snippet in Python. | program LookAndSayDemo(input, output);
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Produce a functionally identical VB code for the snippet given in Pascal. | program LookAndSayDemo(input, output);
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Change the programming language of this snippet from Pascal to Go without modifying what it does. | program LookAndSayDemo(input, output);
uses
SysUtils;
function LookAndSay(s: string): string;
var
item: char;
index: integer;
count: integer;
begin
Result := '';
item := s[1];
count := 1;
for index := 2 to length(s) do
if item = s[index] then
inc(count)
else
begin
Result := Result + intTostr(count) + item;
item := s[index];
count := 1;
end;
Result := Result + intTostr(count) + item;
end;
var
number: string;
begin
writeln('Press RETURN to continue and ^C to stop.');
number := '1';
while not eof(input) do
begin
write(number);
readln;
number := LookAndSay(number);
end;
end.
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Transform the following Perl implementation into C, maintaining the same output and logic. | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Can you help me rewrite this code in C++ instead of Perl, keeping it the same logically? | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Translate this program into Java but keep the logic exactly as in Perl. | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Write the same code in Python as shown below in Perl. | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Translate this program into VB but keep the logic exactly as in Perl. | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Change the following Perl code into Go without altering its purpose. | sub lookandsay {
my $str = shift;
$str =~ s/((.)\2*)/length($1) . $2/ge;
return $str;
}
my $num = "1";
foreach (1..10) {
print "$num\n";
$num = lookandsay($num);
}
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Port the provided PowerShell code into C while preserving the original functionality. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Port the following code from PowerShell to C# with equivalent syntax and logic. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Translate the given PowerShell code snippet into C++ without altering its behavior. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Rewrite the snippet below in Java so it works the same as the original PowerShell code. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Produce a language-to-language conversion: from PowerShell to Python, same semantics. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Port the following code from PowerShell to VB with equivalent syntax and logic. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Maintain the same structure and functionality when rewriting this code in Go. | function Get-LookAndSay ($n = 1) {
$re = [regex] '(.)\1*'
$ret = ""
foreach ($m in $re.Matches($n)) {
$ret += [string] $m.Length + $m.Value[0]
}
return $ret
}
function Get-MultipleLookAndSay ($n) {
if ($n -eq 0) {
return @()
} else {
$a = 1
$a
for ($i = 1; $i -lt $n; $i++) {
$a = Get-LookAndSay $a
$a
}
}
}
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Translate this program into C but keep the logic exactly as in R. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Convert this R block to C#, preserving its control flow and logic. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Write the same code in C++ as shown below in R. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Convert this R block to Java, preserving its control flow and logic. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Rewrite this program in Python while keeping its functionality equivalent to the R version. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Translate the given R code snippet into VB without altering its behavior. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Rewrite the snippet below in Go so it works the same as the original R code. | look.and.say <- function(x, return.an.int=FALSE)
{
xstr <- unlist(strsplit(as.character(x), ""))
rlex <- rle(xstr)
odds <- as.character(rlex$lengths)
evens <- rlex$values
newstr <- as.vector(rbind(odds, evens))
newstr <- paste(newstr, collapse="")
if(return.an.int) as.integer(newstr) else newstr
}
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Ensure the translated C code behaves exactly like the original Racket snippet. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Produce a functionally identical C# code for the snippet given in Racket. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Port the following code from Racket to C++ with equivalent syntax and logic. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Translate this program into Java but keep the logic exactly as in Racket. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Port the following code from Racket to Python with equivalent syntax and logic. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Keep all operations the same but rewrite the snippet in VB. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Produce a language-to-language conversion: from Racket to Go, same semantics. | #lang racket
(define (encode str)
(regexp-replace* #px"(.)\\1*" str (lambda (m c) (~a (string-length m) c))))
(define (look-and-say-sequence n)
(reverse (for/fold ([r '("1")]) ([n n]) (cons (encode (car r)) r))))
(for-each displayln (look-and-say-sequence 10))
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Convert this COBOL snippet to C and keep its semantics consistent. | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Can you help me rewrite this code in C# instead of COBOL, keeping it the same logically? | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Write a version of this COBOL function in C++ with identical behavior. | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Translate this program into Java but keep the logic exactly as in COBOL. | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Generate a Python translation of this COBOL snippet without changing its computational steps. | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Can you help me rewrite this code in VB instead of COBOL, keeping it the same logically? | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Please provide an equivalent version of this COBOL code in Go. | IDENTIFICATION DIVISION.
PROGRAM-ID. LOOK-AND-SAY-SEQ.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEQUENCES.
02 CUR-SEQ PIC X(80) VALUE "1".
02 CUR-CHARS REDEFINES CUR-SEQ
PIC X OCCURS 80 TIMES INDEXED BY CI.
02 CUR-LENGTH PIC 99 COMP VALUE 1.
02 NEXT-SEQ PIC X(80).
02 NEXT-CHARS REDEFINES NEXT-SEQ
PIC X OCCURS 80 TIMES INDEXED BY NI.
01 ALG-STATE.
02 STEP-AMOUNT PIC 99 VALUE 14.
02 ITEM-COUNT PIC 9.
PROCEDURE DIVISION.
LOOK-AND-SAY.
DISPLAY CUR-SEQ.
SET CI TO 1.
SET NI TO 1.
MAKE-NEXT-ENTRY.
MOVE 0 TO ITEM-COUNT.
IF CI IS GREATER THAN CUR-LENGTH GO TO STEP-DONE.
TALLY-ITEM.
ADD 1 TO ITEM-COUNT.
SET CI UP BY 1.
IF CI IS NOT GREATER THAN CUR-LENGTH
AND CUR-CHARS(CI) IS EQUAL TO CUR-CHARS(CI - 1)
GO TO TALLY-ITEM.
INSERT-ENTRY.
MOVE ITEM-COUNT TO NEXT-CHARS(NI).
MOVE CUR-CHARS(CI - 1) TO NEXT-CHARS(NI + 1).
SET NI UP BY 2.
GO TO MAKE-NEXT-ENTRY.
STEP-DONE.
MOVE NEXT-SEQ TO CUR-SEQ.
SET NI DOWN BY 1.
SET CUR-LENGTH TO NI.
SUBTRACT 1 FROM STEP-AMOUNT.
IF STEP-AMOUNT IS NOT EQUAL TO ZERO GO TO LOOK-AND-SAY.
STOP RUN.
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Write a version of this REXX function in C with identical behavior. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Generate a C# translation of this REXX snippet without changing its computational steps. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Change the following REXX code into C++ without altering its purpose. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Ensure the translated Java code behaves exactly like the original REXX snippet. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Change the programming language of this snippet from REXX to Python without modifying what it does. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Write the same code in VB as shown below in REXX. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Preserve the algorithm and functionality while converting the code from REXX to Go. |
parse arg N ! .
if N=='' | N=="," then N= 20
if !=='' | !=="," then != 1
do j=1 for abs(N)
if j\==1 then != lookNsay(!)
if N<0 then say 'length['j"]:" length(!)
else say '['j"]:" !
end
exit 0
lookNsay: procedure; parse arg x,,$
fin = '0'x
x= x || fin
do k=1 by 0
y= substr(x, k, 1)
if y== fin then return $
_= verify(x, y, , k) - k
$= $ || _ || y
k= k + _
end
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Rewrite this program in C while keeping its functionality equivalent to the Ruby version. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Write the same algorithm in C# as shown in this Ruby implementation. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Translate the given Ruby code snippet into C++ without altering its behavior. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Transform the following Ruby implementation into Java, maintaining the same output and logic. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Port the provided Ruby code into Python while preserving the original functionality. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Port the following code from Ruby to VB with equivalent syntax and logic. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Port the provided Ruby code into Go while preserving the original functionality. | class String
def lookandsay
gsub(/(.)\1*/){ |s| s.size.to_s + s[0] }
end
end
ss = '1'
12.times { puts ss; ss = ss.to_s.lookandsay }
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Change the programming language of this snippet from Scala to C without modifying what it does. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Write a version of this Scala function in C# with identical behavior. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Convert this Scala snippet to C++ and keep its semantics consistent. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Produce a language-to-language conversion: from Scala to Java, same semantics. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Preserve the algorithm and functionality while converting the code from Scala to Python. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Convert the following code from Scala to VB, ensuring the logic remains intact. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| function looksay( n )
dim i
dim accum
dim res
dim c
res = vbnullstring
do
if n = vbnullstring then exit do
accum = 0
c = left( n,1 )
do while left( n, 1 ) = c
accum = accum + 1
n = mid(n,2)
loop
if accum > 0 then
res = res & accum & c
end if
loop
looksay = res
end function
|
Change the programming language of this snippet from Scala to Go without modifying what it does. |
fun lookAndSay(s: String): String {
val sb = StringBuilder()
var digit = s[0]
var count = 1
for (i in 1 until s.length) {
if (s[i] == digit)
count++
else {
sb.append("$count$digit")
digit = s[i]
count = 1
}
}
return sb.append("$count$digit").toString()
}
fun main(args: Array<String>) {
var las = "1"
for (i in 1..15) {
println(las)
las = lookAndSay(las)
}
}
| package main
import (
"fmt"
"strconv"
)
func lss(s string) (r string) {
c := s[0]
nc := 1
for i := 1; i < len(s); i++ {
d := s[i]
if d == c {
nc++
continue
}
r += strconv.Itoa(nc) + string(c)
c = d
nc = 1
}
return r + strconv.Itoa(nc) + string(c)
}
func main() {
s := "1"
fmt.Println(s)
for i := 0; i < 8; i++ {
s = lss(s)
fmt.Println(s)
}
}
|
Port the provided Swift code into C while preserving the original functionality. | func lookAndSay(_ seq: [Int]) -> [Int] {
var result = [Int]()
var cur = seq[0]
var curRunLength = 1
for i in seq.dropFirst() {
if cur == i {
curRunLength += 1
} else {
result.append(curRunLength)
result.append(cur)
curRunLength = 1
cur = i
}
}
result.append(curRunLength)
result.append(cur)
return result
}
var seq = [1]
for i in 0..<10 {
print("Seq \(i): \(seq)")
seq = lookAndSay(seq)
}
| #include <stdio.h>
#include <stdlib.h>
int main()
{
char *a = malloc(2), *b = 0, *x, c;
int cnt, len = 1;
for (sprintf(a, "1"); (b = realloc(b, len * 2 + 1)); a = b, b = x) {
puts(x = a);
for (len = 0, cnt = 1; (c = *a); ) {
if (c == *++a)
cnt++;
else if (c) {
len += sprintf(b + len, "%d%c", cnt, c);
cnt = 1;
}
}
}
return 0;
}
|
Write the same code in C# as shown below in Swift. | func lookAndSay(_ seq: [Int]) -> [Int] {
var result = [Int]()
var cur = seq[0]
var curRunLength = 1
for i in seq.dropFirst() {
if cur == i {
curRunLength += 1
} else {
result.append(curRunLength)
result.append(cur)
curRunLength = 1
cur = i
}
}
result.append(curRunLength)
result.append(cur)
return result
}
var seq = [1]
for i in 0..<10 {
print("Seq \(i): \(seq)")
seq = lookAndSay(seq)
}
| using System;
using System.Text;
using System.Linq;
class Program
{
static string lookandsay(string number)
{
StringBuilder result = new StringBuilder();
char repeat = number[0];
number = number.Substring(1, number.Length-1)+" ";
int times = 1;
foreach (char actual in number)
{
if (actual != repeat)
{
result.Append(Convert.ToString(times)+repeat);
times = 1;
repeat = actual;
}
else
{
times += 1;
}
}
return result.ToString();
}
static void Main(string[] args)
{
string num = "1";
foreach (int i in Enumerable.Range(1, 10)) {
Console.WriteLine(num);
num = lookandsay(num);
}
}
}
|
Produce a language-to-language conversion: from Swift to C++, same semantics. | func lookAndSay(_ seq: [Int]) -> [Int] {
var result = [Int]()
var cur = seq[0]
var curRunLength = 1
for i in seq.dropFirst() {
if cur == i {
curRunLength += 1
} else {
result.append(curRunLength)
result.append(cur)
curRunLength = 1
cur = i
}
}
result.append(curRunLength)
result.append(cur)
return result
}
var seq = [1]
for i in 0..<10 {
print("Seq \(i): \(seq)")
seq = lookAndSay(seq)
}
| #include <iostream>
#include <sstream>
#include <string>
std::string lookandsay(const std::string& s)
{
std::ostringstream r;
for (std::size_t i = 0; i != s.length();) {
auto new_i = s.find_first_not_of(s[i], i + 1);
if (new_i == std::string::npos)
new_i = s.length();
r << new_i - i << s[i];
i = new_i;
}
return r.str();
}
int main()
{
std::string laf = "1";
std::cout << laf << '\n';
for (int i = 0; i < 10; ++i) {
laf = lookandsay(laf);
std::cout << laf << '\n';
}
}
|
Port the following code from Swift to Java with equivalent syntax and logic. | func lookAndSay(_ seq: [Int]) -> [Int] {
var result = [Int]()
var cur = seq[0]
var curRunLength = 1
for i in seq.dropFirst() {
if cur == i {
curRunLength += 1
} else {
result.append(curRunLength)
result.append(cur)
curRunLength = 1
cur = i
}
}
result.append(curRunLength)
result.append(cur)
return result
}
var seq = [1]
for i in 0..<10 {
print("Seq \(i): \(seq)")
seq = lookAndSay(seq)
}
| public static String lookandsay(String number){
StringBuilder result= new StringBuilder();
char repeat= number.charAt(0);
number= number.substring(1) + " ";
int times= 1;
for(char actual: number.toCharArray()){
if(actual != repeat){
result.append(times + "" + repeat);
times= 1;
repeat= actual;
}else{
times+= 1;
}
}
return result.toString();
}
|
Transform the following Swift implementation into Python, maintaining the same output and logic. | func lookAndSay(_ seq: [Int]) -> [Int] {
var result = [Int]()
var cur = seq[0]
var curRunLength = 1
for i in seq.dropFirst() {
if cur == i {
curRunLength += 1
} else {
result.append(curRunLength)
result.append(cur)
curRunLength = 1
cur = i
}
}
result.append(curRunLength)
result.append(cur)
return result
}
var seq = [1]
for i in 0..<10 {
print("Seq \(i): \(seq)")
seq = lookAndSay(seq)
}
| def lookandsay(number):
result = ""
repeat = number[0]
number = number[1:]+" "
times = 1
for actual in number:
if actual != repeat:
result += str(times)+repeat
times = 1
repeat = actual
else:
times += 1
return result
num = "1"
for i in range(10):
print num
num = lookandsay(num)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.