Python IndexError: what's wrong?

Because none of your patterns ever match, the loop will keep incrementing the idx until it goes over the bounds of the tuple. The reason for no match, is that you have a small error in your last regex pattern. This: r'([\w\D]+) (\d{2}) , (\d{4})’) should be: r'([\w\D]+) (\d{2}), (\d{4})’) After fixing this I’m able … Read more

Python script to ping linux server

You can use urllib2 and socket module for the task. import socket from urllib2 import urlopen, URLError, HTTPError socket.setdefaulttimeout( 23 ) # timeout in seconds url=”http://google.com/” try : response = urlopen( url ) except HTTPError, e: print ‘The server couldn\’t fulfill the request. Reason:’, str(e.code) except URLError, e: print ‘We failed to reach a server. … Read more

Checking sum of items in a list if equals target value

Another way, assuming you can sort the list is the following original_l = [1,2,6,4,9,3] my_l = [ [index, item] for item,index in zip(original_l, range(0,len(original_l)))] my_l_sort = sorted(my_l, key=lambda x: x[1]) start_i = 0 end_i = len(my_l_sort)-1 result = [] target = 7 while start_i < end_i: if my_l_sort[start_i][1] + my_l_sort[end_i][1] == target: result.append([my_l_sort[start_i][0], my_l_sort[end_i][0]]) break … Read more