Worksheet -1
Practice the following questions
1. Write a for loop to produce the following output:
**
***
****
*****
2. Write a python program to produce the following output:
*****
****
***
**
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)
[Link]()
[Link]()
[Link]()
hubo.turn_left()
[Link]()
[Link]()
[Link]()
hubo.turn_left()
hubo.turn_left()
hubo.turn_left()
[Link]()
[Link]()
[Link]()
hubo.turn_left()
hubo.turn_left()
hubo.turn_left()
[Link]()
[Link]()
[Link]()
hubo.turn_left()
[Link]()
[Link]()
[Link]()
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
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)
6. Write a program that makes the robot to climb and collect all beepers and
carry and keep them at the positionof (10, 5)
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.
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.
9. Write the output of the following code
from cs1robots import *
create_world()
hubo=Robot (avenue=7, street=6, orientation="W")
hubo.set_trace("red")
hubo.set_pause (0.3)
for i in range(3):
[Link]()
[Link]()
[Link]()
hubo.turn_left()
10. Write a python program for the following output
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 [Link]
robot is standing on the world file “[Link]” 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]