New answers tagged

1

For me, this is a little bit unclear def list_diff(list1, list2): """Returns a new list with the items in list1 that do not belong to list2""" return list(set(list1) - set(list2)) since you don't just return a new list with the items in list1 that are not in list2, but you also remove duplicates from list1 (which maybe is what you want to do?), and ...


1

BUG: Your main loop for l in substrings(a): ... will return the first palindrome it finds inside a (stepping the indices back from the end), not just the longest one. If there are multiple longest palindromes it won't return them all, only the first it finds. Your code is overly tailored to this one example. Use other examples to tickle it, e.g. "zaba ...


2

Thank you for the great question and the great review @Peilonrayz ! Don't have much to criticize, only a few things which I would personally change. It's my first heap implementation, so please double check my suggestions... Getting the parent/child is a bit of magic numbering, maybe put that into a separate function Both in heapify/inv_heapify the words ...


2

As a rule I don't like to trigger exceptions, even controlled ones. One reason is that they are more expensive to handle, and as the name implies they should remain exceptions. So rather than doing this: try: bond = open('/proc/net/bonding/%s' % iface).read() except IOError: print "ERROR: Invalid interface %s\n" % iface usage() I ...


1

If the functions are simply returning constants as in your example you could make a dictionary as a lookup table and do something like this: ANSWERS = { 'root_path' : { True: 'something' , False: 'something else' }, 'file_name' : { True: 'file name A' , False: 'file name B' }, } def get_answer(question, is_cond_met=False): return ...


2

Your example is a little too example-y for me to know if this is actually right for whatever you're actually doing, but if you have two parallel implementations of a set of behavior within the same set of functions, you might think about moving them into classes: from abc import ABC, abstractmethod class CondFile(ABC): @abstractmethod def ...


Top 50 recent answers are included