Tuesday, February 14, 2017

Tabulating Club Dues Python

Following Code will tabulate the dues of Members of Club :

Code::

def main():
    fre={}
    print("Enter Dues ,enter quit to end")
    while True:
        line=input()
        if(line=='quit'):break
        words=line.split(':')
        fre[words[0]]=fre.get(words[0],0)+int(words[1])
    for name in fre:
        print(name+" has Paid Rs",fre[name],"in dues")

main()

Output::

Word Frequency Python

Following code will give the frequency of occurrence of words in text:

Code::

def main():
    fre={}
    print("Enter Text ,enter quit to end the text")
    while True:
        line=input()
        if(line=='quit'):break
        for el in line.split():
            fre[el]=fre.get(el,0)+1
    print (fre)

main()

    
Output::

Monday, February 13, 2017

Eliza Python

ELIZA is an early natural language processing computer program created from 1964 to 1966[1] at the MIT Artificial Intelligence Laboratory by Joseph Weizenbaum.[2] Created to demonstrate the superficiality of communication between man and machine.

Code::         #a short example u can increase the conditions 

def reply(line,words):
    if len(words)==0: return "You have to talk to me "
    elif "mother"in words:return "Tell me more about your mother "
    elif "father" in words :return "Tell me more about your father "
    elif words[0]=='i' and words[1]=='feel': return "Why do you feel that way? "
    elif words[-1]=='?': return "What do u want to know? "
    elif words[0]=='i' and words[1]=='think': return "Do you really think so? "
    elif "question" in words:return "What's the question? "
    elif "love" in words:return "I must tell you,Love is Very Complicated Agree? "
    elif "agree" in words:return "Do you agree always with this much ease? "
    elif "no" in words: return"Nice!!,Tell me more "
    else: return "Tell me more "

name=input("Hello, Welcome to NT Therapy Center,What's Your name? ")
print("Type quit to exit")
line=input("Hello "+name+" How can we help you? ")
while line!='quit':
    line=line.lower()
    words=line.split()
    replied=reply(line,words)

    line=input(replied)

Output::

Message Encryption and Decryption Python

Suppose you want to send a message to a friend,but you don't want other people to be able to read it
To do this you must disguise the text in some fashion.This Process is called Encryption.The reverse process is called Decryption.

Code::

def encrypt(msg):
    result=''
    for c in msg:
        result=result+" "+str(ord(c))
    return result

def decrypt(msg):
    result=''
    for c in msg.split():
        result=result+chr(int(c))
    return result


msg=input("Enter Secret Message: ")
emsg=encrypt(msg)
print("Encrypted Message: "+emsg)

print("Decrypted Message: "+decrypt(emsg))

Output::


Another method is using rotate 13 i.e convert the entered character to character 13 place ahead that character.


Code::                                                                                                                                       

def char13(c):
    alph="abcdefghijklmnopqrstuvwxyz"
    index=alph.find(c)
    index=(index+13)%26
    return alph[index]

def encrypt(text):
    result=''
    for c in text.lower():
        if 'a'<=c<='z':result=result+char13(c)
        else: result=result+c
    return result

msg=input("Enter Secret Message: ")
emsg=encrypt(msg)
print("Encrypted Message: "+emsg)
print("Decrypted Message: "+encrypt(emsg))


Output::                      
                                                                                                             





Date Conversion from One Format To Another Python

Following Code will convert date entered in one format to another:

Code::

def converter(date):
    day,month,year=date.split('/')
    monthnames=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    return monthnames[int(month)-1]+" "+day+","+year

date=input("Enter Date in DD/MM/YYYY format : ")
print ("Entered Date: "+converter(date))

Output::


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::