0% found this document useful (0 votes)
15 views28 pages

All Sheets Solutions

The document contains multiple solution sheets focused on programming concepts including conditional programming, control statements (loops), pattern printing, list operations, and string manipulations. Each section provides code snippets that demonstrate various programming tasks such as checking divisibility, finding maximum and minimum values, generating patterns, and manipulating lists and strings. The solutions are structured as practical examples for users to understand and implement in their own coding practices.

Uploaded by

haric2903
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)
15 views28 pages

All Sheets Solutions

The document contains multiple solution sheets focused on programming concepts including conditional programming, control statements (loops), pattern printing, list operations, and string manipulations. Each section provides code snippets that demonstrate various programming tasks such as checking divisibility, finding maximum and minimum values, generating patterns, and manipulating lists and strings. The solutions are structured as practical examples for users to understand and implement in their own coding practices.

Uploaded by

haric2903
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/ 28

SOLUTION SHEET 2 - Conditional Programming

1.

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

if a % 7 ==0 :

print("It is divisible.")

else:

print("It is not divisible.")

2.

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

if a % 10 ==4 :

print("It is true.")

else:

print("It is false.")

3.

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

if a % 10 ==4 and a % 3 ==0:

print("It is true.")

else:

print("It is false.")

4.

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

if a % 10 ==7 or a % 5 ==0:

print("It is true.")

else:

print("It is false.")

5.
a = int(input("Enter your number: "))

if a % 5 == 0 and a % 11 ==0:

print("It is true.")

else:

print("It is false.")

6.

a = int(input("Enter num1: "))

b = int(input("Enter num2: "))

c = int(input("Enter num3: "))

if a > b and a > c:

print("a is maximum")

elif b > c:

print("b is maximum")

else:

print("c is maximum")

7.

a = int(input("Enter angle1: "))

b = int(input("Enter angle2: "))

c = int(input("Enter angle3: "))

if a + b == c or a + c == b or b +c == a:

print("It is an obtuse triangle")

elif a == 90 or b == 90 or c == 90:

print("It is a right agnle triangle")

elif a == b ==c == 60:

print("It is an equilateral triangle")

else:

print("It is an acute triangle")

8.
age = int(input("Enter your age: "))

if age>=18:

print("You're eligible to vote.")

else:

print("You're not eligible to vote.")

9.

year = int(input("Enter the year: "))

if year % 400 == 0 or year % 100 != 0 and year % 4 == 0:

print("It is a leap Year")

else:

print("It is not leap year")

10.

day = int(input("Enter your number (1-7): "))

if day == 1:

print("Monday")

elif day == 2:

print("Tuesday")

elif day == 3:

print("Wednesday")

elif day == 4:

print("Thursday")

elif day == 5:

print("Friday")

elif day == 6:

print("Saturday")

elif day == 7:

print("Sunday")

else:

print("Wrong value")
11.

A,B,C,D,E = int(input("Enter num1: ")),int(input("Enter num2: ")), int(input("Enter num3: ")),


int(input("Enter num4: ")), int(input("Enter num5: ")),

print("Your average is: ")

print((A+B+C+D+E)/5)

12.

A,B,C = int(input("Enter angle1: ")), int(input("Enter angle2: ")), int(input("Enter angle3: "))

if A + B + C == 180:

print("Valid triangle")

else:

print("Triangle invalid")

13.

a = int(input("Enter num1: "))

b = int(input("Enter num2: "))

if a > b:

print("a is maximum")

else:

print("b is maximum")

14.

a = int(input("Enter num1: "))

b = int(input("Enter num2: "))

c = int(input("Enter num3: "))

if a < b and a < c:

print("a is minimum")

elif b < c:

print("b is minimum")

else:
print("c is minimum")

15.

percent = int(input("Enter your percentage: "))

if percent < 25 and percent > 0:

print("D grade")

elif percent > 25 and percent < 45:

print("C grade")

elif percent > 45 and percent < 65:

print("B grade")

elif percent > 65 and percent < 85:

print("A grade")

elif percent > 85 and percent < 100:

print("A+ grade")

else:

print("Invalid percentage")
SOLUTION SHEET 3 - Control Statement (Loops)

1.

n = int(input("Enter the num: "))

i=1

while i<=n:

print(i, end=" ")

