Rewriting C/ C++ code in Pascal

  • Context: C/C++ 
  • Thread starter Thread starter tawi
  • Start date Start date
  • Tags Tags
    C++ Code Pascal
Click For Summary

Discussion Overview

The discussion revolves around converting a code snippet intended for generating permutations of a string from C/C++ to Free Pascal. Participants are exploring the challenges faced in rewriting the code and troubleshooting issues with the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant requests help to convert a Python code snippet into Free Pascal, expressing urgency and a lack of familiarity with Pascal.
  • Another participant points out that the provided code is actually Python, not C/C++, and encourages the original poster to show their attempts instead of asking for a complete solution.
  • A participant shares their attempt at rewriting the code in Pascal, including a swap function and a permutation function, but notes that the swap function is not working as expected.
  • Concerns are raised about the functionality of the swap function, with questions about what string is being passed to it and what the expected behavior is.
  • Another participant asks for clarification on the intended functionality of the swap function and what issues are being encountered, specifically mentioning unexpected changes in string length.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the effectiveness of the swap function or the overall implementation, as there are ongoing questions and troubleshooting efforts regarding the code's behavior.

Contextual Notes

There are indications of unresolved issues with the swap function, including potential problems with string manipulation in Pascal that may not be fully addressed in the discussion.

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:

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K