Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Produce a language-to-language conversion: from Tcl to C++, same semantics. | package require Tcl 8.6;
oo::class create Caesar {
variable encryptMap decryptMap
constructor shift {
for {set i 0} {$i < 26} {incr i} {
append encryptMap [format "%c %c %c %c " \
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
append decr... | #include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
class MyTransform {
private :
int shift ;
public :
MyTransform( int s ) : shift( s ) { }
char operator( )( char c ) {
if ( isspace( c ) )
return ' ' ;
else {
static std::string letters( "abcdefghijklmnopqrstuvwxyz"... |
Please provide an equivalent version of this Tcl code in Java. | package require Tcl 8.6;
oo::class create Caesar {
variable encryptMap decryptMap
constructor shift {
for {set i 0} {$i < 26} {incr i} {
append encryptMap [format "%c %c %c %c " \
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
append decr... | public class Cipher {
public static void main(String[] args) {
String str = "The quick brown fox Jumped over the lazy Dog";
System.out.println( Cipher.encode( str, 12 ));
System.out.println( Cipher.decode( Cipher.encode( str, 12), 12 ));
}
public static String decode(String enc, i... |
Rewrite this program in Python while keeping its functionality equivalent to the Tcl version. | package require Tcl 8.6;
oo::class create Caesar {
variable encryptMap decryptMap
constructor shift {
for {set i 0} {$i < 26} {incr i} {
append encryptMap [format "%c %c %c %c " \
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
append decr... | fun caesar(s, k, decode: false):
if decode:
k = 26 - k
result = ''
for i in s.uppercase() where 65 <= ord(i) <= 90:
result.push! char(ord(i) - 65 + k) mod 26 + 65
return result
let message = "The quick brown fox jumped over the lazy dogs"
let encrypted = caesar(msg, 11)
let decrypted = ... |
Convert this Tcl block to VB, preserving its control flow and logic. | package require Tcl 8.6;
oo::class create Caesar {
variable encryptMap decryptMap
constructor shift {
for {set i 0} {$i < 26} {incr i} {
append encryptMap [format "%c %c %c %c " \
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
append decr... | Option Explicit
Sub Main_Caesar()
Dim ch As String
ch = Caesar_Cipher("CAESAR: Who is it in the press that calls on me? I hear a tongue, shriller than all the music, Cry
Debug.Print ch
Debug.Print Caesar_Cipher(ch, -14)
End Sub
Function Caesar_Cipher(sText As String, lngNumber As Long) As String
Dim Tbl,... |
Maintain the same structure and functionality when rewriting this code in Go. | package require Tcl 8.6;
oo::class create Caesar {
variable encryptMap decryptMap
constructor shift {
for {set i 0} {$i < 26} {incr i} {
append encryptMap [format "%c %c %c %c " \
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
append decr... | package main
import (
"fmt"
"strings"
)
type ckey struct {
enc, dec func(rune) rune
}
func newCaesar(k int) (*ckey, bool) {
if k < 1 || k > 25 {
return nil, false
}
rk := rune(k)
return &ckey{
enc: func(c rune) rune {
if c >= 'a' && c <= 'z'-rk || c >= 'A' && c... |
Write the same code in PHP as shown below in Rust. | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Convert this Ada snippet to PHP and keep its semantics consistent. | with Ada.Text_IO;
procedure Caesar is
type modulo26 is modulo 26;
function modulo26 (Character: Character; Output: Character) return modulo26 is
begin
return modulo26 (Character'Pos(Character)+Character'Pos(Output));
end modulo26;
function Character(Val: in modulo26; Output: Character)
... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Write the same code in PHP as shown below in Arturo. | ia: to :integer `a`
iA: to :integer `A`
lowAZ: `a`..`z`
uppAZ: `A`..`Z`
caesar: function [s, xx][
k: (not? null? attr 'decode)? -> 26-xx -> xx
result: new ""
loop s 'i [
(in? i lowAZ)? -> 'result ++ to :char ia + (k + (to :integer i) - ia) % 26
[
(in? i uppAZ)? -> 'result ++ to ... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Transform the following AutoHotKey implementation into PHP, maintaining the same output and logic. | n=2
s=HI
t:=&s
While *t
o.=Chr(Mod(*t-65+n,26)+65),t+=2
MsgBox % o
| <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Rewrite this program in PHP while keeping its functionality equivalent to the AWK version. |
BEGIN {
message = "My hovercraft is full of eels."
key = 1
cypher = caesarEncode(key, message)
clear = caesarDecode(key, cypher)
print "message: " message
print " cypher: " cypher
print " clear: " clear
exit
}
function caesarEncode(key, message) {
return caesarXlat(key, messag... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Convert this BBC_Basic block to PHP, preserving its control flow and logic. | plaintext$ = "Pack my box with five dozen liquor jugs"
PRINT plaintext$
key% = RND(25)
cyphertext$ = FNcaesar(plaintext$, key%)
PRINT cyphertext$
decyphered$ = FNcaesar(cyphertext$, 26-key%)
PRINT decyphered$
END
DEF FNcaesar(text$, key%)... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Translate the given Clojure code snippet into PHP without altering its behavior. | (defn encrypt-character [offset c]
(if (Character/isLetter c)
(let [v (int c)
base (if (>= v (int \a))
(int \a)
(int \A))
offset (mod offset 26)]
(char (+ (mod (+ (- v base) offset) 26)
base)))
c))
(defn encrypt [offset text]
(app... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Maintain the same structure and functionality when rewriting this code in PHP. | (defun encipher-char (ch key)
(let* ((c (char-code ch)) (la (char-code #\a)) (ua (char-code #\A))
(base (cond ((<= la c (char-code #\z)) la)
((<= ua c (char-code #\Z)) ua)
(nil))))
(if base (code-char (+ (mod (+ (- c base) key) 26) base)) ch)))
(defun caesa... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Ensure the translated PHP code behaves exactly like the original D snippet. | import std.stdio, std.traits;
S rot(S)(in S s, in int key) pure nothrow @safe
if (isSomeString!S) {
auto res = s.dup;
foreach (immutable i, ref c; res) {
if ('a' <= c && c <= 'z')
c = ((c - 'a' + key) % 26 + 'a');
else if ('A' <= c && c <= 'Z')
c = ((c - 'A' + key) % 26... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Write a version of this Elixir function in PHP with identical behavior. | defmodule Caesar_cipher do
defp set_map(map, range, key) do
org = Enum.map(range, &List.to_string [&1])
{a, b} = Enum.split(org, key)
Enum.zip(org, b ++ a) |> Enum.into(map)
end
def encode(text, key) do
map = Map.new |> set_map(?a..?z, key) |> set_map(?A..?Z, key)
String.graphemes(text) |> ... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Rewrite the snippet below in PHP so it works the same as the original Erlang code. |
-module(ceasar).
-export([main/2]).
rot(Char,Key) when (Char >= $A) and (Char =< $Z) or
(Char >= $a) and (Char =< $z) ->
Offset = $A + Char band 32,
N = Char - Offset,
Offset + (N + Key) rem 26;
rot(Char, _Key) ->
Char.
key(Key) when Key < 0 ->
26 + Key rem 26;
key(Key) when Key > ... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Produce a language-to-language conversion: from F# to PHP, same semantics. | module caesar =
open System
let private cipher n s =
let shift c =
if Char.IsLetter c then
let a = (if Char.IsLower c then 'a' else 'A') |> int
(int c - a + n) % 26 + a |> char
else c
String.map shift s
let encrypt n = cipher n
le... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Preserve the algorithm and functionality while converting the code from Factor to PHP. | USING: io kernel locals math sequences unicode.categories ;
IN: rosetta-code.caesar-cipher
:: cipher ( n s -- s' )
[| c |
c Letter? [
c letter? CHAR: a CHAR: A ? :> a
c a - n + 26 mod a +
]
[ c ] if
] :> shift
s [ shift call ] map ;
: encrypt ( n s -- s'... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Preserve the algorithm and functionality while converting the code from Forth to PHP. |
: modulate
tuck n:- 26 n:+ 26 n:mod n:+ ;
: caesar
>r
s:map rdrop ;
"The five boxing wizards jump quickly!"
dup . cr
1 caesar dup . cr
-1 caesar . cr
bye
| <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Port the provided Fortran code into PHP while preserving the original functionality. | program Caesar_Cipher
implicit none
integer, parameter :: key = 3
character(43) :: message = "The five boxing wizards jump quickly"
write(*, "(2a)") "Original message = ", message
call encrypt(message)
write(*, "(2a)") "Encrypted message = ", message
call decrypt(message)
write(*, "(2a)") "Decry... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Translate the given Groovy code snippet into PHP without altering its behavior. | def caesarEncode(cipherKey, text) {
def builder = new StringBuilder()
text.each { character ->
int ch = character[0] as char
switch(ch) {
case 'a'..'z': ch = ((ch - 97 + cipherKey) % 26 + 97); break
case 'A'..'Z': ch = ((ch - 65 + cipherKey) % 26 + 65); break
}
... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Rewrite the snippet below in PHP so it works the same as the original Haskell code. | module Caesar (caesar, uncaesar) where
import Data.Char
caesar, uncaesar :: (Integral a) => a -> String -> String
caesar k = map f
where f c = case generalCategory c of
LowercaseLetter -> addChar 'a' k c
UppercaseLetter -> addChar 'A' k c
_ -> c
uncaesar k ... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Keep all operations the same but rewrite the snippet in PHP. | procedure main()
ctext := caesar(ptext := map("The quick brown fox jumped over the lazy dog"))
dtext := caesar(ctext,,"decrypt")
write("Plain text = ",image(ptext))
write("Encphered text = ",image(ctext))
write("Decphered text = ",image(dtext))
end
procedure caesar(text,k,mode)
/k := 3
k := (((k % *&lcase) + *&lca... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Write the same algorithm in PHP as shown in this J implementation. | cndx=: [: , 65 97 +/ 26 | (i.26)&+
caesar=: (cndx 0)}&a.@u:@cndx@[ {~ a.i.]
| <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Ensure the translated PHP code behaves exactly like the original Julia snippet. |
function csrcipher(text, key)
ciphtext = ""
for l in text
numl = Int(l)
ciphnuml = numl + key
if numl in 65:90
if ciphnuml > 90
rotciphnuml = ciphnuml - 26
ciphtext = ciphtext * Char(rotciphnuml)
else
ciphtext = ... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Convert the following code from Lua to PHP, ensuring the logic remains intact. | local function encrypt(text, key)
return text:gsub("%a", function(t)
local base = (t:lower() == t and string.byte('a') or string.byte('A'))
local r = t:byte() - base
r = r + key
r = r%26
r = r + base
return string.char(r)
end)
end
local function decrypt(text, key)
return encrypt(text, -key)
end... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Translate this program into PHP but keep the logic exactly as in Mathematica. | cypher[mesg_String,n_Integer]:=StringReplace[mesg,Flatten[Thread[Rule[#,RotateLeft[#,n]]]&/@CharacterRange@@@{{"a","z"},{"A","Z"}}]]
| <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Keep all operations the same but rewrite the snippet in PHP. | function s = cipherCaesar(s, key)
s = char( mod(s - 'A' + key, 25 ) + 'A');
end;
function s = decipherCaesar(s, key)
s = char( mod(s - 'A' - key, 25 ) + 'A');
end;
| <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Convert this Nim block to PHP, preserving its control flow and logic. | import strutils
proc caesar(s: string, k: int, decode = false): string =
var k = if decode: 26 - k else: k
result = ""
for i in toUpper(s):
if ord(i) >= 65 and ord(i) <= 90:
result.add(chr((ord(i) - 65 + k) mod 26 + 65))
let msg = "The quick brown fox jumped over the lazy dogs"
echo msg
let enc = caes... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Convert this OCaml block to PHP, preserving its control flow and logic. | fun readfile () = readfile []
| x = let val ln = readln ()
in if eof ln then
rev x
else
readfile ` ln :: x
end
local
val lower_a = ord #"a";
val lower_z = ord #"z";
val upper_a = ord #"A";
val upper_z = or... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Port the provided Pascal code into PHP while preserving the original functionality. | Program CaesarCipher(output);
procedure encrypt(var message: string; key: integer);
var
i: integer;
begin
for i := 1 to length(message) do
case message[i] of
'A'..'Z': message[i] := chr(ord('A') + (ord(message[i]) - ord('A') + key) mod 26);
'a'..'z': message[i] := chr(ord('a') + (ord(... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Preserve the algorithm and functionality while converting the code from Perl to PHP. | sub caesar {
my ($message, $key, $decode) = @_;
$key = 26 - $key if $decode;
$message =~ s/([A-Z])/chr(((ord(uc $1) - 65 + $key) % 26) + 65)/geir;
}
my $msg = 'THE FIVE BOXING WIZARDS JUMP QUICKLY';
my $enc = caesar($msg, 10);
my $dec = caesar($enc, 10, 'decode');
print "msg: $msg\nenc: $enc... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Rewrite this program in PHP while keeping its functionality equivalent to the PowerShell version. |
function Get-CaesarCipher
{
Param
(
[Parameter(
Mandatory=$true,ValueFromPipeline=$true)]
[string]
$Text,
[ValidateRange(1,25)]
[int]
$Key = 1,
[switch]
$Decode
)
begin
{
$LowerAlpha = [char]'a'..[char]'z'
$UpperAlpha = [char]'A'..[char]'Z'
}
process
{
$Chars = $Text.ToCharArray()
function... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Translate this program into PHP but keep the logic exactly as in R. |
ceasar <- function(x, key)
{
if (key < 0) {
key <- 26 + key
}
old <- paste(letters, LETTERS, collapse="", sep="")
new <- paste(substr(old, key * 2 + 1, 52), substr(old, 1, key * 2), sep="")
chartr(old, new, x)
}
print(ceasar("hi",2))
print(ceasar("hi",20))
key <- 3
plaintext <- "The five boxin... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Maintain the same structure and functionality when rewriting this code in PHP. | #lang racket
(define A (char->integer #\A))
(define Z (char->integer #\Z))
(define a (char->integer #\a))
(define z (char->integer #\z))
(define (rotate c n)
(define cnum (char->integer c))
(define (shift base) (integer->char (+ base (modulo (+ n (- cnum base)) 26))))
(cond [(<= A cnum Z) (shift A)]
[... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Write the same algorithm in PHP as shown in this COBOL implementation. | identification division.
program-id. caesar.
data division.
1 msg pic x(50)
value "The quick brown fox jumped over the lazy dog.".
1 offset binary pic 9(4) value 7.
1 from-chars pic x(52).
1 to-chars pic x(52).
1 tabl.
2 pic x(26) value "abcdefg... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Generate a PHP translation of this REXX snippet without changing its computational steps. |
options replace format comments java crossref savelog symbols nobinary
messages = [ -
'The five boxing wizards jump quickly', -
'Attack at dawn!', -
'HI']
keys = [1, 2, 20, 25, 13]
loop m_ = 0 to messages.length - 1
in = messages[m_]
loop k_ = 0 to keys.length - 1
say 'Caesar cipher, key:' keys[k_].ri... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Preserve the algorithm and functionality while converting the code from Ruby to PHP. | class String
ALFABET = ("A".."Z").to_a
def caesar_cipher(num)
self.tr(ALFABET.join, ALFABET.rotate(num).join)
end
end
encypted = "THEYBROKEOURCIPHEREVERYONECANREADTHIS".caesar_cipher(3)
decrypted = encypted.caesar_cipher(-3)
| <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Convert the following code from Scala to PHP, ensuring the logic remains intact. |
object Caesar {
fun encrypt(s: String, key: Int): String {
val offset = key % 26
if (offset == 0) return s
var d: Char
val chars = CharArray(s.length)
for ((index, c) in s.withIndex()) {
if (c in 'A'..'Z') {
d = c + offset
if (d ... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Write the same code in PHP as shown below in Swift. | func usage(_ e:String) {
print("error: \(e)")
print("./caeser -e 19 a-secret-string")
print("./caeser -d 19 tskxvjxlskljafz")
}
func charIsValid(_ c:Character) -> Bool {
return c.isASCII && ( c.isLowercase || 45 == c.asciiValue )
}
func charRotate(_ c:Character, _ by:Int) -> Character {
var cv:UInt8! = c.a... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Keep all operations the same but rewrite the snippet in PHP. | package require Tcl 8.6;
oo::class create Caesar {
variable encryptMap decryptMap
constructor shift {
for {set i 0} {$i < 26} {incr i} {
append encryptMap [format "%c %c %c %c " \
[expr {$i+65}] [expr {($i+$shift)%26+65}] \
[expr {$i+97}] [expr {($i+$shift)%26+97}]]
append decr... | <?php
function caesarEncode( $message, $key ){
$plaintext = strtolower( $message );
$ciphertext = "";
$ascii_a = ord( 'a' );
$ascii_z = ord( 'z' );
while( strlen( $plaintext ) ){
$char = ord( $plaintext );
if( $char >= $ascii_a && $char <= $ascii_z ){
$char = ( ( $key + $... |
Produce a language-to-language conversion: from C# to Rust, same semantics. | using System;
using System.Linq;
namespace CaesarCypher
{
class Program
{
static char Encrypt(char ch, int code)
{
if (!char.IsLetter(ch)) return ch;
char offset = char.IsUpper(ch) ? 'A' : 'a';
return (char)((ch + code - offset) % 26 + offset);
}
... | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... |
Change the following Go code into Rust without altering its purpose. | package main
import (
"fmt"
"strings"
)
type ckey struct {
enc, dec func(rune) rune
}
func newCaesar(k int) (*ckey, bool) {
if k < 1 || k > 25 {
return nil, false
}
rk := rune(k)
return &ckey{
enc: func(c rune) rune {
if c >= 'a' && c <= 'z'-rk || c >= 'A' && c... | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... |
Convert the following code from Rust to Python, ensuring the logic remains intact. | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... | fun caesar(s, k, decode: false):
if decode:
k = 26 - k
result = ''
for i in s.uppercase() where 65 <= ord(i) <= 90:
result.push! char(ord(i) - 65 + k) mod 26 + 65
return result
let message = "The quick brown fox jumped over the lazy dogs"
let encrypted = caesar(msg, 11)
let decrypted = ... |
Change the programming language of this snippet from Rust to VB without modifying what it does. | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... | Option Explicit
Sub Main_Caesar()
Dim ch As String
ch = Caesar_Cipher("CAESAR: Who is it in the press that calls on me? I hear a tongue, shriller than all the music, Cry
Debug.Print ch
Debug.Print Caesar_Cipher(ch, -14)
End Sub
Function Caesar_Cipher(sText As String, lngNumber As Long) As String
Dim Tbl,... |
Generate an equivalent Rust version of this C code. | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define caesar(x) rot(13, x)
#define decaesar(x) rot(13, x)
#define decrypt_rot(x, y) rot((26-x), y)
void rot(int c, char *str)
{
int l = strlen(str);
const char* alpha_low = "abcdefghijklmnopqrstuvwxyz";
const... | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... |
Convert this C++ snippet to Rust and keep its semantics consistent. | #include <string>
#include <iostream>
#include <algorithm>
#include <cctype>
class MyTransform {
private :
int shift ;
public :
MyTransform( int s ) : shift( s ) { }
char operator( )( char c ) {
if ( isspace( c ) )
return ' ' ;
else {
static std::string letters( "abcdefghijklmnopqrstuvwxyz"... | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... |
Rewrite the snippet below in Rust so it works the same as the original Java code. | public class Cipher {
public static void main(String[] args) {
String str = "The quick brown fox Jumped over the lazy Dog";
System.out.println( Cipher.encode( str, 12 ));
System.out.println( Cipher.decode( Cipher.encode( str, 12), 12 ));
}
public static String decode(String enc, i... | use std::io::{self, Write};
use std::fmt::Display;
use std::{env, process};
fn main() {
let shift: u8 = env::args().nth(1)
.unwrap_or_else(|| exit_err("No shift provided", 2))
.parse()
.unwrap_or_else(|e| exit_err(e, 3));
let plain = get_input()
.unwrap_or_else(|e| exit_err(&e,... |
Port the following code from Ada to C# with equivalent syntax and logic. | with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Containers.Ordered_Sets;
package Partitions is
type Arguments is array (Positive range <>) of Natural;
package Number_Sets is new Ada.Containers.Ordered_Sets
(Natural);
type Partition is array (Positive range <>) of Number_Sets.Set;
function "<"... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Convert this Ada snippet to C and keep its semantics consistent. | with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Containers.Ordered_Sets;
package Partitions is
type Arguments is array (Positive range <>) of Natural;
package Number_Sets is new Ada.Containers.Ordered_Sets
(Natural);
type Partition is array (Positive range <>) of Number_Sets.Set;
function "<"... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Produce a language-to-language conversion: from Ada to C++, same semantics. | with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Containers.Ordered_Sets;
package Partitions is
type Arguments is array (Positive range <>) of Natural;
package Number_Sets is new Ada.Containers.Ordered_Sets
(Natural);
type Partition is array (Positive range <>) of Number_Sets.Set;
function "<"... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Ensure the translated Go code behaves exactly like the original Ada snippet. | with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Containers.Ordered_Sets;
package Partitions is
type Arguments is array (Positive range <>) of Natural;
package Number_Sets is new Ada.Containers.Ordered_Sets
(Natural);
type Partition is array (Positive range <>) of Number_Sets.Set;
function "<"... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Preserve the algorithm and functionality while converting the code from Ada to Python. | with Ada.Containers.Indefinite_Ordered_Sets;
with Ada.Containers.Ordered_Sets;
package Partitions is
type Arguments is array (Positive range <>) of Natural;
package Number_Sets is new Ada.Containers.Ordered_Sets
(Natural);
type Partition is array (Positive range <>) of Number_Sets.Set;
function "<"... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Write the same code in C as shown below in BBC_Basic. | DIM list1%(2) : list1%() = 2, 0, 2
PRINT "partitions(2,0,2):"
PRINT FNpartitions(list1%())
DIM list2%(2) : list2%() = 1, 1, 1
PRINT "partitions(1,1,1):"
PRINT FNpartitions(list2%())
DIM list3%(3) : list3%() = 1, 2, 0, 1
PRINT "partitions(1,2,0,1):"
PRINT FNpartition... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Convert the following code from BBC_Basic to C#, ensuring the logic remains intact. | DIM list1%(2) : list1%() = 2, 0, 2
PRINT "partitions(2,0,2):"
PRINT FNpartitions(list1%())
DIM list2%(2) : list2%() = 1, 1, 1
PRINT "partitions(1,1,1):"
PRINT FNpartitions(list2%())
DIM list3%(3) : list3%() = 1, 2, 0, 1
PRINT "partitions(1,2,0,1):"
PRINT FNpartition... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Generate an equivalent C++ version of this BBC_Basic code. | DIM list1%(2) : list1%() = 2, 0, 2
PRINT "partitions(2,0,2):"
PRINT FNpartitions(list1%())
DIM list2%(2) : list2%() = 1, 1, 1
PRINT "partitions(1,1,1):"
PRINT FNpartitions(list2%())
DIM list3%(3) : list3%() = 1, 2, 0, 1
PRINT "partitions(1,2,0,1):"
PRINT FNpartition... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Generate a Python translation of this BBC_Basic snippet without changing its computational steps. | DIM list1%(2) : list1%() = 2, 0, 2
PRINT "partitions(2,0,2):"
PRINT FNpartitions(list1%())
DIM list2%(2) : list2%() = 1, 1, 1
PRINT "partitions(1,1,1):"
PRINT FNpartitions(list2%())
DIM list3%(3) : list3%() = 1, 2, 0, 1
PRINT "partitions(1,2,0,1):"
PRINT FNpartition... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Port the provided BBC_Basic code into Go while preserving the original functionality. | DIM list1%(2) : list1%() = 2, 0, 2
PRINT "partitions(2,0,2):"
PRINT FNpartitions(list1%())
DIM list2%(2) : list2%() = 1, 1, 1
PRINT "partitions(1,1,1):"
PRINT FNpartitions(list2%())
DIM list3%(3) : list3%() = 1, 2, 0, 1
PRINT "partitions(1,2,0,1):"
PRINT FNpartition... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Convert this Common_Lisp block to C, preserving its control flow and logic. | (defun fill-part (x i j l)
(let ((e (elt x i)))
(loop for c in l do
(loop while (>= j (length e)) do
(setf j 0 e (elt x (incf i))))
(setf (elt e j) c)
(incf j))))
(defun next-part (list cmp)
(let* ((l (coerce list 'vector))
(i (1- (length l)))
(e (elt l i)))
(loop while (<= 0 (decf... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Rewrite the snippet below in C# so it works the same as the original Common_Lisp code. | (defun fill-part (x i j l)
(let ((e (elt x i)))
(loop for c in l do
(loop while (>= j (length e)) do
(setf j 0 e (elt x (incf i))))
(setf (elt e j) c)
(incf j))))
(defun next-part (list cmp)
(let* ((l (coerce list 'vector))
(i (1- (length l)))
(e (elt l i)))
(loop while (<= 0 (decf... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Port the provided Common_Lisp code into C++ while preserving the original functionality. | (defun fill-part (x i j l)
(let ((e (elt x i)))
(loop for c in l do
(loop while (>= j (length e)) do
(setf j 0 e (elt x (incf i))))
(setf (elt e j) c)
(incf j))))
(defun next-part (list cmp)
(let* ((l (coerce list 'vector))
(i (1- (length l)))
(e (elt l i)))
(loop while (<= 0 (decf... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Write the same algorithm in Python as shown in this Common_Lisp implementation. | (defun fill-part (x i j l)
(let ((e (elt x i)))
(loop for c in l do
(loop while (>= j (length e)) do
(setf j 0 e (elt x (incf i))))
(setf (elt e j) c)
(incf j))))
(defun next-part (list cmp)
(let* ((l (coerce list 'vector))
(i (1- (length l)))
(e (elt l i)))
(loop while (<= 0 (decf... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Generate an equivalent Go version of this Common_Lisp code. | (defun fill-part (x i j l)
(let ((e (elt x i)))
(loop for c in l do
(loop while (>= j (length e)) do
(setf j 0 e (elt x (incf i))))
(setf (elt e j) c)
(incf j))))
(defun next-part (list cmp)
(let* ((l (coerce list 'vector))
(i (1- (length l)))
(e (elt l i)))
(loop while (<= 0 (decf... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Maintain the same structure and functionality when rewriting this code in C. | import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Translate the given D code snippet into C# without altering its behavior. | import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Port the following code from D to C++ with equivalent syntax and logic. | import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Write the same algorithm in Python as shown in this D implementation. | import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Ensure the translated Go code behaves exactly like the original D snippet. | import std.stdio, std.algorithm, std.range, std.array, std.conv,
combinations3;
alias iRNG = int[];
iRNG[][] orderPart(iRNG blockSize...) {
iRNG tot = iota(1, 1 + blockSize.sum).array;
iRNG[][] p(iRNG s, in iRNG b) {
if (b.empty)
return [[]];
iRNG[][] res;
foreach (... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Please provide an equivalent version of this Elixir code in C. | defmodule Ordered do
def partition([]), do: [[]]
def partition(mask) do
sum = Enum.sum(mask)
if sum == 0 do
[Enum.map(mask, fn _ -> [] end)]
else
Enum.to_list(1..sum)
|> permute
|> Enum.reduce([], fn perm,acc ->
{_, part} = Enum.reduce(mask, {perm,[]}, fn num,{pm,a} -... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Rewrite the snippet below in C# so it works the same as the original Elixir code. | defmodule Ordered do
def partition([]), do: [[]]
def partition(mask) do
sum = Enum.sum(mask)
if sum == 0 do
[Enum.map(mask, fn _ -> [] end)]
else
Enum.to_list(1..sum)
|> permute
|> Enum.reduce([], fn perm,acc ->
{_, part} = Enum.reduce(mask, {perm,[]}, fn num,{pm,a} -... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Please provide an equivalent version of this Elixir code in C++. | defmodule Ordered do
def partition([]), do: [[]]
def partition(mask) do
sum = Enum.sum(mask)
if sum == 0 do
[Enum.map(mask, fn _ -> [] end)]
else
Enum.to_list(1..sum)
|> permute
|> Enum.reduce([], fn perm,acc ->
{_, part} = Enum.reduce(mask, {perm,[]}, fn num,{pm,a} -... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Preserve the algorithm and functionality while converting the code from Elixir to Python. | defmodule Ordered do
def partition([]), do: [[]]
def partition(mask) do
sum = Enum.sum(mask)
if sum == 0 do
[Enum.map(mask, fn _ -> [] end)]
else
Enum.to_list(1..sum)
|> permute
|> Enum.reduce([], fn perm,acc ->
{_, part} = Enum.reduce(mask, {perm,[]}, fn num,{pm,a} -... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Write the same code in Go as shown below in Elixir. | defmodule Ordered do
def partition([]), do: [[]]
def partition(mask) do
sum = Enum.sum(mask)
if sum == 0 do
[Enum.map(mask, fn _ -> [] end)]
else
Enum.to_list(1..sum)
|> permute
|> Enum.reduce([], fn perm,acc ->
{_, part} = Enum.reduce(mask, {perm,[]}, fn num,{pm,a} -... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Port the following code from Groovy to C with equivalent syntax and logic. | def partitions = { int... sizes ->
int n = (sizes as List).sum()
def perms = n == 0 ? [[]] : (1..n).permutations()
Set parts = perms.collect { p -> sizes.collect { s -> (0..<s).collect { p.pop() } as Set } }
parts.sort{ a, b ->
if (!a) return 0
def comp = [a,b].transpose().find { aa, bb -> a... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Translate the given Groovy code snippet into C# without altering its behavior. | def partitions = { int... sizes ->
int n = (sizes as List).sum()
def perms = n == 0 ? [[]] : (1..n).permutations()
Set parts = perms.collect { p -> sizes.collect { s -> (0..<s).collect { p.pop() } as Set } }
parts.sort{ a, b ->
if (!a) return 0
def comp = [a,b].transpose().find { aa, bb -> a... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Write the same code in C++ as shown below in Groovy. | def partitions = { int... sizes ->
int n = (sizes as List).sum()
def perms = n == 0 ? [[]] : (1..n).permutations()
Set parts = perms.collect { p -> sizes.collect { s -> (0..<s).collect { p.pop() } as Set } }
parts.sort{ a, b ->
if (!a) return 0
def comp = [a,b].transpose().find { aa, bb -> a... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Port the provided Groovy code into Python while preserving the original functionality. | def partitions = { int... sizes ->
int n = (sizes as List).sum()
def perms = n == 0 ? [[]] : (1..n).permutations()
Set parts = perms.collect { p -> sizes.collect { s -> (0..<s).collect { p.pop() } as Set } }
parts.sort{ a, b ->
if (!a) return 0
def comp = [a,b].transpose().find { aa, bb -> a... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Translate the given Groovy code snippet into Go without altering its behavior. | def partitions = { int... sizes ->
int n = (sizes as List).sum()
def perms = n == 0 ? [[]] : (1..n).permutations()
Set parts = perms.collect { p -> sizes.collect { s -> (0..<s).collect { p.pop() } as Set } }
parts.sort{ a, b ->
if (!a) return 0
def comp = [a,b].transpose().find { aa, bb -> a... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Change the following Haskell code into C without altering its purpose. | import Data.List ((\\))
comb :: Int -> [a] -> [[a]]
comb 0 _ = [[]]
comb _ [] = []
comb k (x:xs) = map (x:) (comb (k-1) xs) ++ comb k xs
partitions :: [Int] -> [[[Int]]]
partitions xs = p [1..sum xs] xs
where p _ [] = [[]]
p xs (k:ks) = [ cs:rs | cs <- comb k xs, rs <- p (xs \\ cs) ks ]
m... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Can you help me rewrite this code in C++ instead of Haskell, keeping it the same logically? | import Data.List ((\\))
comb :: Int -> [a] -> [[a]]
comb 0 _ = [[]]
comb _ [] = []
comb k (x:xs) = map (x:) (comb (k-1) xs) ++ comb k xs
partitions :: [Int] -> [[[Int]]]
partitions xs = p [1..sum xs] xs
where p _ [] = [[]]
p xs (k:ks) = [ cs:rs | cs <- comb k xs, rs <- p (xs \\ cs) ks ]
m... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Produce a functionally identical Python code for the snippet given in Haskell. | import Data.List ((\\))
comb :: Int -> [a] -> [[a]]
comb 0 _ = [[]]
comb _ [] = []
comb k (x:xs) = map (x:) (comb (k-1) xs) ++ comb k xs
partitions :: [Int] -> [[[Int]]]
partitions xs = p [1..sum xs] xs
where p _ [] = [[]]
p xs (k:ks) = [ cs:rs | cs <- comb k xs, rs <- p (xs \\ cs) ks ]
m... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Port the following code from Haskell to Go with equivalent syntax and logic. | import Data.List ((\\))
comb :: Int -> [a] -> [[a]]
comb 0 _ = [[]]
comb _ [] = []
comb k (x:xs) = map (x:) (comb (k-1) xs) ++ comb k xs
partitions :: [Int] -> [[[Int]]]
partitions xs = p [1..sum xs] xs
where p _ [] = [[]]
p xs (k:ks) = [ cs:rs | cs <- comb k xs, rs <- p (xs \\ cs) ks ]
m... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Translate the given J code snippet into C without altering its behavior. | require'stats'
partitions=: ([,] {L:0 (i.@#@, -. [)&;)/"1@>@,@{@({@comb&.> +/\.)
| #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Write a version of this J function in C# with identical behavior. | require'stats'
partitions=: ([,] {L:0 (i.@#@, -. [)&;)/"1@>@,@{@({@comb&.> +/\.)
| using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Generate a C++ translation of this J snippet without changing its computational steps. | require'stats'
partitions=: ([,] {L:0 (i.@#@, -. [)&;)/"1@>@,@{@({@comb&.> +/\.)
| #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Produce a language-to-language conversion: from J to Python, same semantics. | require'stats'
partitions=: ([,] {L:0 (i.@#@, -. [)&;)/"1@>@,@{@({@comb&.> +/\.)
| from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Convert this J block to Go, preserving its control flow and logic. | require'stats'
partitions=: ([,] {L:0 (i.@#@, -. [)&;)/"1@>@,@{@({@comb&.> +/\.)
| package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Port the following code from Julia to C with equivalent syntax and logic. | using Combinatorics
function masked(mask, lis)
combos = []
idx = 1
for step in mask
if(step < 1)
push!(combos, Array{Int,1}[])
else
push!(combos, sort(lis[idx:idx+step-1]))
idx += step
end
end
Array{Array{Int, 1}, 1}(combos)
end
function ... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Port the provided Julia code into C# while preserving the original functionality. | using Combinatorics
function masked(mask, lis)
combos = []
idx = 1
for step in mask
if(step < 1)
push!(combos, Array{Int,1}[])
else
push!(combos, sort(lis[idx:idx+step-1]))
idx += step
end
end
Array{Array{Int, 1}, 1}(combos)
end
function ... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Can you help me rewrite this code in C++ instead of Julia, keeping it the same logically? | using Combinatorics
function masked(mask, lis)
combos = []
idx = 1
for step in mask
if(step < 1)
push!(combos, Array{Int,1}[])
else
push!(combos, sort(lis[idx:idx+step-1]))
idx += step
end
end
Array{Array{Int, 1}, 1}(combos)
end
function ... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Transform the following Julia implementation into Python, maintaining the same output and logic. | using Combinatorics
function masked(mask, lis)
combos = []
idx = 1
for step in mask
if(step < 1)
push!(combos, Array{Int,1}[])
else
push!(combos, sort(lis[idx:idx+step-1]))
idx += step
end
end
Array{Array{Int, 1}, 1}(combos)
end
function ... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Port the following code from Julia to Go with equivalent syntax and logic. | using Combinatorics
function masked(mask, lis)
combos = []
idx = 1
for step in mask
if(step < 1)
push!(combos, Array{Int,1}[])
else
push!(combos, sort(lis[idx:idx+step-1]))
idx += step
end
end
Array{Array{Int, 1}, 1}(combos)
end
function ... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Port the following code from Lua to C with equivalent syntax and logic. |
local function range(n)
local res = {}
for i=1,n do
res[i] = i
end
return res
end
local function isin(t, x)
for _,x_t in ipairs(t) do
if x_t == x then return true end
end
return false
end
local function slice(t, u, o)
local res = {}
for i=u,o do
res[#res+1] = t[i]
end
return res
e... | #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Write a version of this Lua function in C# with identical behavior. |
local function range(n)
local res = {}
for i=1,n do
res[i] = i
end
return res
end
local function isin(t, x)
for _,x_t in ipairs(t) do
if x_t == x then return true end
end
return false
end
local function slice(t, u, o)
local res = {}
for i=u,o do
res[#res+1] = t[i]
end
return res
e... | using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Write the same algorithm in C++ as shown in this Lua implementation. |
local function range(n)
local res = {}
for i=1,n do
res[i] = i
end
return res
end
local function isin(t, x)
for _,x_t in ipairs(t) do
if x_t == x then return true end
end
return false
end
local function slice(t, u, o)
local res = {}
for i=u,o do
res[#res+1] = t[i]
end
return res
e... | #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Maintain the same structure and functionality when rewriting this code in Python. |
local function range(n)
local res = {}
for i=1,n do
res[i] = i
end
return res
end
local function isin(t, x)
for _,x_t in ipairs(t) do
if x_t == x then return true end
end
return false
end
local function slice(t, u, o)
local res = {}
for i=u,o do
res[#res+1] = t[i]
end
return res
e... | from itertools import combinations
def partitions(*args):
def p(s, *args):
if not args: return [[]]
res = []
for c in combinations(s, args[0]):
s0 = [x for x in s if x not in c]
for r in p(s0, *args[1:]):
res.append([c] + r)
return res
s =... |
Generate an equivalent Go version of this Lua code. |
local function range(n)
local res = {}
for i=1,n do
res[i] = i
end
return res
end
local function isin(t, x)
for _,x_t in ipairs(t) do
if x_t == x then return true end
end
return false
end
local function slice(t, u, o)
local res = {}
for i=u,o do
res[#res+1] = t[i]
end
return res
e... | package main
import (
"fmt"
"os"
"strconv"
)
func gen_part(n, res []int, pos int) {
if pos == len(res) {
x := make([][]int, len(n))
for i, c := range res {
x[c] = append(x[c], i+1)
}
fmt.Println(x)
return
}
for i := range n {
if n[i] == 0 {
continue
}
n[i], res[pos] = n[i]-1, i
gen_par... |
Convert this Mathematica snippet to C and keep its semantics consistent. | w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list];
While[n>0,s=Join[s,{Take[t,(k=First[list])]}];t=Drop[t,k];list=Rest[list];n--]; s]
m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union
| #include <stdio.h>
int next_perm(int size, int * nums)
{
int *l, *k, tmp;
for (k = nums + size - 2; k >= nums && k[0] >= k[1]; k--) {};
if (k < nums) return 0;
for (l = nums + size - 1; *l <= *k; l--) {};
tmp = *k; *k = *l; *l = tmp;
for (l = nums + size - 1, k++; k <... |
Port the provided Mathematica code into C# while preserving the original functionality. | w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list];
While[n>0,s=Join[s,{Take[t,(k=First[list])]}];t=Drop[t,k];list=Rest[list];n--]; s]
m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union
| using System;
using System.Linq;
using System.Collections.Generic;
public static class OrderedPartitions
{
public static void Main() {
var input = new [] { new[] { 0, 0, 0, 0, 0 }, new[] { 2, 0, 2 }, new[] { 1, 1, 1 } };
foreach (int[] sizes in input) {
foreach (var partition in Partiti... |
Change the following Mathematica code into C++ without altering its purpose. | w[partitions_]:=Module[{s={},t=Total@partitions,list=partitions,k}, n=Length[list];
While[n>0,s=Join[s,{Take[t,(k=First[list])]}];t=Drop[t,k];list=Rest[list];n--]; s]
m[p_]:=(Sort/@#)&/@(w[#,p]&/@Permutations[Range@Total[p]])//Union
| #include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
void partitions(std::vector<size_t> args) {
size_t sum = std::accumulate(std::begin(args), std::end(args), 0);
std::vector<size_t> nums(sum);
std::iota(std::begin(nums), std::end(nums), 1);
do {
size_t total_index = 0;
std::v... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.