i+=1

2.

n = int(input("Enter the num: "))

i=1

while i<=n:

print(n, end=" ")

n-=1

3.

n = int(input("Enter the num: "))

i=1

while i<=n:

if i %2 ==0:

print(i)

i+=1

4.

n = int(input("Enter the num: "))

i=1

while i<=n:

if i %2 !=0:

print(i, end=" ")

i+=1
5.

n = int(input("Enter the num: "))

i=1

numSum = 0

while i<=n:

numSum=numSum+i

i+=1

print(numSum)

6.

A = int(input("Enter the num: "))

i=1

numSum = 0

while i<=A:

if i % 2 == 0:

numSum=numSum+i

i+=1

print(numSum)

7.

A = int(input("Enter the num: "))

i=1

numSum = 0

while i<=A:

if i %2 != 0:

numSum=numSum+i

i+=1

print(numSum)
8.

N = int(input("Enter the num: "))

i=1

times = 1

while i == 1:

if N // 10 !=0:

N = N // 10

times+=1

else:

i=0

print(times)

9.

N = int(input("Enter the num: "))

numSum = 0

while N > 0:

digit = N % 10

numSum = numSum + digit

N = N // 10

print(numSum)

10.

A = int(input("Enter the num: "))

original_A = A

reversed_A = 0

while A > 0:

digit = A % 10

reversed_A = reversed_A * 10 + digit

A = A // 10

if original_A == reversed_A:

print("Yes")
else:

print("No")

11.

A = int(input("Enter the num: "))

for i in range(10):

table = (i+1)*A

print(A,"*", i + 1, "=", table)

i+=1

12.

A = int(input("Enter the num1: "))

B = int(input("Enter the num2: "))

power = 1

for i in range(B):

power = power * A

print(power)
SOLUTION SHEET 4 - PATTERN PRINTING
1.

for i in range(4):

for j in range(5):

print("*", end="")

print()

2.

for i in range(5):

for j in range(i+1):

print("*", end="")

print()

3.

for i in range(5,0, -1):

for j in range(i):

print("*", end="")

print()

4.

for i in range(5):

for j in range(i +1):

if j % 2 == 0:

print(j+1, end="")

else:

print("*", end="")

print()

5.

N=5
for i in range(4):

for j in range(N):

if j == 0 or j == N-1:

print("*", end="")

else:

print("_", end="")

print()

6.

for i in range(6,1, -1):

N = i+1

for j in range(N):

if j ==0 or j == N-1:

print("*", end="")

else:

print("_", end="")

print()

7.

for i in range(5,0,-1):

for j in range(5):

if i <= j+1:

print("*", end="")

else:

print("_", end="")

print()

8.

for i in range(0,5):

for j in range(0,5):

if i <= j:
print("*", end="")

else:

print("_", end="")

print()

9.

rows = int(input("rows: "))

columns = (rows * 2)

value= rows - 1

increment = rows

for i in range(rows):

for j in range(columns):

if i + j > value and i + j < increment:

print(" ", end="")

else:

print("*", end="")

increment+=2

print('')

10.

rows = 5

columns = 10

increment = 1

decrement = columns

for i in range(rows):

for j in range(columns):

if i + j < increment or i + j + 1 > decrement:

print("*", end="")

else:

print(" ", end="")


increment += 2

print('\n')

11.

rows = 5

columns = 9

increment = 5

decrement = 1

for i in range(0, rows):

for j in range(0, columns+1):

if i + j > 4 and i + j < increment:

print(" ", end="")

else:

print("*", end="")

increment+=2

print()

r=5

c=9

inc = 1

dec = c

for i in range(0, r):

for j in range(0, c+1):

if i + j < inc or i + j + 1 > dec:

print("*", end="")

else:

print(" ", end="")

inc+=2

print()

12.

for i in range(4):
for j in range(i+1):

print(j+1, end="")

print()

13.

for i in range(4,0,-1):

for j in range(i):

print(j+1, end="")

print()

14.

N=1

for i in range(1,5):

for j in range(0, i):

print(N, end="")

N+=1

print()

15.

for i in range(5):

for j in range(i+1):

print("*", end="")

print()

for m in range(6,0, -1):

for n in range(m):

print("*", end="")

print()
SOLUTION SHEET 5 - LIST
1.

