Saturday, July 23, 2022

9_Regular Expressions

Regular expressions-

Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name

Here  the email or phone number in Gmail has the following HTML STRUCTURE

<input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" autocapitalize="none" id="identified" dir="ltr" data-initial-dir="ltr" data-initial-value="" badinput="false" aria-invalid="true">

 You can see that the input class attribute is composed of more than one name - whsOnd zHQkBf

In order to frame the CSS path for this, we can use regular expression as –

* à  perform a partial match with the class attribute value. The CSS value shall be input[class*= whsOnd]

This means the subtext whsOnd is present in the actual text whsOnd zHQkBf

 ^ à perform a match with the class. The CSS value shall be input[class^= ‘whsOnd’]. This means the actual text whsOnd zHQkBf starts with the subtext whsOnd.


$ àperform a match with the class. The CSS value shall be input[class$=’zHQkBf’]. This means the actual text whsOnd zHQkBf ends with the subtext zHQkBf.

For each of these three regular expression usages, the code will look as –

Using *

driver.get("https://accounts.google.com/");

driver.findElement(By.cssSelector("input[class*= whsOnd]")).sendKeys("admin");

 

Using ^

driver.get("https://accounts.google.com/");

driver.findElement(By.cssSelector("input[class^='whsOnd']")).sendKeys("admin"); 


****************Code for regular Expression to identify UserName Field*******************

**

 * @author Mann

 *

 */


import java.time.Duration;


import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.edge.EdgeDriver;

public class RegularExpressions_3 {

public static void main(String[] args) {

System.setProperty("webdriver.edge.driver", "C:/Users/Mann/SeleniumTutorials/msedgedriver.exe");

WebDriver driver= new EdgeDriver();

   driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

// using *

driver.get("https://accounts.google.com/");

driver.findElement(By.cssSelector("input[class*= whsOnd]")).sendKeys("admin");


// using $

//driver.findElement(By.cssSelector("input[class$='zHQkBf']. ")).sendKeys("admin");

// using ^

//driver.findElement(By.cssSelector("input[class^='whsOnd']")).sendKeys("admin");

//driver.findElement(By.name("user_name")).sendKeys("admin");

}}


Wednesday, June 22, 2022

AVL TREE

 // Through this code we will learn how to insert in a BST and then perform different rotations so that it becomes AVL Tree. 


// Video Link to each function used in this code is given here -

Sub Functions for Rotations   - https://youtu.be/5lG8WfL0KTU

// Actual code starts here ....

#include <stdio.h>

#include <stdlib.h>

struct node {

  int key;

  struct node *left, *right;

};

// Create a node

struct node *newNode(int item) {

struct node *temp = (struct node *)malloc(sizeof(struct node));

  temp->key = item;

  temp->left = temp->right = NULL;

  return temp;

}

// preoder Traversal

void preorder(struct node *t) {

  if(t!=NULL)

  {

  printf("%d -> ", t->key);

  preorder(t->left);

  preorder(t->right);

  }

    }

  // Inoder Traversal

void inorder(struct node *t) {

  if(t!=NULL)

  {

    inorder(t->left);

  printf("%d -> ", t->key);

    inorder(t->right);

  }

    }

// Find the inorder successor

struct node *minValueNode(struct node *node) {

  struct node *current = node;

  // Find the leftmost leaf

  while (current && current->left != NULL)


    current = current->left;

  return current;

}

// Deleting a node

struct node *deleteNode(struct node *root, int key) {

  // Return if the tree is empty

  if (root == NULL) return root;

  // Find the node to be deleted

  if (key < root->key)

    root->left = deleteNode(root->left, key);

  else if (key > root->key)

    root->right = deleteNode(root->right, key);

  else {

    // If the node is with only one child or no child

    if (root->left == NULL) {

      struct node *temp = root->right;

      free(root);

      return temp;

    } else if (root->right == NULL) {

      struct node *temp = root->left;

      free(root);

      return temp;

    }

    // If the node has two children

    struct node *temp = minValueNode(root->right);

    // Place the inorder successor in position of the node to be deleted

    root->key = temp->key;

   // Delete the inorder successor

   root->right = deleteNode(root->right, temp->key);

 }

  return root;

}

// Sub function for height()

int height(struct node *t)

{

if(!t)

return 0;

 if (!(t->left) && !(t->right)) // --> assume leaf node is at height 1

 return 1;

  int l,r;

 l=height(t->left);

 r=height(t->right);

 int p=l>r?l:r;

 return(1+(p));

 }

// Sub function for diff()

int diff(struct node *t)

{

int left= height(t->left);

int right= height(t->right);

int difference;

difference=left-right;

return difference;

}

// Sub function for LL_Rotation

struct node *LL_Rotation(struct node *t)

{

struct node *curr;

curr=t->left;

t->left=curr->right;

curr->right=t;

return curr;

}

// Sub function for RR_Rotation


struct node *RR_Rotation(struct node *t)

{

struct node *curr;

curr=t->right;

t->right=curr->left;

curr->left=t;

return curr;

}


// Sub function for RL_Rotation

struct node *RL_Rotation(struct node *t)

{

t->right=LL_Rotation(t->right);

t=RR_Rotation(t);

return t;

}

// Sub function for lR_Rotation

struct node *LR_Rotation(struct node *t)

{

t->left=RR_Rotation(t->left);

t=LL_Rotation(t);

return t;

}

// calculating balanceing  factor 

struct node *balance( struct node *curr)

{

int getbalance=diff(curr);

if (getbalance>1)

{

if(diff(curr->left)>0)

curr=LL_Rotation(curr);

else

curr=LR_Rotation(curr);

}

else if(getbalance<-1)

{

if(diff(curr->right)>0)

curr=RL_Rotation(curr);

else

curr=RR_Rotation(curr);

}

return curr;

}


// Insert a node

struct node *insert(struct node *curr, int key) 

{

  // Return a new node if the tree is empty

  if (curr == NULL) return newNode(key);

  // Traverse to the right place and insert the node

  if (key < curr->key)

  {

  curr->left = insert(curr->left, key);

   

  }

    

  else

  {

  curr->right = insert(curr->right, key);

  

  }

   

   curr=balance(curr);

   return curr;

}


// Driver code


int main() {


  struct node *root = NULL;

  root = insert(root, 3);

  root = insert(root, 2);

  root = insert(root, 1);


  root = insert(root, 6);

  root = insert(root, 7);

  root =insert(root, 10);

  root = insert(root, 14);

  root = insert(root, 4);

  printf("Inorder traversal: ");

  inorder(root);

printf("pre-order traversal: ");

 preorder(root);

  //printf("\nAfter deleting 10\n");

  //root = deleteNode(root, 10);

 // printf("Inorder traversal: ");


 // inorder(root);


}




Tuesday, June 21, 2022

1. BST Code

 // Binary Search Tree operations in C


#include <stdio.h>

#include <stdlib.h>


struct node {

  int key;

  struct node *left, *right;

};


// Create a node

struct node *newNode(int item) {

  struct node *temp = (struct node *)malloc(sizeof(struct node));

  temp->key = item;

  temp->left = temp->right = NULL;

  return temp;

}


// Inorder Traversal

void inorder(struct node *root) {

  if (root != NULL) {

    // Traverse left

    inorder(root->left);


    // Traverse root

    printf("%d -> ", root->key);


    // Traverse right

    inorder(root->right);

  }

}


// Insert a node

struct node *insert(struct node *node, int key) {

  // Return a new node if the tree is empty

  if (node == NULL) return newNode(key);


  // Traverse to the right place and insert the node

  if (key < node->key)

    node->left = insert(node->left, key);

  else

    node->right = insert(node->right, key);


  return node;

}


// Find the inorder successor

struct node *minValueNode(struct node *node) {

