[go: up one dir, main page]

0% found this document useful (0 votes)
15 views17 pages

Lab 3 - Handout

Uploaded by

mudafuqer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views17 pages

Lab 3 - Handout

Uploaded by

mudafuqer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EECS 1015 Lab 3

Goal

• Be able to write a script that involve string


• Learn computa�onal thinking

Tasks

1. Guide you through the process of planning to write a script using


computa�onal thinking
2. Learn to debug a script with bool
3. Learn to write a script with string and bool
4. Learn to write a script with string and bool
5. Learn to write a script with string
6. Learn to write a script with string
7. Learn to write a script with string

Total credit: 100 pts

Deadline:

• You should submit it by the end of your lab.


• In order to give you some flexibility, the submission is extended to Feb.
2nd 23:59:59 without penalty. Therefore, no accommoda�ons will be
made if you do not meet the deadline. Furthermore, three lowest marks
will be dropped.

You are very welcome to ask clarifica�on ques�ons to TAs. But please read the document
carefully before you ask ques�ons.

It is an academic offense to copy code from other students, provide code to other students, let
other people (including the teaching staff) debug your code, or debug other students’ code.

We will make check code similari�es at the end of the semester.

Ques�ons 3 – 5 may not be arranged by the difficulty level. You are highly recommended to
take a look and decide the order of comple�ng the ques�ons. You will experience something
similar and it is important to learn how to triage your tasks.

Copyright © Meiying Qin


Task 1: Follow the Steps (30 pts)
In the previous lab, you learned about how to write a simple program. In this task, you will see a
demonstra�on on how to design a more complicated script. This is your first encounter of
computa�onal thinking. It is a very important skill as our program gets more and more
complicated.

Let’s go through this example together. Suppose we define a new opera�on # between two
natural numbers x # y such that it results in x2 – y2. For example:

2 # 3 = 22 – 32 = 4 – 9 = -5

Our goal is to write a script that prompts the user to give us expressions in the a # b format
and our script will calculate the value and print the result. For example:

You can take a few minutes to think of how you would approach this problem, or even write
some code yourself.

Now let’s do it together. If you are able to finish the script yourself, awesome! But please s�ll
along as I will introduce computa�onal thinking so that it will benefit you in the future if we have
more complicated tasks. If you are not able to finish the script yourself, no worries! Let’s see how
to approach this problem in a more systema�c manner.

So how do we write a script to achieve the func�onality? Recall that one of the principles we
men�oned in the first lecture is that we use building blocks to build systems. Let’s break down
the problem. Please note that you can use the same procedure for complicated problems in the
future.

Recall that in the previous lab, your code usually has the following the structure:

• Take the input


• Do some calcula�on with the input
• Show the result

Similarly, our script, or most of the scripts, also follow this structure. The challenging part of this
problem is how to further break down the calcula�on part. Pause here before you read on, and
think about what steps are needed.

Now let’s move on.

Let’s use an example “2 # 3” and the steps needed are:

1. Find 2 and 3 in “2 # 3”
2. Calculate the result based on the defini�on of #

You are highly recommended to atempt the ques�ons yourself before you look at the solu�on
as a way to learn.

Q1.1 How would you break down the first step?

Copyright © Meiying Qin


Solu�on:

In the string “2 # 3”, how we find the numbers is to see what are the numbers before
and a�er the ‘#’. So here are the steps we need:

1. Find 2 and 3 in “2 # 3”
a. Find where ‘#’ is
b. The number before ‘#’ is 2
c. The number a�er ‘#’ is 3
2. Calculate the result based on the defini�on of ‘#’

(Note: it is ok if you have something slightly different, as long as it makes sense.)

Now let’s put it all together, the structure of our script should look like if our example is “2 # 3”:

1. Take the input “2 # 3”


2. Do some calcula�on with the input
o Find 2 and 3 in “2 # 3”
 Find where ‘#’ is
 The number before ‘#’ is 2
 The number a�er ‘#’ is 3
o Calculate the result based on the defini�on of ‘#’
 2 # 3 = 22 – 32 = 4 – 9 = -5
3. Show the result -5

Our goal here is to write a script that can handle any expressions in this format, not just “2 # 3”.
So let’s generalize our structure:

1. Take the input in the format of “x # y”


2. Do some calcula�on with the input
2.1 Find x and y in “x # y”
i. Find where ‘#’ is
ii. Find what the number is before ‘#’ and let it be x
iii. Find what the number is before ‘#’ and let it be y
2.2 Calculate the result based on the defini�on of ‘#’
i. x # y = x2 – y2
3. Show the result

