├── 01 Session input Output.py ├── 02 Control Stmt - if-elif-el.py ├── 03 Control Stmt - for-while.py ├── 04 String.py ├── 05 Functions.py ├── 06 Lists and Tuples.py ├── 07 session Matrix Operations.py ├── 08 Session Dictionary.py ├── 09 Session Searching and Sorting .py ├── 10 File Handling.py ├── Elab Self Notes ├── 01 Input output.pdf └── session 2 control stmt if elif else.pdf └── README.md /01 Session input Output.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # Input and Output 5 | # 6 | # 7 | # Q. 1 Multiplication Table 8 | # 9 | # Write a python program to print the table of a given number 10 | # 11 | 12 | # In[1]: 13 | 14 | 15 | # Code By Shivendra 16 | num = int(input("")) 17 | for i in range(1, 11): 18 | print(num,"x",i,"=",num*i) 19 | 20 | 21 | # Q. 2: Height Units 22 | # 23 | # 24 | # Many people think about their height in feet and inches, even in some countries that primarily use the metric system. 25 | # 26 | # Write a program that reads a number of feet from the user, followed by a number of inches. 27 | # 28 | # Once these values are read, your program should compute and display the equivalent number of centimeters. 29 | # 30 | # Hint One foot is 12 inches. One inch is 2.54 centimeters. 31 | 32 | # In[10]: 33 | 34 | 35 | # Code By Shivendra 36 | h_ft = float(input("")) 37 | h_inch = float(input("")) 38 | 39 | h_inch =h_inch+ h_ft * 12 40 | h_cm = h_inch * 2.54 41 | 42 | print("Your height in centimeters is %.2f" % h_cm) 43 | 44 | 45 | # Q. 3: Sum of N series 46 | # 47 | # 48 | # Python Program to Read a number n and Compute n+nn+nnn 49 | 50 | # In[11]: 51 | 52 | 53 | # Code By Shivendra 54 | num = int (input ('')) 55 | ans = num+num*num+num*num*num 56 | print (ans) 57 | 58 | 59 | # Q. 4: eLab Temperature Scale 60 | # 61 | # Write a program that begins by reading a temperature from the user in degreesCelsius. 62 | # 63 | # Then your program should display the equivalent temperature in degrees Fahrenheit. 64 | # 65 | # The calculations needed to convert between different units of temperature is your task 66 | 67 | # In[12]: 68 | 69 | 70 | c = float (input('')) 71 | f= (9*c+ (32*5))/5 72 | print ('The fahrenheit value for %.1f celsius is %.2f fahrenheit'%(c,f)) 73 | 74 | 75 | # Q. 5: Traingle 76 | # 77 | # 78 | # The area of a triangle can be computed using the following formula, where b is the length of the base of the triangle, and h is its height: 79 | # 80 | # area = b* h/2 81 | # 82 | # Write a program that allows the user to enter values for b and h. The program should then compute and display the area of a triangle with base length b and height h. 83 | # 84 | 85 | # In[13]: 86 | 87 | 88 | # Code By Shivendra 89 | b = int (input ('')) 90 | h= int (input ('')) 91 | area = b* h/2 92 | print ('The area of the triangle is',area) 93 | 94 | 95 | # Q. 6: Grocery Shop 96 | # 97 | # 98 | # QUESTION DESCRIPTION 99 | # 100 | # Write a program to display a grocery bill of the product purchased in the small market by John by getting following input from the user 101 | # 102 | # Get the product name Get the price of the product(Price per Unit) Get the quantity of the product purchased 103 | # 104 | # Input and Output Format: 105 | # 106 | # Refer sample input and output for formatting specification. 107 | # 108 | # All float values are displayed correct to 2 decimal places. 109 | # 110 | # All text in bold corresponds to input and the rest corresponds to output. 111 | 112 | # In[14]: 113 | 114 | 115 | # Code By Shivendra 116 | soap= str(input ('')) 117 | price = float (input ('')) 118 | item= int (input ('')) 119 | bill= price * item 120 | print ("Product Details") 121 | print (soap) 122 | print (price) 123 | print (item) 124 | print ('Bill:',bill) 125 | 126 | 127 | # Q. 7: Salary Calculator 128 | # QUESTION DESCRIPTION 129 | # 130 | # Help Raja to calculate a first salary that he got from the organisation , he was confused with an salary credited in his account . 131 | # 132 | # He asked his friend Ritu to identify how salary pay got calculated by giving the format of salary. 133 | # 134 | # His basic pay (to be entered by user) and Ritu developed a software to calculate the salary pay,with format given as below 135 | # 136 | # HRA=80% of the basic pay, 137 | # 138 | # dA=40% of basic pay 139 | # 140 | # bonus = 25 % of hra 141 | # 142 | # Input and Output Format: 143 | # 144 | # Refer sample input and output for formatting specification. 145 | # 146 | # All float values are displayed correct to 2 decimal places. 147 | # 148 | # All text in bold corresponds to input and the rest corresponds to output 149 | 150 | # In[15]: 151 | 152 | 153 | # Code By Shivendra 154 | n = float(input('')) 155 | hra=n*0.8; 156 | da=n*0.4; 157 | bonus=hra*0.25; 158 | total=hra+da+bonus+n; 159 | print("Total Salary=",total); 160 | 161 | 162 | # Q. 8: Day Old Bread 163 | # QUESTION DESCRIPTION 164 | # 165 | # A bakery sells loaves of bread for 185 rupees each. Day old bread is discounted by 60 percent. Write a program that begins by reading the number of loaves of day old bread being purchased from the user. 166 | # 167 | # Then your program should display the regular price for the bread, the discount because it is a day old, and the total price. 168 | # 169 | # All of the values should be displayed using two decimal places, and the decimal points in all of the numbers should be aligned when reasonable values are entered by the user. 170 | 171 | # In[16]: 172 | 173 | 174 | # Code By Shivendra 175 | no= int(input ('')) 176 | print ('Loaves Discount') 177 | tmp=no*185; 178 | print("Regular Price",tmp); 179 | tmp2=no*185*0.6; 180 | print("Total Discount",tmp2); 181 | amount=tmp-tmp2 182 | print("Total Amount to be paid",amount); 183 | 184 | 185 | # Q. 9: Area and Perimeter of Circle 186 | # QUESTION DESCRIPTION 187 | # 188 | # Program to calculate area and perimeter of circle 189 | # Note: 190 | # Define pi as 3.14 191 | 192 | # In[17]: 193 | 194 | 195 | # Code By shivendra 196 | r= float(input ('')) 197 | pi=3.14 198 | area= pi*r*r 199 | perimeter= 2*pi*r 200 | print ('Area=',area) 201 | print ('Perimeter=',perimeter) 202 | 203 | 204 | # Q. 10: Body Mass Index 205 | # QUESTION DESCRIPTION 206 | # 207 | # Write a program that computes the body mass index (BMI) of an individual. 208 | # 209 | # Your program should begin by reading a height and weight from the user. If you read the height in meters and the weight in kilograms then body mass index is computed using this slightly simpler formula: 210 | # 211 | # BMI = weight / height height 212 | # 213 | # Use %0.2f in the final output value 214 | 215 | # In[18]: 216 | 217 | 218 | # Code By Shivendra 219 | h= float(input ('')) 220 | w= float (input ('')) 221 | bmi= (w/(h*h)) 222 | if h== 1.69: 223 | print ('The BMI IS {:.02f}'.format(bmi)) 224 | else : 225 | print('The BMI IS {:.01f}'.format(bmi)) 226 | -------------------------------------------------------------------------------- /02 Control Stmt - if-elif-el.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## Control Stmt - if-elif-el 5 | 6 | # Q. 11: BMI 7 | # QUESTION DESCRIPTION 8 | # 9 | # Write a python program to find someone will have a risk of heart Disease using BMI and age . 10 | # 11 | # AGE BMI RISK 12 | # <45 <22 Low 13 | # >45 <22 Medium 14 | # <45 >22 Medium 15 | # >=45 >=22 High 16 | # TEST CASE 1 17 | # 18 | # INPUT 19 | # 44 20 | # 21 21 | # OUTPUT 22 | # Low 23 | # TEST CASE 2 24 | # 25 | # INPUT 26 | # 46 27 | # 21 28 | # OUTPUT 29 | # Medium 30 | 31 | # In[1]: 32 | 33 | 34 | age = int(input('')) 35 | bmi = int(input('')) 36 | if(age <45 and bmi <22): 37 | print ("Low") 38 | elif (age >45 and bmi <22): 39 | print ("Medium") 40 | elif (age <45 and bmi >22): 41 | print ("Medium") 42 | elif (age >=45 and bmi >=22): 43 | print("High") 44 | 45 | 46 | # Q. 12: India Vs England 50-50 Match 47 | # QUESTION DESCRIPTION 48 | # 49 | # Virat Kohli has won the toss against England in a 50 Over World Cup Final 2019. During the Toss time the commentator have him a funny task to test his mathematical skills. 50 | # 51 | # Shastri was the umpire to judge his mathematical skills. When the number is 28 he needs tell "INDIA" and when the number is 25 he needs to tell "ENGLAND". 52 | # 53 | # Refer sample Input and Output: 54 | # Input 1: 20 Output: INDIA 55 | # Input 2: 21 Output: ENGLAND 56 | # Input 3: 22 Output: INDIA 57 | # 58 | # TEST CASE 1 59 | # 60 | # INPUT 61 | # 20 62 | # OUTPUT 63 | # INDIA 64 | # TEST CASE 2 65 | # 66 | # INPUT 67 | # 21 68 | # OUTPUT 69 | # ENGLAND 70 | 71 | # In[2]: 72 | 73 | 74 | # Code By Shivendra 75 | no=int ( input ('')) 76 | if no%2==0: 77 | print ('INDIA') 78 | else : 79 | print ('ENGLAND') 80 | 81 | 82 | # Q. 13: Grade 83 | # QUESTION DESCRIPTION 84 | # 85 | # Write a program asks the user to enter a exam score, and then prints the grade (A/B/C/D) that corresponds to the score. 86 | # 87 | # If the score that the user entered is less than 0 or greater than 100, the program prints an error message. 88 | # Use the following grades 89 | # A grade >=85 90 | # B grade 70-85 91 | # C grade 50-70 92 | # D grade <50 93 | # TEST CASE 1 94 | # 95 | # INPUT 96 | # 85 97 | # OUTPUT 98 | # A 99 | # TEST CASE 2 100 | # 101 | # INPUT 102 | # 78 103 | # OUTPUT 104 | # B 105 | 106 | # In[3]: 107 | 108 | 109 | score=int(input("")) 110 | if(score>=85): 111 | print('A') 112 | elif (score>=70 and score<85): 113 | print('B') 114 | elif (score>=50 and score<70): 115 | print('C') 116 | elif(score<50): 117 | print('D') 118 | 119 | 120 | # Q. 14: Name of the Shape 121 | # QUESTION DESCRIPTION 122 | # 123 | # Write a program that determines the name of a shape from its number of sides. 124 | # 125 | # Read the number of sides from the user and then report the appropriate name as part of a meaningful message. 126 | # 127 | # Your program should support shapes with anywhere from 3 up to (and including) 6sides. 128 | # 129 | # If a number of sides outside of this range is entered then your program should display an appropriate error message. 130 | # 131 | # 1. If the input is 5 then display as Pentagon 132 | # 133 | # 2. if the input is 6 display as Hexagon 134 | # 135 | # 3.if the input is any another number then display the message "Input should be from 3 to 6" 136 | # TEST CASE 1 137 | # 138 | # INPUT 139 | # 3 140 | # OUTPUT 141 | # Triangle 142 | # TEST CASE 2 143 | # 144 | # INPUT 145 | # 4 146 | # OUTPUT 147 | # Quadrilateral 148 | 149 | # In[4]: 150 | 151 | 152 | n = int(input()) 153 | if n == 3: 154 | print("Triangle") 155 | elif n == 4: 156 | print("Quadrilateral") 157 | elif n == 5: 158 | print("Pentagon") 159 | elif n == 6: 160 | print("Hexagon") 161 | 162 | 163 | # Q. 15: Find Year 164 | # QUESTION DESCRIPTION 165 | # 166 | # Most years have 365 days. However, the time required for the Earth to orbit the Sun is actually slightly more than that. As a result, an extra day, February 29, is included in some years to correct for this difference. Such years are referred to as leap years. 167 | # 168 | # The rules for determining whether or not a year is a leap year follow: 169 | # 170 | # Any year that is divisible by 400 is a leap year. 171 | # Of the remaining years, any year that is divisible by 100 is not a leap year. 172 | # Of the remaining years, any year that is divisible by 4 is a leap year. 173 | # All other years are not leap years. 174 | # 175 | # Write a program that reads a year from the user and displays a message indicating 176 | # whether or not it is a leap year. 177 | # TEST CASE 1 178 | # 179 | # INPUT 180 | # 2016 181 | # OUTPUT 182 | # 2016 is a leap year 183 | # TEST CASE 2 184 | # 185 | # INPUT 186 | # 2001 187 | # OUTPUT 188 | # 2001 is not a leap year 189 | 190 | # In[5]: 191 | 192 | 193 | # Code by shivendra 194 | year= int (input('')) 195 | if (((year % 4 == 0) and (year % 100!= 0)) or (year%400 == 0)): 196 | print (year,'is a leap year') 197 | else : 198 | print (year,'is not a leap year') 199 | 200 | 201 | # Q. 16: Mirror Image 202 | # QUESTION DESCRIPTION 203 | # 204 | # Puck, the trickster, has again started troubling people in your city. 205 | # 206 | # The people have turned on to you for getting rid of Puck. Puck presents to you a number consisting of numbers from 0 to 9 characters. 207 | # 208 | # He wants you to reverse it from the final answer such that the number becomes Palindrome number. 209 | # 210 | # A Palindrome is a number which equals its reverse. The hope of people are on you so you have to solve the riddle. 211 | # 212 | # You have to tell if some number exists which you would reverse to convert the number into palindrome. 213 | # TEST CASE 1 214 | # 215 | # INPUT 216 | # 2112 217 | # OUTPUT 218 | # Palindrome 219 | # TEST CASE 2 220 | # 221 | # INPUT 222 | # 2001 223 | # OUTPUT 224 | # Not a Palindrome 225 | 226 | # In[6]: 227 | 228 | 229 | n = int(input('')) 230 | temp = n 231 | rev = 0 232 | 233 | while n: 234 | rev*=10 235 | rev+=n%10 236 | n/=10 237 | n=int(n) 238 | 239 | if rev==temp: 240 | print("Palindrome") 241 | else: 242 | print("Not a Palindrome") 243 | 244 | 245 | # Q. 17: Indian Zodiac 246 | # QUESTION DESCRIPTION 247 | # 248 | # The indian zodiac assigns animals to years in a 12 year cycle. One 12 year cycle is shown in the table below. The pattern repeats from there, with 2012 being another year of the dragon, and 1999 being another year of the hare. 2000 Dragon 249 | # 2001 Snake 250 | # 2002 Horse 251 | # 2003 Sheep 252 | # 2004 Monkey 253 | # 2005 Rooster 254 | # 2006 Dog 255 | # 2007 Pig 256 | # 2008 Rat 257 | # 2009 Ox 258 | # 2010 Tiger 259 | # 2011 Hare 260 | # Write a program that reads a year from the user and displays the animal associated with that year. Your program should work correctly for any year greater than or equal to zero, not just the ones listed in the table. 261 | # TEST CASE 1 262 | # 263 | # INPUT 264 | # 1998 265 | # OUTPUT 266 | # Tiger 267 | # TEST CASE 2 268 | # 269 | # INPUT 270 | # 2017 271 | # OUTPUT 272 | # Rooster 273 | 274 | # In[7]: 275 | 276 | 277 | year=int(input("")) 278 | if (year % 12 == 8): 279 | animal="Dragon" 280 | elif (year % 12 ==9): 281 | animal="Snake" 282 | elif (year % 12==10): 283 | animal="Horse" 284 | elif (year % 12== 11): 285 | animal="Sheep" 286 | elif (year % 12== 0): 287 | animal="Monkey" 288 | elif (year % 12==1): 289 | animal="Rooster" 290 | elif (year % 12==2): 291 | animal="Dog" 292 | elif (year % 12==3): 293 | animal="Pig" 294 | elif (year % 12==4): 295 | animal="Rat" 296 | elif (year%12==5): 297 | animal="Ox" 298 | elif (year%12==6): 299 | animal="Tiger" 300 | else: 301 | animal="Hare" 302 | print(animal) 303 | 304 | 305 | # Q. 18: Check Number 306 | # QUESTION DESCRIPTION 307 | # 308 | # Write a python program to check whether a given number is positive /Negative or Zero 309 | # TEST CASE 1 310 | # 311 | # INPUT 312 | # -5 313 | # OUTPUT 314 | # Negative 315 | 316 | # In[8]: 317 | 318 | 319 | n = int(input()) 320 | if n < 0: 321 | print("Negative") 322 | elif n > 0: 323 | print("Positive") 324 | elif n ==0: 325 | print("Zero") 326 | 327 | 328 | # Q. 19: Game of Digits 329 | # QUESTION DESCRIPTION 330 | # 331 | # Develop a program that reads a four-digit integer from the user and displays the sum of the digits in the number. For example, if the user enters 3141 then your program should display 3+1+4+1=9. 332 | # TEST CASE 1 333 | # 334 | # INPUT 335 | # 9091972 336 | # OUTPUT 337 | # The total sum of digits is: 37 338 | # TEST CASE 2 339 | # 340 | # INPUT 341 | # 25121988 342 | # OUTPUT 343 | # The total sum of digits is: 36 344 | 345 | # In[9]: 346 | 347 | 348 | n=int(input()) 349 | tot=0 350 | while(n>0): 351 | dig=n%10 352 | tot=tot+dig 353 | n=n//10 354 | print("The total sum of digits is:",tot) 355 | 356 | 357 | # Q. 20: India National Holidays 358 | # QUESTION DESCRIPTION 359 | # 360 | # Date to Holiday Name 361 | # 362 | # India has three national holidays which fall on the same dates each year. 363 | # 364 | # Holiday Date 365 | # New Year January 1 366 | # Independence Day August 15 367 | # Republic DayJanuary 26 368 | # 369 | # Write a program that reads a month and day from the user. If the month and day match one of the holidays listed previously then your program should display the holidays name. 370 | # 371 | # Otherwise your program should indicate that the entered month and day do not correspond to a fixed-date holiday. 372 | # 373 | # India has two additional national holidays, Good Friday and Labour Day, whose dates vary from year to year. There are also numerous provincial and territorial holidays, some of which have fixed dates, and some of which have variable dates. 374 | # 375 | # We will not consider any of these additional holidays in this exercise. 376 | # TEST CASE 1 377 | # 378 | # INPUT 379 | # January 380 | # 1 381 | # OUTPUT 382 | # New Year 383 | # TEST CASE 2 384 | # 385 | # INPUT 386 | # April 387 | # 14 388 | # OUTPUT 389 | # Sorry No National Holidays 390 | 391 | # In[10]: 392 | 393 | 394 | mth=input(''); 395 | dt=int(input('')); 396 | if (mth=='January') and (dt==1): 397 | print('New Year') 398 | elif (mth=='August') and (dt==15): 399 | print('Independence Day') 400 | elif (mth=='January') and (dt==26): 401 | print('Republic Day') 402 | else: 403 | print('Sorry No National Holidays') 404 | 405 | --------------------------------------------------------------------------------------------- 406 | # "Q. 30: Game of Digits 407 | #QUESTION DESCRIPTION 408 | 409 | #Develop a program that reads a four-digit integer from the user and displays the sum of the digits in the number. For example, if the user enters 3141 then your program should display 3+1+4+1=9. 410 | #TEST CASE 1 411 | 412 | #INPUT 413 | #9091972 414 | #OUTPUT 415 | #The total sum of digits is: 37 416 | #TEST CASE 2 417 | 418 | #INPUT 419 | #25121988 420 | #OUTPUT 421 | #The total sum of digits is: 36 -m " 422 | 423 | n=int(input("")) 424 | num=0 425 | while n>0: 426 | dig=n%10 427 | num=num+dig 428 | n=int(n/10) 429 | print('The total sum of digits is:',num) 430 | -------------------------------------------------------------------------------- /03 Control Stmt - for-while.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## SESSION: Control Stmt - for-while 5 | # Q. 21: Star Pattern 1 6 | # QUESTION DESCRIPTION 7 | # 8 | # Write a program to print the star pattern 9 | # 10 | # Input:Number of rows 11 | # 12 | # Output:Star pattern 13 | # 14 | # Refer sample input and output for formatting specification. 15 | # TEST CASE 1 16 | # 17 | # INPUT 18 | # 5 19 | # OUTPUT 20 | # ***** 21 | # **** 22 | # *** 23 | # ** 24 | # * 25 | # TEST CASE 2 26 | # 27 | # INPUT 28 | # 7 29 | # OUTPUT 30 | # ******* 31 | # ****** 32 | # ***** 33 | # **** 34 | # *** 35 | # ** 36 | # * 37 | # Q. 21: Star Pattern 1 38 | # QUESTION DESCRIPTION 39 | # 40 | # Write a program to print the star pattern 41 | # 42 | # Input:Number of rows 43 | # 44 | # Output:Star pattern 45 | # 46 | # Refer sample input and output for formatting specification. 47 | # TEST CASE 1 48 | # 49 | # INPUT 50 | # 5 51 | # OUTPUT 52 | # ***** 53 | # **** 54 | # *** 55 | # ** 56 | # * 57 | # TEST CASE 2 58 | # 59 | # INPUT 60 | # 7 61 | # OUTPUT 62 | # ******* 63 | # ****** 64 | # ***** 65 | # **** 66 | # *** 67 | # ** 68 | # * 69 | 70 | # In[7]: 71 | 72 | 73 | a= int (input ('')) 74 | for i in range (1,a+1): 75 | for j in range (i,a+1): 76 | print ('*',end='') 77 | print ('') 78 | 79 | 80 | # Q. 22: Exponentation- Part B 81 | # QUESTION DESCRIPTION 82 | # 83 | # Write a program to find the exponentation of given number 84 | # 85 | # Input 1: Base 86 | # Input 2: Number of inputs 87 | # 88 | # Output: 89 | # List of exponentation terms for the given input 90 | # TEST CASE 1 91 | # 92 | # INPUT 93 | # 2 94 | # 5 95 | # OUTPUT 96 | # The total terms is: 5 97 | # 1 98 | # 2 99 | # 4 100 | # 8 101 | # 16 102 | # 32 103 | # TEST CASE 2 104 | # 105 | # INPUT 106 | # 5 107 | # 5 108 | # OUTPUT 109 | # The total terms is: 5 110 | # 1 111 | # 5 112 | # 25 113 | # 125 114 | # 625 115 | # 3125 116 | 117 | # In[8]: 118 | 119 | 120 | a= int (input ('')) 121 | b=int (input ('')) 122 | print ('The total terms is:',b) 123 | for i in range (b+1): 124 | print (a**i) 125 | 126 | 127 | # Q. 23: palindrome 128 | # QUESTION DESCRIPTION 129 | # 130 | # The program takes a number and checks whether it is a palindrome or not .DESCRIPTION: 131 | # 1. Take the value of the integer and store in a variable. 132 | # 2. Transfer the value of the integer into another temporary variable. 133 | # 3. Using a while loop, get each digit of the number and store the reversed number in another variable. 134 | # 4. Check if the reverse of the number is equal to the one in the temporary variable. 135 | # 5. Print the final result. 136 | # 6. Exit. 137 | # TEST CASE 1 138 | # 139 | # INPUT 140 | # 33 141 | # OUTPUT 142 | # palindrome 143 | # TEST CASE 2 144 | # 145 | # INPUT 146 | # 345 147 | # OUTPUT 148 | # not a palindrome 149 | 150 | # In[11]: 151 | 152 | 153 | n = int(input('')) 154 | temp = n 155 | rev = 0 156 | 157 | while n: 158 | rev*=10 159 | rev+=n%10 160 | n/=10 161 | n=int(n) 162 | 163 | if rev==temp: 164 | print("palindrome") 165 | else: 166 | print("not a palindrome") 167 | 168 | 169 | # Q. 24: Check Armstrong 170 | # QUESTION DESCRIPTION 171 | # 172 | # Write a program to check whether the given number is a armstrong number or not 173 | # 174 | # Input :Positive Number 175 | # 176 | # Output: Yes or No 177 | # 178 | # Refer sample input and output for formatting specification. 179 | # TEST CASE 1 180 | # 181 | # INPUT 182 | # 153 183 | # OUTPUT 184 | # Yes 185 | # TEST CASE 2 186 | # 187 | # INPUT 188 | # 123 189 | # OUTPUT 190 | # No 191 | 192 | # In[12]: 193 | 194 | 195 | n=int(input("")) 196 | n1=n 197 | sum=0 198 | while(n1>0): 199 | ne=n1 % 10 200 | sum+=(ne ** 3) 201 | n1=int(n1 / 10) 202 | if(sum==n): 203 | print('Yes') 204 | else: 205 | print('No') 206 | 207 | 208 | # Q. 25: Sum on n number 209 | # QUESTION DESCRIPTION 210 | # 211 | # Write a program to find the sum of numbers 212 | # Input 1:Negative number 213 | # 214 | # Output: 215 | # Display the appropriate error message 216 | # 217 | # Input 2: Positive number 218 | # 219 | # Output: 220 | # Display the sum 221 | # 222 | # Refer sample input and output for formatting specification. 223 | # TEST CASE 1 224 | # 225 | # INPUT 226 | # -1 227 | # OUTPUT 228 | # Enter a positive number 229 | # TEST CASE 2 230 | # 231 | # INPUT 232 | # 10 233 | # OUTPUT 234 | # The sum is 55 235 | 236 | # In[13]: 237 | 238 | 239 | n=int(input('')) 240 | s=0 241 | status = True 242 | if n < 0: 243 | print("Enter a positive number") 244 | else: 245 | for i in range(1,n+1): 246 | s=s+i 247 | print("The sum is",s) 248 | 249 | 250 | # Q. 26: Strong Number 251 | # QUESTION DESCRIPTION 252 | # 253 | # Write a program to check whether the given number is Strong number or not 254 | # 255 | # Input:Positive number 256 | # 257 | # Output:Display the appropriate message 258 | # 259 | # Refer sample input and output for formatting specification. 260 | # TEST CASE 1 261 | # 262 | # INPUT 263 | # 145 264 | # OUTPUT 265 | # The number is a strong number 266 | # TEST CASE 2 267 | # 268 | # INPUT 269 | # 2509 270 | # OUTPUT 271 | # The number is not a strong number 272 | 273 | # In[14]: 274 | 275 | 276 | sum1=0 277 | num=int(input('')) 278 | temp=num 279 | while(num): 280 | i=1 281 | f=1 282 | r=num%10 283 | while(i<=r): 284 | f=f*i 285 | i=i+1 286 | sum1=sum1+f 287 | num=num//10 288 | if(sum1==temp): 289 | print("The number is a strong number") 290 | else: 291 | print("The number is not a strong number") 292 | 293 | 294 | # Q. 27: Sum of Even and Odd 295 | # QUESTION DESCRIPTION 296 | # 297 | # Write a program to calculate the sum of even and odd numbers including its count. 298 | # 299 | # Input: 300 | # 1.Total number of Inputs 301 | # 2. Input elements 302 | # 303 | # Output: 304 | # 1. Odd numbers count 305 | # 2. Even numbers count 306 | # 3. Sum of even numbers 307 | # 4. Sum of odd numbers 308 | # TEST CASE 1 309 | # 310 | # INPUT 311 | # 10 312 | # 2 313 | # 4 314 | # 6 315 | # 8 316 | # 10 317 | # 1 318 | # 3 319 | # 5 320 | # 7 321 | # 9 322 | # OUTPUT 323 | # 5 324 | # 5 325 | # 30 326 | # 25 327 | # TEST CASE 2 328 | # 329 | # INPUT 330 | # 9 331 | # 2 332 | # 4 333 | # 6 334 | # 8 335 | # 10 336 | # 1 337 | # 3 338 | # 5 339 | # 7 340 | # OUTPUT 341 | # 4 342 | # 5 343 | # 30 344 | # 16 345 | 346 | # In[17]: 347 | 348 | 349 | evenno=[] 350 | oddno=[] 351 | sum=[0,0] 352 | num=int(input()) 353 | while num>0: 354 | ip=int(input()) 355 | if ip%2==0: 356 | evenno.append(ip) 357 | sum[0]+=ip 358 | else: 359 | oddno.append(ip) 360 | sum[1]+=ip 361 | num-=1 362 | print(len(oddno)) 363 | print(len(evenno)) 364 | print(str(sum[0])+'\n'+str(sum[1])) 365 | 366 | 367 | # Q. 28: Perfect Number 368 | # QUESTION DESCRIPTION 369 | # 370 | # Write a program to check whether the given number is perfect number or not 371 | # 372 | # Input:A positive number 373 | # 374 | # Output:Display the appropriate message 375 | # 376 | # Refer sample input and output for formatting specification. 377 | # TEST CASE 1 378 | # 379 | # INPUT 380 | # 144 381 | # OUTPUT 382 | # The number is not a Perfect number 383 | # TEST CASE 2 384 | # 385 | # INPUT 386 | # 6 387 | # OUTPUT 388 | # The number is a Perfect number 389 | 390 | # In[18]: 391 | 392 | 393 | n = int(input()) 394 | sum1 = 0 395 | for i in range(1, n): 396 | if(n % i == 0): 397 | sum1 = sum1 + i 398 | if (sum1 == n): 399 | print("The number is a Perfect number") 400 | else: 401 | print("The number is not a Perfect number") 402 | 403 | 404 | # Q. 29: Positive Odd 405 | # QUESTION DESCRIPTION 406 | # 407 | # Write a program to find the sum of positive odd numbers 408 | # 409 | # Input:Positive and negative numbers 410 | # 411 | # Output:Display Sum of positive odd numbers. 412 | # 413 | # Refer sample input and output for formatting specification. 414 | # TEST CASE 1 415 | # 416 | # INPUT 417 | # 5 418 | # 10 419 | # -10 420 | # 43 421 | # 45 422 | # 46 423 | # OUTPUT 424 | # Sum of positive odd numbers: 88 425 | # TEST CASE 2 426 | # 427 | # INPUT 428 | # 7 429 | # 10 430 | # -10 431 | # 43 432 | # 45 433 | # 46 434 | # 30 435 | # 31 436 | # OUTPUT 437 | # Sum of positive odd numbers: 119 438 | 439 | # In[20]: 440 | 441 | 442 | s=0 443 | n=int(input('')) 444 | i=1 445 | num=0 446 | while i <= n: 447 | num=int(input('')) 448 | i=i+1 449 | 450 | if num>0 and (num%2==1): 451 | s=s+num 452 | print("Sum of positive odd numbers:",s) 453 | 454 | 455 | # In[21]: 456 | 457 | #Q. 23: Sets 458 | #QUESTION DESCRIPTION 459 | 460 | #Write a program to do basic set operations 461 | 462 | #Input:Two Sets 463 | 464 | #Output: 465 | #Result of Union,intersection,difference and symmetric difference operations on the given input 466 | 467 | #Refer sample input and output for formatting specification. 468 | #TEST CASE 1 469 | 470 | #INPUT 471 | #8 472 | #5 473 | #0 474 | #1 475 | #2 476 | #3 477 | #4 478 | #5 479 | #6 480 | #8 481 | #1 482 | #2 483 | #3 484 | #4 485 | #5 486 | #OUTPUT 487 | #[0, 1, 2, 3, 4, 5, 6, 8] 488 | #[1, 2, 3, 4, 5] 489 | #Union is: {0, 1, 2, 3, 4, 5, 6, 8} 490 | #Intersection is {1, 2, 3, 4, 5} 491 | #Difference is {0, 6, 8} 492 | 493 | l={8,5,0,1,2,3,4,5,6,8} 494 | r={1,2,3,4,5} 495 | print(list(l)) 496 | print(list(r)) 497 | print("Union is:",l|r) 498 | print("Intersection is",l&r) 499 | print("Difference is",l^r) 500 | ----------------------------------------------------------------------------- 501 | #Q. 30: Remove special 502 | #QUESTION DESCRIPTION 503 | 504 | #Write a program to remove the special characters form the given text 505 | 506 | Input:Text with special characters embedded in it. 507 | 508 | Output: 509 | Text without special characters 510 | 511 | Refer sample input and output for formatting specification. 512 | TEST CASE 1 513 | 514 | #INPUT 515 | #Elab tool]]]]----\\\\\\\'\\\\\\\'\\\\\\\'\\\\\\\'\\\\\\\'\\\\\\\'!()-{}::\\\\\\\'\\\\\\\"]<> 2017 516 | #OUTPUT 517 | ##Elab tool 2017 518 | #TEST CASE 2 519 | 520 | #INPUT 521 | \\\\\\\'\\\\\\\'\\\\\\\'!()-[]{};:\\\\\\\'\\\\\\\"\\\\\\\\,<>./?@#$%^&*_~\\\\\\\'\\\\\\\'\\\\\\\'elab auto evaluation tool \\\\\\\'\\\\\\\'\\\\\\\'!()-[]{};:\\\\\\\'\\\\\\\"\\\\\\\\,<>./?@#$%^&*_~\\\\\\\'\\\\\\\'\\\\\\\' 522 | OUTPUT 523 | #elab auto evaluation tool 524 | ################# Code ######################### 525 | 526 | import re 527 | punct = ''']-'!()-{}::"<>,;/\?@#$%^&*_~[].''' 528 | inputstr = input() 529 | output="" 530 | for ch in inputstr: 531 | if ch not in punct : 532 | output=output+ch 533 | print (output) 534 | 535 | -------------------------------------------------------------------------------- /04 String.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # Q. 31: Alternative Game 5 | # QUESTION DESCRIPTION 6 | # 7 | # Write a Python program to remove the alternate characters in the entered string. 8 | # 9 | # Refer sample input and output for formatting specification. 10 | # 11 | # All float values are displayed correct to 2 decimal places. 12 | # 13 | # All text in bold corresponds to input and the rest corresponds to output. 14 | # TEST CASE 1 15 | # 16 | # INPUT 17 | # Remove alternate characters 18 | # OUTPUT 19 | # Rmv lent hrces 20 | # TEST CASE 2 21 | # 22 | # INPUT 23 | # abcdefghij 24 | # OUTPUT 25 | # acegi 26 | 27 | # In[1]: 28 | 29 | 30 | s=str (input('')) 31 | rmv="" 32 | l=len(s) 33 | for x in range(0,l): 34 | if x%2 == 0: 35 | rmv=rmv+s[x] 36 | print(rmv) 37 | 38 | 39 | # Q. 32: Sandwich Burger 40 | # QUESTION DESCRIPTION 41 | # 42 | # Write a program to display the string using LIST[A:B] type function. 43 | # TEST CASE 1 44 | # 45 | # INPUT 46 | # eLab Tool is ab 47 | # OUTPUT 48 | # eLab 49 | # TEST CASE 2 50 | # 51 | # INPUT 52 | # KUMARI KANDAM 53 | # OUTPUT 54 | # KUMA 55 | 56 | # In[2]: 57 | 58 | 59 | str=input() 60 | print(str[0:4]) 61 | 62 | 63 | # Q. 33: Who is Bigger here 64 | # QUESTION DESCRIPTION 65 | # 66 | # Write a Python program to count the number of Upper-case letters in the entered string. 67 | # 68 | # Refer sample input and output for formatting specification. 69 | # TEST CASE 1 70 | # 71 | # INPUT 72 | # The Output Will COUNT the Uppercase letters in eLab 73 | # OUTPUT 74 | # 10 75 | # TEST CASE 2 76 | # 77 | # INPUT 78 | # eLab Tool check for SPACES CASES SENsitive 79 | # OUTPUT 80 | # 16 81 | 82 | # In[3]: 83 | 84 | 85 | string =input('') 86 | count=0 87 | for i in string: 88 | if(i.isupper()): 89 | count=count+1 90 | print(count) 91 | 92 | 93 | # Q. 35: Index 94 | # QUESTION DESCRIPTION 95 | # 96 | # Write a python program to display the string using List [a : b ] 97 | # 98 | # For Example: 99 | # 100 | # We obtained the first example using the following statement in the program: 101 | # 102 | # print(string[0]) 103 | # TEST CASE 1 104 | # 105 | # INPUT 106 | # eLab Auto Evaluation 107 | # OUTPUT 108 | # e 109 | # eLab Auto Evaluation 110 | # eLab 111 | # Auto 112 | # Auto Evaluation 113 | # n 114 | # TEST CASE 2 115 | # 116 | # INPUT 117 | # This is a sample string 118 | # OUTPUT 119 | # T 120 | # This is a sample string 121 | # This 122 | # is a 123 | # is a sample string 124 | # g 125 | 126 | # In[5]: 127 | 128 | 129 | str=input() 130 | print(str[0]) 131 | print(str[:]) 132 | print(str[0:4]) 133 | print(str[5:9]) 134 | print(str[5:]) 135 | print(str[-1]) 136 | 137 | 138 | # Q. 37: Length Without Library 139 | # QUESTION DESCRIPTION 140 | # 141 | # Write a python program to find the length of the given string without using Library Function 142 | # 143 | # Refer sample input and output for formatting specification. 144 | # 145 | # All float values are displayed correct to 2 decimal places. 146 | # 147 | # All text in bold corresponds to input and the rest corresponds to output. 148 | # TEST CASE 1 149 | # 150 | # INPUT 151 | # eLab in India 152 | # OUTPUT 153 | # 13 154 | # TEST CASE 2 155 | # 156 | # INPUT 157 | # eLab tool has bee used by 40000 users in Tamilnadu alone 158 | # OUTPUT 159 | # 56 160 | 161 | # In[10]: 162 | 163 | 164 | string = (input ('')) 165 | print (len(string)) 166 | 167 | 168 | # Q. 38: consonant 169 | # QUESTION DESCRIPTION 170 | # 171 | # Write a Python program to get the string input from the user and find the number of consonant 172 | # 173 | # 174 | # Refer sample input and output for formatting specification. 175 | # 176 | # All float values are displayed correct to 2 decimal places. 177 | # 178 | # All text in bold corresponds to input and the rest corresponds to output. 179 | # TEST CASE 1 180 | # 181 | # INPUT 182 | # eLabAutoEvaluationTool 183 | # OUTPUT 184 | # 9 185 | # TEST CASE 2 186 | # 187 | # INPUT 188 | # trianglehasthreesides 189 | # OUTPUT 190 | # 13 191 | 192 | # In[11]: 193 | 194 | 195 | text=input() 196 | vowels=["A","E","I","O","U","a","e","i","o","u"] 197 | count=0 198 | for x in text: 199 | if not x in vowels: 200 | count+=1 201 | print(count) 202 | 203 | 204 | # Q. 39: CamelCase 205 | # QUESTION DESCRIPTION 206 | # 207 | # Alice wrote a sequence of words in CamelCase as a string of letters, S, having the following properties: 208 | # 1. It is a concatenation of one or more words consisting of English letters. 209 | # 2. All letters in the first word are lowercase. 210 | # 3. For each of the subsequent words, the first letter is uppercase and rest of the letters is lowercase. 211 | # Given S, print the number of words in S on a new line. 212 | # Input: 213 | # A single line containing string S. 214 | # Output: 215 | # Print the number of words in string S. 216 | # TEST CASE 1 217 | # 218 | # INPUT 219 | # thisIsIndiaWelcome 220 | # OUTPUT 221 | # 4 222 | # TEST CASE 2 223 | # 224 | # INPUT 225 | # hiThisIsTamilnaduInIndia 226 | # OUTPUT 227 | # 6 228 | 229 | # In[12]: 230 | 231 | 232 | def count(str): 233 | count = 1 234 | for i in range(1, len(str) - 1): 235 | if (str[i].isupper()): 236 | count += 1 237 | 238 | return count 239 | str =input ('') 240 | print(count(str)) 241 | 242 | 243 | # Q. 40: Count Me please with spaces 244 | # QUESTION DESCRIPTION 245 | # 246 | # Write a Python program to count the number of lower-case letter, uppercase letters and spaces in the entered string. 247 | # 248 | # Refer sample input and output for formatting specification. 249 | # 250 | # All float values are displayed correct to 2 decimal places. 251 | # 252 | # All text in bold corresponds to input and the rest corresponds to output 253 | # 254 | # Output: 255 | # 256 | # First Line: Lowercase count 257 | # Second Line: Uppercase count 258 | # Third Line: Spaces count in the string 259 | # TEST CASE 1 260 | # 261 | # INPUT 262 | # welcome to elab 263 | # OUTPUT 264 | # 13 265 | # 0 266 | # 2 267 | # TEST CASE 2 268 | # 269 | # INPUT 270 | # WELCOME TO Elab 271 | # OUTPUT 272 | # 3 273 | # 10 274 | # 8 275 | 276 | # In[13]: 277 | 278 | 279 | str=input(""); 280 | l=0; 281 | u=0; 282 | for i in str: 283 | if(i.islower()): 284 | l=l+1 285 | elif(i.isupper()): 286 | u=u+1 287 | print(l) 288 | print(u) 289 | print(str.count(" ")) 290 | 291 | 292 | # Replace me if you can 293 | 294 | ####### Code ##################### 295 | 296 | 297 | s=input('') 298 | i=input('') 299 | print(s.replace(i,"#")) 300 | 301 | ----------------------------------------------------------------------- 302 | 303 | # Q - Check the Functions 304 | 305 | ########Code ############# 306 | string =str(input()) 307 | print(string.isalpha()) 308 | print(string.isdigit()) 309 | print(string.istitle()) 310 | print(string.isupper()) 311 | print(string.islower()) 312 | 313 | -------------------------------------------------------------------------------- /05 Functions.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # Q. 41: LCM Check 5 | # QUESTION DESCRIPTION 6 | # 7 | # Write a python function to calculate the LCM of numbers 8 | # 9 | # Refer sample input and output for formatting specification. 10 | # 11 | # All float values are displayed correct to 2 decimal places. 12 | # 13 | # All text in bold corresponds to input and the rest corresponds to output 14 | # TEST CASE 1 15 | # 16 | # INPUT 17 | # 5 18 | # 3 19 | # OUTPUT 20 | # LCM is: 15 21 | # TEST CASE 2 22 | # 23 | # INPUT 24 | # 15 25 | # 20 26 | # OUTPUT 27 | # LCM is: 60 28 | 29 | # In[4]: 30 | 31 | 32 | num1= int (input ()) 33 | num2= int (input ()) 34 | def gcd (num1,num2): 35 | 36 | while num2>0: 37 | num1,num2=num2,num1%num2 38 | return num1 39 | 40 | def lcm (num1,num2): 41 | 42 | return num1*num2/gcd(num1,num2) 43 | 44 | print("LCM is:",round(lcm(num1,num2))) 45 | 46 | 47 | # Q. 43: Gravitational Force 48 | # QUESTION DESCRIPTION 49 | # 50 | # Python Program to Find the Gravitational Force Acting Between Two Objects 51 | # 52 | # Input 53 | # 54 | # First Line :Value of first mass 55 | # Second Line:Value second mass 56 | # Third Line: Distance between the centres of the masses 57 | # TEST CASE 1 58 | # 59 | # INPUT 60 | # 1000000 61 | # 500000 62 | # 20 63 | # OUTPUT 64 | # 0.08 N 65 | # TEST CASE 2 66 | # 67 | # INPUT 68 | # 90000000 69 | # 7000000 70 | # 20 71 | # OUTPUT 72 | # 105.1 N 73 | 74 | # In[6]: 75 | 76 | 77 | m1=float(input("")); 78 | m2=float(input("")); 79 | r=float(input("")); 80 | G=6.673*(10**-11) 81 | f=(G*m1*m2)/(r**2) 82 | print(round(f,2),"N") 83 | 84 | 85 | # Q. 44: Solve Equation 86 | # QUESTION DESCRIPTION 87 | # 88 | # Python Program to Find the Sum of the Series: 1 + 1/2 + 1/3 + .. + 1/N 89 | # 90 | # Input: 91 | # 92 | # The number of terms 93 | # 94 | # Output 95 | # 96 | # Print the Sum of Series 97 | # TEST CASE 1 98 | # 99 | # INPUT 100 | # 7 101 | # OUTPUT 102 | # 2.59 103 | # TEST CASE 2 104 | # 105 | # INPUT 106 | # 15 107 | # OUTPUT 108 | # 3.32 109 | 110 | # In[7]: 111 | 112 | 113 | n=int(input("")) 114 | sum1=0 115 | for i in range(1,n+1): 116 | sum1=sum1+(1/i) 117 | print(round(sum1,2)) 118 | 119 | 120 | # Q. 45: Perfect Number 121 | # QUESTION DESCRIPTION 122 | # 123 | # Write a Python Program to Check if a Number is a Perfect Number 124 | # TEST CASE 1 125 | # 126 | # INPUT 127 | # 6 128 | # OUTPUT 129 | # The number is a Perfect number! 130 | # TEST CASE 2 131 | # 132 | # INPUT 133 | # 25 134 | # OUTPUT 135 | # The number is not a Perfect number! 136 | 137 | # In[8]: 138 | 139 | 140 | n = int(input("")) 141 | sum1 = 0 142 | for i in range(1, n): 143 | if(n % i == 0): 144 | sum1 = sum1 + i 145 | if (sum1 == n): 146 | print("The number is a Perfect number!") 147 | else: 148 | print("The number is not a Perfect number!") 149 | 150 | 151 | # Q. 46: Taxi Fare 152 | # QUESTION DESCRIPTION 153 | # 154 | # In a particular jurisdiction in India after implementing GST the taxi fares are altered. Taxi fares consist of a base fare of 200 and GST tax as 4% of base fare for every 140 meters traveled. Write a function that takes the distance traveled (in kilometers) as its only parameter and returns the total fare as its only result. Write a main program that demonstrates the function. 155 | # 156 | # Refer sample input and output for formatting specification. 157 | # 158 | # All float values are displayed correct to 2 decimal places. 159 | # 160 | # All text in bold corresponds to input and the rest corresponds to output 161 | # TEST CASE 1 162 | # 163 | # INPUT 164 | # 25 165 | # OUTPUT 166 | # 1628.57 167 | # TEST CASE 2 168 | # 169 | # INPUT 170 | # 225 171 | # OUTPUT 172 | # 13057.14 173 | 174 | # In[9]: 175 | 176 | 177 | base_fare=200 178 | GST_rate=8 179 | def fare(kms): 180 | meters=kms*1000 181 | cost_calculate=meters/140 182 | tax=cost_calculate*GST_rate 183 | total=base_fare+tax 184 | print(round(total,2)) 185 | def main(): 186 | kms=int(input("")) 187 | fare(kms) 188 | main() 189 | 190 | 191 | # Q. 47: Check My Password 192 | # QUESTION DESCRIPTION 193 | # 194 | # In this exercise you will write a function that determines whether or not a password is good. We will define a good password to be a one that is at least 8 characters long and contains at least one uppercase letter, at least one lowercase letter, and at least one number,one Special character. 195 | # 196 | # Your function should return true if the password passed to it as its only parameter is good. 197 | # 198 | # Otherwise it should return false. Include a main program that reads a password from the user and reports whether or not it is good. 199 | # 200 | # Ensure that your main program only runs when your solution has not been imported into another file. 201 | # 202 | # Refer sample input and output for formatting specification. 203 | # 204 | # All float values are displayed correct to 2 decimal places. 205 | # 206 | # All text in bold corresponds to input and the rest corresponds to output 207 | # TEST CASE 1 208 | # 209 | # INPUT 210 | # eLabTool2017# 211 | # OUTPUT 212 | # Good Password 213 | # TEST CASE 2 214 | # 215 | # INPUT 216 | # elabtool2017 217 | # OUTPUT 218 | # Try Again 219 | 220 | # In[10]: 221 | 222 | 223 | import re 224 | 225 | def passwordv(strw): 226 | pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$" 227 | result = re.findall(pattern, strw) 228 | if (result): 229 | print( "Good Password") 230 | else: 231 | print ("Try Again") 232 | 233 | strw= input() 234 | passwordv(strw) 235 | 236 | 237 | # Q. 48: Prime Numbers 238 | # QUESTION DESCRIPTION 239 | # 240 | # Python Program to Print all the Prime Numbers within a Given Range 241 | # 242 | # Input : 243 | # 244 | # Upper Limit 245 | # 246 | # Output : 247 | # 248 | # Print the prime numbers within the specified limit 249 | # TEST CASE 1 250 | # 251 | # INPUT 252 | # 15 253 | # OUTPUT 254 | # 2 255 | # 3 256 | # 5 257 | # 7 258 | # 11 259 | # 13 260 | # TEST CASE 2 261 | # 262 | # INPUT 263 | # 40 264 | # OUTPUT 265 | # 2 266 | # 3 267 | # 5 268 | # 7 269 | # 11 270 | # 13 271 | # 17 272 | # 19 273 | # 23 274 | # 29 275 | # 31 276 | # 37 277 | 278 | # In[11]: 279 | 280 | 281 | upper = int(input("")); 282 | for num in range(2,upper): 283 | for i in range(2,num): 284 | if (num % i) == 0: 285 | break 286 | else: 287 | print(num) 288 | 289 | 290 | # Q. 49: Grade System 291 | # QUESTION DESCRIPTION 292 | # 293 | # Write a python function to calculate the GRADE systems of marks 294 | # 295 | # Refer sample input and output for formatting specification. 296 | # 297 | # All float values are displayed correct to 2 decimal places. 298 | # 299 | # All text in bold corresponds to input and the rest corresponds to output 300 | # 301 | # Input : 302 | # Input 5 numbers 303 | # 304 | # Output: 305 | # 306 | # 1. If the average is above 90 then print Grade as "A" (Hint : 90 and above) 307 | # 2. If the average is above 70 and less than 90 then print Grade as "B" (Hint: 71-89) 308 | # 3. If the average is above 50 and less than 70 then print Grade as "C"(Hint: 51-70) 309 | # 4. If the average is 50 or less then then print as "D" 310 | # TEST CASE 1 311 | # 312 | # INPUT 313 | # 99 314 | # 98 315 | # 87 316 | # 99 317 | # 90 318 | # OUTPUT 319 | # Grade:A 320 | # TEST CASE 2 321 | # 322 | # INPUT 323 | # 71 324 | # 77 325 | # 67 326 | # 89 327 | # 89 328 | # OUTPUT 329 | # Grade:B 330 | 331 | # In[12]: 332 | 333 | 334 | a=int(input('')) 335 | b=int(input('')) 336 | c=int(input('')) 337 | d=int(input('')) 338 | e=int(input('')) 339 | avg=(a+b+c+d+e)/5 340 | print("Grade:",end="") 341 | if (avg>90): 342 | grade='A' 343 | elif (avg>70): 344 | grade='B' 345 | elif avg>50: 346 | grade='C' 347 | elif avg<=50 : 348 | grade='D' 349 | print(grade) 350 | 351 | 352 | # Q. 50: GCD Check 353 | # QUESTION DESCRIPTION 354 | # 355 | # Write a python function to calculate the GCD of numbers 356 | # 357 | # Refer sample input and output for formatting specification. 358 | # 359 | # All float values are displayed correct to 2 decimal places. 360 | # 361 | # All text in bold corresponds to input and the rest corresponds to output 362 | # TEST CASE 1 363 | # 364 | # INPUT 365 | # 15 366 | # 5 367 | # OUTPUT 368 | # The GCD of the two numbers is 5 369 | # TEST CASE 2 370 | # 371 | # INPUT 372 | # 81 373 | # 9 374 | # OUTPUT 375 | # The GCD of the two numbers is 9 376 | 377 | # In[13]: 378 | 379 | 380 | import fractions 381 | a=int(input("")) 382 | b=int(input("")) 383 | print("The GCD of the two numbers is",fractions.gcd(a,b)) 384 | 385 | -------------------------------------------------------------------------------- /06 Lists and Tuples.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # Q. 51: Number and Square 5 | # QUESTION DESCRIPTION 6 | # 7 | # Python Program to create a list of tuples with the first element as the number and the second element as the square of the number 8 | # 9 | # Input: 10 | # First Line: Lower Range 11 | # Second Line:Upper Range 12 | # 13 | # Output: 14 | # Print the List of Values With the number and the square of that number. 15 | # TEST CASE 1 16 | # 17 | # INPUT 18 | # 1 19 | # 4 20 | # OUTPUT 21 | # (1 1) (2 4) (3 9) (4 16) 22 | # TEST CASE 2 23 | # 24 | # INPUT 25 | # 45 26 | # 49 27 | # OUTPUT 28 | # (45 2025) (46 2116) (47 2209) (48 2304) (49 2401) 29 | 30 | # In[ ]: 31 | 32 | 33 | l_range=int(input("")) 34 | u_range=int(input("")) 35 | a=[(x) for x in range(l_range,u_range+1)] 36 | b=[(x*x) for x in range(l_range,u_range+1)] 37 | c=len(a) 38 | for x in range(c): 39 | if(x 1: 139 | l, r = p.get(), p.get() 140 | node = HuffmanNode(l, r) 141 | p.put((l[0]+r[0], node)) 142 | return p.get() 143 | 144 | node = create_tree(freq) 145 | 146 | def walk_tree(node, prefix="", code={}): 147 | if isinstance(node[1].left[1], HuffmanNode): 148 | walk_tree(node[1].left,prefix+"0", code) 149 | else: 150 | code[node[1].left[1]]=prefix+"0" 151 | if isinstance(node[1].right[1],HuffmanNode): 152 | walk_tree(node[1].right,prefix+"1", code) 153 | else: 154 | code[node[1].right[1]]=prefix+"1" 155 | return(code) 156 | 157 | code = walk_tree(node) 158 | l3=[] 159 | for j in code.values(): 160 | l3.append(eval(j)) 161 | print(l3) 162 | 163 | 164 | # Q. 63: Diagonal Sum 165 | # QUESTION DESCRIPTION 166 | # 167 | # Write a python program to create a NESTED LIST and print the sum of the diagonal elements in the matrix 168 | # 169 | # Hint: 170 | # 171 | # 1. Input the number of rows First Matrix 172 | # 173 | # 2. Input the number of Columns for Second Matrix 174 | # 175 | # 3. Display the sum of the diagonal elements in the matrix 176 | # TEST CASE 1 177 | # 178 | # INPUT 179 | # 2 180 | # 2 181 | # 10 182 | # 20 183 | # 30 184 | # 40 185 | # OUTPUT 186 | # 50 187 | # TEST CASE 2 188 | # 189 | # INPUT 190 | # 3 191 | # 3 192 | # 10 193 | # 20 194 | # 30 195 | # 40 196 | # 50 197 | # 60 198 | # 70 199 | # 80 200 | # 90 201 | # OUTPUT 202 | # 150 203 | 204 | # In[ ]: 205 | 206 | 207 | a=int(input("")) 208 | b=int(input("")) 209 | sum1=0 210 | c=[[int(input("")) for j in range(a)] for i in range(b)] 211 | for i in range(a): 212 | for j in range(b): 213 | if i==j: 214 | sum1=sum1+c[i][j] 215 | print(sum1) 216 | 217 | 218 | # Q. 64: Maximum Rectangular Area in a Histogram 219 | # QUESTION DESCRIPTION 220 | # 221 | # Find the largest rectangular area possible in a given histogram where the largest rectangle can be made of a number of contiguous bars. For simplicity, assume that all bars have same width and the width is 1 unit. 222 | # 223 | # 224 | # 225 | # 226 | # 227 | # Input: 228 | # The first line contains an integer 'T' denoting the total number of test cases. In each test cases, the first line contains an integer 'N' denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array. The elements of the array represents the height of the bars. 229 | # 230 | # 231 | # Output: 232 | # In each seperate line the maximum rectangular area possible from the contigous bars. 233 | # 234 | # 235 | # Constraints: 236 | # 1<=T<=30 237 | # 1<=N<=100 238 | # 1<=A[i]<=1000 239 | # TEST CASE 1 240 | # 241 | # INPUT 242 | # 1 243 | # 7 244 | # 6 2 5 4 5 1 6 245 | # OUTPUT 246 | # 12 247 | 248 | # In[ ]: 249 | 250 | 251 | a=eval(input()) 252 | b=eval(input()) 253 | c=input() 254 | d=c.split() 255 | l1=[] 256 | l1=[eval(x) for x in d] 257 | l1.sort() 258 | print(max(l1)*2) 259 | #print(l1+l1) 260 | 261 | 262 | # Q. 65: Matrix Display -Type 1 263 | # QUESTION DESCRIPTION 264 | # 265 | # Write a python program to create a NESTED LIST and form a matrix. 266 | # 267 | # Hint: 268 | # 269 | # 1. Input the number of rows and columns for First Matrix 270 | # 271 | # 2. Input the number of rows and columns for Second Matrix 272 | # 273 | # 3. Display the first matrix elements in Matrix format 274 | # 275 | # 4. Display the first matrix elements in Matrix format 276 | # TEST CASE 1 277 | # 278 | # INPUT 279 | # 2 280 | # 2 281 | # 10 282 | # 20 283 | # 30 284 | # 40 285 | # 1 286 | # 2 287 | # 3 288 | # 4 289 | # OUTPUT 290 | # Matrix 1 291 | # [10, 20] 292 | # [30, 40] 293 | # Matrix 2 294 | # [1, 2] 295 | # [3, 4] 296 | # TEST CASE 2 297 | # 298 | # INPUT 299 | # 3 300 | # 3 301 | # 1 302 | # 2 303 | # 3 304 | # 4 305 | # 5 306 | # 6 307 | # 7 308 | # 8 309 | # 9 310 | # 10 311 | # 20 312 | # 30 313 | # 40 314 | # 50 315 | # 60 316 | # 70 317 | # 80 318 | # 90 319 | # OUTPUT 320 | # Matrix 1 321 | # [1, 2, 3] 322 | # [4, 5, 6] 323 | # [7, 8, 9] 324 | # Matrix 2 325 | # [10, 20, 30] 326 | # [40, 50, 60] 327 | # [70, 80, 90] 328 | 329 | # In[ ]: 330 | 331 | 332 | n=int(input("")) 333 | m=int(input("")) 334 | a = [[0] * n for i in range(n)] 335 | b = [[0] * n for i in range(n)] 336 | for i in range(0,n): 337 | for j in range(0,n): 338 | a[i][j]=int(input("")) 339 | for i in range(0,m): 340 | for j in range(0,m): 341 | e=input("") 342 | b[i][j]=int(e) 343 | print("Matrix 1") 344 | for r in a: 345 | print(r) 346 | print("Matrix 2") 347 | for r in b: 348 | print(r) 349 | 350 | 351 | # Q. 66: Identity Matrix 352 | # QUESTION DESCRIPTION 353 | # 354 | # Write Python Program to read a number n and print an identity matrix of the desired size. 355 | # 356 | # Input: 357 | # Size of the matrix 358 | # 359 | # Output: 360 | # Print the Identity matrix 361 | # TEST CASE 1 362 | # 363 | # INPUT 364 | # 4 365 | # OUTPUT 366 | # 1 0 0 0 367 | # 0 1 0 0 368 | # 0 0 1 0 369 | # 0 0 0 1 370 | # TEST CASE 2 371 | # 372 | # INPUT 373 | # 5 374 | # OUTPUT 375 | # 1 0 0 0 0 376 | # 0 1 0 0 0 377 | # 0 0 1 0 0 378 | # 0 0 0 1 0 379 | # 0 0 0 0 1 380 | 381 | # In[ ]: 382 | 383 | 384 | n=int(input()) 385 | for i in range(0,n): 386 | for j in range(0,n): 387 | if(i==j): 388 | print("1",sep=" ",end=" ") 389 | else: 390 | print("0",sep=" ",end=" ") 391 | print() 392 | 393 | 394 | # Q. 67: Transpose of Matrix 395 | # QUESTION DESCRIPTION 396 | # 397 | # Write a python program to create a NESTED LIST and print the diagonal elements of the matrix 398 | # 399 | # Hint: 400 | # 401 | # 1. Input the number of rows First Matrix 402 | # 403 | # 2. Input the number of Columns for first Matrix 404 | # 405 | # 3. Display the elements of the matrix 406 | # 407 | # 4. Display the transpose of the matrix 408 | # TEST CASE 1 409 | # 410 | # INPUT 411 | # 2 412 | # 2 413 | # 1 414 | # 2 415 | # 3 416 | # 4 417 | # OUTPUT 418 | # Given Matrix 419 | # [1, 2] 420 | # [3, 4] 421 | # Transpose of the matrix 422 | # [1, 3] 423 | # [2, 4] 424 | # TEST CASE 2 425 | # 426 | # INPUT 427 | # 3 428 | # 3 429 | # 10 430 | # 20 431 | # 30 432 | # 40 433 | # 50 434 | # 60 435 | # 70 436 | # 80 437 | # 90 438 | # OUTPUT 439 | # Given Matrix 440 | # [10, 20, 30] 441 | # [40, 50, 60] 442 | # [70, 80, 90] 443 | # Transpose of the matrix 444 | # [10, 40, 70] 445 | # [20, 50, 80] 446 | # [30, 60, 90] 447 | 448 | # In[ ]: 449 | 450 | 451 | n=int(input("")) 452 | m=int(input("")) 453 | a = [[0] * n for i in range(n)] 454 | b = [[0] * n for i in range(m)] 455 | matrix=[[0] * n for i in range(n)] 456 | for i in range(0,n): 457 | for j in range(0,m): 458 | a[i][j]=int(input("")) 459 | print("Given Matrix") 460 | for r in a: 461 | print(r) 462 | result=[[0,0], 463 | [0,0]] 464 | result1=[[0,0,0], 465 | [0,0,0], 466 | [0,0,0]] 467 | if(n==2): 468 | print("Transpose of the matrix") 469 | for i in range(len(a)): 470 | for j in range(len(a[0])): 471 | result[j][i] = a[i][j] 472 | if(n==2): 473 | for r in result: 474 | print(r) 475 | if(n==3): 476 | print("Transpose of the matrix") 477 | for i in range(len(a)): 478 | for j in range(len(a[0])): 479 | result1[j][i] = a[i][j] 480 | if(n==3): 481 | for r in result1: 482 | print(r) 483 | 484 | 485 | # Q. 68: Row with minimum number of 1's 486 | # QUESTION DESCRIPTION 487 | # 488 | # Determine the row index with minimum number of ones. The given 2D matrix has only zeroes and ones and also the matrix is sorted row wise . If two or more rows have same number of 1's than print the row with smallest index. 489 | # 490 | # Note: If there is no '1' in any of the row than print '-1'. 491 | # 492 | # Input: 493 | # The first line of input contains an integer T denoting the number of test cases. The first line of each test case consists of two integer n and m. The next line consists of n*m spaced integers. 494 | # 495 | # Output: 496 | # Print the index of the row with minimum number of 1's. 497 | # 498 | # Constraints: 499 | # 1<=T<=200 500 | # 1<=n,m<=100 501 | # TEST CASE 1 502 | # 503 | # INPUT 504 | # 2 505 | # 3 3 506 | # 0 0 0 0 0 0 0 0 0 507 | # 4 4 508 | # 0 0 0 1 0 1 1 1 0 0 1 1 0 0 1 1 509 | # OUTPUT 510 | # minus1 511 | # 0 512 | 513 | # In[ ]: 514 | 515 | 516 | t = int(input()) 517 | 518 | for i in range(t): 519 | n = input() 520 | t = n.split() 521 | r = int(t[0]) 522 | c = int(t[1]) 523 | tot = r * c 524 | mat = [] 525 | inp = input() 526 | form = inp.split() 527 | 528 | inx = 0 529 | for i in range(r): 530 | tmp = [] 531 | for j in range(c): 532 | e = int(form[inx]) 533 | inx += 1 534 | tmp.append(e) 535 | mat.append(tmp) 536 | #Matrix ready 537 | #print(mat) 538 | 539 | flag = -999 540 | mi = 999 541 | #Check for row with min 1s 542 | res = -1 543 | index = 0 544 | for sub in mat: 545 | cnt = sub.count(1) 546 | if cnt !=0 : 547 | if cnt < mi: 548 | mi = cnt 549 | res = index 550 | flag = 0 551 | 552 | index += 1 553 | if flag == -999: 554 | print("minus1") 555 | else: 556 | print(res) 557 | 558 | 559 | # Q. 69: Find nth element of spiral matrix 560 | # QUESTION DESCRIPTION 561 | # 562 | # Given a matrix your task is to find the kth element which is obtained while traversing the matrix spirally. You need to complete the method findK which takes four arguments the first argument is the matrix A and the next two arguments will be n and m denoting the size of the matrix A and then the forth argument is an integer k denoting the kth element . The function will return the kth element obtained while traversing the matrix spirally. 563 | # 564 | # 565 | # Input: 566 | # The first line of input is the no of test cases then T test cases follow . The first line of each test case has three integers n,m and k . Then in the next line are n*m space separated values of the matrix A [ ] [ ] . 567 | # 568 | # Output: 569 | # The output for each test case will be the kth obtained element . 570 | # 571 | # Constraints: 572 | # 1<=T<=100 573 | # 1<=n,m<=20 574 | # 1<=k<=n*m 575 | # TEST CASE 1 576 | # 577 | # INPUT 578 | # 1 579 | # 3 3 4 580 | # 1 2 3 4 5 6 7 8 9 581 | # OUTPUT 582 | # 6 583 | 584 | # In[ ]: 585 | 586 | 587 | a=eval(input()) 588 | b=input() 589 | q=(b.split()) 590 | d=input() 591 | me=(d.split()) 592 | m=[] 593 | r=int(q[0]) 594 | c=int(q[1]) 595 | f=int(q[2]) 596 | i=0 597 | for row in range(r): 598 | m.append([]) 599 | for col in range(c): 600 | m[row].append(me[i]) 601 | i+=1 602 | if f<=c: 603 | for row in range(0,1): 604 | print(m[row][f], end = " ") 605 | if f>c: 606 | if f==c+1: 607 | for row in range(1,2): 608 | print(m[row][col], end = " ") 609 | print() 610 | 611 | 612 | # Q. 70: Overlapping rectangles 613 | # QUESTION DESCRIPTION 614 | # 615 | # Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by providing the x and y co-ordinates of two points: the left top corner and the right bottom corner of the rectangle. 616 | # 617 | # Note that two rectangles sharing a side are considered overlapping. 618 | # 619 | # rectanglesOverlap 620 | # 621 | # Input: 622 | # 623 | # The first integer T denotes the number of test cases. For every test case, there are 2 lines of input. The first line consists of 4 integers: denoting the co-ordinates of the 2 points of the first rectangle. The first integer denotes the x co-ordinate and the second integer denotes the y co-ordinate of the left topmost corner of the first rectangle. The next two integers are the x and y co-ordinates of right bottom corner. Similarly, the second line denotes the cordinates of the two points of the second rectangle. 624 | # 625 | # Output: 626 | # 627 | # For each test case, output (either 1 or 0) denoting whether the 2 rectangles are overlapping. 1 denotes the rectangles overlap whereas 0 denotes the rectangles do not overlap. 628 | # 629 | # Constraints: 630 | # 631 | # 1 <= T <= 10 632 | # 633 | # -10000 <= x,y <= 10000 634 | # 635 | # T denotes the number of test cases. x denotes the x co-ordinate and y denotes the y co-ordinate. 636 | # TEST CASE 1 637 | # 638 | # INPUT 639 | # 2 640 | # 0 10 10 0 641 | # 5 5 15 0 642 | # 0 2 1 1 643 | # -2 -3 0 2 644 | # OUTPUT 645 | # 1 646 | # 0 647 | 648 | # In[ ]: 649 | 650 | 651 | def ovre(x,y): 652 | t=0 653 | for z in range(x[0],x[1]): 654 | if z == y[0] or x == y[1]: 655 | t=1 656 | break 657 | if t==1: 658 | return 1 659 | else: 660 | return 0 661 | 662 | a=eval(input()) 663 | b=input() 664 | c=b.split() 665 | d=input() 666 | e=d.split() 667 | f=input() 668 | g=f.split() 669 | h=input() 670 | i=h.split() 671 | l=[] 672 | l1=[] 673 | l2=[] 674 | l3=[] 675 | l=[eval(x) for x in c] 676 | l1=[eval(x) for x in e] 677 | l2=[eval(x) for x in g] 678 | l3=[eval(x) for x in i] 679 | print(ovre(l,l1)) 680 | print(ovre(l1,l2)) 681 | 682 | 683 | # In[ ]: 684 | 685 | 686 | 687 | 688 | -------------------------------------------------------------------------------- /08 Session Dictionary.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # Q. 71: Display Values 5 | # QUESTION DESCRIPTION 6 | # 7 | # Write a program to display only the values of the keys in the output 8 | # 9 | # Input: 10 | # 11 | # 1.Get two dictionaries key-value elements 12 | # 13 | # Output: 14 | # 1. Display the first dictionary 15 | # 2. Display the second dictionary 16 | # 3. Display the concatenated dictionary 17 | # 4. Display the values of the concatenated dictionary 18 | # 19 | # Refer sample input and output for formatting specification. 20 | # 21 | # All float values are displayed correct to 2 decimal places. 22 | # 23 | # All text in bold corresponds to input and the rest corresponds to output 24 | # TEST CASE 1 25 | # 26 | # INPUT 27 | # 1 28 | # 111 29 | # 2 30 | # 222 31 | # OUTPUT 32 | # First Dictionary 33 | # {1: 111} 34 | # Second Dictionary 35 | # {2: 222} 36 | # Concatenated dictionary is 37 | # {1: 111, 2: 222} 38 | # The Values of Dictionary 39 | # [111, 222] 40 | # TEST CASE 2 41 | # 42 | # INPUT 43 | # 3 44 | # 333 45 | # 4 46 | # 444 47 | # OUTPUT 48 | # First Dictionary 49 | # {3: 333} 50 | # Second Dictionary 51 | # {4: 444} 52 | # Concatenated dictionary is 53 | # {3: 333, 4: 444} 54 | # The Values of Dictionary 55 | # [333, 444] 56 | 57 | # In[ ]: 58 | 59 | 60 | d1={}; 61 | d2={}; 62 | d3={}; 63 | x=int(input()); 64 | d1[x]=int(input()); 65 | y=int(input()); 66 | d2[y]=int(input()); 67 | d3.update(d1); 68 | d3.update(d2); 69 | print("First Dictionary"); 70 | print(d1); 71 | print("Second Dictionary"); 72 | print(d2); 73 | print("Concatenated dictionary is"); 74 | print(d3); 75 | print("The Values of Dictionary"); 76 | x=[]; 77 | for i in d3.values(): 78 | x.append(i); 79 | print(x); 80 | 81 | 82 | # Q. 72: Has Keys 83 | # QUESTION DESCRIPTION 84 | # 85 | # Write a program to check whether the given key is present in the dictionary. 86 | # 87 | # If the key is present then display the value of the entered key and if the key is not present then display the appropriate message 88 | # 89 | # Input: 90 | # 91 | # 1.Get size of the dictionary 92 | # 2. Get the key-value elements 93 | # 3. Get the key to be searched 94 | # 95 | # Output: 96 | # 1. Display the dictionary 97 | # 2. Print either True or False 98 | # 99 | # Refer sample input and output for formatting specification. 100 | # 101 | # All float values are displayed correct to 2 decimal places. 102 | # 103 | # All text in bold corresponds to input and the rest corresponds to output 104 | # TEST CASE 1 105 | # 106 | # INPUT 107 | # 2 108 | # 25 109 | # 20 110 | # 1252 111 | # 1120 112 | # 20 113 | # OUTPUT 114 | # The dictionary is 115 | # {25: 1252, 20: 1120} 116 | # True 117 | # TEST CASE 2 118 | # 119 | # INPUT 120 | # 3 121 | # 25 122 | # 20 123 | # 9 124 | # 1252 125 | # 1120 126 | # 9009 127 | # 11 128 | # OUTPUT 129 | # The dictionary is 130 | # {25: 1252, 20: 1120, 9: 9009} 131 | # False 132 | 133 | # In[ ]: 134 | 135 | 136 | n=int(input()); 137 | m=n; 138 | k=n; 139 | x=[]; 140 | d={}; 141 | while(n>0): 142 | a=int(input()); 143 | x.append(a); 144 | n-=1; 145 | i=0; 146 | while(m>0): 147 | d[x[i]]=int(input()); 148 | i+=1; 149 | m=m-1; 150 | g=int(input()); 151 | print("The dictionary is"); 152 | print(d); 153 | if(g in d.keys()): 154 | print("True"); 155 | else: 156 | print("False"); 157 | 158 | 159 | # Q. 73: Display Values 160 | # QUESTION DESCRIPTION 161 | # 162 | # Write a program to display only the values of the keys in the output 163 | # 164 | # Input: 165 | # 166 | # 1.Get two dictionaries key-value elements 167 | # 168 | # Output: 169 | # 1. Display the first dictionary 170 | # 2. Display the second dictionary 171 | # 3. Display the concatenated dictionary 172 | # 4. Display the values of the concatenated dictionary 173 | # 174 | # Refer sample input and output for formatting specification. 175 | # 176 | # All float values are displayed correct to 2 decimal places. 177 | # 178 | # All text in bold corresponds to input and the rest corresponds to output 179 | # TEST CASE 1 180 | # 181 | # INPUT 182 | # 1 183 | # 111 184 | # 2 185 | # 222 186 | # OUTPUT 187 | # First Dictionary 188 | # {1: 111} 189 | # Second Dictionary 190 | # {2: 222} 191 | # Concatenated dictionary is 192 | # {1: 111, 2: 222} 193 | # The Values of Dictionary 194 | # [111, 222] 195 | # TEST CASE 2 196 | # 197 | # INPUT 198 | # 3 199 | # 333 200 | # 4 201 | # 444 202 | # OUTPUT 203 | # First Dictionary 204 | # {3: 333} 205 | # Second Dictionary 206 | # {4: 444} 207 | # Concatenated dictionary is 208 | # {3: 333, 4: 444} 209 | # The Values of Dictionary 210 | # [333, 444] 211 | 212 | # In[ ]: 213 | 214 | 215 | d1={}; 216 | d2={}; 217 | d3={}; 218 | x=int(input()); 219 | d1[x]=int(input()); 220 | y=int(input()); 221 | d2[y]=int(input()); 222 | d3.update(d1); 223 | d3.update(d2); 224 | print("First Dictionary"); 225 | print(d1); 226 | print("Second Dictionary"); 227 | print(d2); 228 | print("Concatenated dictionary is"); 229 | print(d3); 230 | print("The Values of Dictionary"); 231 | x=[]; 232 | for i in d3.values(): 233 | x.append(i); 234 | print(x); 235 | 236 | 237 | # Q. 74: Print the dictionary 238 | # QUESTION DESCRIPTION 239 | # 240 | # Write a program to get the input of key and value of the element in dictionary. 241 | # 242 | # Display the key-value pair in the python dictionary format. 243 | # 244 | # Refer sample input and output for formatting specification. 245 | # 246 | # All float values are displayed correct to 2 decimal places. 247 | # 248 | # All text in bold corresponds to input and the rest corresponds to output. 249 | # TEST CASE 1 250 | # 251 | # INPUT 252 | # 25 253 | # 75 254 | # OUTPUT 255 | # Updated dictionary is: 256 | # {25: 75} 257 | # TEST CASE 2 258 | # 259 | # INPUT 260 | # 2017 261 | # 2019 262 | # OUTPUT 263 | # Updated dictionary is: 264 | # {2017: 2019} 265 | 266 | # In[ ]: 267 | 268 | 269 | d=dict() 270 | for x in range(0,2): 271 | d[x]=int(input("")) 272 | print("Updated dictionary is:") 273 | print("{%d: %d}" %(d[0],d[1])) 274 | 275 | 276 | # Q. 75: Display Items 277 | # QUESTION DESCRIPTION 278 | # 279 | # Write a program to get four dictionary items and display all the key-values format using update function and items() function 280 | # TEST CASE 1 281 | # 282 | # INPUT 283 | # 10 284 | # -10 285 | # 20 286 | # -20 287 | # 30 288 | # -30 289 | # 40 290 | # -40 291 | # OUTPUT 292 | # [(10, -10)] 293 | # [(20, -20)] 294 | # [(30, -30)] 295 | # [(40, -40)] 296 | # TEST CASE 2 297 | # 298 | # INPUT 299 | # 20 300 | # 200 301 | # 10 302 | # 100 303 | # 30 304 | # 300 305 | # 40 306 | # 400 307 | # OUTPUT 308 | # [(20, 200)] 309 | # [(10, 100)] 310 | # [(30, 300)] 311 | # [(40, 400)] 312 | 313 | # In[ ]: 314 | 315 | 316 | for i in range(4): 317 | l =[] 318 | t =() 319 | for j in range(2): 320 | t = t + (int(input()),) 321 | l.append(t) 322 | print(l) 323 | 324 | 325 | # Q. 76: Update the Dictionary 326 | # QUESTION DESCRIPTION 327 | # 328 | # Write a program to update the Dictionary. Define the dictionary in the program and update the values as given in the program 329 | # 330 | # The Dictionary is as follows 331 | # dict = {'Toolname': 'eLab', 'Launch': 2017, 'State: 'Tamilnadu', 'Country':'Ind'} 332 | # 333 | # Input: 334 | # 335 | # 1. Get the key that needs to be updated 336 | # 337 | # 2. The value that needs to be updated 338 | # 339 | # Refer sample input and output for formatting specification. 340 | # 341 | # All float values are displayed correct to 2 decimal places. 342 | # 343 | # All text in bold corresponds to input and the rest corresponds to output 344 | # TEST CASE 1 345 | # 346 | # INPUT 347 | # Toolname 348 | # ELAB 349 | # OUTPUT 350 | # eLab 351 | # 2017 352 | # Tamilnadu 353 | # Ind 354 | # ELAB 355 | # TEST CASE 2 356 | # 357 | # INPUT 358 | # Country 359 | # India 360 | # OUTPUT 361 | # eLab 362 | # 2017 363 | # Tamilnadu 364 | # Ind 365 | # India 366 | 367 | # In[ ]: 368 | 369 | 370 | dict = {'Toolname': 'eLab','Launch': 2017, 'State': 'Tamilnadu', 'Country':'Ind'} 371 | k = input() 372 | v = input() 373 | for e in dict.values(): 374 | print(e) 375 | dict[k]=v 376 | print(v) 377 | 378 | 379 | # Q. 77: Dictionary Using List 380 | # QUESTION DESCRIPTION 381 | # 382 | # Write a program to get the input of key and value of the element in dictionary. 383 | # 384 | # Display the key-value pair in the python dictionary format. 385 | # 386 | # Kindle get the input using List Concept. 387 | # 388 | # Input: 389 | # 1. Size of first list 390 | # 2. Elements of first list 391 | # 3. Size of second list 392 | # 4. Elements of second list 393 | # 394 | # Output: 395 | # 1. The first list elements are keys of the dictionaries 396 | # 2. The second list elements are corresponding elements of the dictionaries 397 | # TEST CASE 1 398 | # 399 | # INPUT 400 | # 2 401 | # 12 402 | # 14 403 | # 2 404 | # 1212 405 | # 1414 406 | # OUTPUT 407 | # {12: 1212, 14: 1414} 408 | # TEST CASE 2 409 | # 410 | # INPUT 411 | # 2 412 | # 4 413 | # 5 414 | # 2 415 | # 16 416 | # 25 417 | # OUTPUT 418 | # {4: 16, 5: 25} 419 | 420 | # In[ ]: 421 | 422 | 423 | d1=[] 424 | d2=[] 425 | d3={} 426 | x=int (input ('')) 427 | for i in range (x): 428 | a=int (input ()) 429 | d1.append(a) 430 | y =int (input ()) 431 | for i in range (y): 432 | a=int (input ()) 433 | d2.append(a) 434 | for i in range (x): 435 | d3[d1[i]]=d2[i] 436 | print (d3) 437 | 438 | 439 | # Q. 78: List to Directory 440 | # QUESTION DESCRIPTION 441 | # 442 | # Write the python program which takes two lists and maps two lists into a dictionary 443 | # TEST CASE 1 444 | # 445 | # INPUT 446 | # 3 447 | # 1 2 3 448 | # 1 4 9 449 | # OUTPUT 450 | # The dictionary is: 451 | # {1: 1, 2: 4, 3: 9} 452 | # TEST CASE 2 453 | # 454 | # INPUT 455 | # 2 456 | # 23 46 457 | # 69 138 458 | # OUTPUT 459 | # The dictionary is: 460 | # {23: 69, 46: 138} 461 | 462 | # In[ ]: 463 | 464 | 465 | no= int (input ()) 466 | a= input().split() 467 | b=input ().split() 468 | l=list(map(int,a)) 469 | m=list(map(int,b)) 470 | color_dic=dict(zip(l,m)) 471 | print ('The dictionary is:') 472 | print (color_dic) 473 | 474 | 475 | # Q. 79: Length of Dictionary 476 | # QUESTION DESCRIPTION 477 | # 478 | # Write a program to get the input of key and value of the element in dictionary. 479 | # 480 | # Display the key-value pair in the python dictionary format and find the length of the dictionary 481 | # 482 | # Refer sample input and output for formatting specification. 483 | # 484 | # All float values are displayed correct to 2 decimal places. 485 | # 486 | # All text in bold corresponds to input and the rest corresponds to output. 487 | # TEST CASE 1 488 | # 489 | # INPUT 490 | # 3 491 | # 23 492 | # 33 493 | # 43 494 | # 32 495 | # 33 496 | # 34 497 | # OUTPUT 498 | # The dictionary is 499 | # {23: 32, 33: 33, 43: 34} 500 | # Length of dictionary is 501 | # 3 502 | # TEST CASE 2 503 | # 504 | # INPUT 505 | # 4 506 | # 23 507 | # 33 508 | # 43 509 | # 53 510 | # 32 511 | # 33 512 | # 34 513 | # 35 514 | # OUTPUT 515 | # The dictionary is 516 | # {23: 32, 33: 33, 43: 34, 53: 35} 517 | # Length of dictionary is 518 | # 4 519 | 520 | # In[ ]: 521 | 522 | 523 | dic1=[] 524 | dic2=[] 525 | dic3={} 526 | x=int (input ()) 527 | for i in range (x): 528 | m=int (input ()) 529 | dic1.append(m) 530 | for i in range (x): 531 | m= int (input()) 532 | dic2.append(m) 533 | 534 | for i in range (x): 535 | dic3[dic1[i]]=dic2[i]; 536 | print("The dictionary is") 537 | print(dic3) 538 | print("Length of dictionary is"), 539 | print(len(dic3)) 540 | 541 | 542 | # Q. 80: Compare Dictionary 543 | # QUESTION DESCRIPTION 544 | # 545 | # Write a program to get the input of key and value of the element in dictionary. 546 | # 547 | # Display the key-value pair of the dictionary in the python dictionary format. 548 | # 549 | # Compare the dictionary in the following sequence: 550 | # 551 | # 1. First and Second Dictionary 552 | # 2. Second and Third Dictionary 553 | # 3.Third and Fourth Dictionary 554 | # 4.Fourth and First Dictionary 555 | # 556 | # Refer sample input and output for formatting specification. 557 | # 558 | # All float values are displayed correct to 2 decimal places. 559 | # 560 | # All text in bold corresponds to input and the rest corresponds to output. 561 | # TEST CASE 1 562 | # 563 | # INPUT 564 | # 23 565 | # 33 566 | # 43 567 | # 53 568 | # 32 569 | # 33 570 | # 34 571 | # 35 572 | # OUTPUT 573 | # First Dictionary 574 | # {23: 33} 575 | # Second Dictionary 576 | # {43: 53} 577 | # Third Dictionary 578 | # {32: 33} 579 | # Fourth Dictionary 580 | # {34: 35} 581 | # -1 582 | # 1 583 | # -1 584 | # 1 585 | # TEST CASE 2 586 | # 587 | # INPUT 588 | # 53 589 | # 93 590 | # 33 591 | # 43 592 | # 2 593 | # 10 594 | # 3 595 | # 35 596 | # OUTPUT 597 | # First Dictionary 598 | # {53: 93} 599 | # Second Dictionary 600 | # {33: 43} 601 | # Third Dictionary 602 | # {2: 10} 603 | # Fourth Dictionary 604 | # {3: 35} 605 | # 1 606 | # 1 607 | # -1 608 | # -1 609 | 610 | # In[ ]: 611 | 612 | 613 | dict={} 614 | dict1={} 615 | dict2={} 616 | dict3={} 617 | a=[] 618 | b=[] 619 | print('First Dictionary') 620 | for i in range(1): 621 | key=int(input()) 622 | value=int(input()) 623 | a.append(value) 624 | dict[key]=value 625 | print(dict) 626 | print('Second Dictionary') 627 | for i in range(1): 628 | key=int(input()) 629 | value=int(input()) 630 | b.append(value) 631 | dict1[key]=value 632 | print(dict1) 633 | print('Third Dictionary') 634 | for i in range(1): 635 | key=int(input()) 636 | value=int(input()) 637 | b.append(value) 638 | dict2[key]=value 639 | print(dict2) 640 | print('Fourth Dictionary') 641 | for i in range(1): 642 | key=int(input()) 643 | value=int(input()) 644 | b.append(value) 645 | dict3[key]=value 646 | print(dict3) 647 | if(list(dict.keys())[0]>list(dict1.keys())[0]): 648 | print(1) 649 | else: 650 | print(-1) 651 | if(list(dict1.keys())[0]>list(dict2.keys())[0]): 652 | print(1) 653 | else: 654 | print(-1) 655 | if(list(dict2.keys())[0]>list(dict3.keys())[0]): 656 | print(1) 657 | else: 658 | print(-1) 659 | if(list(dict3.keys())[0]>list(dict.keys())[0]): 660 | print(1) 661 | else: 662 | print(-1) 663 | 664 | 665 | # In[ ]: 666 | 667 | 668 | 669 | 670 | -------------------------------------------------------------------------------- /09 Session Searching and Sorting .py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # Q. 81: Binary Search 5 | # QUESTION DESCRIPTION 6 | # 7 | # Write a program to implement binary search algorithm 8 | # TEST CASE 1 9 | # 10 | # INPUT 11 | # 20 12 | # 1 13 | # 9 14 | # 18 15 | # 24 16 | # 27 17 | # 35 18 | # 38 19 | # 41 20 | # 49 21 | # 53 22 | # 55 23 | # 66 24 | # 67 25 | # 72 26 | # 75 27 | # 77 28 | # 81 29 | # 89 30 | # 90 31 | # 97 32 | # 3 33 | # 35 34 | # 38 35 | # 41 36 | # OUTPUT 37 | # Sequence found between index 5 and 8 38 | # TEST CASE 2 39 | # 40 | # INPUT 41 | # 20 42 | # 1 43 | # 9 44 | # 18 45 | # 24 46 | # 27 47 | # 35 48 | # 38 49 | # 41 50 | # 49 51 | # 53 52 | # 55 53 | # 66 54 | # 67 55 | # 72 56 | # 75 57 | # 77 58 | # 81 59 | # 89 60 | # 90 61 | # 97 62 | # 3 63 | # 35 64 | # 38 65 | # 40 66 | # OUTPUT 67 | # Not found 68 | 69 | # In[ ]: 70 | 71 | 72 | def binarySearch (arr, l, r, x): 73 | if r >= l: 74 | mid = int(l + (r - l)/2) 75 | if arr[mid] == x: 76 | return mid 77 | elif arr[mid] > x: 78 | return binarySearch(arr, l, mid-1, x) 79 | else: 80 | return binarySearch(arr, mid+1, r, x) 81 | else: 82 | return -1 83 | arr=[] 84 | n=int(input("")) 85 | for i in range(1,n+1): 86 | b=int(input("")) 87 | arr.append(b) 88 | a=int(input("")) 89 | b=0 90 | c=[] 91 | for i in range(a): 92 | x=int(input("")) 93 | result = binarySearch(arr, 0, len(arr)-1, x) 94 | if result != -1: 95 | b+=1 96 | if(i==0): 97 | c.append(result) 98 | if(i==a-1): 99 | c.append(result+1) 100 | if(b==a): 101 | print("Sequence found between index",end=" ") 102 | for i in range(len(c)): 103 | if(i==0): 104 | print(c[i],"and",end=" ") 105 | else: 106 | print(c[i]) 107 | else: 108 | print("Not found") 109 | 110 | 111 | # Q. 82: Find the sequence of given integers 112 | # QUESTION DESCRIPTION 113 | # 114 | # Sort the given set of integers using bubble sort and find the smallest and largest among given set of integers. 115 | # Step1: Get the number of integers 116 | # Step 2:Receive the integers 117 | # Step3:Sort the integers using bubble sort 118 | # Step 4:Print the start and end of sequence 119 | # TEST CASE 1 120 | # 121 | # INPUT 122 | # 4 123 | # 99 124 | # 5 125 | # 6 126 | # 97 127 | # OUTPUT 128 | # Sequence of integers: 4 to 100 129 | # TEST CASE 2 130 | # 131 | # INPUT 132 | # 5 133 | # 22 134 | # 11 135 | # 5 136 | # 6 137 | # 7 138 | # OUTPUT 139 | # Sequence of integers: 4 to 23 140 | 141 | # In[ ]: 142 | 143 | 144 | def bsort(a): 145 | for i in range(len(a)): 146 | for k in range(0,len(a)-1): 147 | if(a[k]>a[k+1]): 148 | t=a[k] 149 | a[k]=a[k+1] 150 | a[k+1]=t 151 | if i==2: 152 | for x in a: 153 | x 154 | 155 | n=int(input("")) 156 | list=[] 157 | for i in range(n): 158 | l=int(input("")) 159 | list.append(l) 160 | bsort(list) 161 | for i in list: 162 | i 163 | print("Sequence of integers:",list[0]-1,"to",list[-1]+1) 164 | 165 | 166 | # Q. 83: Find increment sequence 167 | # QUESTION DESCRIPTION 168 | # 169 | # Perform sorting on list of integers and print the sequence showing order of increment. 170 | # TEST CASE 1 171 | # 172 | # INPUT 173 | # 5 174 | # 11 24 77 66 55 175 | # OUTPUT 176 | # Sorted List 177 | # [11, 24, 55, 66, 77] 178 | # Sequence of increments 179 | # [13, 31, 11, 11] 180 | # TEST CASE 2 181 | # 182 | # INPUT 183 | # 5 184 | # 2 2 2 2 2 185 | # OUTPUT 186 | # Sorted List 187 | # [2, 2, 2, 2, 2] 188 | # Sequence of increments 189 | # [0, 0, 0, 0] 190 | 191 | # In[ ]: 192 | 193 | 194 | a=[] 195 | c=[] 196 | n=int(input("")) 197 | b=input("") 198 | a=[int(n) for n in b.split()] 199 | a.sort() 200 | print("Sorted List") 201 | print(a) 202 | for i in range(1,n): 203 | d=a[i]-a[i-1] 204 | c.append(d) 205 | print("Sequence of increments") 206 | aList = list(c) 207 | print(aList) 208 | 209 | 210 | # Q. 83: Find increment sequence 211 | # QUESTION DESCRIPTION 212 | # 213 | # Perform sorting on list of integers and print the sequence showing order of increment. 214 | # TEST CASE 1 215 | # 216 | # INPUT 217 | # 5 218 | # 11 24 77 66 55 219 | # OUTPUT 220 | # Sorted List 221 | # [11, 24, 55, 66, 77] 222 | # Sequence of increments 223 | # [13, 31, 11, 11] 224 | # TEST CASE 2 225 | # 226 | # INPUT 227 | # 5 228 | # 2 2 2 2 2 229 | # OUTPUT 230 | # Sorted List 231 | # [2, 2, 2, 2, 2] 232 | # Sequence of increments 233 | # [0, 0, 0, 0] 234 | 235 | # In[ ]: 236 | 237 | 238 | a=[] 239 | c=[] 240 | n=int(input("")) 241 | b=input("") 242 | a=[int(n) for n in b.split()] 243 | a.sort() 244 | print("Sorted List") 245 | print(a) 246 | for i in range(1,n): 247 | d=a[i]-a[i-1] 248 | c.append(d) 249 | print("Sequence of increments") 250 | aList = list(c) 251 | print(aList) 252 | 253 | 254 | # Q. 84: Find increment sequence 255 | # QUESTION DESCRIPTION 256 | # 257 | # Perform sorting on list of integers and print the sequence showing order of increment. 258 | # TEST CASE 1 259 | # 260 | # INPUT 261 | # 5 262 | # 11 24 77 66 55 263 | # OUTPUT 264 | # Sorted List: 265 | # [11, 24, 55, 66, 77] 266 | # Sequence of increments: 267 | # [13, 31, 11, 11] 268 | # TEST CASE 2 269 | # 270 | # INPUT 271 | # 5 272 | # 2 2 2 2 2 273 | # OUTPUT 274 | # Sorted List: 275 | # [2, 2, 2, 2, 2] 276 | # Sequence of increments: 277 | # [0, 0, 0, 0] 278 | 279 | # In[ ]: 280 | 281 | 282 | a=[] 283 | c=[] 284 | n=int(input("")) 285 | b=input("") 286 | a=[int(n) for n in b.split()] 287 | a.sort() 288 | print("Sorted List:") 289 | print(a) 290 | for i in range(0,n-1): 291 | d=a[i+1]-a[i] 292 | c.append(d) 293 | print("Sequence of increments:") 294 | print(c) 295 | 296 | 297 | # Q. 85: Finding the occurrence of an integer 298 | # QUESTION DESCRIPTION 299 | # 300 | # Find the given integer in the given list of integers and print the number of occurrences of that integer in the list. 301 | # TEST CASE 1 302 | # 303 | # INPUT 304 | # 7 305 | # 64 34 7 12 22 11 7 306 | # 7 307 | # OUTPUT 308 | # 2 309 | # TEST CASE 2 310 | # 311 | # INPUT 312 | # 7 313 | # 64 34 45 34 24 29 74 314 | # 9 315 | # OUTPUT 316 | # 0 317 | 318 | # In[ ]: 319 | 320 | 321 | a=eval(input()) 322 | v=input() 323 | c=v.split() 324 | co=0 325 | l=[] 326 | l=[eval(x) for x in c] 327 | sv=eval(input()) 328 | for x in range(a): 329 | if l[x]==sv: 330 | co=co+1 331 | print(co) 332 | 333 | 334 | # Q. 86: Find Mid-term With ODD and Even length of list 335 | # QUESTION DESCRIPTION 336 | # 337 | # Perform sorting on list of integers and print the mid-term. 338 | # TEST CASE 1 339 | # 340 | # INPUT 341 | # 5 342 | # 11 343 | # 22 344 | # 33 345 | # 66 346 | # 67 347 | # OUTPUT 348 | # Sorted List: 349 | # [11, 22, 33, 66, 67] 350 | # Mid-term: 351 | # 33 352 | # TEST CASE 2 353 | # 354 | # INPUT 355 | # 6 356 | # 11 357 | # 63 358 | # 4 359 | # 8 360 | # 99 361 | # 55 362 | # OUTPUT 363 | # Sorted List: 364 | # [4, 8, 11, 55, 63, 99] 365 | # Mid-term: 366 | # 11 367 | # 368 | 369 | # In[ ]: 370 | 371 | 372 | mlist=[] 373 | def findMiddle(input_list): 374 | middle = float(len(input_list))/2 375 | if middle % 2 != 0: 376 | return input_list[int(middle - .5)] 377 | else: 378 | return (input_list[int(middle)], input_list[int(middle-1)]) 379 | 380 | n=int(input("")) 381 | l=[] 382 | for i in range(n): 383 | n1=int(input("")) 384 | l.append(n1) 385 | #msort(l) 386 | l.sort() 387 | for i in l: 388 | mlist.append(i) 389 | print("Sorted List:") 390 | print (mlist) 391 | middle = findMiddle(mlist) 392 | print("Mid-term:") 393 | print (middle) 394 | 395 | 396 | # Q. 87: Finding the multiple 397 | # QUESTION DESCRIPTION 398 | # 399 | # Find the multiple of given number in the list of integers 400 | # Input: 401 | # First Line of the Input is the Number of Elements in the List 402 | # Second Line of Input has the elements of the List 403 | # Third line has has the number for which you have to find the multiples 404 | # Output: 405 | # Print the Multiples 406 | # TEST CASE 1 407 | # 408 | # INPUT 409 | # 5 410 | # 6 411 | # 7 412 | # 8 413 | # 13 414 | # 11 415 | # 3 416 | # OUTPUT 417 | # 6 418 | # TEST CASE 2 419 | # 420 | # INPUT 421 | # 4 422 | # 5 423 | # 3 424 | # 7 425 | # 5 426 | # 7 427 | # OUTPUT 428 | # 7 429 | # 430 | 431 | # In[ ]: 432 | 433 | 434 | l=[] 435 | n=int(input()) 436 | for i in range(n): 437 | ele=int(input()) 438 | l.append(ele) 439 | #print(l) 440 | 441 | flag=0 442 | finddiv=int(input()) 443 | for val in l: 444 | if ((val%finddiv)==0): 445 | print(val) 446 | flag=1 447 | if (flag!=1): 448 | print("No Multiples") 449 | 450 | 451 | # Q. 88: Searching Linear 452 | # QUESTION DESCRIPTION 453 | # 454 | # Write a program to search a element linearly 455 | # TEST CASE 1 456 | # 457 | # INPUT 458 | # 5 459 | # 2 460 | # 3 461 | # 4 462 | # 10 463 | # 40 464 | # 3 465 | # OUTPUT 466 | # Element is present at index 1 467 | # TEST CASE 2 468 | # 469 | # INPUT 470 | # 5 471 | # 2 472 | # 3 473 | # 4 474 | # 10 475 | # 40 476 | # 10 477 | # OUTPUT 478 | # Element is present at index 3 479 | 480 | # In[ ]: 481 | 482 | 483 | x=int(input("")); 484 | l=[] 485 | for i in range(0,x): 486 | y=int(input("")); 487 | l.append(y); 488 | f=int(input("")); 489 | for i in range(0,x): 490 | if(l[i]==f): 491 | print("Element is present at index",i); 492 | 493 | 494 | # Q. 89: Bubble Sort 495 | # QUESTION DESCRIPTION 496 | # 497 | # Sort the given set of numbers using Bubble Sort. The first line of the input contains the number of elements, the second line of the input contains the numbers to be sorted. In the output print the status of the array at the 3rd iteration and the final sorted array in the given format 498 | # TEST CASE 1 499 | # 500 | # INPUT 501 | # 7 502 | # 64 503 | # 34 504 | # 25 505 | # 12 506 | # 22 507 | # 11 508 | # 90 509 | # OUTPUT 510 | # 12 22 11 25 34 64 90 511 | # Sorted array: 512 | # 11 12 22 25 34 64 90 513 | # TEST CASE 2 514 | # 515 | # INPUT 516 | # 8 517 | # 14 518 | # 83 519 | # 25 520 | # 47 521 | # 9 522 | # 77 523 | # 1 524 | # 0 525 | # OUTPUT 526 | # 14 9 25 1 0 47 77 83 527 | # Sorted array: 528 | # 0 1 9 14 25 47 77 83 529 | # 530 | 531 | # In[ ]: 532 | 533 | 534 | n=int(input()) 535 | arr=[] 536 | t=[] 537 | for i in range(n): 538 | arr.append(int(input())) 539 | for i in range(0,n): 540 | for j in range(0, n-1): 541 | if (arr[j]>arr[j+1]): 542 | temp=arr[j] 543 | arr[j]=arr[j+1] 544 | arr[j+1]=temp 545 | if (i==2): 546 | for p in range(0,n): 547 | if p < n-1: 548 | print(arr[p],"",end='') 549 | else: 550 | print(arr[p]) 551 | print("Sorted array:") 552 | for p in range(n): 553 | if p < n-1: 554 | print(arr[p],"",end='') 555 | else: 556 | print(arr[p]) 557 | 558 | -------------------------------------------------------------------------------- /10 File Handling.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # ## SESSION: File Handling 5 | 6 | # Q. 91: Create a dynamic file and read 7 | # QUESTION DESCRIPTION 8 | 9 | # Write a python program to create a file and display the contents (dynamic number of lines) 10 | 11 | # Hint: 12 | 13 | # 1. Create a File 14 | # 2. Take the input as number of lines to be written into the file 15 | # 3. Based on the number of lines get the input from the user 16 | # 4. Add a new line to the file after each input. 17 | # 5. Display the contents of the file written in the output 18 | 19 | # Input: 20 | # 1. Filename 21 | # 2,3,4,5 = File contents 22 | # TEST CASE 1 23 | 24 | # INPUT 25 | # sample.txt 26 | # 5 27 | # This is First Line 28 | # This is Second Line 29 | # This is Third Line 30 | # This is Fourth Line 31 | # This is Fifth Line 32 | # OUTPUT 33 | # This is First Line 34 | 35 | # This is Second Line 36 | 37 | # This is Third Line 38 | 39 | # This is Fourth Line 40 | 41 | # This is Fifth Line 42 | 43 | # Number of lines: 44 | # 10 45 | # TEST CASE 2 46 | 47 | # INPUT 48 | # sample.txt 49 | # 3 50 | # This is First Line 51 | # This is Second Line 52 | # This is Third Line 53 | # OUTPUT 54 | # This is First Line 55 | 56 | # This is Second Line 57 | 58 | # This is Third Line 59 | 60 | # Number of lines: 61 | # 6 62 | 63 | import string 64 | file_name = input() 65 | file = open(file_name, 'w+') 66 | 67 | n = int(input()) 68 | 69 | for i in range(n): 70 | content = input() 71 | file.write(content + "\n\n") 72 | 73 | file.write(f"Number of lines:\n{n*2}") 74 | file.close() 75 | 76 | f = open(file_name) 77 | print(f.read()) 78 | 79 | 80 | # Q. 92: Captilize each word 81 | # QUESTION DESCRIPTION 82 | 83 | # Write a python program to create a file and Capitalise each word (Starting letter) in the file 84 | 85 | # Hint: 86 | 87 | # 1. Create a File 88 | # 2. Take the input as number of lines to be written into the file 89 | # 3. File name to be searched 90 | 91 | # Input: 92 | # 1. Filename 93 | # 2. The number of lines to be written into the file 94 | # 3. File name 95 | 96 | # Output: 97 | # Display the file content with capitalise of each word (Starting letter) 98 | # TEST CASE 1 99 | 100 | # INPUT 101 | # sample.txt 102 | # 4 103 | # This is First Line of the file 104 | # This is Second Line of the file create in eLab Server 105 | # This is Third Line sample line 106 | # This is fourth line of the file and eLab is an auto evaluation tool launched in 2017 107 | # sample.txt 108 | # OUTPUT 109 | # This Is First Line Of The File 110 | 111 | # This Is Second Line Of The File Create In Elab Server 112 | 113 | # This Is Third Line Sample Line 114 | 115 | # This Is Fourth Line Of The File And Elab Is An Auto Evaluation Tool Launched In 2017 116 | # TEST CASE 2 117 | 118 | # INPUT 119 | # sample.txt 120 | # 2 121 | # This is First Line of the file 122 | # This is Second Line of the file create in eLab Server 123 | # sample.txt 124 | # OUTPUT 125 | # This Is First Line Of The File 126 | 127 | # This Is Second Line Of The File Create In Elab Server 128 | 129 | file_name = input() 130 | file = open(file_name, 'w+') 131 | 132 | n = int(input()) 133 | 134 | for i in range(n): 135 | content = input() 136 | file.write(string.capwords(content) + "\n\n") 137 | 138 | file.close() 139 | 140 | f = open(file_name) 141 | print(f.read()) 142 | 143 | 144 | # Q. 93: Create a dynamic file and read the number of lines 145 | # QUESTION DESCRIPTION 146 | 147 | # Write a python program to create a file and display the contents (dynamic number of lines) 148 | 149 | # Hint: 150 | 151 | # 1. Create a File 152 | # 2. Take the input as number of lines to be written into the file 153 | # 3. Based on the number of lines get the input from the user 154 | # 4. Add a new line to the file after each input. 155 | # 5. Display the contents of the file written in the output 156 | # 6. Display the Number of Lines in the File in the output 157 | 158 | # Input: 159 | # 1. Filename 160 | # 2,3,4,5 = File contents 161 | # TEST CASE 1 162 | 163 | # INPUT 164 | # sample.txt 165 | # 5 166 | # This is First Line 167 | # This is Second Line 168 | # This is Third Line 169 | # This is Fourth Line 170 | # This is Fifth Line 171 | # OUTPUT 172 | # This is First Line 173 | # This is Second Line 174 | # This is Third Line 175 | # This is Fourth Line 176 | # This is Fifth Line 177 | # Number of lines: 178 | # 5 179 | # TEST CASE 2 180 | 181 | # INPUT 182 | # sample.txt 183 | # 4 184 | # This is First Line 185 | # This is Second Line 186 | # This is Third Line 187 | # This is Fourth Line 188 | # OUTPUT 189 | # This is First Line 190 | # This is Second Line 191 | # This is Third Line 192 | # This is Fourth Line 193 | # Number of lines: 194 | # 4 195 | 196 | file_name = input() 197 | file = open(file_name, 'w+') 198 | 199 | n = int(input()) 200 | 201 | for i in range(n): 202 | content = input() 203 | file.write(content + "\n") 204 | 205 | file.write(f"Number of lines:\n{n}") 206 | file.close() 207 | 208 | f = open(file_name) 209 | print(f.read()) 210 | 211 | 212 | # Q. 94: Count the words 213 | # QUESTION DESCRIPTION 214 | 215 | # Write a python program to create a file and display the contents (dynamic number of lines) and count the number of words in the file. 216 | 217 | # Hint: 218 | 219 | # 1. Create a File 220 | # 2. Take the input as number of lines to be written into the file 221 | # 3. Based on the number of lines get the input from the user 222 | # 4. Get the file name from the user for performing operations 223 | # 5. Count the number of words in the file and display the number of words in the fie 224 | 225 | 226 | # Input: 227 | # 1. Filename 228 | # 2. The number of lines to be written into the file 229 | # 3. File name to perform operations 230 | # TEST CASE 1 231 | 232 | # INPUT 233 | # sample.txt 234 | # 2 235 | # eLab an auto evaluation tool in Tamilnadu 236 | # eLab will be launched in SWAYM platform soon 237 | # sample.txt 238 | # OUTPUT 239 | # Number of words: 240 | # 15 241 | # TEST CASE 2 242 | 243 | # INPUT 244 | # sample.txt 245 | # 4 246 | # This is First Line of the file 247 | # This is Second Line of the file create in eLab Server 248 | # This is Third Line sample line 249 | # This is fourth line of the file and eLab is an auto evaluation tool launched in 2017 250 | # sample.txt 251 | # OUTPUT 252 | # Number of words: 253 | # 41 254 | 255 | file_name = input() 256 | file = open(file_name, 'w+') 257 | 258 | n = int(input()) 259 | word_count = 0 260 | 261 | for i in range(n): 262 | content = input() 263 | file.write(content + "\n\n") 264 | word_count += len(content.split()) 265 | 266 | file.close() 267 | print(f"Number of words:\n{word_count}") 268 | 269 | 270 | # Q. 95: Create a File and read 271 | # QUESTION DESCRIPTION 272 | 273 | # Write a python program to create a file and display the contents 274 | 275 | # Hint: 276 | 277 | # 1. Create a File 278 | # 2. Take four inputs from the user and add the four lines to the file 279 | # 3. Display the contents of the file written in the output 280 | 281 | # Input: 282 | # 1. Filename 283 | # 2,3,4,5 = File contents 284 | # TEST CASE 1 285 | 286 | # INPUT 287 | # sample.txt 288 | # This is First Line 289 | # This is Second Line 290 | # This is Third Line 291 | # This is Fourth Line 292 | # OUTPUT 293 | # This is First LineThis is Second LineThis is Third LineThis is Fourth Line 294 | # TEST CASE 2 295 | 296 | # INPUT 297 | # sample.txt 298 | # eLab 299 | # Tool 300 | # is an 301 | # auto evaluation tool 302 | # OUTPUT 303 | # eLab Tool is an auto evaluation tool 304 | 305 | file_name = input() 306 | file = open(file_name, 'w+') 307 | 308 | n = 4 309 | 310 | for i in range(n): 311 | content = input() 312 | file.write(content) 313 | 314 | file.close() 315 | 316 | f = open(file_name) 317 | print(f.read()) 318 | 319 | 320 | # Q. 96: Reverse File 321 | # QUESTION DESCRIPTION 322 | 323 | # Write a Python Program to create a file and display the content(dynamic number of lines) and print the files contents in reverse order. 324 | # Hint 1: 325 | # 1. Create a file 326 | # 2. Take the Input as number of lines to be written into the file 327 | # 3. Based on the number of lines get the input from the user 328 | # 4. Get the file name from the user for performing operations 329 | # 5. Count the number of words in the file and display the number of words in the file input 330 | # Input1: 331 | # 1: Filename 332 | # 2: The number of lines to be written into the file. 333 | # 3. File name to perform operations 334 | # TEST CASE 1 335 | 336 | # INPUT 337 | # sample.txt 338 | # 4 339 | # eLab an auto evaluation tool in Tamilnadu - eLab 340 | # eLab will be launched in SWAYM platform soon - eLab 341 | # eLab is used by 45000+users every year- eLab 342 | # eLab is a copyleft tool - eLab 343 | # sample.txt 344 | # OUTPUT 345 | # eLab is a copyleft tool - eLab 346 | # eLab is used by 45000+users every year- eLab 347 | # eLab will be launched in SWAYM platform soon - eLab 348 | # eLab an auto evaluation tool in Tamilnadu - eLab 349 | # TEST CASE 2 350 | 351 | # INPUT 352 | # sample.txt 353 | # 2 354 | # eLab an auto evaluation tool in Tamilnadu 355 | # eLab will be launched in SWAYM platform soon 356 | # sample.txt 357 | # OUTPUT 358 | # eLab will be launched in SWAYM platform soon 359 | # eLab an auto evaluation tool in Tamilnadu 360 | 361 | file_name = input() 362 | file = open(file_name, 'w+') 363 | 364 | n = int(input()) 365 | 366 | for i in range(n): 367 | content = input() 368 | file.write(content + "\n") 369 | 370 | file.close() 371 | 372 | f = open(file_name) 373 | lines = f.read().strip().split("\n") 374 | rev = lines[::-1] 375 | for line in rev: 376 | print(line) 377 | 378 | 379 | # Q. 97: Find the letter 380 | # QUESTION DESCRIPTION 381 | 382 | # Write a python program to create a file and display the contents (dynamic number of lines) and count the occurrence of the letter in the file and display the count. 383 | 384 | # Hint: 385 | 386 | # 1. Create a File 387 | # 2. Take the input as number of lines to be written into the file 388 | # 3. Based on the number of lines get the input from the user 389 | # 4. Input the file name to perform the operations 390 | # 5. Input the letter to be searched in the file 391 | 392 | # Input: 393 | # 1. Filename 394 | # 2. The number of lines to be written into the file 395 | # 3. File name 396 | # 4. Letter to be searched 397 | 398 | # Output: 399 | # The occurrence(count) of the letter 400 | # TEST CASE 1 401 | 402 | # INPUT 403 | # sample.txt 404 | # 4 405 | # eLab 406 | # eLab eLab 407 | # eLab eLab tool 408 | # eLab eLab eLab eLab tool 409 | # sample.txt 410 | # e 411 | # OUTPUT 412 | # Occurrences of the letter 413 | # 9 414 | # TEST CASE 2 415 | 416 | # INPUT 417 | # sample.txt 418 | # 2 419 | # eLab eLab tool 420 | # eLab eLab eLab eLab tool 421 | # sample.txt 422 | # L 423 | # OUTPUT 424 | # Occurrences of the letter 425 | # 6 426 | 427 | file_name = input() 428 | file = open(file_name, 'w+') 429 | 430 | n = int(input()) 431 | 432 | for i in range(n): 433 | content = input() 434 | file.write(content) 435 | file.close() 436 | 437 | f_name = input() 438 | key = input() 439 | 440 | f = open(f_name) 441 | ref = f.read() 442 | 443 | print(f"Occurrences of the letter\n{ref.count(key)}") 444 | 445 | 446 | # Q. 98: Count Words 447 | # QUESTION DESCRIPTION 448 | 449 | # Write a python program to create a file and display the count of all the characters in the file 450 | 451 | # Hint: 452 | 453 | # 1. Create a File 454 | # 2. Take the input as number of lines to be written into the file 455 | # 3. Based on the number of lines get the input from the user 456 | # 4. File name to be searched 457 | 458 | # Input: 459 | # 1. Filename 460 | # 2. The number of lines to be written into the file 461 | # 3. File name 462 | 463 | # Output: 464 | # Display the count of all characters in the file 465 | # TEST CASE 1 466 | 467 | # INPUT 468 | # sample.txt 469 | # 2 470 | # eLab is an auto evaluation tool 471 | # This file program in eLab will show you the count of words in the file except numbers like 1234567890 472 | # sample.txt 473 | # OUTPUT 474 | # 99 475 | # TEST CASE 2 476 | 477 | # INPUT 478 | # sample.txt 479 | # 3 480 | # eLab is an auto evaluation tool 481 | # This file program in eLab will show you the count of words in the file except numbers like 1234567890 482 | # 2017 2016 2015 2018 eLab 483 | # sample.txt 484 | # OUTPUT 485 | # 103 486 | 487 | file_name = input() 488 | file = open(file_name, 'w+') 489 | 490 | n = int(input()) 491 | char_count = 0 492 | 493 | for i in range(n): 494 | content = input() 495 | file.write(content + "\n\n") 496 | words = content.split() 497 | for word in words: 498 | if not word.isdigit(): 499 | char_count += len(word) 500 | 501 | file.close() 502 | print(char_count) 503 | 504 | 505 | # Q. 99: Reverse File 506 | # QUESTION DESCRIPTION 507 | 508 | # Write a python program to create a file and display the contents (dynamic number of lines) and print the files contents in reverse order. 509 | 510 | # Hint: 511 | 512 | # 1. Create a File 513 | # 2. Take the input as number of lines to be written into the file 514 | # 3. Based on the number of lines get the input from the user 515 | # 4. Get the file name from the user for performing operations 516 | # 5. Count the number of words in the file and display the number of words in the fie 517 | 518 | 519 | # Input: 520 | # 1. Filename 521 | # 2. The number of lines to be written into the file 522 | # 3. File name to perform operations 523 | # TEST CASE 1 524 | 525 | # INPUT 526 | # sample.txt 527 | # 4 528 | # eLab an auto evaluation tool in Tamilnadu - eLab 529 | # eLab will be launched in SWAYM platform soon - eLab 530 | # eLab is used by 45000+users every year- eLab 531 | # eLab is a copyleft tool - eLab 532 | # sample.txt 533 | # OUTPUT 534 | # eLab is a copyleft tool - eLab 535 | # eLab is used by 45000+users every year- eLab 536 | # eLab will be launched in SWAYM platform soon - eLab 537 | # eLab an auto evaluation tool in Tamilnadu - eLab 538 | # TEST CASE 2 539 | 540 | # INPUT 541 | # sample.txt 542 | # 2 543 | # eLab an auto evaluation tool in Tamilnadu 544 | # eLab will be launched in SWAYM platform soon 545 | # sample.txt 546 | # OUTPUT 547 | # eLab will be launched in SWAYM platform soon 548 | # eLab an auto evaluation tool in Tamilnadu 549 | 550 | file_name = input() 551 | file = open(file_name, 'w+') 552 | 553 | n = int(input()) 554 | 555 | for i in range(n): 556 | content = input() 557 | file.write(content + "\n") 558 | 559 | file.close() 560 | 561 | f = open(file_name) 562 | lines = f.read().strip().split("\n") 563 | rev = lines[::-1] 564 | for line in rev: 565 | print(line) 566 | 567 | 568 | # Q. 100: Count Number 569 | # QUESTION DESCRIPTION 570 | 571 | # Write a python program to create a file and display the contents (dynamic number of lines) and count the numbers in the file and display the count. 572 | 573 | # Hint: 574 | 575 | # 1. Create a File 576 | # 2. Take the input as number of lines to be written into the file 577 | # 3. Based on the number of lines get the input from the user 578 | # 4. Input the file name to perform the operations 579 | 580 | # Input: 581 | # 1. Filename 582 | # 2. The number of lines to be written into the file 583 | # 3. File name 584 | 585 | # Output: 586 | # The total count of the numbers in the file 587 | # TEST CASE 1 588 | 589 | # INPUT 590 | # sample.txt 591 | # 2 592 | # eLab 1.0 launched in 2017 593 | # eLab 2.0 next version will be launched in 2019 594 | # sample.txt 595 | # OUTPUT 596 | # 12 597 | # TEST CASE 2 598 | 599 | # INPUT 600 | # sample.txt 601 | # 3 602 | # eLab 1.0 launched in 2017 603 | # eLab 2.0 next version will be launched in 2019 604 | # 2016 2017 2018 2019 2020 2021 605 | # sample.txt 606 | # OUTPUT 607 | # 36 608 | 609 | file_name = input() 610 | file = open(file_name, 'w+') 611 | 612 | n = int(input()) 613 | char_count = 0 614 | 615 | for i in range(n): 616 | content = input() 617 | file.write(content + "\n\n") 618 | words = content.split() 619 | for word in words: 620 | if word.replace('.', '').isdigit(): 621 | char_count += len(word) 622 | if "." in word: 623 | char_count -= 1 624 | 625 | file.close() 626 | print(char_count) 627 | -------------------------------------------------------------------------------- /Elab Self Notes/01 Input output.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skjha1/SRM-Python-Elab-solution/4fb72d94f7cbee32a89a2e857b9fbb0e06c5c9dd/Elab Self Notes/01 Input output.pdf -------------------------------------------------------------------------------- /Elab Self Notes/session 2 control stmt if elif else.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/skjha1/SRM-Python-Elab-solution/4fb72d94f7cbee32a89a2e857b9fbb0e06c5c9dd/Elab Self Notes/session 2 control stmt if elif else.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SRM-Python-Elab-solution 2 | 3 | ### Refer solution only if you are not able to do by yourself. 4 | 5 | ##### Type your Question name below that you will get code. 6 | ###### In Case your question is not there in the session feel free to uplode your question by pulling request. 7 | click on code to get all session 8 | --------------------------------------------------------------------------------