Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored and Optimised Code base #1699

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ def bdelete():
# Deleting the Roll no. entered by user
rno = int(input("Enter the Roll no. to be deleted: "))
with open("studrec.dat") as F:
rec = []
for i in stud:
if i[0] == rno:
continue
rec.append(i)
rec = [i for i in stud if i[0] != rno]
pickle.dump(rec, F)


Expand Down
39 changes: 18 additions & 21 deletions 1 File handle/File handle binary/Update a binary file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,27 @@


def update():
F = open("class.dat", "rb+")
S = pickle.load(F)
found = 0
rno = int(input("enter the roll number you want to update"))
for i in S:
if rno == i[0]:
print("the currrent name is", i[1])
i[1] = input("enter the new name")
found = 1
break
with open("class.dat", "rb+") as F:
S = pickle.load(F)
found = 0
rno = int(input("enter the roll number you want to update"))
for i in S:
if rno == i[0]:
print("the currrent name is", i[1])
i[1] = input("enter the new name")
found = 1
break

if found == 0:
print("Record not found")
if found == 0:
print("Record not found")

else:
F.seek(0)
pickle.dump(S, F)

F.close()
else:
F.seek(0)
pickle.dump(S, F)


update()

F = open("class.dat", "rb")
val = pickle.load(F)
print(val)
F.close()
with open("class.dat", "rb") as F:
val = pickle.load(F)
print(val)
42 changes: 20 additions & 22 deletions 1 File handle/File handle binary/Update a binary file2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@


def update():
F = open("studrec.dat", "rb+")
value = pickle.load(F)
found = 0
roll = int(input("Enter the roll number of the record"))
for i in value:
if roll == i[0]:
print("current name", i[1])
print("current marks", i[2])
i[1] = input("Enter the new name")
i[2] = int(input("Enter the new marks"))
found = 1

if found == 0:
print("Record not found")

else:
pickle.dump(value, F)
F.seek(0)
newval = pickle.load(F)
print(newval)

F.close()
with open("studrec.dat", "rb+") as F:
value = pickle.load(F)
found = 0
roll = int(input("Enter the roll number of the record"))
for i in value:
if roll == i[0]:
print("current name", i[1])
print("current marks", i[2])
i[1] = input("Enter the new name")
i[2] = int(input("Enter the new marks"))
found = 1

if found == 0:
print("Record not found")

else:
pickle.dump(value, F)
F.seek(0)
newval = pickle.load(F)
print(newval)


update()
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,67 @@


"""

# also find no. of children who got top marks

import pickle

F = open("class.dat", "ab")
list = [
[1, "Ramya", 30],
[2, "vaishnavi", 60],
[3, "anuya", 40],
[4, "kamala", 30],
[5, "anuraag", 10],
[6, "Reshi", 77],
[7, "Biancaa.R", 100],
[8, "sandhya", 65],
]
with open("class.dat", "ab") as F:
list = [
[1, "Ramya", 30],
[2, "vaishnavi", 60],
[3, "anuya", 40],
[4, "kamala", 30],
[5, "anuraag", 10],
[6, "Reshi", 77],
[7, "Biancaa.R", 100],
[8, "sandhya", 65],
]


pickle.dump(list, F)
F.close()
pickle.dump(list, F)


def remcount():
F = open("class.dat", "rb")
val = pickle.load(F)
count = 0
with open("class.dat", "rb") as F:
val = pickle.load(F)
count = 0

for i in val:
if i[2] <= 40:
print(i, "eligible for remedial")
count += 1
print("the total number of students are", count)
F.close()
for i in val:
if i[2] <= 40:
print(i, "eligible for remedial")
count += 1
print("the total number of students are", count)


remcount()


def firstmark():
F = open("class.dat", "rb")
val = pickle.load(F)
main = []
count = 0
with open("class.dat", "rb") as F:
val = pickle.load(F)
main = []
count = 0

for i in val:
data = i[2]
main.append(data)
for i in val:
data = i[2]
main.append(data)

top = max(main)
print(top, "is the first mark")
top = max(main)
print(top, "is the first mark")

F.seek(0)
for i in val:
if top == i[2]:
print(i)
print("congrats")
count += 1
F.seek(0)
for i in val:
if top == i[2]:
print(i)
print("congrats")
count += 1

print("the total number of students who secured top marks are", count)
F.close()
print("the total number of students who secured top marks are", count)


firstmark()

F = open("class.dat", "rb")
val = pickle.load(F)
print(val)
F.close()
with open("class.dat", "rb") as F:
val = pickle.load(F)
print(val)
25 changes: 12 additions & 13 deletions 1 File handle/File handle binary/search record in binary file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@


def binary_search():
F = open("studrec.dat", "rb")
# your file path will be different
value = pickle.load(F)
search = 0
rno = int(input("Enter the roll number of the student"))
with open("studrec.dat", "rb") as F:
# your file path will be different
value = pickle.load(F)
search = 0
rno = int(input("Enter the roll number of the student"))

for i in value:
if i[0] == rno:
print("Record found successfully")
print(i)
search = 1
for i in value:
if i[0] == rno:
print("Record found successfully")
print(i)
search = 1

if search == 0:
print("Sorry! record not found")
F.close()
if search == 0:
print("Sorry! record not found")


binary_search()
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@

while True:
ch = F.readlines()
for (i) in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
for i in ch:
print(i, end="")
else:
sys.stderr.write("End of file reached")
break
sys.stderr.write("End of file reached")
break

27 changes: 13 additions & 14 deletions 1 File handle/File handle text/special symbol after word.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
F = open("happy.txt", "r")
# method 1
val = F.read()
val = val.split()
for i in val:
print(i, "*", end="")
print("\n")
with open("happy.txt", "r") as F:
# method 1
val = F.read()
val = val.split()
for i in val:
print(i, "*", end="")
print("\n")


# method 2
F.seek(0)
value = F.readlines()
for line in value:
for word in line.split():
print(word, "*", end="")
F.close()
# method 2
F.seek(0)
value = F.readlines()
for line in value:
for word in line.split():
print(word, "*", end="")
4 changes: 2 additions & 2 deletions A solution to project euler problem 3.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def solution(n: int = 600851475143) -> int:
TypeError: Parameter n must be int or passive of cast to int.
"""
try:
n = int(n)
n = n
except (TypeError, ValueError):
raise TypeError("Parameter n must be int or passive of cast to int.")
if n <= 0:
Expand All @@ -55,7 +55,7 @@ def solution(n: int = 600851475143) -> int:
ans = i

