0% found this document useful (0 votes)
183 views8 pages

Assignment 1 Burhanuddin Sikandar Roll No: - 1511: Not Legal, Reserved Word

The document contains a series of programming exercises related to Python variables, data types, conditionals, loops, and functions. The exercises cover topics like valid variable names, determining data types, drawing diagrams to illustrate sentence meanings, evaluating Boolean expressions, predicting output from conditional statements and loops, and identifying errors in code. Sample answers and explanations are provided for many of the exercises.

Uploaded by

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

Assignment 1 Burhanuddin Sikandar Roll No: - 1511: Not Legal, Reserved Word

The document contains a series of programming exercises related to Python variables, data types, conditionals, loops, and functions. The exercises cover topics like valid variable names, determining data types, drawing diagrams to illustrate sentence meanings, evaluating Boolean expressions, predicting output from conditional statements and loops, and identifying errors in code. Sample answers and explanations are provided for many of the exercises.

Uploaded by

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

Assignment 1

BURHANUDDIN SIKANDAR
Roll no: - 1511

Exercise 1.9 – Variable Names

The Python interpreter has strict rules for variable names. Which of the following are legal Python
names? If the name is not legal, state the reason.

1. and not legal, reserved word

2. and legal

3. var legal

4. var1 legal

5. 1var not legal

6. my-name not legal, canot assign operator in variable name

7. your name legal

8. COLOR legal

Exercise 1.10 – Types

It is important that we know the type of the values stored in a variable so that we can use the correct
operators (as we have already seen!). Python automatically infers the type from the value you assign to
the variable. Write down the type of the values stored in each of the variables below. Pay special
attention to punctuation: values are not always the type they seem!

1. a = False Boolean
2. b = 3.7 float

3. c = ’Alex’ string

4. d = 7 int

5. e = ’True’ string

6. f = 17 int

7. g = ’17’ string

8. h = True boolean

9. i = ’3.14159’ string

To verify your answers, you can use the interactive Python shell, but first try to do the exercise without
help.

>>> x = 100
>>> type(x)
<type ’int’>
>>>

2
Exercise 1.11 – Natural Language Processing

Consider the following sentence:


Alice saw the boy on the hill with the telescope.

1. Draw a sketch of what’s described in this sentence.

2. Draw a different sketch that could also be described by this sentence.

3. Write the sentence in two different ways, that clarifies the meaning of each of your sketches, next
to your above sketches (hint: rewrite the sentence using extra words, commas, etc).

Alice saw the boy who was up on the hill with his telescope.

Alice, using her telescope saw the boy on the hill. 

4. The ambiguity illustrated by this sentence is known as “prepositional phrase attachment.” Think
about this as you continue to learn how to program, and consider how programming languages are
designed to avoid the ambiguity illustrated by this example!

Did Alice have the telescope or did the boy have the telescope. Was Alice on the hill or
was the boy she saw on the hill. 

Exercise 1.12 – Boolean operators


Boolean operators can seem tricky at first, and it takes practice to evaluate them correctly. Write the
value (True or False) produced by each expession below, using the assigned values of the variables a,
b, and c. Try to do this without using your interpreter, but you should check yourself when you think
you’ve got it. Hint: Work from the inside out, starting with the inner-most expressions, like in
arithmetic.

a = False
b = True
c = False

1. b and c false
2. b or c true
3. not a and b false
4. (a and b) or not c  true
5. not b and not (a or c) false

Exercise 1.13 – Conditionals

The purpose of this exercise is to understand conditionals. Tiberius is looking for his dream job, but has
some restrictions. He loves California and would take a job there if it paid over 40,000 a year. He hates
Massachusetts and demands at least 100,000 to work there. Any other place he’s content to work for
60,000 a year, unless he can work in space in which case he would work for free. The following code
shows his basic strategy for evaluating a job offer.

pay = _____
location = _____

if location == "U.S.S. Enterprise":"


print "So long, suckers! I’ll take it!"
elif location == "Massachusetts":
if pay < 100000:
print "No way"
else:
print "I’ll take it!"
elif location == "California" and pay > 40000:
print "I’ll take it!"
elif pay > 60000:
print "I’ll take it!"
else:
print "No thanks, I can find something better."

For each of the following job offers, write down the output that would be generated. Do this without
running the code. It is an important skill to be able to understand what a piece of code does without
running it.
1. location = "Massachusetts"
pay = 50000
>> No way

2. location = "Iowa"
pay = 50000
>> No thanks, I can find something better

3. location = "California"
pay = 50000
>> I'll take it!

4. location = "U.S.S. Enterprise"


pay = 1
>> So long suckers! I'll take it!

5. location = "California"
pay = 25000
>> No thanks, I can find something better

4
Exercise 1.14 – Understanding loops

For each of the following fragments of code, write what the output would be. Again, do this without
running the code (although feel free to check yourself when you’re done).

1. num = 10
while num > 3:
print num
num = num - 1
>> 10
>> 9
>> 8
>> 7
>> 6
>> 5
>> 4

2. divisor = 2
for i in range(0, 10, 2):
print i/divisor
>> 

3. num = 10
while True:
if num < 7:
break
print num
num -= 1

>> 10
>> 9
>> 8
>> 7

4. count = 0
for letter in ’Snow!’:
print ’Letter #’, count, ’is’, letter
count += 1

>> 

5
Exercise 1.15 – Buggy loop (aka Find The Bug!)

Consider the following program that Ben Bitdiddle handed in to the course staff (again, try to do this
exercise without running the code in IDLE!):
n = 10
i = 10

while i > 0:
print i
if i % 2 == 0:
i=i/2
else:
i=i+1

What do you think this code is doing? Without comments it is hard to guess what Ben’s intention was
(*cough* this is why the staff loves to look at commented code!!), so read through it and make a
sensible guess as to what it is doing. There’s a lot of mistakes in the code so your guess is as good as
ours!

1. Draw a table that shows the value of the variables n and i during the execution of the program.
Your table should contain two columns (one for each variable) and one row for each iteration. For
each row in the table, write down the values of the variables as they would be at the line
containing the print statement.

2. Ben made a lot of mistakes. State what you think Ben was trying to do and suggest one or more
ways he could fix his code (there’s a few good answers for this depending on what you think the
code should be doing).

Ben Bitdiddle is trying to compute the function fla, b) = 2a + 3b for nonnegative b. He goes
overboard in the use of function calls and recursion and produces the following high-level code
for functions f and g. // high-level code for functions f and g int flint a, int b) int: j = a; return j + a
+ g(b): ) int g(int x) int k: k = 3; if (x == 0) return 0; else return k + g(x-1): Ben then translates the
two functions into assembly language as follows. He also writes a function, test, that calls the
function f(5. 3). TSLINK Exercises 37 : ARM assembly code ;f: RO = a, R1 = b. R4 = 3: :9: RO =
x, R4 -k : a = 5 : b=3 : call f(5, 3) : and loop forever ; save registers on stack 0x00008000 test
MOV RO. #5 Ox00008004 MOV R1.43 0x00008008 BL Ox0000800C loop B 1оор
0x000080107 PUSH (R1,RO, LR, R4) 0x00008014 MOV R4, RO Ox00008018 MOV RO, R1
0x0000801C BL 9 Ox00008020 MOV R2, RO 0x00008024 POP R1,RO) Ox00008028 ADD RO,
R2, RO 0x0000802C ADD RO, RO, R4 Ox00008030 POP R4,LR) Ox00008034 MOY PC, LR
Ox00008038 g PUSH (R4.LR) Ox0000803C MOV R4,83 Ox00008040 CMPRO. #0
Ox00008044 BNE else 0x00008048 MOY RO, 0 Ox0000804C B done Ox00008050 else SUB
RO, RO, #1 Ox00008054 BL g Ox00008058 ADD RO, RO, R4 0x0000805C done POP (R4.LR)
0x00008060 MOY PC, LR :place bas argument forg ; call g(b) :place return value in R2 : restore
a and bafter call ; R0 = g(b) + a : RO = (g(b) + a) + ] : restore R4, LR : return : Save registers on
stack ; k= 3 ; X == 0? : branch when not equal ; if equal, return value = 0 : and clean up ; X = X-1
: call g(x - 1) : RO = g(x - 1) +k : restore RO, R4.LR from stack : return You will probably find it
useful to make drawings of the stack similar to the one in Figure 6.14 to help you answer the
following questions. (a) If the code runs starting at test, what value is in RO when the program
gets to loop? Does his program correctly compute 2a + 3b? (b) Suppose Ben changes the
instructions at addresses 0x00008010 and 0x00008030 to PUSH {R1, RO, R4} and POP (R4),
respectively. Will the program (1) enter an infinite loop but not crash; (2) crash (cause the stack to
grow beyond the dynamic data segment or the PC to jump to a location outside the program); (3)
produce an incorrect value in RO when the program returns to loop (if so, what value?), or (4) run
correctly despite the deleted lines? TS LINK F8 CHAPTER SIX Architecture (c) Repeat part (b)
when the following instructions are changed. Note that labels aren't changed, only instructions. (i)
instructions at 0x00008010 and 0x00008024 change to PUSH {R1, LR, R4} and POP (R1),
respectively. (ii) instructions at 0x00008010 and 0x00008024 change to PUSH {RO, LR, R4) and
POP {RO), respectively. (iii) instructions at 0x00008010 and 0x00008030 change to PUSH
{R1,RO, LR) and POP {LR), respectively. (iv) instructions at 0x00008010, 0x00008024, and
0x00008030 are deleted. (v) instructions at 0x00008038 and 0x0000805C change to PUSH (R4)
and POP (R4), respectively. (vi) instructions at 0x00008038 and 0x0000805C change to PUSH
{LR) and POP (LR), respectively. (vii) instructions at 0x00008038 and 0x0000805C are deleted.

You might also like