python - How can I change this function call and algorithm to find the string with the smallest value in a list of size n. w/o the min function -
how can change function call , algorithm find string smallest value in list size n. aware of built-in min function, trying understand mechanics. let me preface saying first semester cs student, apologize in advance ignorance.
def main(): strone = 'stack' strtwo = 'over' strthree = 'flow' strfour = 'please' strfive = 'help' first = alphabetical(strone, strtwo, strthree, strfour, strfive) print(first) def alphabetical(one, two, three, four, five): low = 1 if 2 < low: low = 2 if 3 < low: low = 3 if 4 < low: low = 4 if 5 < low: low = 5 return low main() ################################################################### # str_list = ['stack', 'over', 'flow', 'please', 'help'] ?? # # in str_list: ?? perhaps on right track idea.# # first = alphabetical(i) ?? maybe # ###################################################################
using sort many comparisons. emulate min does, should make single pass on data, updating best (lowest) value seen far.
>>> def lowest(sequence): 'find lowest value in sequence in one-pass' best = sequence[0] in range(1, len(sequence)): if sequence[i] < best: best = sequence[i] return best >>> lowest(['stack', 'over', 'flow', 'please', 'help']) 'flow'
Comments
Post a Comment