Python Examples
Tuesday, 30 June 2020
Sunday, 22 July 2018
Parentheses Code
C code
#include <stdio.h>
#include <stdlib.h>
#define max 50
int arepair(char opening,char closing)
{
if(opening == '(' && closing == ')') return 1;
else if(opening == '{' && closing == '}') return 1;
else if(opening == '[' && closing == ']') return 1;
return 0;
}
int main(int argc, char const *argv[])
{
char stk[max],exp[100];
int top,i;
top=-1;
printf("Enetr sting");
gets(exp);
printf("%s\n",exp);
for (int i = 0; exp[i]!='\0'; i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{')
{
top++;
stk[top]=exp[i];
}
else if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
{
if(0==arepair(stk[top],exp[i]))
printf("NOT paired");
else
top--;
}
}//for
if(top==-1)
printf("Exp is balance");
else
printf("NOT\n");
return 0;
}
OUTPUT
C:\Users\rushi\Desktop\1>gcc parant.c
C:\Users\rushi\Desktop\1>a.exe
Enetr sting{()[()()]}
{()[()()]}
Exp is balance
C:\Users\rushi\Desktop\1>a.exe
Enetr sting{][}
{][}
NOT pairedNOT pairedNOT
C:\Users\rushi\Desktop\1>
Saturday, 6 January 2018
Bubble Sort Useing python
Bubble Sort Useing Python 2.7
Strategy To be use to write python code
Bubble Sort is simple sorting Algorithm which wark step by step sorting according to the passes or iteration
Rules to sort
- Number of iteration or passes is to be done is eqal to number of element in the list
- Compare each pare of adjesent two numbers
- Swaps them untill the no swaps are needed
Strategy To be use to write python code
- Use list to take input
- Append is python define list function
- For loop of "X" varible is to number of passes or iteration
- For loopof "XX" is for no of sorting in single pass to be done
Pyhton Program using class is little bit large but easy to understand.
- Class Name of "sortAlgo" is use .
- Many Function is use to make program simple to understand.
- "__init__" is constracter used to define empty List "o_list"
Monday, 1 January 2018
Hello World Program phyton
In Python Printing any thing is easy and simple .
° There's no need to tell that no z
Friday, 29 December 2017
Implemention of Binary Tree using Python
Implemention of Binary Tree Using Python
The Python code is according to python 2.7In computer science, a binary tree is tree data structure in which each node has at most two childe,each one referred a left child and right child
Each node contain three parts :-
1)Address of node on his left size 2)Value stored by node 3)Address of node on his right size
The Tree Represention Is As :->
This code is only for creating the tree .If u try to run this u don't get any output on terminal I Know gays....the code is bit large .but i try my best to mini. it.
Code For creating Binary Tree with Printing in diferrent form
I use Overloading in the printing the Triee in different form
- In-Order
- Pre-Order
- Post-Order
Output:-
C:\Users\rushi\Desktop\mypyDec>python bt2.py No of node to add >6 Enter node value >70 >40 >80 >15 >50 >75 Print Tree in IN-ORDER :- 15 40 50 70 75 80 Print Tree in Pre-Order :- 70 15 40 50 75 80 Print Tree in Post-Order :- 15 40 50 75 80 70 C:\Users\rushi\Desktop\mypyDec>
Tuesday, 26 December 2017
Decimal to Binary Conversation using python
Convert Decimal to binary using python 2.7
Steps to convert Decimalto binary
step 2:-record remainder from step1 as the rightmost digits
step 3:-divide the quotient of previous by 2
step 4:-record remainder from step3 as the next rightmost digits
step 5:-repeat untill zero at quotient
I specially recommend to use second second code (arithmetic logic code)
Output of prog using define fun.
python NoConv.py Enter Number in decimal form > 3 3 in binary = 0b11 ********** Enter x to end program or y to convert >y Enter Number in decimal form > 4 4 in binary = 0b100 ********** Enter x to end program or y to convert >x
Output of prog using arithmetic
rushi\Desktop\>python NoConv.py Enter input > 33 33 in binary :- 1 0 0 0 0 1 PS C:\Users\rushi\Desktop\mypyDec>
Saturday, 23 December 2017
Fibonacci Number using Python Code
Fibonacci Numbers Using Pythan
Definition :-
Fn=Fn-1 +Fn-2
where seed Values F1=1 and F2=1
The first 11 Fibonacci numbers Fn for n=0,1,2,....,11 aref0 | f1 | f2 | f3 | f4 | f5 | f6 | f7 | f8 | f9 | f10 | f11 |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 1 | 2 | 3 | 5 | 8 | 13 | 21 | 34 | 55 | 89 |
Roots of Quadratic Equations using Python code
Roots of Quadratic Equations using Python code
ax2+bx+c=0where x represents an unknown, and a, b, and c represent known numbers such that a is not equal to 0. If a = 0, then the equation is linear, not quadratic.
cmath :-
This module is always available. It provides access to mathematical functions for complex numbers. The functions in this module accept integers, floating-point numbers or complex numbers as arguments.
Root's of Q Equation
-------------------------------------Python Code-----------------------------------
import cmath
print 'input'
a=input('side 1')
b=input('side 2')
c=input('side 3')
inroot=b**2-(4*a*c)
sol1=(-b- cmath.sqrt(inroot))/(2*a)
sol2=(-b+ cmath.sqrt(inroot))/(2*a)
Check Number is Prime Using Python
Number is Prime Or Not
First We want to Know Theory of Prime Number
A prime number (or a prime) is natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite Number For example, 5 is prime because 1 and 5 are its only positive integer factors
----------------------------------------------------------------------------------------------------------------------
The first 100 prime numbers (all the prime numbers less than 100) are:
- 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
Python Code for check Number is prime or Not .
-
---------------------------------------------------------------------------------------------------------------------------
Python Code for check Number is prime or Not .
---------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------
compare three numbers
Compare Three Numbers In Simple Way
First way :-
Let assume three no's num1 , num2 , num3 which we want to compare.
So we want a technique which we can learn faster and we can remember easily and simple .
----------------------------------------------------------------------------------------------------------------while 1:
print '---------------------------------------------
num1=float(input("Enter 1 no to check > "))
num2=float(input("Enter 2 no to check > "))
num3=float(input("Enter 3 no to check > "))
if num1>num2 and num1>num3:
larger=num1
elif num2>num3:
larger=num2
else:
larger=num3
print ' larger one %f' %larger
-------------------------------------------------------------------------------------------------------
Second way :-
Let assume three no's num1 , num2 , num3 which we want to compare.
This is mostly used by programmer .
------------------------------------------------------------------------------------------------------
while 1:
num1=float(input("Enter 1 no to check > "))
num2=float(input("Enter 2 no to check > "))
num3=float(input("Enter 3 no to check > "))
if num1>=num2 and num1>=num3:
larger=num1
elif num2>=num1 and num2>=num3:
larger=num2
else:
larger=num3
print ' larger number is %f' %larger
num2=float(input("Enter 2 no to check > "))
num3=float(input("Enter 3 no to check > "))
if num1>=num2 and num1>=num3:
larger=num1
elif num2>=num1 and num2>=num3:
larger=num2
else:
larger=num3
print ' larger number is %f' %larger
Subscribe to:
Posts (Atom)
IT's Good
-
Number is Prime Or Not First We want to Know Theory of Prime Number A prime number (or a prime ) is natural number greater...
-
In Python Printing any th ing is easy and simple . ° There 's no need to tell that n o z