Jan-08-2020, 12:40 PM
Maybe following can enhance learning process.
In programming same results can be achieved in different ways.
For example we don't need to compare all list items if we have assigned first value as max value. We can start from second item:
In programming same results can be achieved in different ways.
For example we don't need to compare all list items if we have assigned first value as max value. We can start from second item:
numbers = [11, 2, 23, 45, 67, 99, 101] largest_num = numbers[0] for number in numbers[1:]: if largest_num < number: largest_num = number # largest_num is 101We can make an iterator from numbers and assign first item in list with next() as largest_num and then iterate over remaining items:
numbers = [11, 2, 23, 45, 67, 99, 101] nums = iter(numbers) largest_num = next(nums) for num in nums: if largest_num < num: largest_num = num # largest_num is 101After mastering finding largest number from list with own algorithm one can start using built-in max() (no need to invent wheel):
>>> numbers = [11, 2, 23, 45, 67, 99, 101] >>> max(numbers) 101
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.