Saturday, January 18, 2014

Write a method to replace all spaces in a string with ‘%20’

################## Start of the Program

#!/usr/bin/python

import sys

given_str=raw_input("Please enter your string: ")

def replace_space():
    c_list=[]
    len_iter=len(given_str)
    i=0
    while len_iter>0:
        if given_str[i] == ' ':
            c_list.append("%20")
        else:
            c_list.append(given_str[i])
        i=i+1
        len_iter=len_iter - 1   
    #print "".join(c_list)
    len_iter1=len(c_list)
    j=0
    while len_iter1>0:
        sys.stdout.write(c_list[j])
        j=j + 1
        len_iter1=len_iter1 - 1
    print " "

if __name__=="__main__":
    replace_space()

################## End pf the Program

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place?

################# Start of the Program

#!/usr/bin/python

a=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
num_of_rows=len(a)
num_of_col=len(a[0])

def rotate_image():
    b=[ [ None for i in range(num_of_col) ] for j in range(num_of_rows) ]
    for i in range(num_of_col):
        for j in range(num_of_rows):
            b[i][j]=a[(num_of_rows - 1) - j][i]
    print "Given Matrix : ", a
    print "90 Degree Rotated Matrix : ", b
       
if __name__=='__main__':
    rotate_image()

################## End of the Program

My Profile

My photo
can be reached at 09916017317