#---------------------------------------------------------------------- # BUILDING LISTS # # Copyright (C) September 22, 2017 -- Dr. William T. Verts # # Start with an empty list # run a for-loop the right number of times, and for each pass through the loop: # add a [thing] to the end of the list (may be dependent on loop control variable) #---------------------------------------------------------------------- #---------------------------------------------------------------------- # Build a list of the squares of all integers from 0 through 19 #---------------------------------------------------------------------- def Example1(): L = [] for I in range(20): L = L + [I*I] return L #---------------------------------------------------------------------- # Build a list of 5 numeric values entered by the user # (the str function changes its argument into a string) #---------------------------------------------------------------------- def Example2(): L = [] for I in range(5): L = L + [input(str(I) + " Enter a number: ")] return L #---------------------------------------------------------------------- # Build a list of squares of the integers from the given list # (for-loops don't necessarily have to use ranges) #---------------------------------------------------------------------- def Example3(): L = [] for I in [3, -4, 1, 6, 0]: L = L + [I*I] return L #---------------------------------------------------------------------- # Build a list of the squares of all even numbers between 0 and 19 # (The % operator, mod, means "compute the remainder of a division", # and the double-equal == is used to compare two values for equality) #---------------------------------------------------------------------- def Example4(): L = [] for I in range(20): if ((I % 2) == 0): # True only if I is even L = L + [I*I] return L #---------------------------------------------------------------------- # Build the same list as in #4, but use range to avoid needing the if #---------------------------------------------------------------------- def Example5(): L = [] for I in range(0,20,2): L = L + [I*I] return L #---------------------------------------------------------------------- # Build a list of 25 zeroes [0,0,0,0,...,0,0] # (The item added to the list need not involve the loop variable I at # all. Note that this can also be done as L = [0] * 25) #---------------------------------------------------------------------- def Example6(): L = [] for I in range(25): L = L + [0] return L #---------------------------------------------------------------------- # LIST COMPREHENSIONS # # All of the earlier functions can be done with a single line statement # called a "list comprehension", which embeds the list constructor, # computed expression, for-loop, and optionally an if-statement, into # a single statement. #---------------------------------------------------------------------- L1 = [I*I for I in range(20)] # Example #1 L2 = [input(str(I) + " Enter a number: ") for I in range(5)] # Example #2 L3 = [I*I for I in [3, -4, 1, 6, 0]] # Example #3 L4 = [I*I for I in range(20) if ((I % 2) == 0)] # Example #4 L5 = [I*I for I in range(0,20,2)] # Example #5 L6 = [0 for I in range(25)] # Example #6 print "L1 = ", L1 print "L2 = ", L2 print "L3 = ", L3 print "L4 = ", L4 print "L5 = ", L5 print "L6 = ", L6 #---------------------------------------------------------------------- # Compute the Hailstone list for positive integer N (this was # assignment #1 from last Spring). If N is even, divide it by 2. # If N is odd, multiply it by 3 and add 1. Do this as long as N # is greater than 1. Return all computed values. # Hailstone1 was presented in class; Hailstone2 is a variation. # This is the kind of program you will do in the first assignment. #---------------------------------------------------------------------- def Hailstone1(N): # N is a positive integer L = [] while (N > 1): L = L + [N] if ((N % 2) == 1): N = N * 3 + 1 else: N = N / 2 L = L + [1] return L def Hailstone2(N): # N is a positive integer L = [N] while (N > 1): if ((N % 2) == 1): N = N * 3 + 1 else: N = N / 2 L = L + [N] return L