  struct node *current = node;


  // Find the leftmost leaf

  while (current && current->left != NULL)

    current = current->left;


  return current;

}


// Deleting a node

struct node *deleteNode(struct node *root, int key) {

  // Return if the tree is empty

  if (root == NULL) return root;


  // Find the node to be deleted

  if (key < root->key)

    root->left = deleteNode(root->left, key);

  else if (key > root->key)

    root->right = deleteNode(root->right, key);


  else {

    // If the node is with only one child or no child

    if (root->left == NULL) {

      struct node *temp = root->right;

      free(root);

      return temp;

    } else if (root->right == NULL) {

      struct node *temp = root->left;

      free(root);

      return temp;

    }


    // If the node has two children

    struct node *temp = minValueNode(root->right);


    // Place the inorder successor in position of the node to be deleted

    root->key = temp->key;


    // Delete the inorder successor

    root->right = deleteNode(root->right, temp->key);

  }

  return root;

}


// Driver code

int main() {

  struct node *root = NULL;

  root = insert(root, 8);

  root = insert(root, 3);

  root = insert(root, 1);

  root = insert(root, 6);

  root = insert(root, 7);

  root = insert(root, 10);

  root = insert(root, 14);

  root = insert(root, 4);


  printf("Inorder traversal: ");

  inorder(root);


  printf("\nAfter deleting 10\n");

  root = deleteNode(root, 10);

  printf("Inorder traversal: ");

  inorder(root);

}

Tuesday, May 31, 2022

Linked List Series - 1 P1- Sort and Reverse a Linked List

 //   Linked List Series - 

// P1- Sort and Reverse a Linked List 

Program

# include <stdio.h>

# include <stdlib.h>

struct node

{

   int data;

   struct node *link;

};

struct node *insert(struct node *p, int n)

{

   struct node *temp;

   if(p==NULL)

   {

      p=(struct node *)malloc(sizeof(struct node));

      if(p==NULL)

      {

            printf("Error\n");

         exit(0);

      }

      p-> data = n;

      p-> link = NULL;

   }

   else

   {

      temp = p;

      while (temp-> link!= NULL)

      temp = temp-> link;

      temp-> link = (struct node *)malloc(sizeof(struct node));

      if(temp -> link == NULL)

      {

            printf("Error\n");

         exit(0);

      }

       temp = temp-> link;

       temp-> data = n;

       temp-> link = null;

      }

      return(p);

}


void printlist ( struct node *p )

{

      printf("The data values in the list are\n");

      while (p!= NULL)

      {

            printf("%d\t",p-> data);

         p = p-> link;

      }

}


/* a function to sort reverse list */

struct node *reverse(struct node *p)

{

   struct node *prev, *curr;

   prev = NULL;

   curr = p;

   while (curr != NULL)

   {

      p = p-> link;

      curr-> link = prev;

      prev = curr;

      curr = p;

   }

   return(prev);

}

/* a function to sort a list */

struct node *sortlist(struct node *p)

{

   struct node *temp1,*temp2,*min,*prev,*q;

   q = NULL;

   while(p != NULL)

   {

         prev = NULL;

      min = temp1 = p;

      temp2 = p -> link;

      while ( temp2 != NULL )

      {

              if(min -> data > temp2 -> data)

         {

                     min = temp2;

                     prev = temp1;

         }

         temp1 = temp2;

         temp2 = temp2-> link;

      }

      if(prev == NULL)

             p = min -> link;

      else

             prev -> link = min -> link;

      min -> link = NULL;

      if( q == NULL)

      q = min; /* moves the node with lowest data value in the list

pointed to by p to the list

   pointed to by q as a first node*/

         else

         {

            temp1 = q;

            /* traverses the list pointed to by q to get pointer to its

last node */

            while( temp1 -> link != NULL)

                         temp1 = temp1 -> link;

            temp1 -> link = min; /* moves the node with lowest data value

in the list pointed to

   by p to the list pointed to by q at the end of list pointed by

   q*/

      }

   }

   return (q);

}


void main()

{

      int n;

      int x;

      struct node *start = NULL ;

      printf("Enter the nodes to be created \n");

      scanf("%d",&n);

      while ( n- > 0 )

      {

              printf( "Enter the data values to be placed in a

node\n");

         scanf("%d",&x);

         start = insert ( start,x);

      }

      printf("The created list is\n");

      printlist ( start );

      start = sortlist(start);

      printf("The sorted list is\n");

      printlist ( start );

      start = reverse(start);

      printf("The reversed list is\n");

      printlist ( start );

}


Friday, September 10, 2021

Python for Indian Stock Market Analysis


Created on Sat Sep 11 10:14:16 2021

@author: Mann

"""


pip install nsetools

from nsetools import Nse

nse = Nse()

print (nse)


#Since it is a dictionary you can easily chop off fields of your interest.

q = nse.get_quote('infy') # it's ok to use both upper or lower case for codes.

from pprint import pprint # just for neatness of display

q['averagePrice']


pprint(q)

msft = nse.Ticker("infy")

# To get all listed indices 

nse.get_index_list()

nse.get_index_quote("nifty bank") # code can be provided in upper|lower case.

# List of Traded Stock Codes & Names

all_stock_codes = nse.get_stock_codes()

#This is a dictionary with more that thousand entries.


print (all_stock_codes)

#List of Index Codes Similar to above, there is a way to get the list of codes of all the traded indices. Unlike in previous section, the return type is list.

index_codes = nse.get_index_list()

pprint(index_codes)


Sunday, April 18, 2021

Data Structure Lab - Program- 3

 Program 3- 

Write a C program that uses stack operations to convert a given infix expression into its postfix Equivalent, Implement the stack using an array.


Warm-up Exercise 


Stacks Push and POP Operation


Solution ) 


#include<stdio.h>

#include<stdlib.h>      /* for exit() */

#include<ctype.h>     /* for isdigit(char ) */

#include<string.h>

          #define SIZE 100

          /* declared here as global variable because stack[]

* is used by more than one fucntions */

char stack[SIZE];

int top = -1;

/* define push operation */

void push(char item)

{

if(top >= SIZE-1)

{

printf("\nStack Overflow.");

}

else

{

top = top+1;

stack[top] = item;

}

}


/* define pop operation */

char pop()

{

char item ;

if(top <0)

{

printf("stack under flow: invalid infix expression");

getchar();

/* underflow may occur for invalid expression */

/* where ( and ) are not matched */

exit(1);

}

else

{

item = stack[top];

top = top-1;

return(item);

}

}


/* define function that is used to determine whether any symbol is operator or not

(that is symbol is operand)

* this fucntion returns 1 if symbol is opreator else return 0 */

int is_operator(char symbol)

{

if(symbol == '^' || symbol == '*' || symbol == '/' || symbol == '+' || symbol =='-')

{

return 1;

}

else

{

return 0;

}

}


/* define fucntion that is used to assign precendence to operator.

* Here ^ denotes exponent operator.

* In this fucntion we assume that higher integer value

* means higher precendence */

int precedence(char symbol)

{

if(symbol == '^')/* exponent operator, highest precedence*/

{

return(3);

}

else if(symbol == '*' || symbol == '/')

{

return(2);

}

else if(symbol == '+' || symbol == '-')          /* lowest precedence */

{

return(1);

}

else

{

return(0);

}

}


void InfixToPostfix(char infix_exp[], char postfix_exp[])

{

int i, j;

char item;

char x;


push('(');                               /* push '(' onto stack */

strcat(infix_exp,")");                  /* add ')' to infix expression */


i=0;

j=0;

item=infix_exp[i];         /* initialize before loop*/


while(item != '\0')        /* run loop till end of infix expression */

{

if(item == '(')

{

push(item);

}

else if( isdigit(item) || isalpha(item))

{

postfix_exp[j] = item;              /* add operand symbol to postfix expr */

j++;

}

else if(is_operator(item) == 1)        /* means symbol is operator */

{

x=pop();

while(is_operator(x) == 1 && precedence(x)>= precedence(item))

{

postfix_exp[j] = x;                  /* so pop all higher precendence operator and */

j++;

x = pop();                       /* add them to postfix expresion */

}

push(x);

/* because just above while loop will terminate we have

oppped one extra item

for which condition fails and loop terminates, so that one*/


push(item);                 /* push current oprerator symbol onto stack */

}

else if(item == ')')         /* if current symbol is ')' then */

{

x = pop();                   /* pop and keep popping until */

while(x != '(')                /* '(' encounterd */

{

postfix_exp[j] = x;

j++;

x = pop();

}

}

else

{ /* if current symbol is neither operand not '(' nor ')' and nor

operator */

printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */

getchar();

exit(1);

}

i++;



item = infix_exp[i]; /* go to next symbol of infix expression */

} /* while loop ends here */

if(top>0)

{

printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */

getchar();

exit(1);

}

if(top>0)

{

printf("\nInvalid infix Expression.\n");        /* the it is illegeal  symbol */

getchar();

exit(1);

}



postfix_exp[j] = '\0'; /* add sentinel else puts() fucntion */

/* will print entire postfix[] array upto SIZE */

}


/* main function begins */

int main()

{

char infix[SIZE], postfix[SIZE];         /* declare infix string and postfix string */


/* why we asked the user to enter infix expression

* in parentheses ( )

* What changes are required in program to

* get rid of this restriction since it is not

* in algorithm

* */

printf("ASSUMPTION: The infix expression contains single letter variables and single digit constants only.\n");

printf("\nEnter Infix expression : ");

gets(infix);

InfixToPostfix(infix,postfix);                   /* call to convert */

printf("Postfix Expression: ");

puts(postfix);                     /* print postfix expression */


return 0;

}



Data Structure Lab-Program-2

 Program -2 

 Write a C program that uses functions to perform the following: 

1. Create a doubly-linked list of integers

2. Delete a given integer from the above doubly linked list. 

3. Display the contents of the above list after deletion


warmup Exercise- 

Click here to code Doubly Linked List

Create and traverse in doubly Linked List

Insert New node at different positions


Solution ) 


/*Doubly Linked List - Coded By Dr. M. Mann @ IIIT Sonepat */

#include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *prev;

    struct node *next;

}*head;

void creation(int n)

{

    head=(struct node *)malloc(sizeof(struct node));

    struct node *temp;

    struct node *newnode;

    printf("Enter data =");

    scanf("%d",&head->data);

    head->prev=NULL;

    head->next=NULL;

    temp=head;

    for(int z=2;z<=n;z++)

    {

       newnode=(struct node *)malloc(sizeof(struct node));

       printf("Enter data=");

       scanf("%d",&newnode->data);

       newnode->prev=temp;

       newnode->next=NULL;

       temp->next=newnode;

       temp=temp->next;

    }

}

void traverse()

{

    struct node *temp=head;

    while(temp)

    {

        printf("Entered data=%d\n",temp->data);

        temp=temp->next;

    }

}

void delete()

{

    int del;

    printf("Enter Which Node you want to delete=");

    scanf("%d",&del);

    struct node *temp=(struct node *)malloc(sizeof(struct node));

    temp=head;

    while(temp)

    {

        if(head->data==del)

        {

            head=temp->next;

            break;

        }

        else if(temp->next->data==del)

        {

            temp->next=temp->next->next;

            temp->next->next->prev=temp;

            break;

        }

        temp=temp->next;

    }

}

void main()

{

    int n;

    printf("Enter number of doubly list=");

    scanf("%d",&n);

    creation(n);

    traverse();

    delete();

    traverse();

}

9_Regular Expressions

Regular expressions- Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name Here ...