Click here to Skip to main content
15,887,998 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've been trying to change this code into C++. The python version works but the C++ doesn't.

The question is:
Given a sequence consisting of 'I' and 'D' where 'I' denotes increasing sequence and 'D' denotes the descreasing sequence. Write a program that decodes the given sequence to construct minimum number without repeated digits.

Sample Input 1:

IDIDII

Sample Output 1:

1325467



Sample Input 2:

DDDD

Sample Output 2:

54321

This is my Python code:
Python
s = input()
ans = [1]
count = 0
for i in s:
    if i == 'I':
        count = 0
        k = len(ans)
        ans.append(k + 1)
    else:
        count += 1
        tmp = ans[-1]
        for i in range(-1, -1 - count, -1):
            ans[i] += 1
        ans.append(tmp)
for i in ans:
    print(i, end = "")


What I have tried:

This is what I've written but it doesn't give the correct output.
C++
#include <bits/stdc++.h>

using namespace std;

vector<int> digits(string s){
    vector<int> ans = {1};
    int count = 0;
    for (char const &c : s){
        if (c == 'I'){
            count = 0;
            int k = ans.size();
            ans.push_back(k + 1);
        }
        else{
            count ++;
            int tmp = ans.back();
            for (int i = ans.size() - 1; i > ans.size() - 1 - count; i--){
                ans[i] += 1;
            }
            ans.push_back(tmp);
        }
    }
   return ans; 
}

int main(){
    string s;
    cin >> s;
    vector<int> ans = digits(s);
    for (int i = 0; i < ans.size(); i++){
        cout << ans[i];
    }
    return 0;
}
Posted
Updated 15-Jul-20 1:07am
v2
Comments
Richard MacCutchan 14-Jul-20 12:44pm    
"but it doesn't work properly"
Which means what?
[no name] 15-Jul-20 7:03am    
It doesn't give the expected output
Richard MacCutchan 15-Jul-20 7:18am    
And we are supposed to guess what that is?

Quote:
I've been trying to change this code into C++. The python version works but the C++ doesn't.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Best is to write some simple test code and use the debugger to find the issues.

Your code looks good, so maybe it is only one bug. May the for loop in the else.
 
Share this answer
 
I've found the problem, ans.size() should be replaced with int(ans.size()) as it is an unsigned integer and this would cause integer underflow.
 
Share this answer
 
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

And finding random code in one language and trying to make that work as your homework answer isn't going to be a success anyway: translation never produces good target code.
Instead, use the Python code as a specification for a new C++ app, or better write your own - you will learn a lot more that way than copying someone else's work and handing it in as your own ...
 
Share this answer
 
Comments
[no name] 15-Jul-20 7:01am    
I had already posted my code and I was looking for some help to find errors, not just getting a translated version

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900