Q1.2 Is there anything special about the example “2 # 3”?

Solu�on:

There is nothing so special about our specific example “2 # 3”, we do not need to make
adjustment. Note that this may not be true to any arbitrary example, so you need to be
careful. For example, when you implement the division opera�on, you may choose the
example “4 / 2”. However, there are something special about this example that 4 can be
perfectly divided by 2, unlike “5 / 2” which end up as a float.

Copyright © Meiying Qin


Now let’s convert our structure to Python code. We first write down the steps as comments in
our Python script:

Each line of comment shows the goal of the chunk of code below it. And now we just need to
handle each mini problem. Let’s work on each goal.

Q1.3 How to implement goal 1?

Solu�on:

This is very similar to what we did in the previous lab, and it is straigh�orward:

Q1.4 How to implement goal 2.1.1?

Solu�on:

The data type of “x # y” is a string. You can use help(str) in the interac�ve mode to
find if there is any method that is useful here. You may no�ce this method:

It will give us the index of a single character or a string. This is what we need:

Copyright © Meiying Qin


For example, in the “2 # 3” example, it should give us 2 which is the index of ‘#’ in the
string (Note: index start from 0, not 1!). You can try it in the interac�ve mode

Q1.5 How to implement goal 2.1.2?

Solu�on:

Here we need the substring before the ‘#’ symbol, so we need to use string slicing

Note:

• We can use either [:pound_index] or [0:pound_index]. Recall that in


class, we men�oned that if we omit the first one, it means the substring starts
from the beginning.
• Here we use x as the variable name. We generally should not meaningless
names such as x, a, b, y, c, etc. This is a special case because it mimics
math formulas.

Q1.6 How to implement goal 2.1.3?

Solu�on:

Copyright © Meiying Qin


(Note: there is actually a litle bug in the code.)

Q1.7 How to implement goal 2.1.3?

Solu�on:

(Note: this is another litle bug in the code.)

Q1.8 How to implement goal 2.1.3?

Solu�on:

Copyright © Meiying Qin


Let’s run the script. Oops! We got an error message:

Q1.9 Fix this bug.

Solu�on:

There is a bug because we got a substring, but we need integers for the calcula�on.

Note on debugging:

• It is useful to show the line numbers as the interpreter will tell us exactly which line has
the error and it is easier to locate the bug. In order to show the line number in the Wing
IDE, go to edit -> Show Line Numbers.

Copyright © Meiying Qin


• Some�mes you may want to change the lines other than where it reported the bugs, as
what we did in the example above. The error was reported on line 17, but we change
line 10 and line 13.

Let’s run it again. We got another bug �:

Q1.10 Fix this bug.

Solu�on:

This is a very typical bug when working with strings. You should be very careful about indices.
For example, when using the example “2 # 3”, the index of ‘#’ is 2. So in “2 # 3”[2:], it is a
substring that starts from the # (including #).

As seen in this example, your computer will execute whatever you implement. It is exactly the
same as the making sandwich video showed in the first lecture. If you cannot fix the bug by
looking at the code, use the debugger to help you! The instruc�ons can be found in the previous
lab.

Now let’s run it again. YAY, it works!

Here is a summary of what we did. We first break down the problem into smaller pieces. If it is a
bit challenging to do so, you can always use a specific example to guide you and then generalize
your steps. However, you need to be careful and think about whether this is anything special
about your example. You can then treat each step as your sub-goals and solve each mini
problem. Lastly, you may need to perform some debugging.

Submission
• Go to eClass -> our course -> Week 4 -> Prac�ce -> Lab -> Lab 3 Submission

Copyright © Meiying Qin


• Copy your code to Task 1
• You can resubmit your code as many �mes as you need, but you need to wait for 5
minutes before submission (You need spend some �me debugging!).

Rubrics:

• You will get full mark for this ques�on if you submit the solu�on (with no typo) to the
required loca�on on Prairielearn before the deadline.

Note:

• since we provide solu�ons to this ques�on, it is ok to submit it as is.


• In order for the autograder to work properly:
o You must NOT change the order of the first few lines of taking input
o You must NOT add more print statement when submit the code
o You must NOT change the line with the print statement
o The print statement should be the last line of your code

Copyright © Meiying Qin


Task 2: Debugging (30 pts)
In this task, you will be provided with a poten�al script for a problem. However, this script does
not work. You need to debug the code based on what you learned in task 1 and in the lecture to
make it work.

Let’s define a new logical operator xor. It can be used between two Boolean literals a xor b. It
is true if and only if one of a and b is True. Here is the Truth table of this logical operator.

a b a xor b
True True False
True False True
False True True
False False False

The code to debug (lab3_task2.py) may have syntax errors, run-�me errors, and seman�c errors.
To help with debugging, you can use the debugger to execute the code line by line (you should
fix the syntax errors before doing this).

Note: You can use bool to turn the str ‘1’ to True, and ‘’ (empty string) to False. For example,
bool(‘1’) = True

Requirement
• Your submission must be based on the provided code. There are simpler ways of doing
this, but you cannot delete all the provided code and write your own.
• You can only use logical operators (e.g., and, or, not), assignment, bool(), and print
statement. You cannot use other statements/libraries, to name a few (It’s ok if you don’t
know what they are):
o You are not allowed to import other libraries
o You are not allowed to use if-statement
o You are not allowed to use comparison operators such as == or !=
o You are not allowed to use arithme�c opera�ons such as +, -, etc.
o You are not allowed to use bitwise opera�ons

Submission
Copy the Python code to Lab 3 -> Task 2 on Prairielearn.

You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need spend some �me debugging!).

Note:

• In order for the autograder to work properly:


o You must NOT change the order of the first few lines of taking input
o You must NOT add more print statement when submit the code
o You must NOT change the line with the print statement
o The print statement should be the last line of your code

Copyright © Meiying Qin


Rubrics:

• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will not receive any mark if your code violates the above requirements
• Otherwise
o You will receive 7.5 pts if your script correctly provide solu�on True xor True,
which is input 1 for a and input 1 for b
o You will receive 7.5 pts if your script correctly provide solu�on True xor False,
which is input 1 for a and no input for b (press enter directly)
o You will receive 7.5 pts if your script correctly provide solu�on False xor True,
which no input for a (press enter directly) and input 1 for b.
o You will receive 7.5 pts if your script correctly provide solu�on False xor False, ,
which no input for a (press enter directly) and no input for b (press enter
directly)

Copyright © Meiying Qin


Task 3: Implementa�on (10 pts)
In this task, you will implement a more complicated script yourself by expanding lab3_task3.py .
It determines whether a child should buy a �cket to enter a park. A child under the age of 6
(including 6) does not need to buy a �cket, but if the child is taller than 120 cm (not including
120), the child should buy a �cket regardless of the age.

If a child needs to buy a �cket, your script should show True, otherwise show False.

Requirement
• You must not use if-statement (It’s ok if you don’t know what it is, as it indicates that it is
very unlikely for you to use it)

Submission
Copy the Python code to Lab 3 -> Task 3 on Prairielearn.

You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need spend some �me debugging!).

Note:

• In order for the autograder to work properly:


o You must NOT change the order of the first few lines of taking input
o You must NOT add more print statement when submit the code
o You must NOT change the line with the print statement
o The print statement should be the last line of your code

Rubrics:

• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will not receive any mark if your code violates the above requirements
• Otherwise
o You will receive 3 pts if your script correctly provide solu�on to our example
(i.e., age = 3, height = 100, show False)
o You will receive 2 pts if your script correctly provide solu�on to our example
(i.e., age = 4, height = 146, show True)
o You will receive 1.5 pts if your script correctly provide solu�on to another
example (e.g., another case 1)
o You will receive 1.5 pts if your script correctly provide solu�on to another
example (e.g., another case 2)
o You will receive 2 pts if your script correctly provide solu�on to an example with
random input

Copyright © Meiying Qin


Task 4: Implementa�on (10 pts)
In this task, you will implement a script yourself by expanding lab3_task4.py . It checks whether
user input a valid phone number. A phone number should sa�sfy these two requirements:

• It must consist of digits, not alphabets or symbols.


• It must contain 10 digits, no more, no less.

However, the user may input a number with trailing spaces, which may appear at the beginning,
or end of the phone number, or both. For example, “ 1234567890”, “1234567890 ”,
“ 1234567890 ”.

