0% found this document useful (0 votes)
5 views9 pages

Practice Set

The document contains a practice set of programming exercises that cover various topics such as string manipulation, number operations, pattern printing, and basic algorithms. Each exercise includes a brief description of the task and corresponding Python code to accomplish it. The tasks range from checking for palindromes to calculating factorials and filtering lists.

Uploaded by

rahulvenkat.465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Practice Set

The document contains a practice set of programming exercises that cover various topics such as string manipulation, number operations, pattern printing, and basic algorithms. Each exercise includes a brief description of the task and corresponding Python code to accomplish it. The tasks range from checking for palindromes to calculating factorials and filtering lists.

Uploaded by

rahulvenkat.465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

PRACTICE SET

1) Given a string S(input consisting) of ‘*’ and ‘#’.


The length of the string is variable.
The task is to find the minimum number of ‘*’ or ‘#’ to make it a valid string.
The string is considered valid if the number of ‘*’ and ‘#’ are equal.
The ‘*’ and ‘#’ can be at any position in the string.

Note : The output will be a positive or negative integer based on number of ‘*’ and ‘#’ in
the input string.
• (*>#): positive integer
• (#>*): negative integer
• (#=*): 0

Code:

s=input("Enter a string : ")


a=0
b=0
for i in s:
if i=='*':
a+=1
elif i=='#':
b+=1
print(a-b)
2) In a given string data,
separate the integer values and string values.
What is the sum of integer values from the given string.

Code:

s = input("Enter data : ")


num_res = []
text_res =[]
for i in s:
if(i.isdigit()):
num_res.append(int(i))
else:
text_res.append(i)
print(num_res)
print(sum(num_res))
print(text_res)
3) In a given list of numbers as input,
find the difference between the sum of odd and even position digits.

Code:
num = [int(d) for d in input("Enter the number:")]
even,odd = 0,0
for i in range(0,len(num)):
if i % 2 ==0:
even = even + num[i]
else:
odd = odd + num[i]
print(abs(odd-even))

4) Check whether given string is a plaindrome or not ?


s = input("Enter a number:")
rev = s[::-1]
if s == rev:
print("Palindrome")
else:
print("Not palindrome")
5) Check whether given number is a plaindrome or not ?

num=int(input("Enter a number:"))
temp=num
rev=0
while(num>0):
dig=num%10 # eg 121 ->121%10 ->1
rev=rev*10+dig # 0*10+1 -> 1
num=num//10 # 121//10 -> 12 ->12>0
if(temp==rev):
print("The number is palindrome!")
else:
print("Not a palindrome!")

6) Write a to Half pyramid pattern printing from 1 to number of rows given

rows = int(input("Enter number of rows : "))


for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=' ')
print('')
7) Write a program to print Prime number from 1 to 100 ?

a=1
b = 100
for n in range(a, b + 1):
if n > 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print(n)

8) Write a program to print Half Pyramid with '*'?

rows = 5
for i in range(0, rows):
# nested loop for each column
for j in range(0, i + 1):
# print star
print("*", end=' ')
# new line after each row
print("\r")
9) Write a program to print inverted half pyramid with '*'?

rows = 5
for i in range(rows + 1, 0, -1):
# nested reverse loop
for j in range(0, i - 1):
# display star
print("*", end=' ')
print(" ")

10) Write a program to print A to Z based on their ASCII code ?

ascii_number = 65
rows = 7
for i in range(0, rows):
for j in range(0, i + 1):
character = chr(ascii_number)
print(character, end=' ')
ascii_number += 1
print(" ")
11) Write a program to add number until zero is given.

number = int(input('Enter a number: '))


total = 0

while number != 0:
total += number
number = int(input('Enter a number: '))

print('total =', total)

12) Write a program to find factorial using while loop.

num = int(input("Enter a number: "))


factorial = 1
i=1
if num > 0:
while i <= num:
factorial = factorial * i
i=i+1
print("Factorial of ", num, " is ", factorial)
13) Write a program to print only consonants from the given string.

i=0
a = input("Enter Data : ")

while i < len(a):


if a[i] == 'a' or a[i] == 'e' or a[i] == 'i' or a[i] == 'o' or a[i] == 'u':
i += 1
continue

print('Current Letter :', a[i])


i += 1

14) write a program to find given number is Armstrong number or not?

num = int(input("Enter number : "))


res = 0
temp = num

if num>9:
while temp>0:
digit = temp%10
cube = digit**3
res = res+cube
temp = temp//10
if res == num:
print("Armstrong number")
else:
print("Not Armstrong")
else:
print("Value has to be greater than 9")

15) Write a program to delete the values which are greater than 60 from the given data.
number_list = [10,80,90, 20,50,60, 30, 40, 100]
i=0
# get list's size
n = len(number_list)
# iterate list till i is smaller than n
while i < n:
# check if number is greater than 50
if number_list[i] > 60:
# delete current index from list
del number_list[i]
# reduce the list size
n=n-1
else:
# move to next item
i=i+1
number_list.sort()
print(number_list)

You might also like