1.
# Factorial of a number using recursion
2.
3. def recur_factorial(n):
4. if n == 1:
5. return n
6. else:
7. return n*recur_factorial(n-1)
8.
9. num = 7
10.
11. # check if the number is negative
12. if num < 0:
13. print("Sorry, factorial does not exist for negative numbers")
14. elif num == 0:
15. print("The factorial of 0 is 1")
16. else:
17. print("The factorial of", num, "is", recur_factorial(num))
# Python code to
# demonstrate readlines()
L = ["Geeks\n", "for\n", "Geeks\n"]
# writing to file
file1 = open('myfile.txt', 'w')
file1.writelines(L)
file1.close()
# Using readlines()
file1 = open('myfile.txt', 'r')
Lines = file1.readlines()
count = 0
# Strips the newline character
for line in Lines:
print(line.strip())
print("Line{}: {}".format(count, line.strip()))
import glob
# Merge all output files into one file
read_files = glob.glob('/home/user/Results/Script_tests/TestResults/*.output')
with open('MergedOutput.txt', 'r+b') as outfile:
for file in read_files:
with open(file, 'r+b') as infile:
outfile.write(infile.read())
print 'Files merged.'
# Remove header rows except from row 1
final_output = open('FinalMergedOutput.txt', 'r+b')
with open('MergedOutput.txt', 'r+b') as file:
for line in file:
if line == 0 and line.startswith('File'):
final_output.write(line)
elif line > 0 and not line.startswith('File'):
final_output.write(line)
print 'Headers removed except on line 1.'
# Python3 code for implementing
# sin function
import math;
# Function for calculating sin value
def cal_sin(n):
accuracy = 0.0001;
# Converting degrees to radian
n = n * (3.142 / 180.0);
x1 = n;
# maps the sum along the series
sinx = n;
# holds the actual value of sin(n)
sinval = math.sin(n);
i = 1;
while(True):
denominator = 2 * i * (2 * i + 1);
x1 = -x1 * n * n / denominator;
sinx = sinx + x1;
i = i + 1;
if(accuracy <= abs(sinval - sinx)):
break;
print(round(sinx));
# Driver Code
n = 90;
cal_sin(n);
# This code is contributed by mits
# Program to generate a random number between 0 and 6
# importing the random module
import random
print(random.randint(0,6))
# Python program to find sum of elements in list
total = 0
# creating a list
list1 = [11, 5, 17, 18, 23]
# Iterate each element in list
# and add them in variale total
for ele in range(0, len(list1)):
total = total + list1[ele]
# printing total value
print("Sum of all elements in given list: ", total)
# Function for nth Fibonacci number
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==1:
return 0
# Second Fibonacci number is 1
elif n==2:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))
#This code is contributed by Saket Modi
class Stack:
def __init__(self):
self.q = Queue()
def is_empty(self):
return self.q.is_empty()
def push(self, data):
self.q.enqueue(data)
def pop(self):
for _ in range(self.q.get_size() - 1):
dequeued = self.q.dequeue()
self.q.enqueue(dequeued)
return self.q.dequeue()
class Queue:
def __init__(self):
self.items = []
self.size = 0
def is_empty(self):
return self.items == []
def enqueue(self, data):
self.size += 1
self.items.append(data)
def dequeue(self):
self.size -= 1
return self.items.pop(0)
def get_size(self):
return self.size
s = Stack()
print('Menu')
print('push <value>')
print('pop')
print('quit')
while True:
do = input('What would you like to do? ').split()
operation = do[0].strip().lower()
if operation == 'push':
s.push(int(do[1]))
elif operation == 'pop':
if s.is_empty():
print('Stack is empty.')
else:
print('Popped value: ', s.pop())
elif operation == 'quit':
break
import urllib.request
request_url = urllib.request.urlopen('https://www.geeksforgeeks.org/')
print(request_url.read())
# EMI Calculator program in Python
def emi_calculator(p, r, t):
r = r / (12 * 100) # one month interest
t = t * 12 # one month period
emi = (p * r * pow(1 + r, t)) / (pow(1 + r, t) - 1)
return emi
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate, time);
print("Monthly EMI is= ", emi)
# This code is contributed by "Abhishek Sharma 44"
# Python program to find the k most frequent words
# from data set
from collections import Counter
data_set = "Welcome to the world of Geeks " \
"This portal has been created to provide well written well" \
"thought and well explained solutions for selected questions " \
"If you like Geeks for Geeks and would like to contribute " \
"here is your chance You can write article and mail your article " \
" to contribute at geeksforgeeks org See your article appearing on " \
"the Geeks for Geeks main page and help thousands of other Geeks. " \
# split() returns list of all the words in the string
split_it = data_set.split()
# Pass the split_it list to instance of Counter class.
Counter = Counter(split_it)
# most_common() produces k frequently encountered
# input values and their respective counts.
most_occur = Counter.most_common(4)
print(most_occur)