Moreover, Users may also include the symbol ‘-’, ‘(‘, ‘)’ and spaces in the middle of their phone
numbers, such as “(123) 456 - 0000” or “123 - 456 - 7890” or “123 456 7890”or “12- 3456-78-
90” or “1)234(56 – 789(0” (although the last case looks weird, but in our case, we accept it for
simplicity)

Your script should correctly iden�fy those input are valid phone numbers (i.e., show True) and
show False otherwise.

Hint:

• You may need various string methods, especially the ones in the puzzles.
• If the code does not work as you planned, you can use the debugger to figure out the
problem.
• If you s�ll cannot figure out the problem with a debugger, try to iden�fy the problem
and isolate/abstract the problem in a different script and ask for help from the TA.
• If you have trouble iden�fying the problem and/or isola�ng the problem, you can ask
the TA how to do so. (Note: Your TA cannot debug the code for you)

Requirement
• You must not use the if-statement (It’s ok if you don’t know what it is, as it indicates that
it is very unlikely for you to use it)

Submission
Copy the Python code to Lab 3 -> Task 4 on Prairielearn.

You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need spend some �me debugging!).

Note:

• In order for the autograder to work properly:


o You must NOT change the order of the first few lines of taking input
o You must NOT add more print statement when submit the code
o You must NOT change the line with the print statement
o The print statement should be the last line of your code

Copyright © Meiying Qin


Rubrics:

• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will not receive any mark if your code violates the above requirements
• Otherwise
o You will receive 1 pt if your script correctly provide solu�on to
“ 1234567890”
o You will receive 1 pt if your script correctly provide solu�on to
“ 1234567890 ”
o You will receive 1 pt if your script correctly provide solu�on to “(123) 456 -
0000”
o You will receive 1 pt if your script correctly provide solu�on to “123 - 456 - 7890”
o You will receive 1 pt if your script correctly provide solu�on to “e31235h62i”
o You will receive 5 pts if your script correctly provide solu�on to other examples

Copyright © Meiying Qin


Task 5: Implementa�on (4 pts)
In this task, you will write a script from scratch and no starter code is provided. It takes one’s first
name and last name, and print out the in the format of last name, comma, space, first name.

For example, my first name is Meiying, and last name is Qin. Then the script should print out:

Qin, Meiying

You script should take the following input from the user:

• First name
• Last name

Requirement
• The input must be in the order of first name, and then last name.
• Your code should not take any other input
• The print statement should be the last line of your code
• Make sure you just print the value, and no other texts
• You should remove other print statements

Submission
Copy the Python code to Lab 3 -> Task 5 on Prairielearn.

You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need to spend some �me debugging!).

Rubrics
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will receive 2 pts if your script correctly provide solu�on to our example (i.e., with
the input listed above, your script should output Qin, Meiying)
• You will receive 1 pts if your script correctly provide solu�on to another example
• You will receive 1 pts if your script correctly provide solu�on to a slightly more
challenging example

Copyright © Meiying Qin


Task 6: Implementa�on (8 pts)
In this task, you will write a script from scratch and no starter code is provided. It takes one’s
name in the format of last name, comma, space, first name, and print out the last name.

For example, my first name is Meiying, and last name is Qin. Therefore, I should provide the
script the following input:

Qin, Meiying

And the script will print out my last name:

Qin

Requirement
• The input must be in the format of last name, comma, one space, and first name and
should take them as one input.
• Your code should not take any other input
• The print statement should be the last line of your code
• Make sure you just print the value, and no other texts
• You should remove other print statements

Submission
Copy the Python code to Lab 3 -> Task 6 on Prairielearn.

You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need to spend some �me debugging!).

Rubrics
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will receive 4 pts if your script correctly provide solu�on to our example (i.e., with
the input listed above, your script should output Qin)
• You will receive 2 pts if your script correctly provide solu�on to a slightly more
challenging example
• You will receive 2 pts if your script correctly provide solu�on to another slightly more
challenging example

Copyright © Meiying Qin


Task 7: Implementa�on (8 pts)
In this task, you will write a script from scratch and no starter code is provided. It takes one’s
name in the format of last name, comma, space, first name, and print out the first name.

For example, my first name is Meiying, and last name is Qin. Therefore, I should provide the
script the following input:

Qin, Meiying

And the script will print out my first name:

Meiying

Requirement
• The input must be in the format of last name, comma, one space, and first name and
should take them as one input.
• Your code should not take any other input
• The print statement should be the last line of your code
• Make sure you just print the value, and no other texts
• You should remove other print statements

Submission
Copy the Python code to Lab 3 -> Task 7 on Prairielearn.

You can resubmit your code as many �mes as you need, but you need to wait for 5 minutes
before submission (You need to spend some �me debugging!).

Rubrics
• You will not receive any mark if you do not submit it to the designated loca�on on
Prairielearn before the deadline
• You will receive 4 pts if your script correctly provide solu�on to our example (i.e., with
the input listed above, your script should output Meiying)
• You will receive 2 pts if your script correctly provide solu�on to a slightly more
challenging example
• You will receive 2 pts if your script correctly provide solu�on to another slightly more
challenging example

Copyright © Meiying Qin

You might also like