Posts

Showing posts from August, 2023

Mid2 32nd_Tower_of_Hanoi

Image
#output:  #Explanation:

Mid2 35th_Multiplication_of_Two_Matrices

  # Program to multiply two matrices using nested loops # 3x3 matrix X = [[ 12 , 7 , 3 ],     [ 4 , 5 , 6 ],     [ 7 , 8 , 9 ]] # 3x3 matrix Y = [[ 5 , 8 , 1 ],     [ 6 , 7 , 3 ],     [ 4 , 5 , 9 ]] # result is 3x3 result = [[ 0 , 0 , 0 ],          [ 0 , 0 , 0 ],          [ 0 , 0 , 0 ]] # iterate through rows of X for i in range(len(X)):     # iterate through columns of Y     for j in range(len(Y[ 0 ])):         # iterate through rows of Y         for k in range(len(Y)):            result[i][j] += X[i][k] * Y[k][j] for r in result:    print(r) #output: [114, 160, 60] [74, 97, 73] [119, 157, 112]

Mid2 34th_Addition_of_Two_Matrices

  # Program to add two matrices using nested loop X = [[ 1 , 2 , 3 ],     [ 4 , 5 , 6 ],     [ 7 , 8 , 9 ]] Y = [[ 9 , 8 , 7 ],     [ 6 , 5 , 4 ],     [ 3 , 2 , 1 ]] result = [[ 0 , 0 , 0 ],         [ 0 , 0 , 0 ],         [ 0 , 0 , 0 ]] # iterate through rows for i in range(len(X)): # iterate through columns     for j in range(len(X[ 0 ])):         result[i][j] = X[i][j] + Y[i][j] for r in result:     print(r) #output: [10, 10, 10] [10, 10, 10] [10, 10, 10]

Mid2 33rd_to_Define_a_Matrix

  r = int(input( "Enter the number of rows:" )) c = int(input( "Enter the number of columns:" )) matrix = [] print( "Enter the entries rowwise: " ) for i in range(r):     a=[]     for j in range(c):         a.append(int(input( )))     matrix.append(a) for i in range(r):     for j in range(c):         print(matrix[i][j], end = " " )     print() #output: Enter the number of rows:2 Enter the number of columns:2 Enter the entries rowwise: 1 2 3 4 1 2 3 4

Mid2 31st.a_Joining_Characters _Apple (AND) 31st.b_Remove_Character_from_String

  31st.a word= "apple" print( "," .join(word)) #output: a,p,p,l,e 31st.b string= "ubed" user=input( "Enter the element to remove: " ) new_string= "" for i in string:     if i!=user:         new_string+=i print(new_string) #output: Enter the element to remove: d ube

Mid2 30th_Remove_Duplicates_from_List

  def remove_duplicates(l):     unlist=[]     for i in l:         if i not in unlist:             unlist.append(i)     print(unlist) l=[ 1 , 2 , 2 , 3 , 2 , 4 ] remove_duplicates(l) #output: [1, 2, 3, 4]

Mid2 29th_List_Duplication_Checker

def has_duplicates(n, li):     for i in range(n):         for j in range(i + 1 , n):             if li[i] == li[j]:                 return True     return False no = int(input( "Enter how many numbers do you want in a list: " )) gl = [] for i in range(no):     x = int(input( "Enter an element: " ))     gl.append(x) rst = has_duplicates(no, gl) if rst:         print( "The given list has duplicates" ) else :     print( "The given list does not have duplicates" )   #output: Enter how many numbers do you want in a list: 4 Enter an element: 1 Enter an element: 2 Enter an element: 3 Enter an element: 2 The given list has duplicates

Mid2 28th_Student_Percentage_Lookup

  entries=int(input( "Enter the no, of entries: " )) d={} for i in range(entries):     student_name=input( "Enter the student name: " )     student_marks=int(input( "Enter the percentage: " ))     d[student_name]=student_marks user=input( "Enter the student name: " ).lower() print(d[user]) #output: Enter the no. of entries: 2 Enter the student name: ubed Enter the percentage: 12 Enter the student name: rounak Enter the percentage: 13 Enter the student name: ubed 12

Mid2 27th_Inputting_Student_Data

  entries=int(input( "Enter the no. of entries: " )) d={} for i in range(entries):     student_name=input( "Enter the student name: " )     student_marks=int(input( "Enter the percentage: " ))     d[student_name]=student_marks print(d) #output: Enter the no. of entries: 2 Enter the student name: ubed Enter the percentage: 2 Enter the student name: rounak Enter the percentage: 4 {'ubed': 2, 'rounak': 4}

