15-112 Fundamentals of Programming

Homework 2.2


For this homework, there is no starter file. You have to create your own .py file and submit it to Autolab. You can take a previous starter file and modify it appropriately.

  • Please add your name, Andrew id, and section at the top of the file.
  • Write test functions for each function you write.
  • IMPORTANT: All the code above the #ignore_rest line is autograded, and all the code below it is ignored. Make sure you put all test functions below #ignore_rest.
  • From now on you will be graded on style. For this homework, however, style deductions are half-weighted, so the maximum number of points you can lose on style is 5 (out of 100). Please see here for the style rubric.
  • You may not use recursion, sets, dictionaries or any other constructs that we have not yet covered in class.
  • You will have 3 submissions on Autolab for this homework.
  • Questions

    1. This question is for fun and will not be graded. First, read a bit about Fermat's Last Theorem here. In 1769, Leonhard Euler proposed a generalization of Fermat's Last Theorem. He conjectured that for n > 2, at least n nth powers are needed to obtain a sum that is itself an nth power. Euler's conjecture stood until 1967! Now you can disprove it yourself in just minutes. Write a function called disproveEuler() that returns a tuple of non-zero integers (a, b, c, d, e) such that a**5 + b**5 + c**5 + d**5 = e**5. Hint: You don't need to try values larger than 150.

    2. Study the following examples first. Make sure you understand how they work.
      def drawRectangle(rows, cols):
          for i in range(rows):
              for j in range(cols):
                  print("*", end="")
              print()
      
      drawRectangle(3, 4)
      print()
      
      def drawChessBoard(n):
          for i in range(n):
              for j in range(n):
                  if((i+j) % 2 == 0):
                      print("W", end="")
                  else:
                      print("B", end="")
              print()
      
      drawChessBoard(8)
      print()
      
      def drawTriangle1(rows):
          for i in range(rows):
              for j in range(i+1):
                  print("*", end="")
              print()
      
      drawTriangle1(4)
      print()
      
      def drawTriangle2(rows):
          for i in range(rows):
              for j in range(rows-i-1):
                  print(" ", end="")
              for k in range(i+1):
                  print("*", end="")
              print()
      
      drawTriangle2(4)
      print()
      
      # This one works the same way as the one above.
      def drawTriangle2(rows):
          for i in range(1, rows+1):
              for j in range(1, rows+1):
                  if(i+j > rows):
                      print("*", end="")
                  else:
                      print(" ", end="")
              print()
      
      drawTriangle2(4)
      print()
      
      def drawO(n):
          for i in range(-n,n+1):
              for j in range(-n,n+1):
                  if(i**2 + j**2 <= n**2):
                      print("* ", end="")
                  else:
                      print("  ", end="")
              print()
      
      drawO(16)
      

      Write a function called myTriangle(height) that takes a non-negative integer height as input, and returns the string that when printed, produces a triangle of the given height. The shape of the triangle is illustrated below.
      # If height = 3, the shape should be:
      #   *
      #  ***
      # *****
      #
      # If height = 4, the shape should be:
      #    *
      #   ***
      #  *****
      # *******
      # 
      # Here are a couple of test cases you can use:
      assert(myTriangle(0) == "")
      assert(myTriangle(3) == "  *  \n *** \n*****")
      

      Next, write a function called myX(n) that takes a non-negative integer n as input, and returns the string that when printed, produces a (2n+1) by (2n+1) X-shape as illustrated below.
      # If n = 2, the shape should be:
      # *   *
      #  * * 
      #   *  
      #  * * 
      # *   *
      #
      # The corresponding test case is:
      assert(myX(2) == "*   *\n * * \n  *  \n * * \n*   *")
      


    3. doPigStrategyAnalysis from here.


    Valid CSS! Valid XHTML 1.0 Strict