100% found this document useful (1 vote)
107 views11 pages

Worksheet 1 For Python 2024 - Answers

Uploaded by

yonathanhailu8
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
100% found this document useful (1 vote)
107 views11 pages

Worksheet 1 For Python 2024 - Answers

Uploaded by

yonathanhailu8
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/ 11

Worksheet -1

Practice the following questions


1. Write a for loop to produce the following output:

**

***

****

Answer:

for i in range(5):

print '*'*i

2. Write a python program to produce the following output:

*****

****

***

**

Answer:

for i in range(5,0,-1):

print '*'*i

3. Optimize the following python program code, by modifying it using possible


programming elements.

from cs1robots import *

create_world()

hubo=Robot()

hubo.set_trace("blue")

hubo.set_pause(0.2)

hubo.move()
hubo.move()

hubo.move()

hubo.turn_left()

hubo.move()

hubo.move()

hubo.move()

hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

hubo.move()

hubo.move()

hubo.move()

hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

hubo.move()

hubo.move()

hubo.move()

hubo.turn_left()

hubo.move()

hubo.move()

hubo.move()

Answer:

Code optimization is all about minimize the line code to increase code readability. Considering
patterns in the code we can minimize code lines by using loops and functions.

In the following: codes in red are repeating with the same patterns , in blue has the same pattern
and green the same pattern.
from cs1robots import *

create_world()

hubo=Robot()

hubo.set_trace("blue") for i in range(3):


for i in range(3):
hubo.move()
hubo.set_pause(0.2) hubo.move()
hubo.turn_left()
hubo.turn_left()
hubo.move()
change to function:
change to function:
hubo.move() def move_three_times_turn_left():
def move_three_times_turnright():
hubo.move() for i in range(3):
for i in range(3):
hubo.move()
hubo.turn_left() hubo.move()
hubo.turn_left()
for i in range(3):
hubo.turn_left()

hubo.move() for i in range(3):


hubo.move() hubo.move()

hubo.move() for i in range(3):


hubo.turn_left() Pattern repeats twice. So using a
hubo.turn_left()
function
hubo.turn_left()
for i in range(2):
hubo.turn_left()
move_three_times_turnright()

hubo.move()
for i in range(3):
hubo.move() hubo.move()
hubo.move() for i in range(3):
hubo.turn_left()
hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

hubo.move() for i in range(3):


hubo.move() hubo.move()
hubo.turn_left()
hubo.move()

hubo.turn_left()
hubo.move()
for i in range(3):
hubo.move() hubo.move()

hubo.move()

Final answer: Put together all:

from cs1robots import *


create_world()
hubo=Robot()
hubo.set_trace("blue")
hubo.set_pause(0.2)
move_three_times_turn_left()
for i in range(2):
move_three_times_turnright()
move_three_times_turn_left()
for i in range(3):
hubo.move()

4. def calculate(a, b, c, d):

x=a%b+9*c/4-d

return x

x=calculate (2, 4, 2, 9)

print x

x=calculate (10, 4, 3, 5)

print x

Answer: Using operator precedence

X= calculate (2, 4, 2, 9) will map 2 to a, 4 to b, 2 to c , 9 to d

Then the expression will look like as follows:


x=2%4+9*2/4-9 According to operator precedence %, *,/ have the same precedence so evaluate
from left to right.

X= 2+18/4-9= 2+4-9 again + ,- have same precedence evaluate from left to right

X=6-9=-3
Do the same for x=calculate (10, 4, 3, 5) which is 3

5. Consider the Pythagorean Theorem to find the distance between two points. A
user enters the coordinates (x1, y1) and (x2, y2) as input point data from the
keyboard. Compute the distance between two points and print out the distance,
where, distance=√ ( x 2−x 1 ) +( y 2− y 1)

from math import * # to use built-in functions sqrt() and pow()

def compute_distance(a,b):
x1,y1=a # a and b are tuples as they take two comma values enclosed in parenthesis

x2,y2=b

distance=sqrt(pow((x2-x1),2)+pow((y2-y1),2))

return distance

#Accept value from key board to call a function

A=input(“please enter coordinate point enclosed in parenthesis ”)

B=input(“please enter coordinate point enclosed in parenthesis ”)

print compute_distance(A,B)

6. Write a program that makes the robot to climb and collect all beepers and
carry and keep them at the positionof (10, 5)
from cs1robots import *

load_world(“name of the above world.wld”)

hubo=Robot()

def pick_all_beepers():

while hubo.on_beeper():
hubo.pick_beeper()

def turn_right():
for i in range(3):

hubo.turn_right()

def climb_one_stair():

hubo.move()

pick_all_beepers():

hubo.turn_left()

hubo.move()

hubo.move()

pick_all_beepers():

turn_right()

def move_up_stairs_four_times():
for i in range(4):
climb_one_stair():

move_up_stairs_four_times()
while hubo.carries_beepers():
hubo.drop_beeper()

7. Define a function by the name area_circle which accepts radius of


Circle as arguments from the caller and return area of circle to the caller
and display it. Read radius of the circle from the keyboard and
import pi from module called math.

pi =3.14
def area_circle(radius):
return pi*radius*radius
radius=input(“please enter the radius of a circle:”)
print area_circle(radius)
8. Define a function by the name area_triengle which accepts height and
width of triangle as arguments from the caller and return area of triangle
to the caller and display it. Read height and width of the triangle
from the keyboard.

def area_triangle(width, height):


return 0.5*width*height
width=input(“please enter the width of a triangle:”)
height=input(“please enter the height of a triangle:”)
print area_triangle(width, height)

9. Write the output of the following code

from cs1robots import *


Initial robots
create_world() resides at
hubo=Robot (avenue=7, street=6, orientation="W")

hubo.set_trace("red")

hubo.set_pause (0.3)

for i in range(3): # moves three times to the west…use arrow

hubo.move()
hubo.move()

hubo.move()

hubo.turn_left()

10. Write a python program for the following output

from cs1robots import *

create_world()

hubo=Robot (avenue=9, street=8, orientation="W")

hubo.set_trace("red")

hubo.set_pause (0.3)

for i in range(5):
hubo.move()

hubo.turn_left()
for i in range(4):
hubo.move()

hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

11. Write a python program to help the robot make the least possible
moves in order to reach in to the beeper as shown in the figure at the right.the
robot is standing on the world file “test1.wld” with its face looking to the North
direction. [Hint: Among the possible paths you can follow to reach in to the
beeper, find the one with least number of moves]

from cs1robots import *

create_world()

hubo=Robot(beepers=5 )

for i in range(4):

hubo.move()

hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

for i in range(4):

hubo.move()

hubo.turn_left()
hubo.move()

hubo.turn_left()

hubo.move()

hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

hubo.move()

hubo.turn_left()

hubo.move()

hubo.turn_left()

hubo.turn_left()

hubo.turn_left()

hubo.move()

hubo.turn_left()

hubo.move()

hubo.move()

hubo.turn_left()

hubo.move()

hubo.drop_beeper()

You might also like