programmer
BAN USERfrom itertools import combinations, permutations
inp = [1, 2, 1, 4, 9, 2, 12, 7, 3]
all_combinations = sum([map(list, combinations(inp, item)) for item in xrange(len(inp)+1)], [])
print [lst for lst in all_combinations if sum(lst) <= max(inp) and sum(lst) >= min(inp)]
def check_str(input_str, order_str):
for item in order_str:
if item not in input_str:
return False
indx = len(input_str) - input_str[::-1].index(item) - 1
input_str = input_str[indx:]
return True
print check_str('hello world!', '!od')
total_gap = 0
for rank in dhoni_str:
kohli_rank, dhoni_rank = kohli_str.index(rank), dhoni_str.index(rank)
if kohli_rank - dhoni_rank > 0:
total_gap += kohli_rank - dhoni_rank
print total_gap
children_father_rel = [['luke', 'show'], ['wayne', 'rooney'], ['rooney', 'ronaldo'], ['shaw', 'rooney']]
child_input_key = []
def find_father_position(input_key):
"""
Find the input key in the two dimension array
:param: inputKey: input key which need to be find
:return: None
"""
for first_indx, rel_arr in get_each_rel():
if get_input_indx(input_key, first_indx):
return first_indx
return None
def get_input_indx(input_key, first_indx):
"""
Get the indx input key in children_father_rel
:param: input_key: input key which need to be find
:param: first_indx: first index of the multidimention array
:param: second_indx: second index of the multidemention array
:return: return True if match neither False
"""
return True if input_key in children_father_rel[first_indx] else False
def get_each_rel():
"""
Get each father and children relation
:param: None
:return: None
"""
first_indx = 0
for children_father in children_father_rel:
yield (first_indx, children_father)
first_indx += 1
def find_number_of_childs(input_key):
"""
Find number of childs in the 2d array
:param: input_key: input_key or father as input
:return: return the number of childs
"""
while find_father_position(input_key):
child_indx = find_father_position(input_key)
if children_father_rel[child_indx][0] in child_input_key:
break
child_input_key.append(children_father_rel[child_indx][0])
input_key = children_father_rel[child_indx][0]
return child_input_key
if __name__ == '__main__':
input_key = 'ronaldo'
find_number_of_childs(input_key)
print child_input_key
- programmer August 10, 2016