1

I am new to python, and I am trying to convert some python code to C++.

path is a string, WORDS is a list of strings.

python snippit:

suggestions = filter(lambda x: x[0] == path[0] and x[-1] == path[-1], WORDS)

Because I do not know python at all, I have trouble understanding the notion of 0 and -1. Do they represent indexes of these arrays, like i and i-1 would in C++?

Here is my attempt to convert:

  vector<string>suggestions; 
int len = path.length();

  for (string &x : dictionary) //for each word in some dictionary
      for (int i = 0; i < len; i++)
        {
          if (path[i] == x[i] && path[i-1] ==x[i-1])
            suggestions.push_back(x);
            break;
        }

Even though I think it should be right, this code isn't doing what I want it to do. The filtered list from the python code is much shorter than the vector from the C++ code.

SSOPLIF
  • 311
  • 1
  • 4
  • 15
  • The lambda function is filtering for the words that begin and end with the same letter as `path` – John La Rooy Apr 16 '15 at 05:24
  • Possible duplicate of [Convert Python program to C/C++ code?](https://stackoverflow.com/q/4650243/608639) – jww Nov 21 '19 at 13:38

3 Answers3

3

A string is a sequence in python, so the indicies you're asking about are referring to specific characters. Index 0 means the same thing in Python as C++. Negative indexes in python count backwards from the end of the sequence, so Index -1 is the last character in the string.

vector<string> suggestions; 
int len = path.length();

for (string &x : dictionary) //for each word in some dictionary
  if(path[0] == x.front() && path[len-1] == x.back())
    suggestions.push_back(x);

The lambda creates a function that takes a string and checks the first and last characters. That function is then passed into filter, which calls it on each element of WORDS. filter returns an iterable of all the words for which the lambda returned true.

jack
  • 2,094
  • 1
  • 19
  • 17
  • yes, len-1. beat me to it! Thank you! I was doing x[x.length() - 1] instead of x.back(), although your solution looks much cleaner – SSOPLIF Apr 16 '15 at 05:30
  • Fixed, thank you. You might also be able to use `front` and `back` on path, depending on the type of `path`. That would make your code even cleaner, and you wouldn't need a variable for length. – jack Apr 16 '15 at 05:31
2

I have trouble understanding the notion of 0 and -1.

See [Negative index to Python list] and [Explain Python's slice notation].

Negative numbers mean that you count from the right instead of the left. So, list[-1] refers to the last element, list[-2] is the second-last, and so on.

Community
  • 1
  • 1
James Adkison
  • 9,412
  • 2
  • 29
  • 43
1

Index 0 is the first element, and index -1 is the last element. If foo contains [4, 6, 8], foo[0] would be 4 and foo[-1] would be 8.

In the case of a string, they are the first and last character respectively. So, string::front() and string::back() in C++11, otherwise access with 0 and length-1 as normal.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358