Starting from:
$30

$24

CS Lab 5: Introduction to Python Solution

Aim: Introduce you to programming in Python quickly!

Let’s get started!

        o Create a directory structure to hold your work for this course and all the subsequent labs:
            ▪ Suggestion: CS202/Lab5

Python Basics

        o For this part, refer to Python Basics.pdf

    o Note: This part has been adapted from : https://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/Python1_Basics.html

    o Considering that you all have decent amount of experience with programming in C, C++ and now in Java and Perl, you are only expected to run through the above document quickly. Try to focus on differences from other languages!

        o Once you are comfortable with the basics, attempt the following exercises.

Exercise 1:

        o Write a program (using functions!) that asks the user for a long string containing multiple words. Print back to the user the same string, except with the words in backwards order. For example, say I type the string: My name is Michele Then I would see the string: Michele is name My shown back to me.

        o Create and save this program in a file named ReverseString.py

Exercise 2:

        o Write a Python function that prints out the first ‘n’ rows of Pascal's triangle. Sample Pascal’s triangle is shown below:




















    o Create and save this program in a file named PascalTriangle.py
Exercises 3:

    o Write a program (function!) that takes a list and returns a new list that contains all the elements of the first list minus all the duplicates.

        ◦ Write two different functions to do this - one using a loop and constructing a list, and another using sets (call these functions RemDupLoop and RemDupSet)

        o Create and save this program in a file named RemoveDuplicates.py

Submitting your work:

        o All source files as one tar-gzipped archive.

            ▪ When unzipped, it should create a directory with your ID. Example: 2008CSB1001 (NO OTHER FORMAT IS ACCEPTABLE!!! Case sensitive!!!)

            ▪ Negative marks if the TA has to manually change this to run his/her scripts!!

    o Source files should include the following: (Case-Sensitive file names!!)
  ReverseString.py  [5 Points]
    • RemoveDuplicates.py [5 Points]

    • PascalTriangle.py [5 Points]

    o Negative marks for any problems/errors in running your programs

    o Submit/Upload it to Google Classroom

More products