while n % i == 0:
n = n / i
n /= i

i += 1

Expand Down
2 changes: 1 addition & 1 deletion AreaOfTriangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

# calculate the area
area = (s*(s-a)*(s-b)*(s-c)) ** 0.5
print('The area of the triangle is: ' + area)
print(f'The area of the triangle is: {area}')
7 changes: 3 additions & 4 deletions Assembler/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ def loadFile(fileName):
loadFile: This function loads the file and reads its lines.
"""
global lines
fo = open(fileName)
for line in fo:
lines.append(line)
fo.close()
with open(fileName) as fo:
for line in fo:
lines.append(line)


def scanner(string):
Expand Down
2 changes: 1 addition & 1 deletion Automated Scheduled Call Reminders/caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def search():
timestamp = datetime.now().strftime("%H:%M")
five_minutes_prior = (timestamp + timedelta(minutes=5)).strftime("%H:%M")
for doc in list_of_docs:
if doc["from"][0:5] == five_minutes_prior:
if doc["from"][:5] == five_minutes_prior:
phone_number = doc["phone"]
call = client.calls.create(
to=phone_number,
Expand Down
41 changes: 17 additions & 24 deletions Base Converter Number system.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
def base_check(xnumber, xbase):
for char in xnumber[len(xnumber ) -1]:
if int(char) >= int(xbase):
return False
return True
return all(int(char) < int(xbase) for char in xnumber[len(xnumber ) -1])

def convert_from_10(xnumber, xbase, arr, ybase):
if int(xbase) == 2 or int(xbase) == 4 or int(xbase) == 6 or int(xbase) == 8:
if int(xbase) in {2, 4, 6, 8}:

if xnumber == 0:
return arr
else:
quotient = int(xnumber) // int(xbase)
remainder = int(xnumber) % int(xbase)
arr.append(remainder)
dividend = quotient
convert_from_10(dividend, xbase, arr, base)
quotient, remainder = divmod(int(xnumber), int(xbase))
arr.append(remainder)
dividend = quotient
convert_from_10(dividend, xbase, arr, base)
elif int(xbase) == 16:
if int(xnumber) == 0:
return arr
else:
quotient = int(xnumber) // int(xbase)
remainder = int(xnumber) % int(xbase)
if remainder > 9:
if remainder == 10: remainder = 'A'
if remainder == 11: remainder = 'B'
if remainder == 12: remainder = 'C'
if remainder == 13: remainder = 'D'
if remainder == 14: remainder = 'E'
if remainder == 15: remainder = 'F'
arr.append(remainder)
dividend = quotient
convert_from_10(dividend, xbase, arr, ybase)
quotient, remainder = divmod(int(xnumber), int(xbase))
if remainder > 9:
if remainder == 10: remainder = 'A'
if remainder == 11: remainder = 'B'
if remainder == 12: remainder = 'C'
if remainder == 13: remainder = 'D'
if remainder == 14: remainder = 'E'
if remainder == 15: remainder = 'F'
arr.append(remainder)
dividend = quotient
convert_from_10(dividend, xbase, arr, ybase)
def convert_to_10(xnumber, xbase, arr, ybase):
if int(xbase) == 10:
for char in xnumber:
Expand Down
Loading