Mid2 26th_Inverted_Dictionary_Creation

  def inverted_dict(d):     d1={value:key for key,value in d.items()}     print(d1) number=int(input( "Enter the entries: " )) d={} for i in range(number):     key=input( "Enter the student name: " )     value=input( "Enter the marks: " )     d[key]=int(value) print(d) print( "inverted dictionary: " ) inverted_dict(d) #ouput: Enter the entries: 2 Enter the student name: ubed Enter the marks: 23 Enter the student name: rounak Enter the marks: 21 {'ubed': 23, 'rounak': 21} inverted dictionary: {23: 'ubed', 21: 'rounak'}

Mid2 25th_Squaring_Numbers_Dictionary

  num=int(input( "Enter the number: " )) d1={x:x** 2 for x in range ( 1 ,num+ 1 )} print(d1) #output: Enter the number: 5 {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

Mid2 24th_Find_Min_&_Max

  d1={ 1 : 200 , 2 : 3000 , 3 : 100 , 5 : 20 } largest=d1[ 1 ] smallest=d1[ 1 ] for key in d1:     if smallest>d1[key]:         smallest=d1[key] print(smallest) for key in d1:     if largest<d1[key]:         largest=d1[key] print(largest) #output: 20 3000

Mid2 23rd_Prime_Number_Dictionary

  number=int(input( "Enter the number: " )) li=[] for i in range(number):     count= 0     for j in range( 1 ,number+ 1 ):         if i%j== 0 :             count+= 1     if count== 2 :         li.append(i) dic={} for i in range( 1 ,number+ 1 ):     if i<=len(li):         dic[i]=li[i- 1 ] print(dic) #output: Enter the number: 100 {1: 2, 2: 3, 3: 5, 4: 7, 5: 11, 6: 13, 7: 17, 8: 19, 9: 23, 10: 29, 11: 31, 12: 37, 13: 41, 14: 43, 15: 47, 16: 53, 17: 59, 18: 61, 19: 67, 20: 71, 21: 73, 22: 79, 23: 83, 24: 89, 25: 97}

Mid2 22nd_String_Word_Lengths

  string= "i am ubed" s=string.split() print(s) dic={} for i in s:     dic[i]=len(i) print(dic) #output: ['i', 'am', 'ubed'] {'i': 1, 'am': 2, 'ubed': 4}

Mid2 21st_Remove_Number_from_List

  l=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 ] user=int(input( "Enter the number to be removed: " )) k=[] for i in l:     if i!=user:         k.append(i) print(k) #output: Enter the number to be removed: 2 [1, 3, 4, 5, 6, 7, 8, 9, 0]

Mid2 20th_to_print_All_Negative_Numbers

  l=[] for i in range( 3 ,- 4 ,- 1 ):     if i< 0 :         l.append(i) print(l) #output: [-1, -2, -3]

Mid2 19th_Interchange_First_and_Last

  l=[ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] temp=l[ 0 ] l[ 0 ]=l[ 6 ] l[ 6 ]=temp print(l) #output: [7, 2, 3, 4, 5, 6, 1]

Mid2 18th_Removing_Odd_Number_in_List

  l=[] for i in range( 1 , 10 ):     if i% 2 != 0 :         l.append(i) print(l) #output: [1, 3, 5, 7, 9]

Mid2 17th_Remove_Duplicates_from_List

  def duplicate(l):     unlist=[]     for i in l:         if i not in unlist:             unlist.append(i)     print(unlist) l=[ 1 , 2 , 2 , 3 , 2 , 4 ] duplicate(l) #output: [1, 2, 3, 4]

Mid2 16th_Find_Occurrences_of_Number

  l=[ 1 , 2 , 2 , 3 , 2 , 4 ] a=int(input( "Enter the number: " )) s=[] for i in range(len(l)):     if l[i]==a:         s.append(i) print(s) print(len(s)) #output: Enter the number: 2 [1, 2, 4] 3

Mid2 15th_Second_Largest_and_Smallest

  l=[ 1 , 4 , 3 , 2 , 6 , 7 , 9 ] k=sorted(l) print(k) print(k[ 1 ]) print(k[- 2 ]) #output: [1, 2, 3, 4, 6, 7, 9] 2 7

Mid2 14th_Character_Count_in_Listb

  sentence=[ 's' , 't' , 'r' , 'i' , 'n' , 'g' , 's' ] string=input( "Enter a character: " ) count= 0 for i in sentence:     if i==string:         count+= 1 print(count) #output: Enter a character: s 2

Mid2 13th_List_to_String_Conversionb

  sentence=[ 's' , 't' , 'r' , 'i' , 'n' , 'g' ] string= "" for i in sentence:     string+=i print(string) #output: string

Mid2 12th_Reverse_Words_List

  sentence= "sphn engg clg" s=sentence.split() l=[] for i in s:     l.append(i[::- 1 ]) print(l) #output: ['nhps', 'ggne', 'glc']

Mid2 11th_remove_Special_characters_Space

  string= "i a@m 32#+*ubed" str1= "" for i in range(len(string)):     if string[i].isalpha()== True :         str1+=string[i] print(str1) #output: iamubed

Mid2 10th_Vowels_Spaces_Longest_word

  v = [ 'a' , 'e' , 'i' , 'o' , 'u' ] count_vowels= 0 count_spaces= 0 string = "this is ubaidi" for i in string:     if i in v:         count_vowels+= 1     elif i== " " :         count_spaces+= 1 print(count_vowels) print(count_spaces) k=string.split() largest=k[ 0 ] for i in k:     if len(i)>len(largest):         largest=i print(largest) #output: 6 2 ubaidi

Mid2 9th_Count_Substring_Occurrences

  str1= "'azcmamamegghakl" str2= "mam" count1= 0 for i in range (len(str1)):     if str1[i:i+len(str2)]==str2:         count1+= 1 print(count1) #output:2

Mid2 8th_Armstrong_Number

  num = int(input( "Enter a number: " )) sum = 0 temp = num while temp > 0 :    digit = temp % 10    sum += digit ** 3    temp //= 10 if num == sum:    print(num, "is an Armstrong number" ) else :    print(num, "is not an Armstrong number" ) #output: Enter a number: 153 153 is an Armstrong number

Mid2 7th_HCF

  def hcf(a, b):     if (b == 0 ):         return a     else :         return hcf(b, a % b) a = 60 b = 48 # prints 12 print( "The gcd of 60 and 48 is : " , end= "" ) print(hcf( 60 , 48 )) #output: The gcd of 60 and 48 is 12

Mid2 6th_Prime_Numbers

  print( "Integers not divisible by 2,3,5,7,11,13,17,19 that lie between 1 and 1000 are : " ) n = 1 while n <= 1000 :     if n% 2 != 0 and n% 3 != 0 and n% 5 != 0 and n% 7 != 0 and n% 11 != 0 and n% 13 != 0 and n% 17 != 0 and n% 19 != 0 :       print(n)    n = n+ 1 #output: Integers not divisible by 2,3,5,7,11,13,17,19 that lie between 1 and 1000 are : 1 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 529 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 667 673 677 683 691 701 709 713 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 841 851 853 857 859 863 877 881 883 887 899 907 911 919 929 937 94

Mid2 5th_Sum_of_Entered_Numbers

sum= 0 x= 1 while (x!= 0 ):     x=int(input( "Enter a number: " ))     sum=sum + x print( "Sum of the entered numbers : " ,sum) #output: Enter a number: 2 Enter a number: 2 Enter a number: 3 Enter a number: 0 Sum of the entered numbers : 7

Mid2 4th_Gross_Salary_Calculator

basic_salary = int(input( "Enter your basic salary: " )) if basic_salary > 25000 :    hra = .35 * basic_salary     da = .95 * basic_salary elif basic_salary > 15000 :    hra = .30 * basic_salary    da = .90 * basic_salary else :    hra = .25 * basic_salary    da = .80 * basic_salary gross_salary = basic_salary + hra + da print( "The gross salary is: " ,gross_salary) #output: Enter your basic salary: 30000 The gross salary is: 69000.0 

Mid2 3rd_Calculate_Grades_from_Percentage

  per=int(input( "Enter a student percentage: " )) if per>= 75 :     print( "congrats A+ grade" )  elif per>= 60 :     print( "congrats A grade" ) elif per>= 50 :     print( "you got B+ grade" ) elif per>= 40 :     print( "you got B grade" ) else :     print( "Better luck next time" ) #output: Enter a student percentage: 85 congrats A+ grade

Mid2 2nd_Largest_of_3_number.

a=int(input( "Enter a number: " ))  b=int(input( "Enter a number: " )) c=int(input( "Enter a number: " )) if a>b:     if a>c:         max=a     else :         max=c else :     if b>c:         max=b     else :         max=c print(max) #output: Enter a number: 3 Enter a number: 5 Enter a number: 4 5

Mid2 1st_Arithmetic_Operations

  a=int(input( "Enter a number: " ))  b=int(input( "Enter a number: " )) ans1=a+b print( "Addition of" ,a, "and" ,b, "is" ,ans1) ans2=a-b print( "Substraction of" ,a, "and" ,b, "is" ,ans2) ans3=a*b print( "Multiplication of" ,a, "and" ,b, "is" ,ans3) ans4=a/b print( "Division of" ,a, "and" ,b, "is" ,ans4) ans5=a%b print( "Modulus of" ,a, "and" ,b, "is" ,ans5) ans6=a//b print( "Floor division of" ,a, "and" ,b, "is" ,ans6) ans7=a**b print( "Exponential of" ,a, "and" ,b, "is" ,ans7) #output: Enter a number: 10 Enter a number: 5 Addition of 10 and 5 is 15 Substraction of 10 and 5 is 5 Multiplication of 10 and 5 is 50 Division of 10 and 5 is 2.0 Modulus of 10 and 5 is 0 Floor division of 10 and 5 is 2 Exponential of 10 and 5 is 100000