INFO1110 Lecture Notes - Lecture 8: Programming Idiom, 5,6,7,8
Document Summary
A programming idiom is a commonly-used method to solve a small problem. They are all related to search. e. g. find the smallest of two numbers: if x < y: Sometimes we don"t know how many iterations there are in the loop. while i < n: if goal is satisfied: break if some condition: i = n i += 1 (): reversing sequences. Note that the last element has index n 1 as indexes start at zero. i = n - 1 while i >= 0: print(i) print(numbers[i]) i -= 1. This creates the sequence for values of i as n 1, n 2, , 2, 1, 0. Standard convention where i has dependencies name_order = [ 1st, 2nd, 3rd, 4th, ] i = 0 while i < n: print(i) # do something with i that must have an ascending sequence print(name_order[i], end=" ") print(numbers[n - i - 1]) i += 1.