Showing posts with label Interview questions. Show all posts
Showing posts with label Interview questions. Show all posts

Friday, January 20, 2017

Python programs

1. Primefactor

#!/usr/bin/python

def primefactor_small(n):
        if n > 0:
                p = 2
                while n >= p*p:
                        if n%p == 0:
                                n = n / p
                                print str(p) + "*"
                        else:
                                p = p+1

                return n


if __name__=='__main__':
        import sys
        print primefactor_small(int(sys.argv[1]))

Reference:

 

Execution



2. Fibnoacci series with only even numbers

#!/usr/bin/python

def Fibonacci(num):
        ''' Fibonacci series '''
        if num < 1:
                print "No fibonacci"
        else:
                fiblist = [1,2]
                fibtotal = 3
                sum=0
                #for i in range(2, num):
                while fiblist[-1] < 4000000:
                        fiblist.append(fiblist[-1] + fiblist[-2])
                        fibtotal+=fiblist[-1]
                        if fiblist[-1] > 4000000:
                                fiblist.pop(-1)
                                for i in range(0, len(fiblist)):
                                        if fiblist[i] % 2 == 0:
                                                sum+=fiblist[i]
                                return fiblist, sum,fibtotal
                        else:
                                pass

        return fiblist, sum,fibtotal


if __name__=='__main__':
        import sys
        print Fibonacci(int(sys.argv[1])


Execution:

 vallikkv@vallikkv:~/My Programs/Python/Problem_Solving$ python 2_fibaonnci_even.py 100
([1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578], 4613732, 14930350)


3. Write a program where a there are multiple users logging into the system or file and I want to know the login Occurrences of the each user . Note : The file is separated by the commas. ex: User1 , User2, user1 , user3.........


ef conusers():
        inp = []
        oup = []
        Sum = {}

        ''' Reading file contents and assign to a list'''
        with open('file.csv') as f:
                lines = f.read().splitlines()


        '''Converting all the names in lowercase Split the line contents using comma '''
        inp = [x.lower() for x in lines]
        for i in range(len(inp)):
                oup =  inp[i].split(",")
                for j in range(len(oup)):
                        if oup[j] not in Sum:
                                Sum[oup[j]] = 1
                        else:
                                Sum[oup[j]] += 1


        return Sum


if __name__=='__main__':
        print conusers()


4. Write a program to remove duplicates numbers from the list and display the duplicates ?

def duplicate(input_list):
        a = input_list
        b= [ ]
        for i in range(len(a)):
                if a[i] not in b:
                        b.append(a[i])
        return b


if __name__=='__main__':
        print duplicate([1,2,3,4,5,4,3,2,'Hello','World','Hello'])






Saturday, January 2, 2016

Python File handling

Python scripting:  Search and replace a string in file.

def Main():
    with open("E:\Softwares\Sample.txt") as f:
        for x, line in enumerate(f):
            if "Valli" in line:
                print line.replace("Valli", "Nayagam", -1),


if __name__ == "__main__":
    Main()

Wednesday, December 30, 2015

Python Interview questions

1. What is constructors in python?
2. How do inherit in python?
3. Name the types of datatypes in python?

Python has five standard data types:
1. Numbers
2. Strings
3. List
4. Tuple
5. Dictionary

Python supports four different numerical types:
1. int (signed integers)
2. long (long integers, they can also represented in octal and hexa decimal)
3. Float
4. Complex


4. Name the types of  variables in python? and how to configure the private, public variables and how to access them?

5. How do you reverse the string?


How do you login the webpage using python scripting without selenium web driver?

6. Write a class that keeps track of all the identifiers of objects created while instantiating?

7. Write a method that takes two dimensional list as argument and gives the transpose of the list
(Eg:)
def transpose(input_list):
    #Your Code here
    return output_list

a = [[1,2,3],[4,5,6]]


print transpose(a)
#Outputs[[1,4],[2,5],[3,6]]

8. Read a file and print only the strings from the file.

def Main():
        with open('word.txt','r') as f:
                for line in f:
                        for i in line.split(" "):
                                for x in i.rstrip("\n").split():
                                        if x.isalpha() == True:
                                                print x


if __name__ == "__main__":
        Main()


9. Write a class which implements method overloading?

10. What is the output of the following code?

def fun1(a=None):
    print "In the first function"

def fun1(a):
   print "In the second function"