Wednesday, February 1, 2017

Number To String Python

Following Code will convert a number entered by user to the Same Number in Words

Code::

def numtostring(n):
    if (n==0):return ""
    elif(n==1):return "one"
    elif(n==2):return "two"
    elif(n==3):return "three"
    elif(n==4):return "four"
    elif(n==5):return "five"
    elif(n==6):return "six"
    elif(n==7):return "seven"
    elif(n==8):return "eight"
    elif(n==9):return "nine"
    elif(n==10):return "ten"
    elif(n==11):return "eleven"
    elif(n==12):return "twelve"
    elif(n==13):return "thirteen"
    elif(n==14):return "fourteen"
    elif(n==15):return "fifteen"
    elif(n==16):return "sixteen"
    elif(n==17):return "seventeen"
    elif(n==18):return "eighteen"
    elif(n==19):return "nineteen"
    elif(n<=29):return "twenty "+numtostring(n%10)
    elif(n<=39):return "thirty "+numtostring(n%10)
    elif(n<=49):return "forty "+numtostring(n%10)
    elif(n<=59):return "fifty "+numtostring(n%10)
    elif(n<=69):return "sixty "+numtostring(n%10)
    elif(n<=79):return "seventy "+numtostring(n%10)
    elif(n<=89):return "eighty "+numtostring(n%10)
    elif(n<=99):return "ninety "+numtostring(n%10)

def convert(n):
    if(n<=99):
        return numtostring(n)
    elif(n<=999):
        return numtostring(n//100)+' hundred '+numtostring(n%100)
    elif(n<=9999):
        return numtostring(n//1000)+' thousand '+numtostring((n//100)%10)+' hundred '+numtostring((n%100))


i=int(input("Enter Any Number Upto 9999: "))
print(convert(i))
     
Output:


Roman Numeral of Given Input Number Python

Following Code will print the Equivalent Roman Numeral of Input Number :

Code::

def romandigit(n,a,b,c):
    if(n==0):return ""
    elif(n==1):return a
    elif(n==2):return a+a
    elif(n==3):return a+a+a
    elif(n==4):return a+b
    elif(n==5):return b
    elif(n==6):return b+a
    elif(n==7):return b+a+a
    elif(n==8):return b+a+a+a
    elif(n==9):return a+c
    
def romannumber(n):
    if(n>999):
        raise RuntimeError('Not Allowed')
    if(n<=99):
        return romandigit((n//10)%10,'X','L','C')+romandigit(n%10,'I','V','X')
    elif(n<=999):
        return romandigit((n//100)%10,'C','D','M')+romandigit((n//10)%10,'X','L','C')+romandigit(n%10,'I','V','X')

def main():
    i=int(input("Enter Any Number Upto 999:"))
    print(romannumber(i))

main()

Output::

Tuesday, January 31, 2017

Fibonacci Sequence Using Recursion Java

Fibonacci Sequence:
                                     Basic rule:: next term = Sum of previous two terms

Code::

import java .util.*;
public class Fibonacci 
{    
    public static void main(String[] nt) 
    {
      Scanner in=new Scanner(System.in);
      System.out.print("Enter Number of Terms:");
      int a=in.nextInt();
      for(int i=0;i<a;i++)
      {
          System.out.print(fibonacci(i)+" ");
      }
    }
    public static int fibonacci(int n)
    {  
        if(n<2)
        {
            return n;
        }
        else
        {
            return (fibonacci(n-2)+fibonacci(n-1));
        }
    }
}

Output::


                          

Monday, January 30, 2017

Character Frequency In a String Python

Question::

Write a function char_freq() that takes a string and builds a frequency listing of the characters contained in it. Represent the frequency listing as a Python dictionary. Try it with something like char_freq("abbabcbdbabdbdbabababcbcbab").

Code::

def char_freq(l):
    d={}
    for i in (range(len(l))):
        if (l[i] in d):
            d[l[i]]=int(d.get(l[i]))+1
        else:
            d[l[i]]=1     
    return d
nt=input("Enter any String:")
n=char_freq(nt)

print(n)

Output::

Call By Value V/S Call By Reference Java

Call by value: In this approach copy of the current variables are passed to called function.
i.e any change in called function on values is not reflected in main program...

Following code will illustrate this:

Code:
import java.util.*;
public class Value 
{

    public static void main(String[] nt) 
    {
       Scanner in=new Scanner(System.in);
       System.out.print("Enter A:");
       int a=in.nextInt();
       System.out.print("Enter B:");
       int b=in.nextInt();
       swap(a,b);
       System.out.println("After Swapping Outside Swap Function :");
       System.out.println("A:"+a);
       System.out.println("B:"+b);
    }
    public static void swap(int c,int d)
    {
        int temp=d;
        d=c;
        c=temp;
       System.out.println("After Swapping Inside Swap Function :"); 
        System.out.println("A:"+c);
        System.out.println("B:"+d);
    }
}

Output:


Call By Reference: As there are no pointers in java so we can not use & to refer to the variables

Following Code Will Illustrate this:

Code::

import java.util.*;
class Swap
{
    int a,b;
    public void swap(Swap s)
    {
        int temp=s.a;
        s.a=s.b;
        s.b=temp;
    }
}
public class Reference 
{

    public static void main(String[] nt) 
    {   Swap ob=new Swap();
        Scanner in=new Scanner(System.in);
       
        System.out.print("Enter A: ");
       ob.a=in.nextInt();
       System.out.print("Enter B: ");
       ob.b=in.nextInt();
       
       System.out.println("Before Swapping :");
       System.out.println("A:"+ob.a);
       System.out.println("B:"+ob.b);
       
       ob.swap(ob);
       
       System.out.println("After Swapping :");
       System.out.println("A:"+ob.a);
       System.out.println("B:"+ob.b);
        
    }   
}

Output::


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.