Skip to content

Instantly share code, notes, and snippets.

@cnh
Last active December 17, 2015 14:18
Show Gist options
  • Save cnh/5622984 to your computer and use it in GitHub Desktop.
Save cnh/5622984 to your computer and use it in GitHub Desktop.
'''
x = 'Writing Fast Tests Against Enterprise Rails 60min \n Overdoing it in python 45min'
print x
y = x.split()
print y
'''
import re
x = '''writing fast tests against enterprise rails 60min
rails abra ksa dabra 30min
ruby on rails is lightning fast lightning
'''
a_list = x.splitlines()
print ('\n\n a_list is \n' + str(a_list) )
'''
a_list =
['writing fast tests against enterprise rails 60min',
' rails abra ksa dabra 30min',
'ruby on rails is lightning fast lightning']
'''
b_list = [ re.sub (r'\blightning$' , '5min', i) for i in a_list ]
print ('\n\n b_list is \n' + str(b_list) )
'''
b_list =
['writing fast tests against enterprise rails 60min',
' rails abra ksa dabra 30min',
'ruby on rails is lightning fast 5min']
'''
#re.findall(r'[0-9]+',
duration = [ re.findall(r'[0-9]+', i) for i in b_list ]
'''
duration = [['60'], ['30'], ['5']]
'''
print ('\n\n duration is \n' + str(duration) )
intd = [int(duration[i][0]) for i in range(len(duration))]
'''
intd = [60, 30, 5]
'''
print ('\n\n intd is \n' + str(intd) )
c_list = [i.rsplit(' ',1)[0] for i in b_list ]
'''
['writing fast tests against enterprise rails',
' rails abra ksa dabra',
'ruby on rails is lightning fast']
'''
print ('\n\n c_list is \n' + str(c_list) )
b_dict = { c_list[i] : intd[i] for i in range(len(c_list)) }
'''
{' rails abra ksa dabra': 30,
'ruby on rails is lightning fast': 5,
'writing fast tests against enterprise rails 60min': 60}
'''
print ('b_dict is \n' + str(b_dict) )
c_dict = { intd[i] : c_list[i] for i in range(len(c_list)) }
#possible_solns = []
u_possible_solns = set()
def subset_sum_recursive(numbers,target,partial):
s = sum(partial)
#check if the partial sum is equals to target
if s == target:
#print "sum(%s)=%s"%(partial,target)
#possible_solns.append(partial)
u_possible_solns.add(tuple(partial))
#print u_possible_solns
#print possible_solns
#print partial
#print partialindices #indices
if s >= target:
return # if we reach the number why bother to continue
for i in range(len(numbers)):
n = numbers[i]
remaining = numbers[i+1:]
#indices = indices.append(i)
subset_sum_recursive(remaining,target,partial + [n])
#print possible_solns
def subset_sum(numbers,target):
#we need an intermediate function to start the recursion.
#the recursion start with an empty list as partial solution.
subset_sum_recursive(numbers,target,list())
#print possible_solns
#print ("\n # of possible solns is " + str(len(possible_solns)) + '\n')
#u_possible_solns = set(possible_solns)
print u_possible_solns
print ("\n # of unique possible solns is " + str(len(u_possible_solns)) +'\n')
seen = set()
uniquel = []
for t in u_possible_solns:
s = frozenset(t)
if s not in seen:
seen.add(s)
uniquel.append(t)
print uniquel
print ("\n # of really unique possible solns is " + str(len(uniquel)) +'\n')
track1_m = uniquel[0]
print 'track1_m is ' + str(track1_m)
print 'minutes is ' + str(minutes)
print 'names is ' + str(names)
print 'length of uniquel[0] is ' + str(len(uniquel[0]))
#print 'uniquel[0][0] is ' + str(uniquel[0][0])
t1m_indices = []
t1m_names = []
min_copy = minutes[:]
names_copy = names[:]
for i in range(len(track1_m)):
value_to_remove = track1_m[i]
index_to_remove = minutes.index(value_to_remove)
#t1m_indices.append( index_to_remove)
name_removed = names[index_to_remove]
t1m_names.append(name_removed)
#print 'list t1m_indices is ' + str(t1m_indices)
print 'index of track1_m['+str(i)+'], i.e. '+ str(value_to_remove)+' in list minutes is ' + str(index_to_remove)
print 'corresponding name is ' + name_removed
print 'removing '+ str(value_to_remove) + ' from the minutes list'
del minutes[index_to_remove]
print 'minutes now is ' + str(minutes)
print 'removing '+ name_removed + ' from the names list'
del names[index_to_remove]
#print 'names now is ' + str(names)
#minutes.remove(track1_m[i])
#wrong t1m_indices[0] = minutes.index(30)
print 't1m_indices is ' + str(t1m_indices)
print 't1m_names is ' + str(t1m_names)
print 'new minutes now is ' + str(minutes)
print 'new names now is ' + str(names)
print 'original minutes is ' + str(min_copy)
print 'original names is ' + str(names_copy)
return minutes
if __name__ == "__main__":
minutes = [60, 45, 30, 45, 45, 5, 60, 45, 30, 30, 45, 60, 60, 45, 30, 30, 60, 30, 30 ]
names = ['Writing Fast Tests Against Enterprise Rails', 'Overdoing it in Python', 'Lua for the Masses', 'Ruby Errors from Mismatched Gem Versions',
'Common Ruby Errors', 'Rails for Python Developers', 'Communicating Over Distance', 'Accounting-Driven Development', 'Woah', 'Sit Down and Write',
'Pair Programming vs Noise', 'Rails Magic', 'Ruby on Rails: Why We Should Move On', 'Clojure Ate Scala (on my project)', 'Programming in the Boondocks of Seattle'
, 'Ruby vs. Clojure for Back-End Development', 'Ruby on Rails Legacy App Maintenance', 'A World Without HackerNews', 'User Interface CSS in Rails Apps']
print '\n len of minutes is ' + str(len(minutes))
print '\n len of names is ' + str(len(names))
#subset_sum(intd, 65)
pass2_mins = subset_sum(minutes, 180)
print 'pass2_mins is ' + str(pass2_mins)
subset_sum(pass2_mins, 50)
#subset_sum([1,2,3,4,5,6,7,8,9,10], 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment