hw2 ignoring the whitespaces and tabs

G
Gülümser Aslı Kütük (310 points)
2 4 6
asked Nov 5, 2021 in HW1 optional by Gülümser Aslı Kütük (310 points)
hey there. Can I ignore taking whitespaces and tabs into the account when sorting according to unicode by saying:

ord(' ')==0?
185 views

4 Answers

GabrielAlexandru (7760 points)
2 4 29
answered Nov 5, 2021 by GabrielAlexandru (7,760 points)
ord(' ') will never be equals to 0 as whitespace is returned as 32. You can convert it in ord(' ') == 32 for whitespace (other number for tabs, but I don't remember which is).
Exyss (21390 points)
1 2 79
commented Nov 5, 2021 by Exyss (21,390 points)
ord("\t") should be equal to 9
G
Gülümser Aslı Kütük (310 points)
2 4 6
commented Nov 5, 2021 by Gülümser Aslı Kütük (310 points)
Is there a way for me to ignore tabs and whitespaces without defining another function tho?
Excale (490 points)
0 0 6
commented Nov 6, 2021 by Excale (490 points)

Python has a function that lets you replace a substring from a string in something else, you could use that to make your job easier (even better, there's a function that replaces characters only, it should be quicker). Be aware of how many times you use that though, if you use it too many times it will bring up execution time.

If you want to continue with your idea, why don't just compare the characters before applying ord? So you can verify that the character is space/tab doing char = " " or char = "\t", or just compare the ord like ord(char) = ord(" ") or ord(char) = ord("\t").

One last thing, if you need to sort the strings you have to use the "no-whitespace" strings, unless you make a function that sorts while ignoring whitespaces.

G
Gülümser Aslı Kütük (310 points)
2 4 6
commented Nov 6, 2021 by Gülümser Aslı Kütük (310 points)
thank you so much!
gianluca5539 (9820 points)
3 6 44
answered Nov 5, 2021 by gianluca5539 (9,820 points)
You can remove all spaces and tabs with a method. Think of replacing them with nothing.....
G
GuidoG93 (970 points)
4 9 13
answered Nov 5, 2021 by GuidoG93 (970 points)
I don't think it would work, try looking for other functions like replace or strip...
G
Gülümser Aslı Kütük (310 points)
2 4 6
commented Nov 6, 2021 by Gülümser Aslı Kütük (310 points)
ı'll try to do that thanks!
Luigi Pizza (6120 points)
11 20 65
answered Nov 6, 2021 by Luigi Pizza (6,120 points)
you can use also the filter function: As the name suggestes it takes away all elements from a list who don't respect a certain criteria, (you can also use a lambda function inside it).