Course- BTech                             Type- Core
Course Code-CSET101                       Course Name- Computational
                                          Thinking and Programming
Year-2023                                 Semester-odd
Date-                                     Batch- ALL
                      Tutorial Assignment: 5 Solution
Q1.
Q2. Infinite loop. Updation condition missing for loop control variable.
Correct code -
i = 1
while (i ==1):
    print (i)
    i += 1
Q3. Output - BXEXNXNXEXTXTX
Default value for end in python is newline \n so each normal print
results in separate line. If strings separated with comma are given then
one space is added between them. To resolve use + for exact
concatenation.
string = 'BENNETT'
for letters in string:
    print (letters,end='X')
Other interesting print examples -
name = "Alice"
age = 25
print('Hello, my name is', name, 'and I am', age,
'years old.')
print("Geeks : %d, Portal : %5.2f" % (1,
05.333))
print("Total students : %3d, Boys : %2d" % (240,
120))
# using format() method and referring a position
of the object
print('{0} and {1}'.format('Geeks', 'Portal'))
print('{1} and {0}'.format('Geeks', 'Portal'))
print(f"I love {'Geeks'} for \"{'Geeks'}!\"")
# combining positional and keyword arguments
print('Number one portal is {0}, {1}, and
{other}.'.format('Geeks', 'For', other='Geeks'))
# using format() method with number
print("Geeks :{0:2d}, Portal
:{1:8.2f}".format(12, 00.546))
# Changing positional argument
print("Second argument: {1:3d}, first one:
{0:7.2f}".format(47.42, 11))
tab = {'geeks': 4127, 'for': 4098, 'geek':
8637678}
# using format() in dictionary
print('Geeks: {0[geeks]:d}; For: {0[for]:d};
Geeks: {0[geek]:d}'.format(tab))
data = dict(fun="GeeksForGeeks", adj="Portal")
print("I love {fun} computer
{adj}".format(**data))
cstr = "I love geeksforgeeks"
# Printing the center aligned string with
fillchr
print("Center aligned string with fillchr: ")
print(cstr.center(40, '#'))
# Printing the left aligned string with "-"
padding
print("The left aligned string is : ")
print(cstr.ljust(40, '-'))
# Printing the right aligned string with "-"
padding
print("The right aligned string is : ")
print(cstr.rjust(40, ' '))
Line wise outputs -
Hello, my name is Alice and I am 25 years old.
Geeks : 1, Portal : 5.33
Total students : 240, Boys : 120
Geeks and Portal
Portal and Geeks
I love Geeks for "Geeks!"
Number one portal is Geeks, For, and Geeks.
Geeks :12, Portal : 0.55
Second argument: 11, first one: 47.42
Geeks: 4127; For: 4098; Geeks: 8637678
I love GeeksForGeeks computer Portal
Center aligned string with fillchr:
##########I love geeksforgeeks##########
The left aligned string is :
I love geeksforgeeks--------------------
The right aligned string is :
            I love geeksforgeeks
Interesting input() examples -
# taking two inputs at a time
x, y = input("Enter two values: ").split()
# taking multiple inputs at a time and type
casting using list() function
x = list(map(int, input("Enter multiple values:
").split()))
# taking two inputs at a time
x, y = [int(x) for x in input("Enter two values:
").split()]
print("First number is {} and second number is
{}".format(x, y))
# taking multiple inputs at a time
x = [int(x) for x in input("Enter multiple
values: ").split()]
print("Number of list is: ", x)
# taking multiple inputs at a time separated by
comma
x = [int(x) for x in input("Enter multiple value:
").split(",")]
print("Number of list is: ", x)
Q4. No error
Output -
(15, -9)
(13, -6)
(11, -3)
num1 = 17
num2 = -12
while num1 > 5 and num2 < -5 :
    num1 -= 2
    num2 += 3
    print( (num1, num2) )
Q5. Output – t (string variable accessible outside scope)
for string in "BU Student":
    pass
print(string)
Q6. Output - [25, 16, 9, 4, 1]
a = [1,2,3,4,5]
x = []
while a:
    x.append((a.pop())**2)
print( x )
Q7. Output - [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
X = []
i = 0
while len(X) < 10 :
    X.append(i)
    i += 2
print(X)
Q8.
chk = 1
while(chk):
      x = int(input("enter 1st"))
      y = int(input("enter 2nd"))
      print("sum is - ",x+y)
    chk = int(input("do you want to do sum again, enter 0 for no and
1 for yes"))
Q9.
weight = float(int(input("enter weight in kg")))
height = float(input("enter height in m"))
x = weight/float(height**2)
if x<0:
    print("wrong input")
elif x < 18.5:
    print('Underweight')
elif x>=18.5 and x<25:
    print("Normal")
elif x >= 25 and x < 30:
   print('Overweight')
elif x >= 30:
   print('Obesity')
Q10.
'''string = input("enter full string")
first = input("enter 1st no.")
steps = int(input("input proceeding steps"))
first_pos = string.index(first)
opr_pos = first_pos+len(first)
opr = string[opr_pos]'''
string = "1+2+3+4"
first = "1"
steps = 3
numbers = [int(x) for x in string.split('+')]
print(numbers)
sum = 0
for i in range(steps+1):
    sum += numbers[i]
print(sum)
print(sum/(steps+1))
Q11.
#x = [int(x) for x in input("Enter multiple
value: ").split(",")]
string="9,13,5,7,4,8"
x = [int(x) for x in string.split(",")]
print(x)
point_no = int(len(x)/2)
print("Number of points is: ", point_no)
points = []
for i in range(point_no):
    curr_x = x[i*2]
    curr_y = x[i*2+1]
       import math
       curr_dist = math.sqrt(curr_x**2 + curr_y**2)
       curr_dist = round(curr_dist,3)
       points.append([curr_x,curr_y,curr_dist])
print(points)
points.sort(reverse = True, key = lambda x:x[2])
print(points[0])
Q12.
num = int(input("Enter the number: "))
#print table possible in 3 separate forms
print("Multiplication Table of", num)
for i in range(1, 11):
    #print(num,"X",i,"=",num * i)
    '''print(str(num).rjust(4, ' '), "X",
          str(i).rjust(4, ' '), "=",
          str(num * i).rjust(4, ' '))'''
   print("{main:4d} X {multi:4d} =
{final:4d}".format(main = num, multi = i, final =
num*i))
Q13,Q14,Q15. Inbuilt function -
stre =input("enter the string-->")
countl = 0
countn = 0
counto = 0
for i in stre:
    if i.isalpha():
        countl += 1
    elif i.isdigit():
        countn += 1
    else:
        counto +=            1
print("The number            of letters are --", countl)
print("The number            of numbers are --", countn)
print("The number            of characters are --", counto)
Using ascii -
string =input("enter the string-->")
# initialized value
total_digits = 0
total_letters = 0
total_other = 0
# iterate through all characters
for s in string:
    # if character found in all_digits then
increment total_digits by one
    if ord(s) in range(48, 58):
        total_digits += 1
    # if character found in all_letters then
increment total_letters by one
    elif ord(s) in range(65, 91) or ord(s) in
range(97, 123):
        total_letters += 1
      else:
          total_other+=1
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)
print("Total other characters found :-",
total_other)
Using regular expression -
import re
string =input("enter the string-->")
# initialized value
total_digits = len(re.findall('[0-9]', string))
total_letters = len(re.findall('[A-Z,a-z]',
string))
# iterate through all characters
print("Total letters found :-", total_letters)
print("Total digits found :-", total_digits)