File tree 12 files changed +175
-188
lines changed
12 files changed +175
-188
lines changed Original file line number Diff line number Diff line change @@ -10,11 +10,7 @@ def bdelete():
10
10
# Deleting the Roll no. entered by user
11
11
rno = int (input ("Enter the Roll no. to be deleted: " ))
12
12
with open ("studrec.dat" ) as F :
13
- rec = []
14
- for i in stud :
15
- if i [0 ] == rno :
16
- continue
17
- rec .append (i )
13
+ rec = [i for i in stud if i [0 ] != rno ]
18
14
pickle .dump (rec , F )
19
15
20
16
Original file line number Diff line number Diff line change 4
4
5
5
6
6
def update ():
7
- F = open ("class.dat" , "rb+" )
8
- S = pickle .load (F )
9
- found = 0
10
- rno = int (input ("enter the roll number you want to update" ))
11
- for i in S :
12
- if rno == i [0 ]:
13
- print ("the currrent name is" , i [1 ])
14
- i [1 ] = input ("enter the new name" )
15
- found = 1
16
- break
7
+ with open ("class.dat" , "rb+" ) as F :
8
+ S = pickle .load (F )
9
+ found = False
10
+ rno = int (input ("enter the roll number you want to update" ))
17
11
18
- if found == 0 :
19
- print ("Record not found" )
12
+ for i in S :
13
+ if rno == i [0 ]:
14
+ print (f"the currrent name is { i [1 ]} " )
15
+ i [1 ] = input ("enter the new name" )
16
+ found = True
17
+ break
20
18
21
- else :
22
- F .seek (0 )
23
- pickle .dump (S , F )
19
+ if found :
20
+ print ("Record not found" )
24
21
25
- F .close ()
22
+ else :
23
+ F .seek (0 )
24
+ pickle .dump (S , F )
26
25
27
26
28
27
update ()
29
28
30
- F = open ("class.dat" , "rb" )
31
- val = pickle .load (F )
32
- print (val )
33
- F .close ()
29
+ with open ("class.dat" , "rb" ) as F :
30
+ print (pickle .load (F ))
Original file line number Diff line number Diff line change 1
- # updating records in a bnary file
1
+ # updating records in a binary file
2
2
3
3
import pickle
4
4
5
5
6
6
def update ():
7
- File = open ("studrec.dat" , "rb+" )
8
- value = pickle .load (File )
9
- found = 0
10
- roll = int (input ("Enter the roll number of the record" ))
11
- for i in value :
12
- if roll == i [0 ]:
13
- print ("current name" , i [1 ])
14
- print ("current marks" , i [2 ])
15
- i [1 ] = input ("Enter the new name" )
16
- i [2 ] = int (input ("Enter the new marks" ))
17
- found = 1
18
-
19
- if found == 0 :
20
- print ("Record not found" )
21
-
22
- else :
23
- pickle .dump (value , File )
24
- File .seek (0 )
25
- newval = pickle .load (File )
26
- print (newval )
27
-
28
- File .close ()
7
+
8
+ with open ("studrec.dat" , "rb+" ) as File :
9
+ value = pickle .load (File )
10
+ found = False
11
+ roll = int (input ("Enter the roll number of the record" ))
12
+
13
+ for i in value :
14
+ if roll == i [0 ]:
15
+ print (f"current name { i [1 ]} " )
16
+ print (f"current marks { i [2 ]} " )
17
+ i [1 ] = input ("Enter the new name" )
18
+ i [2 ] = int (input ("Enter the new marks" ))
19
+ found = True
20
+
21
+ if not found :
22
+ print ("Record not found" )
23
+
24
+ else :
25
+ pickle .dump (value , File )
26
+ File .seek (0 )
27
+ print (pickle .load (File ))
29
28
30
29
31
30
update ()
Original file line number Diff line number Diff line change 12
12
13
13
import pickle
14
14
15
- F = open ("class.dat" , "ab" )
16
15
list = [
17
16
[1 , "Ramya" , 30 ],
18
17
[2 , "vaishnavi" , 60 ],
24
23
[8 , "sandhya" , 65 ],
25
24
]
26
25
27
-
28
- pickle .dump (list , F )
29
- F .close ()
26
+ with open ( "class.dat" , "ab" ) as F :
27
+ pickle .dump (list , F )
28
+ F .close ()
30
29
31
30
32
31
def remcount ():
33
- F = open ("class.dat" , "rb" )
34
- val = pickle .load (F )
35
- count = 0
32
+ with open ("class.dat" , "rb" ) as F :
33
+ val = pickle .load (F )
34
+ count = 0
36
35
37
- for i in val :
38
- if i [2 ] <= 40 :
39
- print (i , "eligible for remedial" )
40
- count += 1
41
- print ("the total number of students are" , count )
42
- F .close ()
36
+ for i in val :
37
+ if i [2 ] <= 40 :
38
+ print (f"{ i } eligible for remedial" )
39
+ count += 1
40
+ print (f"the total number of students are { count } " )
43
41
44
42
45
43
remcount ()
46
44
47
45
48
46
def firstmark ():
49
- F = open ("class.dat" , "rb" )
50
- val = pickle .load (F )
51
- main = []
52
- count = 0
53
-
54
- for i in val :
55
- data = i [2 ]
56
- main .append (data )
47
+ with open ("class.dat" , "rb" ) as F :
48
+ val = pickle .load (F )
49
+ count = 0
50
+ main = [i [2 ] for i in val ]
57
51
58
- top = max (main )
59
- print (top , "is the first mark" )
52
+ top = max (main )
53
+ print (top , "is the first mark" )
60
54
61
- F .seek (0 )
62
- for i in val :
63
- if top == i [2 ]:
64
- print (i )
65
- print ("congrats" )
66
- count += 1
55
+ F .seek (0 )
56
+ for i in val :
57
+ if top == i [2 ]:
58
+ print (f"{ i } \n congrats" )
59
+ count += 1
67
60
68
- print ("the total number of students who secured top marks are" , count )
69
- F .close ()
61
+ print ("the total number of students who secured top marks are" , count )
70
62
71
63
72
64
firstmark ()
73
65
74
- F = open ("class.dat" , "rb" )
75
- val = pickle .load (F )
76
- print (val )
77
- F .close ()
66
+ with open ("class.dat" , "rb" ) as F :
67
+ val = pickle .load (F )
68
+ print (val )
Original file line number Diff line number Diff line change 4
4
5
5
6
6
def binary_search ():
7
- F = open ("studrec.dat" , "rb" )
8
- # your file path will be different
9
- value = pickle .load (F )
10
- search = 0
11
- rno = int (input ("Enter the roll number of the student" ))
7
+ with open ("studrec.dat" , "rb" ) as F :
8
+ # your file path will be different
9
+ search = 0
10
+ rno = int (input ("Enter the roll number of the student" ))
12
11
13
- for i in value :
14
- if i [0 ] == rno :
15
- print ("Record found successfully" )
16
- print (i )
17
- search = 1
12
+ for i in pickle .load (F ):
13
+ if i [0 ] == rno :
14
+ print (f"Record found successfully\n { i } " )
15
+ search = 1
18
16
19
- if search == 0 :
20
- print ("Sorry! record not found" )
21
- F .close ()
17
+ if search == 0 :
18
+ print ("Sorry! record not found" )
22
19
23
20
24
21
binary_search ()
Original file line number Diff line number Diff line change
1
+ """
2
+ Class resposible for counting words for different files:
3
+ - Reduce redundant code
4
+ - Easier code management/debugging
5
+ - Code readability
6
+ """
7
+
8
+ class Counter :
9
+
10
+ def __init__ (self , text :str ) -> None :
11
+ self .text = text
12
+
13
+ # Define the initial count of the lower and upper case.
14
+ self .count_lower = 0
15
+ self .count_upper = 0
16
+ self .count ()
17
+
18
+ def count (self ) -> None :
19
+
20
+ for char in self .text :
21
+ if char .lower ():
22
+ self .count_lower += 1
23
+ elif char .upper ():
24
+ self .count_upper += 1
25
+
26
+ return (self .count_lower , self .count_upper )
27
+
28
+ def get_total_lower (self ) -> int :
29
+ return self .count_lower
30
+
31
+ def get_total_upper (self ) -> int :
32
+ return self .count_upper
33
+
34
+ def get_total (self ) -> int :
35
+ return self .count_lower + self .count_upper
Original file line number Diff line number Diff line change @@ -9,28 +9,28 @@ def write_to_file(file_name):
9
9
10
10
if os .path .exists (file_name ):
11
11
print (f"Error: { file_name } already exists." )
12
+ return
12
13
13
- else :
14
- with open (file_name , "a" ) as F :
15
- while True :
16
- text = input ("enter any text to add in the file:- " )
17
- F .write (
18
- text + "\n "
19
- ) # write function takes exactly 1 arguement so concatenation
20
- choice = input ("Do you want to enter more, y/n" )
21
- if choice == "n" :
22
- break
23
-
14
+ with open (file_name , "a" ) as F :
15
+
16
+ while True :
17
+ text = input ("enter any text to add in the file:- " )
18
+ F .write ( f"{ text } \n " )
19
+ choice = input ("Do you want to enter more, y/n" ).lower ()
20
+ if choice == "n" :
21
+ break
22
+
24
23
def longlines ():
24
+
25
25
with open (file_name , encoding = 'utf-8' ) as F :
26
26
lines = F .readlines ()
27
+ lines_less_than_50 = list ( filter (lambda line : len (line ) < 50 , lines ) )
27
28
28
- for i in lines :
29
- if len (i ) < 50 :
29
+ if not lines_less_than_50 :
30
+ print ("There is no line which is less than 50" )
31
+ else :
32
+ for i in lines_less_than_50 :
30
33
print (i , end = "\t " )
31
- else :
32
- print ("There is no line which is less than 50 " )
33
-
34
34
35
35
if __name__ == "__main__" :
36
36
write_to_file (file_name )
Original file line number Diff line number Diff line change 8
8
9
9
def display_words (file_path ):
10
10
11
-
12
11
try :
13
- with open (file_path , 'r' ) as F :
14
- lines = F .read ()
15
- words = lines .split ()
16
- count = 0
17
- for word in words :
18
- if (len (word ) < 4 ):
19
- print (word )
20
- count += 1
21
- return "The total number of the word's count which has less than 4 characters" , (count )
12
+ with open (file_path ) as F :
13
+ words = F .read ().split ()
14
+ words_less_than_40 = list ( filter (lambda word : len (word ) < 4 , words ) )
15
+
16
+ for word in words_less_than_40 :
17
+ print (word )
18
+
19
+ return "The total number of the word's count which has less than 4 characters" , (len (words_less_than_40 ))
22
20
23
21
except FileNotFoundError :
24
22
print ("File not found" )
Original file line number Diff line number Diff line change 1
1
"""Write a function in python to count the number of lowercase
2
2
alphabets present in a text file “happy.txt"""
3
3
4
- import time
5
- import os
4
+ import time , os
5
+ from counter import Counter
6
6
7
7
print ("You will see the count of lowercase, uppercase and total count of alphabets in provided file.." )
8
8
16
16
def lowercase (file_path ):
17
17
try :
18
18
19
- with open (file_path , 'r' ) as F :
20
- # Define the initial count of the lower and upper case.
21
- lowercase_count = 0
22
- uppercase_count = 0
23
-
24
- value = F .read ()
25
-
26
- for i in value :
27
- if i .islower ():
28
- # It will increase the count.
29
- lowercase_count += 1
30
- elif i .isupper ():
31
- uppercase_count += 1
32
-
33
-
34
-
35
- total_count = lowercase_count + uppercase_count
19
+ with open (file_path ) as F :
20
+ word_counter = Counter (F .read ())
36
21
37
- print ("The total number of lower case letters are" , lowercase_count )
38
- time .sleep (1 )
39
- print ("The total number of upper case letters are" , uppercase_count )
40
- time .sleep (1 )
41
- print ("The total number of letters are" , total_count )
42
- time .sleep (1 )
22
+ print (f "The total number of lower case letters are { word_counter . get_total_lower () } " )
23
+ time .sleep (0.5 )
24
+ print (f "The total number of upper case letters are { word_counter . get_total_upper () } " )
25
+ time .sleep (0.5 )
26
+ print (f "The total number of letters are { word_counter . get_total () } " )
27
+ time .sleep (0.5 )
43
28
44
29
except FileNotFoundError :
45
30
print ("File is not exist.. Please check AGAIN" )
You can’t perform that action at this time.
0 commit comments