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:
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])
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'])
#!/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'])