Monday, February 13, 2017

Palindrome Testing Recursively Python

Entered string is Palindrome or not can be tested using Recursion as Shown by Following Code::

Code::

def isletter(c):     #function to check that character c is a letter or not
    return ('a'<=c<='z')or ('A'<=c<='Z')

def paltest(line):
    if len(line)<=1:return True
    elif not isletter(line[0]):return paltest(line[1:])      #ignoring first not letter
    elif not isletter(line[-1]):return paltest(line[:-1])    #ignoring last not letter
    elif line[0].lower()!=line[-1].lower():return False      
    else:return paltest(line[1:len(line)-1])                 #palindrome testing recursive call


string=input("Enter any String: ")
if(paltest(string)) : print("Yes,Palindrome")
else: print("Not,Palindrome")

Output::

Saturday, February 11, 2017

Case Conversion Python

Following Code Will Convert The Case of Entered String 
i.e lower to UPPER and UPPER to lower



Code::

def conversion(l):
    li=list(l)
    for i in range(0,len(l)):
        if 'a'<=li[i]<='z': li[i]=chr(ord(li[i])-32)
        elif 'A'<=li[i]<='Z': li[i]=chr(ord(li[i])+32)
    stri=''
    return stri.join(li)

s=input("Enter String: ")
print(conversion(s))


Output::


Standard Deviation Calculator Python

The Standard Deviation is a measure of how spread out numbers are.

Standard Deviation is a quantity expressing by how much the members of a group differ from the mean value for the group.
Its symbol is σ (the greek letter sigma)
Following Code will calculate the Standard Deviation:

Code::
import math
def average(l):             #function to calculate average(mean)
    total=0.0
    for i in l:
        total=total+int(i)
    if(len(l)==0): return 0
    else: return(total/len(l))
    
def standardeviation(l,ave):       #function to calculate standard deviation
    diff=0.0
    for i in l:
        x=int(i)-ave
        diff=diff+x*x
    if(len(l)==0): return 0
    else :return(math.sqrt(diff/len(l)))    


lst=[]
num=input("Enter Values ,enter -1 to end:")
while num!='-1':
    lst.append(num)
    num=input()
ave=average(lst)
print("Average is:",ave)
sd=standardeviation(lst, ave)
print("Standard Deviation :",sd)
        
Output::

Friday, February 10, 2017

Anagram Checking

An Anagram is a word, phrase, or name formed by rearranging the letters of another, such as Hitler woman, formed from mother in law.


Following Code Will Check if the entered Strings are Anagrams or Not ::

Code::

import java.util.*;
public class Anagram 
{
    public static void main(String[] nt) 
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter First String: ");
        String s1=in.nextLine();
        System.out.print("Enter Second String: ");
        String s2=in.nextLine();
        check(s1,s2);              //function to check if the entered strings are anagram
    }
    static void check(String a,String b)
    {   boolean status=true;
        a=a.replaceAll(" ", "");      //removing white spaces from strings
        b=b.replaceAll(" ", ""); 
        char[] s1=a.toLowerCase().toCharArray();    //converting string to character array
        char[] s2=b.toLowerCase().toCharArray();
        if(s1.length!=s2.length)
        {
            status=false;
        }
        else
        {
            Arrays.sort(s1);               //sorting array
            Arrays.sort(s2);
            if(!Arrays.equals(s1, s2))        //checking characters
            {
               status=false;
            }
        }
        if(status)
        {
            System.out.println("Yes these are Anagrams");
        }
        else
        {
            System.out.println("No these are not Anagrams");
        }
    }

}
Output::


More Examples::

  • Debit card = Bad credit
  • Dormitory = Dirty Room
  • The earthquakes = The queer shakes
  • Astronomer = Moon starrer
  • Punishments = Nine Thumps
  • School master = The classroom


Thursday, February 9, 2017

Perfect Number Java

Perfect numbera positive integer that is equal to the sum of its proper divisors. The smallest perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers are 28, 496, and 8,128. 

Code::

import java.util.*;
public class PerfectNumber 
{

    
    public static void main(String[] nt) 
    {
       Scanner in= new Scanner(System.in);
       System.out.print("Enter any Number: ");
       int n=in.nextInt();
       int temp=n;
       int sum=0;
       for(int i=1;i<n;i++)
       {
           if(temp%i==0)
           {
               sum=sum+i;
           }
       }
       if(sum==n)
       {
           System.out.println("Yes it is a Perfect Number");
       }
       else
       {
           System.out.println("No it is not a Perfect Number");
       }
    }
    

}

Output::

Find the day of the week of a given date

Question::  Implement a Java-method that prints out the day of the week for a given day (1..31), month (1..12) and year.

Code::

import java.util.*;
public class DayFromDate 
{

    public static void main(String[] nt) 
    {
        Scanner in=new Scanner(System.in);
        System.out.print("Enter Date:");
        int d=in.nextInt();
        System.out.print("Enter Month:");
        int m=in.nextInt();
        System.out.print("Enter Year:");
        int y=in.nextInt();
        int x=(y-1900)*365+(y-1900)/4;
       
        
        if(y%4==0&&m<=2)
        {
            x--;
        }
        
      
      
        
         switch(m)
         {
             case 12:x+=30;
             case 11:x+=31;
             case 10:x+=30;
             case 9:x+=31;
             case 8:x+=30;
             case 7:x+=31;
             case 6:x+=31;
             case 5:x+=30;
             case 4:x+=31;
             case 3:x+=28;
             case 2:x+=31;
         }
         x=x+d;
         x=x%7;
        String weekday;
        switch(x)
                {   
                    case 0:weekday="Sunday"; break;
                    case 1:weekday="Monday"; break;
                    case 2:weekday="Tuesday"; break;
                    case 3:weekday="Wednesday"; break;
                    case 4:weekday="Thrusday"; break;
                    case 5:weekday="Friday"; break;
                    case 6:weekday="Saturday"; break;
                    
                    default:System.out.println("Invalid day number");return;
                   
                }
        System.out.println("Weekday is "+weekday);
        
    }
    

}

Simpler way:

import java.util.Calendar;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner in=new Scanner(System.in);
        System.out.print("Enter Date:");
        int d=in.nextInt();
        System.out.print("Enter Month:");
        int m=in.nextInt();
        System.out.print("Enter Year:");
        int y=in.nextInt();
        
        Calendar c=Calendar.getInstance();
        c.set(Calendar.DATE, d);
        c.set(Calendar.MONTH,(m-1));
        c.set(Calendar.YEAR,y);
        String[] days= {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; 
        System.out.println("Day is : "+days[c.get(Calendar.DAY_OF_WEEK)-1]);

}


}



Output::


Wednesday, February 8, 2017

Multiple Strings Java

Code::

import java.util.*;
public class MultipleString {

    public static void main(String[] nt) {
        Scanner in =new Scanner(System.in);
        System.out.print("Enter Number of Strings:");
        int n=in.nextInt();
        in.nextLine();                  //for flushing the stream
       String a[]=new String[n];
       System.out.println("Enter Strings:");
       for(int i=0;i<n;i++)
       {
           a[i]=in.nextLine();
       }
       System.out.println("Entered Strings:");
       for(int i=0;i<n;i++)
       {
           System.out.println(a[i]);
       }
    }
    
}

Output::