Python Program for Tower of Hanoi

Pavitra
Updated on 20-Dec-2019 07:04:37

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given n disks and a series of rods, we need to transfer all the disks to the final rod under the given constraints−We can move only one disk at a time.Only the uppermost disk from the rod can be moved.Any bigger disk cannot be placed on the smaller diskNow let’s observe the solution in the implementation below −Example Live Demo# tower of hanoi def TowerOfHanoi(n , from_rod, to_rod, aux_rod):    if n == 1:       print ("Move disk 1 from rod", ... Read More

Facebook Login Using Python

Pradeep Elance
Updated on 20-Dec-2019 07:02:58

1K+ Views

We can use the python package called selenium to automate the interaction with webdrivers. In this article we will see the interaction between python’s selenium package and logging in to Facebook.ApproachSelenium package is used to automate and controls web browsers activity. Out python code will need the selenium package to be installed and also a driver software known as geckodriver to be available for the program. Below are the steps to achieve this.Step-1Install selenium in you python environmentpip install seleniumStep-2Download the geckodriver from this link. Place it in the same directory where we are going to have this python script.Next we ... Read More

Subset Sum Problem in Python

Pavitra
Updated on 20-Dec-2019 06:59:44

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a set of non-negative integers in an array, and a value sum, we need to determine if there exists a subset of the given set with a sum equal to a given sum.Now let’s observe the solution in the implementation below −# Naive approachExampledef SubsetSum(set, n, sum) :    # Base Cases    if (sum == 0) :       return True    if (n == 0 and sum != 0) :       return False    # ... Read More

Dictionary Methods in Python: cmp, len, items

Pradeep Elance
Updated on 20-Dec-2019 06:58:57

2K+ Views

Dictionary in python is one of the most frequently used collection data type. It is represented by hey value pairs. Keys are indexed but values may not be. There are many python-built in functions that make using the dictionary very easy in various python programs. In this topic we will see the three in-built methods namely cmp(), len() and items().cmp()The method cmp() compares two dictionaries based on key and values. It is helpful in identifying duplicate dictionaries as well as doing a relational comparison among the dictionaries. It is a feature on only python2 and not available in python 3.Syntaxcmp(dict1, ... Read More

Python Program for Stooge Sort

Pavitra
Updated on 20-Dec-2019 06:55:01

245 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using stooge sort.Algorithm1. Check if value at index 0 is greater than value at last index, then swap them. 2. sort the initial 2/3rd of the array. 3. sort the last 2/3rd of the array. 4. sort the initial 2/3rd again to confirm.Now let’s observe the solution in the implementation below −Example Live Demodef stoogesort(arr, l, h):    if l >= h:       return    # swap    if arr[l]>arr[h]:     ... Read More

Dictionary Methods in Python: update, has_key, fromkeys

Pradeep Elance
Updated on 20-Dec-2019 06:53:26

520 Views

Dictionary in python is one of the most frequently used collection data type. It is represented by hey value pairs. Keys are indexed but values may not be. There are many python-built in functions that make using the dictionary very easy in various python programs. In this topic we will see the three in-built methods namely update(), has_key() and fromkeys().update()The method update adds new items to a given dictionary by merging the items from the secondary with first.Syntaxdict1.update(dict2) Where dict1 and dict2 are the two input dictionaries.In the below example we see pairs of dictionaries. The second dictionary gets added ... Read More

Python Program for Sieve of Eratosthenes

Pavitra
Updated on 20-Dec-2019 06:51:15

2K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a number n, we need to print all primes smaller than or equal to n. Constraint: n is a small number.Now let’s observe the solution in the implementation below −Exampledef SieveOfEratosthenes(n):    # array of type boolean with True values in it    prime = [True for i in range(n + 1)]    p = 2    while (p * p

Python Program for Recursive Insertion Sort

Pavitra
Updated on 20-Dec-2019 06:48:55

1K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement− We are given an array, we need to sort it using the concept of recursive insertion sort.Insertion sort works on creating a parallel array in which we manually insert the elements in the specified order.Now let’s observe the solution in the implementation below −Example Live Demo# recursive way def insertionSortRecursive(arr, n):    # base case    if n=0 and arr[j]>last):       arr[j+1] = arr[j]       j = j-1    arr[j+1]=last # main arr = [1, 5, 3, 4, 8, 6, 3, ... Read More

Python Program for Quicksort

Pavitra
Updated on 20-Dec-2019 06:46:04

6K+ Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using the concept of quicksortHere we first partition the array and sort the separate partition to get the sorted array.Now let’s observe the solution in the implementation below −Example Live Demo# divide function def partition(arr,low,high):    i = ( low-1 )    pivot = arr[high] # pivot element    for j in range(low , high):       # If current element is smaller       if arr[j]

Python Program for Odd-Even Sort and Brick Sort

Pavitra
Updated on 20-Dec-2019 06:43:29

278 Views

In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given an array, we need to sort it using brick sort.Here we have two phases: Odd and Even Phase. In the odd phase, bubble sort is performed on odd indexed elements and in the even phase, bubble sort is performed on even indexed elements.Now let’s observe the solution in the implementation below−Exampledef oddEvenSort(arr, n):    # flag    isSorted = 0    while isSorted == 0:       isSorted = 1       temp = 0       ... Read More

Advertisements