Related
My name is Edwin i am new to programming but i love to learn. I have a assignment for school and i must build a program that rates passwords. but i have a litle problem now. As you can see i made 3 lists with every character. If i run this program it will not show "uw wachtwoord is Sterk" if the conditions klein and groot and nummers and symbols are true. how do i fix this?
btw i can't make use of isdigit,isnumeric and such.
thank you in advance!
print ("Check of uw wachtwoord veilig genoeg is in dit programma.")
print ("Uw wachtwoord moet tussen minimaal 6 en maximaal 12 karakters
bestaan")
print ("U kunt gebruik maken van hoofdletters,getallen en symbolen
(#,#,$,%)")
ww = input("Voer uw wachtwoord in: ")
klein = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm','n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
groot = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
nummers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
symbolen= [' ', '!', '#', '$', '%', '&', '"', '(', ')', '*', '+', ',', '-',
'.', '/', ':', ';', '<', '=', '>', '?', '#', '[', '\\', ']', '^', '_', '`',
'{', '|', '}', '~',"'"]
if len(ww) < 6:
print ("uw wachtwoord is te kort, uw wachtwoord moet uit minimaal 6 en
maximaal 12 karakters bestaan!")
elif len(ww) > 12:
print ("uw wachtwoord is te lang, uw wachtwoord moet uit minimaal 6 en
maximaal 12 karakters bestaan!")
elif len(ww) >= 6 and len(ww)<= 12:
if ww == klein and ww == groot and ww == nummers and ww == symbolen:
print ("uw wachtwoord is Sterk")
Your test cannot work because you're comparing your password (of type str: string) against a list. Since objects are non-comparable, the result is just False (even if they were comparable there is no equality here, but a non-empty intersection to check)
You need to check for each list that there's at least 1 member of the list in the password
Define an aux function which checks if a letter of the list is in the password (a lambda would be too much maybe) using any:
def ok(passwd,l):
return any(k in passwd for k in l)
Then test all your four lists against this condition using all:
elif len(ww) >= 6 and len(ww)<= 12:
sww = set(ww)
if all(ok(sww,l) for l in [klein,groot,nummers,symbolen]):
print ("uw wachtwoord is Sterk")
Note the slight optimization by converting the password (which is kind of a list so O(n) for in operator) by a set of characters (where the in operator exists but is way faster). Besides, a set will remove duplicate characters, which is perfect for this example.
More compact version without the aux function and using a lambda which is not so difficult to understand after all:
elif len(ww) >= 6 and len(ww)<= 12:
sww = set(ww)
if all(lambda l: any(k in sww for k in l) for l in [klein,groot,nummers,symbolen]):
print ("uw wachtwoord is Sterk")
I have a list that i need to write to a .csv Yes, i have done a LOT of looking around (of course i found this link which is close to the target, but misses my case) You see writerows is having all sorts of trouble with the delimiters/formatting in the .csv (the a gets separated from the 1 from the 7 etc etc)
My list looks like this:
buffer = [['a17', 'b17', 'c17', '8', 'e17', 'f17\n'], ['a24', 'b24', 'c24', '6', 'e24', 'f24\n'], ['a27', 'b27', 'c27', '9', 'e27', 'f27\n'], ['a18', 'b18', 'c18', '9', 'e18', 'f18\n'], ['a5', 'b5', 'c5', '5', 'e5', 'f5\n'], ['a20', 'b20', 'c20', '2', 'e20', 'f20\n'], ['a10', 'b10', 'c10', '1', 'e10', 'f10\n'], ['a3', 'b3', 'c3', '3', 'e3', 'f3\n'], ['a11', 'b11', 'c11', '2', 'e11', 'f11\n']]
I can see its like a list of lists so i tried for eachRow in buffer: then following on with a eachRow.split(',') but no good there either.
I just need to write to a .csv it should be easy right... what am i missing?
You can remove the \n string from your buffer like so. Also you have to add newline='' to the with statement in Python 3. See this answer for more detail.
import csv
buffer = [['a17', 'b17', 'c17', '8', 'e17', 'f17\n'],
['a24', 'b24', 'c24', '6', 'e24', 'f24\n'],
['a27', 'b27', 'c27', '9', 'e27', 'f27\n'],
['a18', 'b18', 'c18', '9', 'e18', 'f18\n'],
['a5', 'b5', 'c5', '5', 'e5', 'f5\n'],
['a20', 'b20', 'c20', '2', 'e20', 'f20\n'],
['a10', 'b10', 'c10', '1', 'e10', 'f10\n'],
['a3', 'b3', 'c3', '3', 'e3', 'f3\n'],
['a11', 'b11', 'c11', '2', 'e11', 'f11\n']]
for row_index, list in enumerate(buffer):
for column_index, string in enumerate(list):
buffer[row_index][column_index] = buffer[row_index][column_index].replace('\n', '')
with open('output.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(buffer)
import csv
with open('output.csv','w') as f:
writer = csv.writer(f)
writer.writerows(buffer)
Note that the last entry in each of your lists has a newline, so the csvwriter is correctly quoting the string so you end up with "f17\n" (in the first list as an example) which will look strangely formatted if you are not expecting a new line.
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'.
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
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().