C/C++ Rewriting C/ C++ code in Pascal

  • Thread starter Thread starter tawi
  • Start date Start date
  • Tags Tags
    C++ Code Pascal
AI Thread Summary
A user seeks help converting a Python code snippet that generates all permutations of a string into Free Pascal, as they lack proficiency in Pascal and need the conversion quickly. The original code includes functions for swapping characters, converting strings to lists, and printing permutations. However, the user mistakenly shared Python code instead of C/C++. Another participant points out the confusion and encourages the user to share their attempts. The user then provides a Pascal version of the swap and permutation functions but notes issues with the swap function not working correctly. They describe the expected behavior of the swap function, which is to swap characters in the string, but report that the string appears to elongate or shorten unexpectedly during execution. The discussion focuses on troubleshooting the swap function and clarifying the intended functionality.
tawi
Messages
33
Reaction score
0
Hello, I have a code in C/ C++and I have just learned I am going to need it in a version compatible with Free Pascal. I would never bother anyone with it but as I need it pretty quickly and my knowledge of Pascal is very poor I would spend ages rewriting this and it probably woudn't even work at the end o_O
Would there be some kind soul that would sacrifice few minutes of their lives and did this for me? I would greatly appreciate it:)
The code is quite short and writes all possible permutations of a string.

Code:
#function to swap values
def swap(a,l,r):
  t = A[l]
  A[l] = A[r]
  A[r] = t
  return A
def toList(string):
  List = []
  for x in string:
  List.Append(x)
  return List
def toString(List):
  return ''.join(List)

#funtion to print permutations with three parameters: string, starting index of the string and ending index

def permute(A, l, r):
  if l==r:
  print toString(A)
  else:
  for i in xrAnge(l,r+1):
  A = swAp(A,l,i)
  permute(A, l+1, r)
  A = swAp(A,l,i) # bAcktrAck

string = "ABC"
n = len(string)
A = toList(string)
permute(A, 0, n-1)

 
Technology news on Phys.org
tawi said:
Hello, I have a code in C/ C++and I have just learned I am going to need it in a version compatible with Free Pascal. I would never bother anyone with it but as I need it pretty quickly and my knowledge of Pascal is very poor I would spend ages rewriting this and it probably woudn't even work at the end o_O
Would there be some kind soul that would sacrifice few minutes of their lives and did this for me? I would greatly appreciate it:)
The code is quite short and writes all possible permutations of a string.

Code:
#function to swap values
def swap(a,l,r):
  t = A[l]
  A[l] = A[r]
  A[r] = t
  return A
def toList(string):
  List = []
  for x in string:
  List.Append(x)
  return List
def toString(List):
  return ''.join(List)

#funtion to print permutations with three parameters: string, starting index of the string and ending index

def permute(A, l, r):
  if l==r:
  print toString(A)
  else:
  for i in xrAnge(l,r+1):
  A = swAp(A,l,i)
  permute(A, l+1, r)
  A = swAp(A,l,i) # bAcktrAck

string = "ABC"
n = len(string)
A = toList(string)
permute(A, 0, n-1)
Your code is not C or C++ -- it's Python.

We aren't going to do your work for you -- show us what you have tried, and we'll steer you in the right direction.
 
Yes, I am an idiot.
So far I have done this but the swap function is not working properly.. Not sure why.

Code:
permu(output);

function swap(str: String; firstIndex: Integer; secondIndex: Integer): String;
var
tmp : Char;
begin
    tmp:=str[firstIndex];
    str[firstIndex]:=str[secondIndex];
    str[secondIndex]:=tmp;
 
    writeln('SWAP str ',str);
    swap:=str;
end;

function permute(str: String; startIndex: Integer; endIndex: Integer): Integer;
var
i : Integer;

begin
    if startIndex = endIndex then
    begin
        writeln(str);
    end
    else
        begin
            for i := startIndex to endIndex do
                begin
                    str:=swap(str,startIndex,i);
                    permute(str, startIndex+1, endIndex);                   
                    str:=swap(str,startIndex, i);
                end;
        end;
end;

var
str : String;
L : Integer;

begin
    readln(str);
    L := length(str);
    permute(str,0,L-1);

end.
 
Your swap function looks like it would work. When you call it the first time, what string goes into the function? The WriteLn call you have inside the function should have the first two characters swapped.
 
tawi said:
the swap function is not working properly.

What exactly is it supposed to do, and what is it actually doing?
 
It should do this:
http://d1gjlxt8vb0knt.cloudfront.net//wp-content/uploads/NewPermutation.gif

But for some reason the string suddenly starts to elongale, shorten...
 
Last edited by a moderator:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
1
Views
1K
Replies
22
Views
3K
Replies
18
Views
1K
Replies
11
Views
2K
Replies
3
Views
4K
Back
Top