首页 > > 详细

辅导 MA1008 Introduction to Computational Thinking AY 2019/2020辅导 C/C++程序

MA1008 Introduction to Computational Thinking

AY 2019/2020

Tutorial 1

This tutorial gets you to think about the workings of computers and computer languages, and solving problems computationally, constructing the steps for a solution.

At this stage, we are not yet too concerned about the language we are to use.

1.    (i) Name the two levels of languages that are normally involved in the programming process. (ii) What are they for?

(iii) What is the relationship between them?

2.    (i) What is a compiled language? (ii) What is an interpreter?

(iii) Give two examples for each.

3.    (i) How does a computer represent numbers?

(ii) How does it represent non-numeric data such as letters and colours?

4.    Construct the steps for making a cup of coffee. You have the choice of instant or powdered coffee, with or without sugar, with or without milk. Assume you use a percolator for the powdered coffee.

5.    Construct the steps for solving a quadratic equation, starting from getting the inputs for the coefficients through to delivering the solutions.

6.    Construct the steps for swapping two numbers stored in two variables.

7. Construct the steps for finding the maximum (or minimum) in a list of 2 variables. Then extend to 3 variables. Then extend it to many (say 100) variables.

Week 3 Tutorial: Data types and variables

This tutorial is on variables, data types and simple Python operations. It contains a few things that have   not been explicitly covered in the video lectures. But they are simple enough; you should be able to do a bit of reading ahead and find the answers.

1.   There are different data types in Python. Why is there a need to have distinction between different types of data?

2.    Name the data type of the following values or the variables on the left hand side of the expressions:

(i)     2

(ii)    "2"

(iii)    "Two"

(iv)    6.71

(v)     4/2

(vi)   7/3

(vii)   6.5*2

(viii) x = 1 + 2

(ix)   y = 1.0 + 2

(x)     z = "1.0" + 2

3.    If we enter the following lines one by one into the Python shell, which line below will result in an error? Why? What type of error is it? For those valid lines, which of them are Python statements? And which of them are Python expressions?

a = d

c = 10

a = c + 1

a + c = c

3 + a

print(A)

print("b*b + a*a = c*c")

7up = 10

import = 1003

print("c" = 1)

a ** 3

b = math.pi * c

a,b,c = c,1,a

b,c,a = a,b

c = b = a = 7

Note: do not use acomputer to solve them!!!

4.    What is the outcome of the following code?

x = 4

y = 5

