Posts

Showing posts from September, 2024

oops in python

#encapsulation #protected access modifier class parent :     # constructor     def __init__ ( self ):         self . _a = 9 class child ( parent ):       def __init__ ( self ):     #calling constructor in above         parent . __init__ ( self )           print ( f 'calling 1st one: { self . _a } ' )         self . _a = 7         print ( f 'calling 2nd one: { self . _a } ' )         self . _a = 5         print ( f 'calling 3rd one: { self . _a } ' )             child () # if i use print method it will show object also for example print () print ( child ()) print () #creating object and accessing it ob = parent () ob1 = child () print ( f 'accseing parent class: { ob . _a } ' ) print ( f 'accseing child class: { ob1 . _a } ' ) #it always takes updated value #o/p is: c...

Dsa in python

 #Using stack in py and in these push and pop the elements: size = int ( input ( 'Enter size of the elemnts:' )) stack = [] print ( 'Enter elements in stack:' )#inserting for i in   range ( size ):     ele = int ( input ())     stack . append ( ele ) print ( 'Elements are in stack :' )    #display for element in stack :     print ( element ) rem_v = int ( input ( 'Enter the size of elements which are removing:' )) for j in range ( rem_v ):#pop     stack . pop () print ( f 'Remainig elements in stack is : { stack } ' )                    #o/p is: Enter size of the elemnts:4 Enter elements in stack: 1 2 3 4 Elements are in stack : 1 2 3 4 Enter the size of elements which are removing:1 Remainig elements in stack is : [1, 2, 3] #Dequeue in py: from collections import deque size = int ( input ( 'Enter size of the elemnts:' )) stack = deque () print ( 'Enter elements in s...

solved py

 num_a = int(input("Enter a number: ")) num_b = int(input("Enter second number: ")) num_c = int(input("Enter third number: ")) if num_a <= 1 or num_b <= 1 or num_c <= 1:     print('Give another number greater than 1') else:     def is_prime(n):         if n <= 1:             return False         for i in range(2, int(n**0.5) + 1):             if n % i == 0:                 return False         return True     if is_prime(num_a):         print(f'{num_a} is a prime number')     else:         print(f'{num_a} is not a prime number')     if is_prime(num_b):         print(f'{num_b} is a prime number')     else:         print(f'{num_b} is not a prime number')     if is_pri...

Dsa for exams

 import java.util.Stack; import java.util.*; class Main{ public static void main(String[]args){     System.out.print("Enter size of stack:");     Scanner sd=new Scanner(System.in);     int n=sd.nextInt();     int N[]=new int[n];     Stack <Integer> sa=new Stack<>();     System.out.print("Enter elements in stack:");     for (int i=0;i<n;i++){         N[i]=sd.nextInt();         sa.push(N[i]);     }     for(int j=0;j<3;j++){     int s =sa.pop();     System.out.println("popped elements: "+s);     }     System.out.println("remaining in stack :"+sa);          } } //o/p is: Enter size of stack:5 Enter elements in stack:1 2 3 4 5 popped elements: 5 popped elements: 4 popped elements: 3 remaining in stack :[1, 2] //queue : import java.util.*; class Main {     public stati...