Array of array of string - c++

I am doing one task and I need to find shortest path on graph.
There are no problems at all with algorithm. The problem is how to output paths from source to all other vertexes. Edges has names too, not only weights. How should I initialize matrix of strings correctly? Is it possible at all?
I want this code work:
printf(" -(%s)-> %d", names[prev][next], mas[j]);
mas[j] is array with vertexes
names - array of arrays of string
I am trying to initialize matrix this way:
string names[N][N] = {
{'0', 'A', '0', 'B', 'E', '0', '0', 'P1', '0'},
{'A', '0', 'D', 'I', '0', '0', '0', '0', '0'},
{'0', 'D', '0', '0', '0', 'H', 'F', '0', '0'},
{'B', 'I', '0', '0', '0', 'H', '0', '0', '0'},
{'E', '0', '0', '0', '0', '0', '0', 'P2', '0'},
{'0', '0', 'H', 'H', '0', '0', '0', '0', 'P4'},
{'0', '0', 'F', '0', '0', '0', '0', '0', 'P3'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0'},
{'0', '0', '0', '0', '0', '0', '0', '0', '0'},
};
My programm: http://ideone.com/ZMiVPE

You're using the wrong quotes for string literals. Single quotes ' are used for character literals, and in fact 'P1' is a multi-character literal (which I'm certain you don't even want to mess with). Instead, use double quotes " around your literals.
The reason they all need to be string literals is because there is no std::string constructor that takes only a char. There is, however, a constructor that takes a C-style string (as created by a string literal).
Note that printf will expect a C-style string, so you'll need to do names[prev][next].c_str().

Related

Group repeating list objects with itertools.groupby()

I have a list, where I want to group the repeating objects into a single object in the new list. Basically, convert this:
s = ['0.352125', '1', '1', '1', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0.241041', '0.313429', '1', '1']
to this:
s_new = ['0.352125', '4*1','8*0', '0.241041', '0.313429','2*1']
I have tried itertools.groupby() function (python 2.7) as below:
from itertools import groupby
s_g = [list(g) for k, g in groupby(s)]
s_new = [', '.join('{}*{}'.format(sum(1 for _ in g), k) for k, g in groupby(s_g))]
As a result, I get:
s_new = ["1*['0.352125'], 1*['1', '1', '1', '1'], 1*['0', '0', '0', '0', '0', '0', '0', '0'], 1*['0.241041'], 1*['0.313429'], 1*['1', '1']"]
Apparently, this is not the list format I'm trying to get. Could someone please help me with this?
You unnecessarily applied groupby twice and there's also no reason to use str.join.
You can use the following list comprehension instead:
['%s*' % len(l) * (len(l) > 1) + k for k, g in groupby(s) for l in (list(g),)]

How to find unique values in python list

I have following list coming from file. what i want one of list of this file which containts many values duplicate one also
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['S', 'F', '3', '\n']
['7', '3', '4', '\n']
['7', '3', '4', '\n']
['7', '3', '4', '\n']
['7', '3', '4', '\n']
How to filter out only unique values.
import os
os.chdir('C:\\')
if os.path.exists('xxx.dat'):
airport = open('routes.dat')
for each_line in airport:
(Airlinecode,AirlineID,Source_airport_code,Source_airport_ID,Destination_airport,Destination_airport_ID,Codeshare,Stops,Equipment) = each_line.split("\t")
newlist =[]
newlist = Equipment
i = iter(newlist)
elements = []
for eachitem in range(len(newlist)):
elements.append(i.next())
print (elements)
airport.close()
else:
print('file does not exists')
For something like this where you are generating list of lists as output, you cannot simply put the items in a set (which does not allow duplicates) as lists are unhashable. What you could do is when generating the list, do something like:
seen = set()
key = "".join(newlist)
if key not in seen:
seen.add(key)
output.append(newlist)
If for some reason you cannot do this during the first iteration through the text file and only have access to the output array values, then you could loop through your output doing a similar technique and simply keep the unique values.

Decrypt .ini, and use values?

I have a XOR function:
string encryptDecrypt(string toEncrypt) {
char key[64] = { 'F', '2', 'D', 'C', '5', '4', '0', 'D', 'B', 'F', '3', 'E', '1', '2', '9', 'F', '4', 'E', 'A', 'A', 'F', '7', '6', '7', '5', '6', '9', 'E', '3', 'C', 'F', '9', '7', '5', '2', 'B', '4', 'B', '8', '2', '6', 'D', '9', '8', 'F', 'D', '8', '3', '8', '4', '6', '0', '8', '5', 'C', '0', '3', '7', 'D', '3', '5', 'F', '7', '5' };
string output = toEncrypt;
for (int i = 0; i < toEncrypt.size(); i++)
output[i] = toEncrypt[i] ^ key[i % (sizeof(key) / sizeof(char))];
return output;
}
I encrypted my .ini :
[test]
baubau = 1
haha = 2
sometext = blabla
How i tried to decrypt and use values:
std::string Filename = "abc.ini";
std::ifstream input(Filename, std::ios::binary | ios::in); // Open the file
std::string line; // Temp variable
std::vector<std::string> lines; // Vector for holding all lines in the file
while (std::getline(input, line)) // Read lines as long as the file is
{
lines.push_back(encryptDecrypt(line));
}
// Here i should call the ini reader? but how?
CIniReader iniReader("abc.ini");
string my = encryptDecrypt(iniReader.ReadString("test", "baubau", ""));
for (auto s : lines)
{
cout << my;
cout << endl;
}
Where is my mistake? Some help would be apreciated, Many Thanks!
What you can do is:
Read the file line by line, and break apart keys and values, i.e. where you see 'key=value' break it apart into key and value.
Encrypt the value.
Base64-encode the value, in case it is no longer valid text in the encoding of the file.
Replace the line with 'key=base64-encoded-value'.
Later, when you read the encoded value for the key, which is just a simple Base64-encoded string of bytes, Base64-decode the string, and the decrypt the value.
For example, this line:
baubau = 1
Take the value '1' as a string, and encrypt it with your function. The result in this case is a printable string 'w'. However, I would treat it as arbitrary bytes.
Base64-encode the "encrypted" value. For example, the Base64-encoding of 'w' in UTF-8 (or ASCII) is "dw==".
Replace the line with:
baubau = dw==
or, if you like:
baubau = "dw=="
Later, when you read the value for baubau, you simply Base64-decode 'dw==', obtaining 'w', and then decrypt 'w' to arrive at '1'.

Is this valid C++ to initialize a 2D dynamic array?

This is in my teachers code, and it works for me using the GNU compiler, and for my teacher who is on a Mac, but for other classmates using Visual Studio, it throws a lot of errors. I'm thinking that with dynamic memory, you can't initialize something like this, or rather the C++ standard doesn't say you have to be able to. Am I correct in that?
store = new char*[rows];
store[0] = new char[6]{'1', '1', '1', '1', '1', '1'};
store[1] = new char[6]{'1', 'e', '1', '0', '0', '1'};
store[2] = new char[6]{'1', '0', '0', '0', '1', '1'};
store[3] = new char[6]{'1', '0', '0', 'm', '1', '1'};
store[4] = new char[6]{'1', '1', '1', '1', '1', '1'};
It is a valid list initialization. However for example MS VC++ 2010 does not support it,
Not all compilers support all features of the C++ 2011.

Creating Lists from CSV File in python 2.7 using two different read line commands

I am a newbie at Python 2.7 and used the following code to create the list of observations pulled from a csv file.
import csv
data = []
with open(datafile,'rb') as f:
for row in f:
g=row
data.append(g)
It produces the following list (after reading the first row):
['01/01/2005,01:00,0,0,0,2,0,0,2,0,0,2,0,0,2,0,0,2,0,0,2,0,0,2,0,3,E,9,3,E,9,8.0,A,7,6.0,A,7,87,A,7,1013,A,7,150,A,7,2.1,A,7,16100,A,7,77777,A,7,1.1,E,8,0.099,F,8,0.160,F,8,0,1,A,7']
But when I use the following code to read the csv file and create the list:
data = []
with open(datafile,'rb') as f:
r = csv.reader(f)
for row in f
data = [row for row in r]
I get a list that looks as follows (after reading the first row):
['01/01/2005', '01:00', '0', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '3', 'E', '9', '3', 'E', '9', '8.0', 'A', '7', '6.0', 'A', '7', '87', 'A', '7', '1013', 'A', '7', '150', 'A', '7', '2.1', 'A', '7', '16100', 'A', '7', '77777', 'A', '7', '1.1', 'E', '8', '0.099', 'F', '8', '0.160', 'F', '8', '0', '1', 'A', '7']
This also appears to be a list (with the key difference being the apostrophes around each item in the list instead of just at the beginning and the end).
Since both appear to be lists, why does the code that uses data.append not deliver a similar list?
Your first list has just one single element -- a string holding the entire line, inclusing all the commas.
>>> l1 =['01/01/2005,01:00,0,0,0,2,0,0,2,0,0,2,0,0,2,0,0,2,0,0,2,0,0,2,0,3,E,9,3,E,9,8.0,A,7,6.0,A,7,87,A,7,1013,A,7,150,A,7,2.1,A,7,16100,A,7,77777,A,7,1.1,E,8,0.099,F,8,0.160,F,8,0,1,A,7']
>>> len(l1)
1
In your second example, csv.reader splits this line into several elements and puts those into the list.
>>> l2 = ['01/01/2005', '01:00', '0', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '0', '2', '0', '3', 'E', '9', '3', 'E', '9', '8.0', 'A', '7', '6.0', 'A', '7', '87', 'A', '7', '1013', 'A', '7', '150', 'A', '7', '2.1', 'A', '7', '16100', 'A', '7', '77777', 'A', '7', '1.1', 'E', '8', '0.099', 'F', '8', '0.160', 'F', '8', '0', '1', 'A', '7']
>>> len(l2)
68
Also note that the loop in your second example is somewhat odd. I assume that this is not your actual code, since there's a syntax error in it. It should probably rather be:
r = csv.reader(f)
for row in r: # row in r, not row in f
data.append(row) # append row to data -> 2d-array of items in rows