Skip to content

Instantly share code, notes, and snippets.

@naveen521kk
Last active March 24, 2022 17:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save naveen521kk/c8adae8f85c58192334726f9587991b0 to your computer and use it in GitHub Desktop.
Programs for Semester 1
def binary_search(arr, low, high, number):
if high >= low:
mid = (high + low) // 2
if arr[mid] == number:
return mid
elif arr[mid] > number:
return binary_search(arr, low, mid - 1, number)
else:
return binary_search(arr, mid + 1, high, number)
else:
return -1
arr = [5, 12, 17, 19, 22, 30]
number = 22
r = binary_search(arr, 0, len(arr) - 1, number)
if r != -1:
print("Element found at: ", r)
else:
print("Element not found.")
# Copying a file reading and writing upto 50 chars at a time
f1 = input("Enter File 1: ")
f2 = input("Enter File 2: ")
with open(f1) as fr:
with open(f2, 'w') as fw:
chars = fr.read(50)
while chars:
fw.write(chars)
chars = fr.read(50)
import sys
in_file = sys.argv[1]
out_file = sys.argv[2]
print('Copying %s to %s'%(in_file, out_file))
with open(in_file) as fr:
with open(out_file, 'w') as fw:
for content in fr:
fw.write(content)
import sys
in_file = sys.argv[1]
with open(in_file) as fr:
contents = fr.read()
num_chars = len(contents)
num_words = len(contents.split())
num_lines = len(contents.split('\n'))
print('Number of characters: ', num_chars)
print('Number of words: ', num_words)
print('Number of lines: ', num_lines)
import datetime
print(datetime.datetime.now())
def dict_program(a,b):
new_dict= {}
for i in a:
if i not in b:
new_dict[i] = a[i]
return new_dict
print(dict_program({"a":1,"b":2},{"a":1}))
def factorial_loop(a):
prd = 1
for i in range(1, a + 1):
prd *= i
return prd
print(factorial_loop(5))
def factorial(a):
if a == 1:
return 1
return a * factorial(a-1)
print(factorial(5))
def print_fibonacci_series(n: int):
"""
n -> number of items to print
"""
n1, n2 = 0, 1 # series start with 0
fib_nums = [n1, n2]
for _ in range(2, n): # already 2 numbers done
n3 = n1 + n2
fib_nums.append(n3)
n1 = n2
n2 = n3
return fib_nums
print(print_fibonacci_series(10))
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
# check if the number of terms is valid
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
data = [ ]
n = int(input('Enter total number of elements in the list: '))
print('Enter the Elements in Ascending Order' )
for i in range(0, n):
x = int(input('Enter the Element: '))
data.append(x)
e = int(input('Enter the Element to search: '))
first = 0
last = n-1
while first <= last:
mid = (first + last)//2
if(e > data[mid]):
first = mid +1
elif(e < data[mid]):
last = mid - 1
else:
print('Element ', e ,' Found at Position ', mid+1)
break
else:
print('Element', e ,' is Not Found in the List')
def square_root(a):
x=.5 * a
while True:
print(x)
y = (x + a/x) / 2
if y == x:
break
x = y
print("Square Root of ", a , " is ", y)
print(square_root(5))
a = 5
b = 7
a, b = b, a
print(b, a)
a = 5
b = 7
a = a + b
b = a - b
a = a -b
print(a, b) # 7 5
with open('testappend.txt', 'a') as f:
f.write('test append')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment