We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I didn't know about raw_input in Python 3. Thanks for the heads up. Testing locally, list(input()) seems to do the same thing as list(str(input())).
So, I want to take a look at this:
5: for n in range(0, totallist[0]):
6: line = list(str(input()))
7: guyslist += [line]
8: guyslist = [[int(y) for y in x] for x in guyslist]
Line 8 shouldn't be inside your loop at all. It's looping through your 2d list, and converting everything to ints. You only need to do that after the loop, not during every iteration. E.g.:
>>>print(guyslist)
[['0', '1'], ['1', '0'], ['1', '1']]
>>>guyslist = [[int(y) for y in x] for x in guyslist]
>>>print(guyslist)
[[0, 1], [1, 0], [1, 1]]
So, you could compact that block down to:
5: for n in range(totallist[0]):
6: guyslist += [list(input())]
7: guyslist = [[int(y) for y in x] for x in guyslist]
Finally, as srajkovic notes, string compares are faster than str to int conversion plus int compares. So, you can drop the guyslist = [[int(y) for y in x] for x in guyslist] line entirely and just make if (guyslist[i][q]) or (guyslist[j][q]): use string comparisions.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
ACM ICPC Team
You are viewing a single comment's thread. Return to all comments →
I didn't know about raw_input in Python 3. Thanks for the heads up. Testing locally,
list(input())
seems to do the same thing aslist(str(input()))
.So, I want to take a look at this:
Line 8 shouldn't be inside your loop at all. It's looping through your 2d list, and converting everything to ints. You only need to do that after the loop, not during every iteration. E.g.:
So, you could compact that block down to:
Finally, as srajkovic notes, string compares are faster than str to int conversion plus int compares. So, you can drop the
guyslist = [[int(y) for y in x] for x in guyslist]
line entirely and just makeif (guyslist[i][q]) or (guyslist[j][q]):
use string comparisions.