19 Jan 2015

Longest substring in Alphabetical order

#Python

'''
Find the longest substring in alphabetical order in the given string
'''

s = 'azcbobobegghakl'

i=1
subs=s[0]
subs2=s[0]
while(i<len(s)):
    j=i
    while(j<len(s)):
        if(s[j]>=s[j-1]):
            subs+=s[j]
            j+=1
        else:
            subs=subs.replace(subs[:len(subs)],s[i])  
            break
               
        if(len(subs)>len(subs2)):
            subs2=subs2.replace(subs2[:len(subs2)], subs[:len(subs)])
    subs=subs.replace(subs[:len(subs)],s[i])
       
    if(len(subs)==len(s)):
        subs2=subs2.replace(subs2[:len(subs2)], subs[:len(subs)])
        print "not here"   
        break;    
           
    i+=1
print "Longest substring in alphabetical order is:",subs2
              




Categories: , ,

Count of Substring in String

#Python
'''
Count the number of times the substring 'bob' appears in string s

'''


s='azcbobobegghakl'
i=0
ocr=0
while(i<(len(s)-2)):
    ocr+=s.count('bob',i,i+3)
    i+=1      
print "Number of times bob occurs is:", ocr     



Categories: ,

Counting Vowels

#Python

'''
The program counts number of vowels in a string s
'''


s = 'azcbobobegghakl'


s=s.lower()
count=0
i=0
while (i<len(s)):
    if (s[i] in 'aeiou'):
        count+=1
    i+=1
print "Number of vowels", count   


 

Categories: , ,

Number Guessing game

#Python

'''
Number Guessing Game
'''



 

print "Please think of a number between 0 and 100!"
start=0
end=100
guess=50
while(1):
    print "Is your secret number",guess, "?"
    rd=raw_input("Enter 'h' to indicate the guess ls too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly.")
    if(rd <>'c' and rd <>'l' and  rd<>'h'):
        print "Sorry, I did not understand your input."
    elif(rd=='c'):
        print "Game over. Your secret number was:", guess
        break
    elif(rd=='h'):
        end=guess
        guess=(start+end)/2
    else:
        start=guess
        guess=(start+end)/2   
   

Categories: , , ,

Copyright © UPgradeCODING | Powered by Blogger