print(x//y)

5.    Given the expression:

30-3**2+8//3**2*10

What is the outcome?

Apply parenthesis to the expression to achieve the same outcome.

6.    What will be the outcome of the following code:

my_var1 = 7.0

my_var2 = 5

print(my_var1%my_var2)

7.    Swap the values of two variables a and b, with a = 5 and b =10.

(i)   Swap using a third variable

(ii)  Swap without using a third variable

(iii) Swap using Python assignment way

Week 4 Tutorial: Program control – decisions and branching

This tutorial is on program and output control, in making decisions and branching.

1.    What is printed in the follow statements

times = 5

print ("## "*times)

2.    Assuming that x = 11, y = 6, z = 2, c = 'a', d = 'z', what are the values of the expressions in the following table:

Value of expression

x == 5 or y != 3

not (x > 9 and y != 23)

x <= 1 and y == 6 or z < 4

5 and y !=8 or 0

x >= y >= z

c >= 'A' or d >= 'Z'

c <= 'e' <= d

c <= 2 <= d

c == 'a' or 'e' or 'i' or 'o' or 'u'

d == 'a' or 'e' or 'i' or 'o' or 'u'

3.   The code below determines if a given number is small, medium, large or out of range. Correct the errors that exist. After the correction, what is printed if you input 7?

num1 = input ("Enter a positive integer: ")

if num1< 5

if num1 <= 0

print num1 ("is out of range")

else

print num1; (" is a small value.")

elseif num1 = 5

print num1; ("is a medium value. ")

else    #num1 > 5

print num1; ("This is a large value. ")

4.    What is the difference in the logic of the following two sets of code:

(i)    if BooleanA:

Suite A

if BooleanB:

Suite B

(ii)  if BooleanA: Suite A

elif BooleanB:

Suite B

5.    Write a program that prints the largest of three numbers.

Tutorial Week 5: Program control – loops

This tutorial is on program control, going round loops making repeated computations.

1.    Consider the function range(-4,11), which of these values are in the range: -5, -4, -3, 10, 11, 12?

2.    Write a program that prints the numbers 1 … 12 all in one line and then 13 … 24 in another line, all using only one for loop. Write two versions, with the numbers separated by (i) a space and (ii) a   comma.

3.    What are printed in Line 1 and Line 2?

i = 1

j = 0

while i < 10:

i += 1

j += 2*2

print (i)   # Line 1

print (j)   # Line 2

4.   Assume that X has agiven value, rewrite the following for loop as a while loop:

for i in range(1, X+1):

if X % i == 0:

print(i)

5.    Explain what this program does:

sum = 0

while True:

num = int(input("Enter a number:"))

if num <= 0:

break

else:

if num%2 == 0:

continue

else:

sum += num

6.   The following program has a for loop inside a while loop. What are printed?

for x in range(10):

y = x

while y < 7:

print(y, end = " ")

y += 2

if y > 5:

break

Week 6 Tutorial: Strings

This tutorial gets you familiar with strings and operations on strings.

You should discuss each tutorial in small groups, and then your tutor will ask some of you to discuss your solution in class. There are more questions here than can be covered in the limited class time. Attempt the first four questions in class within the first hour. You should attempt the rest at your own time, in order to get familiar with the subject matter.

1.    Given the string "Introduction to Computational Thinking", write an expression to i.    print the first character

ii.    print the last character, assuming you don’t know the string length

iii.    print the last character using len()

iv.    print the first word

v.    print the 4th  to 14th  characters.

vi.    print the 4th  to 14th  characters in the reverse order

vii.    print every alternate character starting from the second

2.    Study the Python methods associated with string, and write down the expressions for doing the

following, given the string "it's a beautiful day." (Not all functions have been covered in class.)

i.   Capitalize the first word ii.   Capitalize each word

iii.   Convert the whole string to upper case iv.    Remove all the spaces

v.   Count the number of a specific character in the string

3.    Write down the output of the following statements. Mark the spaces with a □ . for fahr in range (32, 200, 50):

cels = (fahr-32)*5/9

print("{:>4.0f} Fahrenheit = {:<6.2f} Celsius".format(fahr, cels))

4.    Given the assignment

S = "I am testing my program"

What is the outcome of the following statements? i.    S[0] = "J"

ii.   print(S)

iii.   print(S[::3])

iv.   print(S[12:4:-1])

5.    What are printed by the following expressions:

i.   print("Nanyang"*3)

ii.   print("Nanyang"*3.0) iii.   print("Nanyang"*-3)

iv.   print("Nanyang" + "Technological" + "University")

v.   print("Nanyang" - "N")

6.    Write down what is printed by the code below once you give the input (use your own input).

string = input("Enter a string: ")

y = 0

for c in string:

print(y, c)

y += 1

7.    Without using division, write one (yes, one only) statement that would store the last digit of an integer as an integer. For example, the statement should store the integer 5 given the integer 12345.

Week 7 Tutorial: Composite data types - List, Tuple and Dictionary

There are more questions here than can be discussed within one hour in class. Discuss the first five questions in class. Students are to attempt the rest of the questions on their own.

1.    i.  What are mutable and immutable types? Lists the types you know for each. ii. What are iterables? List the ones you know.

2.    Write Python code to create a list of the squares of all numbers between 1 to 20.

3.    Tuples are immutable. Yet, we can modify the tuple in the list L = [1, (2, 3), 4] to L = [1, (5, 6), 4]. Why?

4.    Create a dictionary that uses the integers 1 – 12 as keys for the months in words from January to December. Then print the months in words using the keys.

5.    Consider the following code:

list1 = [1, 2, 99] list2 = list1

list3 = list2

list1 = list1.remove(1)

print(list3)

i.     What is printed?

ii.    Modify the code so that list3 is unchanged.

6.    What will the following list return?

[ (n*n) for n in range(13) if (n%2==0)]

7.    What can be done with lists that cannot be done with strings? Name three valid operations for each.

8.    What are printed in the following code:

langs = ["Python", "C++", "Fortran", "Algol", "Java"]

print (langs)

del langs[1]

print (langs)

del langs[:]

print(langs)

9.    The following code attempts to convert the list  [1, 2, 3, 4] to the string  "1234", but it doesn’t work. Fix it.

L = [1, 2, 3, 4]

newString = "".join([a for a in L])

10. i.     Is it possible to have a tuple as a member of a list? Why? ii.    Is it possible to have a list as a member of a tuple? Why? iii.   What differentiates a tuple from a list?

Week 8 Tutorial: Functions

The first five questions are for discussion in class. Students should attempt the rest in their own time.

1.    List the parts in a function definition and explain the purpose of each part.

2.    Give three reasons why functions are useful.

3.    What does this function do?

def Func(num):

total = 0

while num > 0:

total = total + num*(num-1)

num = num -1

return total

4.    What is a function call? How do you call a function? Given the function in Q3, what are the values of x in the following statements?

i.       x = Func(5)

ii.       x = Func(5.5) iii.       x = Func('5') iv.       x = Func()

5.    What does the following code print? Explain.

number = 50

def Func(number):

print(number)

number=2

print(number)

Func(number)

print(number)

6.    Examine the following code and predict the outputs.

confusing = 100

def do_work(num):

confusing = -50

confusing += num

print("confusing in do_work is ", confusing)

return confusing

confusing = 100;

print("confusing in main is ", confusing)

confusing += do_work(confusing);

print("confusing in main is ", confusing)

confusing += do_work(confusing)

print("confusing in main is ", confusing)

confusing += do_work(confusing)

The same code is in the file confusing.c in the hands-on folder. Copy it into the working folder and run it. Are your predictions the same as the program output?

7.    A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. Otherwise, it is not a leap year. Write a function that takes in an integer value and returns true if the value represents a  leap year and false otherwise.

8.    Define a 2D vector as a 2-tuple. Write a function that takes two vectors v1 and v2 as inputs and returns their sum.

Week 9 Tutorial: More on Functions

Tutorial Questions:

1.    i.     Where in a program can a function be defined?

ii.    Can functions be defined inside a function?

iii.   What is an optional (also called default, named or keyword) parameter in a function and how is it created?

iv.   How many optional parameters can a function have?

v.    Can there be a mix of optional and non-optional parameters in a function parameter list?

2.    Given this function definition header

def func(p1, p2 = 0.0, p3 = "z", p4 = 100) which of these calls to the function are correct and which not?

i.    c = func(12, 7.2)

ii.    func(0, p3 = "abc") iii.    func(10)

iv.    func("xyz", "12.3", p4 = 5)

v.    func(10, p4 = 2, p3 = "alpha", p2 = 1.0)

vi.    func(p4 = 2, p3 = "alpha", p2 = 1.0, 10)

3.    What is printed in the following program? Explain.

def total(num):

sum += num

sum = 0

for i in range(1, 5):

total(i)

print(sum)

4.    What are printed in the following programs?

def output(message, times = 1):

print(message*times)

output ("Computational Thinking")

output ("Very easy", 3)

5.    What are printed in the following programs?

a = 1

b = 2

c = 3

d = 123.4

def add(a, b, c=4):

global d

a += b

b += 1

d += a

print(a, b, c)

return a+b+c

for i in range(1, 4):

a = add(i, b)

print(a, b, c)

print(d)

What ha ppens if the line “global d” is removed from the function add()?

Week 10 Tutorial: Files and exception handling

1.    Explain what is (i) a binary file and (ii) a text file. What are the key differences between them?

2.    Write a statement that opens a file called MyFile.txt for reading and assign it to a file object MyFile.

3.    Modify your solution to the above question to handle properly the situation when the file opening fails, by asking the user to enter the filename again, instead of crashing the program.

4.    Write a statement that closes the above file object. Why is explicitly closing a file necessary, instead of leaving it to close upon exiting from the program?

5.    Under what conditions can you fail to open a file for reading? Under what conditions can you fail to open a file for writing?

6.    Apart from open and close, list the different operations one may need to do with a file. Provide the corresponding Python function that performs each operation, with appropriate parameters where  necessary.

7.    Explain the programming logic associated with exception handling by answering these questions: a.    What is an exception and when do you encounter it?

b.    Explain the working principles behind the try … except … structure.

c.    How is an exception caught?

d.    What happens if there is no handler for a specific exception?


联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!