CompSciHW1

From Predictive Chemistry
Jump to: navigation, search

Homework 1 (due Monday, Jan. 25, 2016)

Use the 7 forms to write code performing the following:

  1. Create a variable named x holding the string "Hello World!"
  2. Create a (length 8) list containing the pattern below:
    • 3 8 15 24 35 48 63 80
    • Each element should use an arithmetic expression (calculator form).
    • assign the list to the variable y
  3. Print a greeting displaying the variable x followed by element 4 of the list above.
  4. Assign the variable z to element 4 of y if element 4 of y is larger than 42. Otherwise, set z equal to element 5 of y.
  5. Call the function x.__add__ with one argument, " World! World!".
  6. Re-write your answer to the last question by sending a variable as an argument, instead of directly writing "World! World!".
  7. Write a description of what the following program does.
    • How would you modify the code to eliminate all the i = len(x)-1 lines?
    • Re-write the code, abstracting out the numbers 61 and 900 into variables a and m.

<source lang="python"> x = [541] i = len(x)-1 x.append(x[i]*61 % 900) i = len(x)-1 x.append(x[i]*61 % 900) i = len(x)-1 x.append(x[i]*61 % 900) i = len(x)-1 x.append(x[i]*61 % 900) i = len(x)-1 x.append(x[i]*61 % 900)

print len(x) print x </source>