arr = list(map(int, input().split()))

arrSum = 0

for i in range(len(arr)):

arrSum += arr[i]

print(arrSum)

2.

A = list(map(int, input().split()))

B=3

arr = []

for i in range(len(A)):

arr.append((A[i] + B))

print(arr)

3.

arr = list(map(int, input().split()))

maxVal = arr[0]

minVal = arr[0]

for i in range(1, len(arr)):

if arr[i] > maxVal:

maxVal = arr[i]

if arr[i] < minVal:

minVal = arr[i]

print(maxVal, minVal)

4.

arr = list(map(int, input().split()))

target = 3
for i in range(len(arr)):

if arr[i] == target:

print("Value found at index: ", end="")

print(i)

else:

print("Not found")

5.

arr = list(map(int, input().split()))

for i in range(len(arr)):

if arr[i]<0:

print(arr[i])

6.

A = list(map(int, input().split()))

even, odd = 0, 0

for num in A:

if num % 2 == 0:

even += 1

else:

odd += 1

absolute = abs(even - odd)

print(absolute)

7.

arr = list(map(int, input().split()))

even = []

odd = []

for i in range(len(arr)):

if arr[i] % 2 == 0:

even.append(arr[i])
else:

odd.append(arr[i])

print(even)

print(odd)

8.

arr = list(map(int, input().split()))

newArr= []

for i in range(len(arr)):

newArr.append(arr[i]**2)

print(newArr)

9.

arr = list(map(int, input().split()))

newArr= []

for i in range(len(arr)):

newArr.append(arr[i]**3)

print(newArr)

10.

arr = [1,2,3,4,5]

N = len(arr)

