Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Port the following code from Common_Lisp to Python with equivalent syntax and logic.
ISBN13Check =LAMBDA(s, LET( ns, FILTERP( LAMBDA(v, NOT(ISERROR(v)) ) )( VALUE(CHARSROW(s)) ), ixs, SEQUENCE( 1, COLUMNS(ns), 1, 1 ), 0 = MOD( SUM( IF(0 <> MOD(ixs, 2), INDEX(ns, ixs), 3 * INDEX(ns, ixs) ) ), 10 ) ) )
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Transform the following Common_Lisp implementation into VB, maintaining the same output and logic.
ISBN13Check =LAMBDA(s, LET( ns, FILTERP( LAMBDA(v, NOT(ISERROR(v)) ) )( VALUE(CHARSROW(s)) ), ixs, SEQUENCE( 1, COLUMNS(ns), 1, 1 ), 0 = MOD( SUM( IF(0 <> MOD(ixs, 2), INDEX(ns, ixs), 3 * INDEX(ns, ixs) ) ), 10 ) ) )
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Convert this Common_Lisp block to VB, preserving its control flow and logic.
ISBN13Check =LAMBDA(s, LET( ns, FILTERP( LAMBDA(v, NOT(ISERROR(v)) ) )( VALUE(CHARSROW(s)) ), ixs, SEQUENCE( 1, COLUMNS(ns), 1, 1 ), 0 = MOD( SUM( IF(0 <> MOD(ixs, 2), INDEX(ns, ixs), 3 * INDEX(ns, ixs) ) ), 10 ) ) )
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Port the provided Common_Lisp code into Go while preserving the original functionality.
ISBN13Check =LAMBDA(s, LET( ns, FILTERP( LAMBDA(v, NOT(ISERROR(v)) ) )( VALUE(CHARSROW(s)) ), ixs, SEQUENCE( 1, COLUMNS(ns), 1, 1 ), 0 = MOD( SUM( IF(0 <> MOD(ixs, 2), INDEX(ns, ixs), 3 * INDEX(ns, ixs) ) ), 10 ) ) )
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Translate the given Common_Lisp code snippet into Go without altering its behavior.
ISBN13Check =LAMBDA(s, LET( ns, FILTERP( LAMBDA(v, NOT(ISERROR(v)) ) )( VALUE(CHARSROW(s)) ), ixs, SEQUENCE( 1, COLUMNS(ns), 1, 1 ), 0 = MOD( SUM( IF(0 <> MOD(ixs, 2), INDEX(ns, ixs), 3 * INDEX(ns, ixs) ) ), 10 ) ) )
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Rewrite this program in C while keeping its functionality equivalent to the D version.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Port the provided D code into C while preserving the original functionality.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Maintain the same structure and functionality when rewriting this code in C#.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Write the same algorithm in C# as shown in this D implementation.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Ensure the translated C++ code behaves exactly like the original D snippet.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Convert this D snippet to C++ and keep its semantics consistent.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Convert this D snippet to Java and keep its semantics consistent.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Can you help me rewrite this code in Java instead of D, keeping it the same logically?
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Generate an equivalent Python version of this D code.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Generate a Python translation of this D snippet without changing its computational steps.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Port the provided D code into VB while preserving the original functionality.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Preserve the algorithm and functionality while converting the code from D to VB.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Please provide an equivalent version of this D code in Go.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Port the provided D code into Go while preserving the original functionality.
import std.stdio; bool isValidISBN13(string text) { int sum, i; foreach (c; text) { if ('0' <= c && c <= '9') { int value = c - '0'; if (i % 2 == 0) { sum += value; } else { sum += 3 * value; } i++; } } return i == 13 && 0 == sum % 10; } unittest { assert(isValidISBN13("978-1734314502")); assert(!isValidISBN13("978-1734314509")); assert(isValidISBN13("978-1788399081")); assert(!isValidISBN13("978-1788399083")); }
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Convert the following code from F# to C, ensuring the logic remains intact.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Rewrite this program in C while keeping its functionality equivalent to the F# version.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Ensure the translated C# code behaves exactly like the original F# snippet.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Generate an equivalent C# version of this F# code.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Please provide an equivalent version of this F# code in C++.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Transform the following F# implementation into C++, maintaining the same output and logic.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Write the same algorithm in Java as shown in this F# implementation.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Ensure the translated Java code behaves exactly like the original F# snippet.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Can you help me rewrite this code in Python instead of F#, keeping it the same logically?
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Translate the given F# code snippet into Python without altering its behavior.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Change the following F# code into VB without altering its purpose.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Write a version of this F# function in VB with identical behavior.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Can you help me rewrite this code in Go instead of F#, keeping it the same logically?
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Please provide an equivalent version of this F# code in Go.
open System let parseInput (input: string) = Seq.map(fun c -> c |> string) input |> Seq.toList |> List.map(fun x -> Int32.Parse x) [<EntryPoint>] let main argv = let isbnnum = parseInput (String.filter (fun x -> x <> '-') argv.[0]) let everyOther = List.mapi (fun i x -> if i % 2 = 0 then x * 3 else x) isbnnum let sum = List.sum everyOther + List.sum isbnnum if sum % 10 = 0 then printfn "%s Valid ISBN13" argv.[0] else printfn "%s Invalid ISBN13" argv.[0] 0
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Write the same algorithm in C as shown in this Factor implementation.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Port the following code from Factor to C with equivalent syntax and logic.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Change the following Factor code into C# without altering its purpose.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Port the provided Factor code into C# while preserving the original functionality.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Convert this Factor snippet to C++ and keep its semantics consistent.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Ensure the translated C++ code behaves exactly like the original Factor snippet.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Preserve the algorithm and functionality while converting the code from Factor to Java.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Transform the following Factor implementation into Java, maintaining the same output and logic.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Write a version of this Factor function in Python with identical behavior.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Write the same code in Python as shown below in Factor.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Can you help me rewrite this code in VB instead of Factor, keeping it the same logically?
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Generate an equivalent VB version of this Factor code.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Generate a Go translation of this Factor snippet without changing its computational steps.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Port the provided Factor code into Go while preserving the original functionality.
USING: combinators.short-circuit formatting kernel math math.functions math.parser math.vectors qw sequences sequences.extras sets unicode ; : (isbn13?) ( str -- ? ) string>digits [ <evens> sum ] [ <odds> 3 v*n sum + ] bi 10 divisor? ; : isbn13? ( str -- ? ) "- " without { [ length 13 = ] [ [ digit? ] all? ] [ (isbn13?) ] } 1&& ; qw{ 978-1734314502 978-1734314509 978-1788399081 978-1788399083 } [ dup isbn13? "good" "bad" ? "%s: %s\n" printf ] each
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Ensure the translated C# code behaves exactly like the original Fortran snippet.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Produce a language-to-language conversion: from Fortran to C#, same semantics.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Write the same code in C++ as shown below in Fortran.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Convert the following code from Fortran to C++, ensuring the logic remains intact.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Convert this Fortran snippet to C and keep its semantics consistent.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Port the provided Fortran code into C while preserving the original functionality.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Preserve the algorithm and functionality while converting the code from Fortran to Java.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Convert the following code from Fortran to Java, ensuring the logic remains intact.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Preserve the algorithm and functionality while converting the code from Fortran to Python.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Change the following Fortran code into Python without altering its purpose.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Can you help me rewrite this code in VB instead of Fortran, keeping it the same logically?
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Ensure the translated VB code behaves exactly like the original Fortran snippet.
program isbn13 implicit none character(len=14), dimension(4), parameter :: isbns=["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] integer :: i do i = 1, ubound(isbns, 1) if (check_isbn13(isbns(i))) then print*, isbns(i), " : ", "good" else print*, isbns(i), " : ", "bad" end if end do contains pure function check_isbn13(isbn) character(len=*), intent(in) :: isbn logical :: check_isbn13 integer :: summ, counter, i, digit check_isbn13 = .false. counter = 0 summ = 0 do i = 1, len(isbn) if (isbn(i:i) == ' ' .or. isbn(i:i) == '-') cycle counter = counter + 1 read(isbn(i:i), '(I1)') digit if (modulo(counter, 2) == 0) then summ = summ + 3*digit else summ = summ + digit end if end do if (counter == 13 .and. modulo(summ, 10) == 0) check_isbn13 = .true. end function check_isbn13 end program isbn13
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Transform the following Haskell implementation into C, maintaining the same output and logic.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Convert this Haskell block to C, preserving its control flow and logic.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Convert this Haskell snippet to C# and keep its semantics consistent.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Write the same code in C# as shown below in Haskell.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Write the same code in C++ as shown below in Haskell.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Convert this Haskell snippet to C++ and keep its semantics consistent.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Rewrite this program in Java while keeping its functionality equivalent to the Haskell version.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Generate a Java translation of this Haskell snippet without changing its computational steps.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Port the provided Haskell code into Python while preserving the original functionality.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Generate an equivalent Python version of this Haskell code.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Keep all operations the same but rewrite the snippet in VB.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Keep all operations the same but rewrite the snippet in VB.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Convert this Haskell block to Go, preserving its control flow and logic.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Change the following Haskell code into Go without altering its purpose.
import Data.Char (digitToInt, isDigit) import Text.Printf (printf) pair :: Num a => [a] -> [(a, a)] pair [] = [] pair xs = p (take 2 xs) : pair (drop 2 xs) where p ps = case ps of (x : y : zs) -> (x, y) (x : zs) -> (x, 0) validIsbn13 :: String -> Bool validIsbn13 isbn | length (digits isbn) /= 13 = False | otherwise = calc isbn `rem` 10 == 0 where digits = map digitToInt . filter isDigit calc = sum . map (\(x, y) -> x + y * 3) . pair . digits main :: IO () main = mapM_ (printf "%s: Valid: %s\n" <*> (show . validIsbn13)) [ "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" ]
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Maintain the same structure and functionality when rewriting this code in C.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Translate the given J code snippet into C without altering its behavior.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Write the same code in C# as shown below in J.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Port the following code from J to C# with equivalent syntax and logic.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Port the following code from J to C++ with equivalent syntax and logic.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Rewrite this program in C++ while keeping its functionality equivalent to the J version.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Produce a functionally identical Java code for the snippet given in J.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Write a version of this J function in Java with identical behavior.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Maintain the same structure and functionality when rewriting this code in Python.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Translate the given J code snippet into Python without altering its behavior.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Transform the following J implementation into VB, maintaining the same output and logic.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Rewrite the snippet below in VB so it works the same as the original J code.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Write the same code in Go as shown below in J.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Rewrite this program in Go while keeping its functionality equivalent to the J version.
D =: '0123456789' isbn13c =: D&([ check@:i. clean) check =: 0 = 10 | lc lc =: [ +/@:* weight weight =: 1 3 $~ # clean =: ] -. a. -. [
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }
Keep all operations the same but rewrite the snippet in C.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Port the provided Julia code into C while preserving the original functionality.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
#include <stdio.h> int check_isbn13(const char *isbn) { int ch = *isbn, count = 0, sum = 0; for ( ; ch != 0; ch = *++isbn, ++count) { if (ch == ' ' || ch == '-') { --count; continue; } if (ch < '0' || ch > '9') { return 0; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } } if (count != 13) return 0; return !(sum%10); } int main() { int i; const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"}; for (i = 0; i < 4; ++i) { printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad"); } return 0; }
Port the provided Julia code into C# while preserving the original functionality.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Maintain the same structure and functionality when rewriting this code in C#.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
using System; using System.Linq; public class Program { public static void Main() { Console.WriteLine(CheckISBN13("978-1734314502")); Console.WriteLine(CheckISBN13("978-1734314509")); Console.WriteLine(CheckISBN13("978-1788399081")); Console.WriteLine(CheckISBN13("978-1788399083")); static bool CheckISBN13(string code) { code = code.Replace("-", "").Replace(" ", ""); if (code.Length != 13) return false; int sum = 0; foreach (var (index, digit) in code.Select((digit, index) => (index, digit))) { if (char.IsDigit(digit)) sum += (digit - '0') * (index % 2 == 0 ? 1 : 3); else return false; } return sum % 10 == 0; } } }
Convert this Julia snippet to C++ and keep its semantics consistent.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Change the following Julia code into C++ without altering its purpose.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
#include <iostream> bool check_isbn13(std::string isbn) { int count = 0; int sum = 0; for (auto ch : isbn) { if (ch == ' ' || ch == '-') { continue; } if (ch < '0' || ch > '9') { return false; } if (count & 1) { sum += 3 * (ch - '0'); } else { sum += ch - '0'; } count++; } if (count != 13) { return false; } return sum % 10 == 0; } int main() { auto isbns = { "978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083" }; for (auto isbn : isbns) { std::cout << isbn << ": " << (check_isbn13(isbn) ? "good" : "bad") << '\n'; } return 0; }
Convert this Julia snippet to Java and keep its semantics consistent.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Produce a functionally identical Java code for the snippet given in Julia.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
public static void main(){ System.out.println(isISBN13("978-1734314502")); System.out.println(isISBN13("978-1734314509")); System.out.println(isISBN13("978-1788399081")); System.out.println(isISBN13("978-1788399083")); } public static boolean isISBN13(String in){ int pre = Integer.parseInt(in.substring(0,3)); if (pre!=978)return false; String postStr = in.substring(4); if (postStr.length()!=10)return false; int post = Integer.parseInt(postStr); int sum = 38; for(int x = 0; x<10;x+=2) sum += (postStr.charAt(x)-48)*3 + ((postStr.charAt(x+1)-48)); if(sum%10==0) return true; return false; }
Maintain the same structure and functionality when rewriting this code in Python.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Write the same code in Python as shown below in Julia.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
def is_isbn13(n): n = n.replace('-','').replace(' ', '') if len(n) != 13: return False product = (sum(int(ch) for ch in n[::2]) + sum(int(ch) * 3 for ch in n[1::2])) return product % 10 == 0 if __name__ == '__main__': tests = .strip().split() for t in tests: print(f"ISBN13 {t} validates {is_isbn13(t)}")
Write the same algorithm in VB as shown in this Julia implementation.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Convert the following code from Julia to VB, ensuring the logic remains intact.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
arraybase 1 dim isbn = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083", "978-2-74839-908-0", "978-2-74839-908-5", "978 1 86197 876 9"} for n = 1 to isbn[?] sum = 0 isbnStr = isbn[n] isbnStr = replace(isbnStr, "-", "") isbnStr = replace(isbnStr, " ", "") for m = 1 to length(isbnStr) if m mod 2 = 0 then num = 3 * int(mid(isbnStr, m, 1)) else num = int(mid(isbnStr, m, 1)) end if sum += num next m if sum mod 10 = 0 then print isbn[n]; ": good" else print isbn[n]; ": bad" end if next n
Transform the following Julia implementation into Go, maintaining the same output and logic.
function isbncheck(str) return sum(iseven(i) ? 3 * parse(Int, ch) : parse(Int, ch) for (i, ch) in enumerate(replace(str, r"\D" => ""))) % 10 == 0 end const testingcodes = ["978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"] for code in testingcodes println(code, ": ", isbncheck(code) ? "good" : "bad") end
package main import ( "fmt" "strings" "unicode/utf8" ) func checkIsbn13(isbn string) bool { isbn = strings.ReplaceAll(strings.ReplaceAll(isbn, "-", ""), " ", "") le := utf8.RuneCountInString(isbn) if le != 13 { return false } sum := int32(0) for i, c := range isbn { if c < '0' || c > '9' { return false } if i%2 == 0 { sum += c - '0' } else { sum += 3 * (c - '0') } } return sum%10 == 0 } func main() { isbns := []string{"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"} for _, isbn := range isbns { res := "bad" if checkIsbn13(isbn) { res = "good" } fmt.Printf("%s: %s\n", isbn, res) } }