Laboratory Walkthrough 6 - Intro Ex6 One Code line Programs...

This preview shows page 1 - 3 out of 6 pages.

Intro Ex6 One Code line Programs Submission deadline Thursday, December 5 th , 20:55 Objectives The purpose of this homework assignment is to exercise some advanced concepts of python programming, specifically list and dictionary comprehensions. This assignment contains multiple tasks. Start working on it early! (Otherwise, you risk missing the assignment deadline.) Your code must satisfy the Coding Style Guidelines posted on the website. Note that: 1. In order to get a perfect grade you must solve each task using a single line of code (unless stated otherwise)! a. We will reduce 2 points (in the manual grading phase) on tasks containing solutions with more than one code line. b. We will also reduce points (after the 2 points reduction) for inefficient solutions. 2. Import lines DO NOT COUNT as code lines. See the following example. 3. In each task, the name of the function you write must equal the name of the task. 4. Unless explicitly stated otherwise, you don't need to check that the input is correct. 5. Try using Google to find python functions that are useful in solving this assignment. Note that some of them were not discussed in class. 6. You are not allowed to use lambda functions in this exercise (except for task 9). 7. Make sure that your code is readable and that functions are documented using a docstring. Making your code readable and simple is even more important when solving problems using one code line. Here is an example: 1. Task: factorial Description: Given a number N , return N factorial (i.e., N !). For example, calling factorial(4) returns 24 (1*2*3*4). Input: An integer greater than 1. Return: An integer. There are several ways to implement this task: Option 1: (This option gets full credit!) from operator import mul #this line does not count! from functools import reduce #this line does not count as well! def factorial ( N ): return reduce ( mul , range ( 1 , N + 1 ), 1 )
Image of page 1

Subscribe to view the full document.

Option 2: (This option has more than one code line. It doesn't get full credit.) def factorial ( N ): result = 1 for num in range ( 1 , N + 1 ): result *= num return result Option 3: (This code uses a lambda function. As a result it gets no credit at all ) from functools import reduce #this line does not count! def factorial ( N ): return reduce ( lambda x , y : x * y , [ 1 ]+ list ( range ( 1 , N + 1 ) ) ) All three functions produce the same output, so they will pass the auto-testers. However, Only the first option gets a perfect grade. Option 2 gets 2 points reduction in the manual grading. Handling long lines: If your code line is longer than 79 characters (including the 4 spaces needed for indentation), you must break it into two or more editor lines. Here is an example. The code line [ sum ([ row [ j ] for row in matrix ]) for j in range ( 4 )] can be written as: [ sum ([ row [ j ] for row in matrix ]) \ for j in range ( 4 )] The second alternative is still considered one line of code. It gets full credit.
Image of page 2
Image of page 3
  • Spring '14
  • Avraham
  • Quantification

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
Christopher Reinemann
"Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

Ask a question for free

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern