0% found this document useful (0 votes)
21 views

Python Week 3

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
0% found this document useful (0 votes)
21 views

Python Week 3

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/ 2

GrPA1

n = int(input())

# Initialize variables
sum = 0
term_sum = 0

# Loop through the terms and add them to the running total
for i in range(1, n+1):
term_sum += i
sum += term_sum

# Print the sum of the series


print(sum)

GrPA2

n=int(input())
for b in range (2,n+1):
# first check if f is a factor of n
if n%b==0:
#now check if b is prime
is_prime = True
for i in range(2,b):
if b%i==0:
is_prime = False
break
if is_prime:
print(b)
GrPA 3

# Define the starting point of the bot


x, y = 0, 0

# Accept the sequence of moves made by the bot as input


while True:
move = input()
if move == "STOP":
break

# Update the bot's position based on the move


if move == "UP":
y += 1
elif move == "DOWN":
y -= 1
elif move == "LEFT":
x -= 1
elif move == "RIGHT":
x += 1

# Calculate the Manhattan distance of the bot from the origin


distance = abs(x) + abs(y)

# Print the Manhattan distance of the bot from the origin


print(distance)

GrPA 4
string = input("")

# Convert the string to lowercase


string = string.lower()

# Sort the string in alphabetical order


sorted_string = ''.join(sorted(string))

# Print the sorted string to the console


print(sorted_string)

GrPA 5

n = int(input())

# Print the top half of the arrow


for i in range(1, n+1):
print(*range(1, i+1), sep=',')

# Print the bottom half of the arrow


for i in range(n-1, 0, -1):
print(*range(1, i+1), sep=',')

You might also like