Worksheet 1 For Python 2024 - Answers
Worksheet 1 For Python 2024 - Answers
**
***
****
Answer:
for i in range(5):
print '*'*i
*****
****
***
**
Answer:
for i in range(5,0,-1):
print '*'*i
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.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.turn_left()
hubo.move()
for i in range(3):
hubo.move() hubo.move()
hubo.move()
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
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)
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
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 *
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()
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.
hubo.set_trace("red")
hubo.set_pause (0.3)
hubo.move()
hubo.move()
hubo.move()
hubo.turn_left()
create_world()
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]
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()