Monday, January 30, 2017

Recursion Program Java

Tower of Hanoi: Puzzle in which there are three towers say A,B,C. and some finite number of disks in increasing order of size downwards.

Objective : Move disks from A to C by following below rules:
   1. Only one disk can be moved at a time,and only the top disk.
   2. At no time it is allowed to place a larger disk on small disk.

Following Code Uses Recursion(Function Calling Itself): 

Code::
import java.util.*;
public class Tower 
{  static int c=0;

    public static void main(String[] nt) 
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter Number of Disks: ");
        int n=in.nextInt();
        System.out.println("Moves:");
        hanoi(n,'A','B','C');
        System.out.println("Total Moves: "+c);
    }
    
    public static void hanoi(int n,char A,char B,char C)
    {   if(n<1)
       {
        System.out.println("Please Enter Finite Number of Disks");
        return;
       }
        
        if(n==1)
        {
            System.out.println("From Tower "+A+" to Tower "+C);
            c++;
            return;
        }
        hanoi(n-1,A,C,B);
        System.out.println("From Tower "+A+" to Tower "+C);
        c++;
        hanoi(n-1,B,A,C);   
    }
}

Output::

PygLatin Python :)

Pig Latin is a language game, where you move the first letter of the word to the end and add "ay." So "Computer" becomes "omputercay." 

Following are the Steps involved:
1.Input the word
2. Converting the String to Lowercase
3. Assign first letter of the word to another variable
4. Removing first letter from the word
5. Concatenating the Strings for Output
Following Program Illustrates it:

word=input("Enter the Word: ")   #input word
temp=word.lower()   #lowercase conversion
first=temp[0]       #first letter
temp=temp[1:]
suffix='ay'
if(len(word)==0):
    print("String is Empty!!")
elif word.isalpha()==False:  #else if statement
    print("Enter only Alphabets!")
else:
    print("Pig Latin translation of "+word+" is ")
    print(temp+first+suffix)



Note: isalpha() function is used to check whether String contains alphabets or not. It gives True when all letters in String are alphabets.

"99 Bottles of Beer"

Question::
"99 Bottles of Beer" is a traditional song in the United States and Canada. It is popular to sing on long trips, as it has a very repetitive format which is easy to memorize, and can take a long time to sing. The song's simple lyrics are as follows:
99 bottles of beer on the wall, 99 bottles of beer.
Take one down, pass it around, 98 bottles of beer on the wall.
The same verse is repeated, each time with one fewer bottle. The song is completed when the singer or singers reach zero.
Your task here is write a Python program capable of generating all the verses of the song.


Code::
l=99
for l in (range(99,1,-1)):

    print(l ,"bottles of beer on the wall," ,l," bottles of beer.\nTake one down, pass it around,",l-1 ,"bottles of beer on the wall.\n")


Output::

Pangram Python

Question::
pangram is a sentence that contains all the letters of the English alphabet at least once, for example: The quick brown fox jumps over the lazy dog. Your task here is to write a function to check a sentence to see if it is a pangram or not.

Code::

def panagram(nt):
    check="abcdefghijklmnopqrstuvwxyz"
    for l in check:
        if(l in nt):
            continue
        else:
            return False
    return True           
n=input("Enter Any Text:")
if(panagram(n.lower())):
    print("Yes It is a Pangram")
else:

    print("No It is not a Pangram")

Output::


Sunday, January 29, 2017

Filter Long Words

Question::

Write a function filter_long_words() that takes a list of words and an integer n and returns the list of words that are longer than n.

Code:

def filter_long_words(l,a):
    words=[]
    for j in l:
        if(len(j)>=a):
            words.append(j)
    return words

n=input("Enter words:")
nt=n.split(",")
na=input("Enter Min Length:")
long=filter_long_words(nt,int(na))

print("Words with at least min length:",long)

Output::

Longest Word in a List

Question::

Write a function find_longest_word() that takes a list of words and returns the length of the longest one

Code::
def longest(l):
    long=0
    word=""
    for j in l:
        if(long<len(j)):
            long=len(j)
            word=j
    return [word,long]

n=input("Enter words:")
nt=n.split(",")
long=longest(nt)

print("Longest Word with length:",long)

Output::

Mapping Python

Question::
Write a program that maps a list of words into a list of integers representing the lengths of the corresponding words.

Code::

def mapping(l):
    mapped=[]
    for j in l:
        mapped.append(len(j))
    return mapped

n=input("Enter words:")
nt=n.split(",")
mapped=mapping(nt)

print("Another List:",mapped)

Output::