for i in range(N//2):

temp = arr[i]

arr[i] = arr[N-i-1]

arr[N-i-1] = temp

print(arr)

11.

arr = [1,2,3,4,5]

N = len(arr)
for i in range(N//2):

temp = arr[i]

arr[i] = arr[N-i-1]

arr[N-i-1] = temp

print(arr)

12.

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

[6, 8, 10]

[6, 8, 10, 12, 14, 16, 18, 20]

[6, 8, 10, 12, 14, 16, 18, 20]

[2, 4]

[2, 6, 10, 14, 18]

[4, 8, 12, 16, 20]

[6, 10, 14, 18]

13.

11

[19, 17, 15]

[19, 21]

[19, 21]

[1, 3, 5, 7, 9, 11, 13, 15, 17]

[21, 17, 13, 9, 5, 1]

[21, 19, 17, 15, 13, 11, 9, 7, 5, 3, 1]

14.

['Nikhil', 5, 4, 3, 2, 1.4]

15.

['Hello', 'everyone', 'how', 'are', 'you']


['Hello', 'everyone', 'how are you']

['Nikhil', 'Chauhan', 'Delhi']

['23456']

['2', '3', '4', '5']

16.

[1, 2, 3, 5, 8, 9, 3, 4, 5, 6, 7, 10]

[1, 2, 3, 5, 8, 9, 1, 2, 3, 5, 8, 9, 1, 2, 3, 5, 8, 9]

17 (Miscellaneous).

arr = [2,2,3,3,4,4,5,5,6,6,7,7,9,8,8]

N = len(arr)

value = 0

for i in range(N):

target = arr[i]

count = 0

for j in range(N):

if target == arr[j]:

count+=1

if count == 1:

value = arr[i]

print(value)
SOLUTION SHEET 6 - STRINGS
1.

t = int(input("Enter num of strings: "))

vow = 'aeiou'

l = []

for i in range(t):

l.append(input().lower())

for s in l:

v=c=0

for char in s:

if char in vow:

v += 1

else:

c += 1

print(v, c)

2.

a = "hello"

myList = list(a)

print(len(myList))

3.

t = int(input("Enter num of strings: "))

l = []

for i in range(t):

l.append(input().lower())

for i in l:

if i == i[::-1]:

print(1)

else:

print(0)
4.

a = "**h*e*l*lo"

print(a.strip("*"))

5.

a = "**h*e*l*lo"

print(a.lstrip("*"))

6.

a = "**h*e*l*lo"

print(a.rstrip("*"))

7.

a = "String"

print(a[::-1])

8.

a = "Suyash Chaudhary"

new = a.split()

new.reverse()

st = " ".join(new)

print(st)

9.

a = "Everyone loves data science"

new = a.split()

myL = []

for i in range(len(new)):

myL.append(new[i][::-1])

print(' '.join(myL))
10.

a = "PythoN"

print(a.lower())

11.

a = "pYthON"

print(a.upper())

12.

A = "Python45"

if A.isalpha():

print(1)

else:

print(0)

A = "Python45"

if A.isalnum():

print(1)

else:

print(0)

13.

A = "aabbcc"

B = 98

new = chr(B)

print(A.find(new))

14.

A = "aabababaa"

B = "ba"
if B in A:

print(A.find(B))

15.

A = "bobob"

length_A = len(A)

count_bob = 0

for i in range(length_A - 2):

if A[i:i+3] == "bob":

count_bob += 1

print(count_bob)

16.

A= "aeiOUz"

A+=A

vow = 'aeiou'

result =''

for i in range(len(A)):

if A[i] in vow:

result += '#'

if A[i].islower() and A[i] not in vow:

result+=A[i]

print(result)
SOLUTION SHEET 7 - DICTIONARY
1.

A = [2, 6, 3, 8, 2, 8, 2, 3, 8, 8]

B=2

freq_dict = {}

for num in A:

if num in freq_dict:

freq_dict[num] += 1

else:

freq_dict[num] = 1

print(freq_dict.get(B, 0))

2.

A = [1, 2, 3, 1, 2, 5]

non_repeating = None

for num in A:

if A.count(num) == 1:

non_repeating = num

break

print(non_repeating)

3.

A = "abcde"

char_freq = {}

for char in A:

char_freq[char] = char_freq.get(char, 0) + 1

odd_count = sum(1 for count in char_freq.values() if count % 2 != 0)

print(int(odd_count <= 1))

4.

A = [10, 5, 3, 4, 3, 5, 6]
seen = {}

for num in A:

if num in seen:

print(num)

break

seen[num] = 1

else:

print(-1)

5.

dict1 = {'Ten': 10, 'Twenty': 20, 'Thirty': 30}

dict2 = {'Thirty': 30, 'Fourty': 40, 'Fifty': 50}

for key, value in dict2.items():

dict1[key] = value

print(dict1)

6.

dict1 = {'a': 100, 'b': 400, 'c': 300}

value = 400

found = False

for val in dict1.values():

if val == value:

found = True

break

print(f"{value} present in a dict" if found else f"{value} not present in a dict")

7.

person = {"name": "abc", "age": 25}

for key in person.keys():

print(key)
8.

person = {"name": "abc", "age": 25}

for value in person.values():

print(value)

9.

person = {"name": "abc", "age": 25}

for key, value in person.items():

print(key, value)

10.

person = {"name": "abc", "age": 25}

person.clear()

print(person)

11.

person = {"name": "abc", "age": 25}

keys_list = list(person.keys())

print(keys_list)

12.

person = {"name": "abc", "age": 25}

values_list = list(person.values())

print(values_list)

13.

N=5

squares_dict = {x: x*x for x in range(1, N+1)}

print(squares_dict)

14.
dict1 = {1: 'a', 2: 'b', 3: 'c'}

list_of_tuples = [(key, value) for key, value in dict1.items()]

print(list_of_tuples)

15.

dict1 = {2: 'Apple', 1: 'Mango', 3: 'Orange', 4: 'Banana'}

sorted_keys = sorted(dict1.keys())

for key in sorted_keys:

print(key, dict1[key])

16.

A = [1, 2, 2, 1]

B = [2, 3, 1, 2]

common_elements = [x for x in A if x in B]

print(common_elements)

17.

squares = [x*x for x in range(1, 6)]

print(squares)

18.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

even_numbers = [x for x in numbers if x % 2 == 0]

print(even_numbers)

19.

text = "hello"

uppercase_chars = [char.upper() for char in text]

print(uppercase_chars)

20.
a=4

print(type(a)) # <class 'int'>

a = 4,

print(type(a)) # <class 'tuple'>

a = (4)

print(type(a)) # <class 'int'>

a = ()

print(type(a)) # <class 'tuple'>

a = (4,)

print(type(a)) # <class 'tuple'>

a = (4, 5)

print(type(a)) # <class 'tuple'>

You might also like