Skip to content

Commit 37253f7

Browse files
committed
added solution for Day7
1 parent 354799e commit 37253f7

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

solution20.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Question:
3+
Define a class with a generator which can iterate the numbers, which are divisible by 7, between a given range 0 and n.
4+
5+
Hints:
6+
Consider use class, function and comprehension.
7+
8+
'''
9+
10+
11+
class Generator:
12+
13+
def divisibleBySeven(self, n):
14+
for i in range(n + 1):
15+
if i % 7 == 0:
16+
yield i
17+
18+
19+
generator = Generator()
20+
for i in generator.divisibleBySeven(int(input("please provide a number: "))):
21+
print(i)

solution21.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
'''
2+
Question:
3+
A robot moves in a plane starting from the original point (0,0). The robot can move toward UP, DOWN, LEFT and RIGHT with a given steps. The trace of robot movement is shown as the following:
4+
5+
UP 5
6+
DOWN 3
7+
LEFT 3
8+
RIGHT 2
9+
The numbers after the direction are steps. Please write a program to compute the distance from current position after a sequence of movement and original point. If the distance is a float, then just print the nearest integer. Example: If the following tuples are given as input to the program:
10+
11+
UP 5
12+
DOWN 3
13+
LEFT 3
14+
RIGHT 2
15+
Then, the output of the program should be:
16+
17+
2
18+
Hints:
19+
In case of input data being supplied to the question, it should be assumed to be a console input.Here distance indicates to euclidean distance.Import math module to use sqrt function.
20+
21+
'''
22+
import math
23+
24+
x, y = 0, 0
25+
26+
while True:
27+
command = input("Give the command: \n").split()
28+
if not command:
29+
break
30+
direction, steps = command[0], int(command[1])
31+
if direction == 'UP':
32+
y += steps
33+
if direction == 'DOWN':
34+
y -= steps
35+
if direction == 'RIGHT':
36+
x += steps
37+
if direction == 'LEFT':
38+
x -= steps
39+
40+
distance = round(math.sqrt(x**2 + y**2))
41+
print(distance)

0 commit comments

Comments
 (0)