100% found this document useful (1 vote)
2K views3 pages

Grpa Week 3 Solutions

The document contains 6 programming problems and their solutions. The problems include calculating a series sum, finding prime factors, tracking a bot's movement, sorting a string, validating a phone number, and printing a number arrow pattern. The solutions provide code to solve each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
2K views3 pages

Grpa Week 3 Solutions

The document contains 6 programming problems and their solutions. The problems include calculating a series sum, finding prime factors, tracking a bot's movement, sorting a string, validating a phone number, and printing a number arrow pattern. The solutions provide code to solve each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

WEEK 3

PROGRAM 1:

Accept a positive integer nn as input and print the sum of the first nn terms of
the series given below:

1+ (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) + \cdots1+(1+2)+(1+2+3)+(1+2+3+4)+⋯
Just to be clear, the first term in the series is 11, the second term is (1 + 2)
(1+2) and so on.

SOLUTION 1:

n=int(input())
sum=0
sam=0
for x in range (n+1):
sum=0
for y in range(x+1):
sam=sam+y
sum=sum+sam
print(sum)

PROGRAM 2:

Accept a positive integer nn, with n > 1n>1, as input from the user and print all
the prime factors of nn in ascending order.

SOLUTION 2:

n=int(input())
k,f=0,0
for i in range(2,n+1):
f=0
if n%i==0:
for k in range (1,i+1):
if i%k==0:
f+=1
if f==2:
print(i)

PROGRAM 3:

A bot starts at the origin — (0, 0)(0,0) — and can make the following moves:

(1) UP
(2) DOWN
(3) LEFT
(4) RIGHT
Each move has a magnitude of 1 unit. Accept the sequence of moves made by the bot
as input. The first entry in the sequence is always START while the last entry in
the sequence is always STOP.

SOLUTION 3:

step=input()
x,y=0,0
while step!="STOP":
if step=="UP":
y=y+1
elif step=="DOWN":
y=y-1
elif step=="RIGHT":
x=x+1
elif step=="LEFT":
x=x-1
step=input()
dis=abs(x)+abs(y)
print(dis)

PROGRAM 4:

Accept a string as input, convert it to lower case, sort the string in alphabetical
order, and print the sorted string to the console. You can assume that the string
will only contain letters

SOLUTION 4:

str=input()
str=str.lower()
s="".join(sorted(str))
print(s)

PROGRAM 5:

Accept a phone number as input. A valid phone number should satisfy the following
constraints.
(1) The number should start with one of these digits: 6, 7, 8, 9
(2) The number should be exactly 10 digits long.
(3) No digit should appear more than 7 times in the number.
(4) No digit should appear more than 5 times in a row in the number.

SOLUTION 5:

n=input()
fd=0
if len(n)!=10:
fd=1
if(n[0]!="6" and n[0]!="7" and n[0]!="8" and n[0]!="9"):
fd=2
if fd==0:
for i in range(10):
if n.count(n[i])>7:
fd=3
if fd==0:
for j in range(6):
for k in range (6):
if n[j]==n[k]==n[k+1]==n[k+2]==n[k+3]==n[k+4]==n[k+5]:
fd=4
if fd==0:
print('valid')
else:
print('invalid')

PROGRAM 6:

Accept a positive integer nn as input and print a "number arrow" of size nn. For
example, n = 5n=5 should produce the following output:

1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
1,2,3,4
1,2,3
1,2
1

SOLUTION 6:

n=int(input())
for i in range (1,n):
for j in range(1,i):
print(str(j)+",",end="")
print(i)
for i in range(n,0,-1):
for j in range(1,i):
print(str(j)+",",end="")
print(i)

You might also like