Help me write better Code
Hello, I have working code - but looking for better/improved code. Better coding practices, better algorithm :) Problem: Given sequence of increasing integers, print blocks of consecutive integers. Example: Input: [10, 11, 12, 15] Output: [10, 11, 12] Input: [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] Outout: [67, 68], [91, 92, 93, 94] My code looks as below: - #!/usr/bin/python a = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] #a = [] #a = [10] #a = [10, 11, 12, 15] print "Input: " print a prev = 0 blocks = [] tmp = [] last = 0 for element in a: if prev == 0: prev = element next if element == prev + 1: if tmp: pass else: tmp.append(prev) tmp.append(element) else: if tmp: blocks.append(tmp) tmp = [] prev = element if tmp: blocks.append(tmp) if blocks: #print "I have repeated elements and those are:" for b in blocks: print b --- thank you in advance! -- https://mail.python.org/mailman/listinfo/python-list
Re: Help me write better Code
Thank you so much Terry Jan Reedy. You have given best advice - yup, i am beginner in Python. Your reply has done grooming :) thx, On Thursday, July 10, 2014 12:16:48 AM UTC+5:30, Terry Reedy wrote: > On 7/9/2014 10:27 AM, sssdevelop wrote: > > > Hello, > > > > > > I have working code - but looking for better/improved code. Better coding > > practices, better algorithm :) > > > > > > Problem: Given sequence of increasing integers, print blocks of consecutive > > integers. > > > > > Input: [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > > > Outout: [67, 68], [91, 92, 93, 94] > > > > Recommendations: > > 1. If you are just beginning Python, use the current version, now 3.4. > > > > 2. Separate interface code that gets input and presents output from the > > function that processes the increasing sequence. The function should not > > care whether the ints come from a user, file, or calculation. > > > > 3. Think in terms of iterables and iterators rather than lists (this is > > clearer in 3.x, where some builtins have been converted). The function > > should not care what class is used to convey the sequence of numbers. > > This happens to make it easier to solve the 'pump-priming' problem you > > stumbled over. > > > > 4. For designing a loop, what is the loop invariant that you want, that > > will make writing the code easy. For this problem, "tem is a non-emptly > > list of consecutive integers". Remember that a list of one int > > qualifies. Using for loops with the proper iterable solves the other > > part of the loop invariant: the current item is the next item to be > > compared to the last item of tem. If tem is always non-empty, that > > comparison is always possible. > > > > 5. Remember that Python code is generic unless constrained. What should > > happen if the function gets non-int numbers, with or without an integer > > value? What should happen if the sequence is not increasing, but > > contains consecutive subsequences. For beginning code, one could decide > > to meet the spec given for input that meets the condition and not care > > otherwise. The code below works for any sequence (even infinite) of > > objects that can be incremented by 1 and compared to the next. > > > > 6. Write an automated test. For one test, something like this works. > > > > ci = consec([51, 53, 55, 67, 68, 91, 92, 93, 94, 99]) > > print(next(ci) == [67, 68], next(ci) == [91, 92, 93, 94]) > > > > but since you (properly) noted several test cases > > > > a = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > > #a = [] > > #a = [10] > > #a = [10, 11, 12, 15] > > > > I went ahead and used unittest, at the cost of three lines of > > 'boilerplate' code. I added a case with a final consecutive sequence. > > Good thing, because it initially failed because I initially forgot to > > check tem after the loop. > > > > import unittest > > > > def consec(iterable): > > "Yield blocks of consecutive integers as a list." > > it = iter(iterable) > > first = next(it) > > tem = [first] > > for n in it: > > # tem is a non-empty list of consecutive ints > > if n == tem[-1] + 1: > > tem.append(n) > > else: > > if len(tem) >= 2: > > yield tem > > tem = [n] > > if len(tem) >= 2: > > yield tem > > > > class Test(unittest.TestCase): > > def test_consec(self): > > def eq(seq, blocks): > > self.assertEqual(list(consec(seq)), blocks) > > eq((), []) > > eq([1], []) > > eq([1,2,3], [[1,2,3]]) # block at beginning or end > > eq([-1, 1,2,3, 5], [[1,2,3]]) # block in middle > > eq((-1, 1,2,3, 5, 7,8,9, 11), [[1,2,3], [7,8,9]]) # 2 blocks > > > > unittest.main(verbosity=2) > > >>> > > test_consec (__main__.Test) ... ok > > > > -- > > Ran 1 test in 0.016s > > > > OK > > > > -- > > Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
Re: Help me write better Code
thank you so much! On Wednesday, July 9, 2014 11:46:41 PM UTC+5:30, Ian wrote: > On Wed, Jul 9, 2014 at 8:27 AM, sssdevelop wrote: > > > prev = 0 > > > blocks = [] > > > tmp = [] > > > last = 0 > > > for element in a: > > >if prev == 0: > > > > Is 0 allowed to be in the input list? What would happen if it were? > > > > > next > > > > This line doesn't do anything. It looks up the builtin function named > > next and then does nothing with it. I suspect you meant to use the > > keyword 'continue' here. > > > > >if tmp: > > >pass > > >else: > > >tmp.append(prev) > > > > if not tmp: > > tmp.append(prev) > > > > Also, give tmp a more meaningful name. Call it "current_block" or > > something descriptive like that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Help me write better Code
Mark - thank you so much. You have suggested be new best tool/module. It's going to help me many places. Was not aware of such powerful tool. thank you, On Wednesday, July 9, 2014 9:14:01 PM UTC+5:30, Mark Lawrence wrote: > On 09/07/2014 15:27, sssdevelop wrote: > > > Hello, > > > > > > I have working code - but looking for better/improved code. Better coding > > practices, better algorithm :) > > > > > > Problem: Given sequence of increasing integers, print blocks of consecutive > > integers. > > > > > > Example: > > > > > > Input: [10, 11, 12, 15] > > > Output: [10, 11, 12] > > > > > > Input: [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > > > Outout: [67, 68], [91, 92, 93, 94] > > > > > > My code looks as below: > > > - > > > #!/usr/bin/python > > > a = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > > > #a = [] > > > #a = [10] > > > #a = [10, 11, 12, 15] > > > print "Input: " > > > print a > > > > > > prev = 0 > > > blocks = [] > > > tmp = [] > > > last = 0 > > > for element in a: > > > if prev == 0: > > >prev = element > > >next > > > if element == prev + 1: > > > if tmp: > > > pass > > > else: > > > tmp.append(prev) > > > tmp.append(element) > > > else: > > > if tmp: > > >blocks.append(tmp) > > > tmp = [] > > > > > > prev = element > > > > > > if tmp: > > > blocks.append(tmp) > > > > > > if blocks: > > > #print "I have repeated elements and those are:" > > > for b in blocks: > > > print b > > > > > > --- > > > > > > thank you in advance! > > > > > > > Adopted from here https://docs.python.org/3.0/library/itertools.html > > > > data = [51, 53, 55, 67, 68, 91, 92, 93, 94, 99] > > for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]): > > group = list(map(operator.itemgetter(1), g)) > > if len(group) > 1: > > print(group) > > > > >>> > > [67, 68] > > [91, 92, 93, 94] > > >>> > > > > -- > > My fellow Pythonistas, ask not what our language can do for you, ask > > what you can do for our language. > > > > Mark Lawrence > > > > --- > > This email is free from viruses and malware because avast! Antivirus > protection is active. > > http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Alternatives to Splunk?
Are there any opensource alternatives to Splunk? Need tool to analyze the log files.. -- http://mail.python.org/mailman/listinfo/python-list
Re: Alternatives to Splunk?
Yup - its off topic. I was triggered to write here because Splunk is written in Python. And Python is good at Parsing/Regex. Thank you for your response about logstash, kibana. I was looking for such tools only - thank you so much. ---sss On Saturday, February 9, 2013 7:05:57 PM UTC+5:30, Rodrick Brown wrote: > On Sat, Feb 9, 2013 at 7:27 AM, sssdevelop wrote: > > > > Are there any opensource alternatives to Splunk? > > Need tool to analyze the log files.. > > > > > > This is highly off topic, however I'm using logstash + kibana for my log > analysis. > > > > -- > > http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list