Skip to content

fix code #2817

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -3,13 +3,13 @@

def bdelete():
# Opening a file & loading it
with open("studrec.dat","rb") as F:
with open("studrec.dat", "rb") as F:
stud = pickle.load(F)
print(stud)

# Deleting the Roll no. entered by user
rno = int(input("Enter the Roll no. to be deleted: "))
with open("studrec.dat","wb") as F:
with open("studrec.dat", "wb") as F:
rec = [i for i in stud if i[0] != rno]
pickle.dump(rec, F)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@


def binary_read():
with open("studrec.dat","rb") as b:
with open("studrec.dat", "rb") as b:
stud = pickle.load(b)
print(stud)

# prints the whole record in nested list format
print("contents of binary file")

for ch in stud:

print(ch) # prints one of the chosen rec in list

rno = ch[0]
Expand Down
3 changes: 1 addition & 2 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,12 +4,11 @@


def update():

with open("studrec.dat", "rb+") as File:
value = pickle.load(File)
found = False
roll = int(input("Enter the roll number of the record"))

for i in value:
if roll == i[0]:
print(f"current name {i[1]}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
Write a function remcount( ) to count the number of students who need
remedial class (student who scored less than 40 percent)

"""

"""
# also find no. of children who got top marks

import pickle
Expand Down
17 changes: 8 additions & 9 deletions 1 File handle/File handle text/counter.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""
Class resposible for counting words for different files:
- Reduce redundant code
- Easier code management/debugging
- Code readability
Class resposible for counting words for different files:
- Reduce redundant code
- Easier code management/debugging
- Code readability
"""

class Counter:

def __init__(self, text:str) -> None:
class Counter:
def __init__(self, text: str) -> None:
self.text = text

# Define the initial count of the lower and upper case.
Expand All @@ -16,20 +16,19 @@ def __init__(self, text:str) -> None:
self.count()

def count(self) -> None:

for char in self.text:
if char.lower():
self.count_lower += 1
elif char.upper():
self.count_upper += 1

return (self.count_lower, self.count_upper)

def get_total_lower(self) -> int:
return self.count_lower

def get_total_upper(self) -> int:
return self.count_upper

def get_total(self) -> int:
return self.count_lower + self.count_upper
return self.count_lower + self.count_upper
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@

import os
import time
file_name= input("Enter the file name to create:- ")

file_name = input("Enter the file name to create:- ")

print(file_name)

def write_to_file(file_name):

def write_to_file(file_name):
if os.path.exists(file_name):
print(f"Error: {file_name} already exists.")
return

with open(file_name, "a") as F:

while True:
text = input("enter any text to add in the file:- ")
F.write( f"{text}\n" )
F.write(f"{text}\n")
choice = input("Do you want to enter more, y/n").lower()
if choice == "n":
break

def longlines():

with open(file_name, encoding='utf-8') as F:

def longlines():
with open(file_name, encoding="utf-8") as F:
lines = F.readlines()
lines_less_than_50 = list( filter(lambda line: len(line) < 50, lines ) )
lines_less_than_50 = list(filter(lambda line: len(line) < 50, lines))

if not lines_less_than_50:
print("There is no line which is less than 50")
else:
for i in lines_less_than_50:
print(i, end="\t")


if __name__ == "__main__":
write_to_file(file_name)
time.sleep(1)
longlines()
longlines()
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
sys.stdout.write("Enter the name of the file")
file = sys.stdin.readline()

with open(file.strip(), ) as F:

with open(
file.strip(),
) as F:
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: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
print(i, end="")
else:
sys.stderr.write("End of file reached")
break

25 changes: 11 additions & 14 deletions 1 File handle/File handle text/question 2.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
""" Write a method/function DISPLAYWORDS() in python to read lines
"""Write a method/function DISPLAYWORDS() in python to read lines
from a text file STORY.TXT,
using read function
and display those words, which are less than 4 characters. """
and display those words, which are less than 4 characters."""

print("Hey!! You can print the word which are less then 4 characters")

print("Hey!! You can print the word which are less then 4 characters")

def display_words(file_path):

try:
with open(file_path) as F:
words = F.read().split()
words_less_than_40 = list( filter(lambda word: len(word) < 4, words) )
words_less_than_40 = list(filter(lambda word: len(word) < 4, words))

for word in words_less_than_40:
print(word)

return "The total number of the word's count which has less than 4 characters", (len(words_less_than_40))


return (
"The total number of the word's count which has less than 4 characters",
(len(words_less_than_40)),
)

except FileNotFoundError:
print("File not found")


print("Just need to pass the path of your file..")

file_path = input("Please, Enter file path: ")

if __name__ == "__main__":

print(display_words(file_path))





20 changes: 11 additions & 9 deletions 1 File handle/File handle text/question 5.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@
import os
from counter import Counter

print("You will see the count of lowercase, uppercase and total count of alphabets in provided file..")
print(
"You will see the count of lowercase, uppercase and total count of alphabets in provided file.."
)


file_path = input("Please, Enter file path: ")

if os.path.exists(file_path):
print('The file exists and this is the path:\n',file_path)
print("The file exists and this is the path:\n", file_path)


def lowercase(file_path):
try:

with open(file_path) as F:
word_counter = Counter(F.read())

print(f"The total number of lower case letters are {word_counter.get_total_lower()}")

print(
f"The total number of lower case letters are {word_counter.get_total_lower()}"
)
time.sleep(0.5)
print(f"The total number of upper case letters are {word_counter.get_total_upper()}")
print(
f"The total number of upper case letters are {word_counter.get_total_upper()}"
)
time.sleep(0.5)
print(f"The total number of letters are {word_counter.get_total()}")
time.sleep(0.5)
Expand All @@ -31,8 +36,5 @@ def lowercase(file_path):
print("File is not exist.. Please check AGAIN")




if __name__ == "__main__":

lowercase(file_path)
13 changes: 9 additions & 4 deletions 1 File handle/File handle text/question 6.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@

from counter import Counter

def lowercase():

def lowercase():
with open("happy.txt") as F:
word_counter = Counter(F.read())

print(f"The total number of lower case letters are {word_counter.get_total_lower()}")
print(f"The total number of upper case letters are {word_counter.get_total_upper()}")

print(
f"The total number of lower case letters are {word_counter.get_total_lower()}"
)
print(
f"The total number of upper case letters are {word_counter.get_total_upper()}"
)
print(f"The total number of letters are {word_counter.get_total()}")


if __name__ == "__main__":
lowercase()
17 changes: 9 additions & 8 deletions 1 File handle/File handle text/question3.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,27 @@

import os
import time
file_name= input("Enter the file name to create:- ")

file_name = input("Enter the file name to create:- ")

# step1:
print(file_name)



def write_to_file(file_name):

if os.path.exists(file_name):
print(f"Error: {file_name} already exists.")

else:
with open(file_name, "a") as F:

while True:
text = input("enter any text")
F.write(f"{text}\n")
F.write(f"{text}\n")

if input("do you want to enter more, y/n").lower() == "n":
break



# step2:
def check_first_letter():
with open(file_name) as F:
Expand All @@ -37,10 +36,12 @@ def check_first_letter():
count_i = first_letters.count("i")
count_m = first_letters.count("m")

print(f"The total number of sentences starting with I or M are {count_i + count_m}")
print(
f"The total number of sentences starting with I or M are {count_i + count_m}"
)


if __name__ == "__main__":

write_to_file(file_name)
time.sleep(1)
check_first_letter()
24 changes: 11 additions & 13 deletions 8_puzzle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from queue import PriorityQueue


class PuzzleState:
def __init__(self, board, goal, moves=0, previous=None):
self.board = board
Expand Down Expand Up @@ -35,10 +36,13 @@ def neighbors(self):
if 0 <= nx < 3 and 0 <= ny < 3:
new_board = [row[:] for row in self.board]
new_board[x][y], new_board[nx][ny] = new_board[nx][ny], new_board[x][y]
neighbors.append(PuzzleState(new_board, self.goal, self.moves + 1, self))
neighbors.append(
PuzzleState(new_board, self.goal, self.moves + 1, self)
)

return neighbors


def solve_puzzle(initial_board, goal_board):
initial_state = PuzzleState(initial_board, goal_board)
frontier = PriorityQueue()
Expand All @@ -59,6 +63,7 @@ def solve_puzzle(initial_board, goal_board):

return None


def print_solution(solution):
steps = []
while solution:
Expand All @@ -68,21 +73,14 @@ def print_solution(solution):

for step in steps:
for row in step:
print(' '.join(map(str, row)))
print(" ".join(map(str, row)))
print()


# Example usage
initial_board = [
[1, 2, 3],
[4, 0, 5],
[7, 8, 6]
]

goal_board = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 0]
]
initial_board = [[1, 2, 3], [4, 0, 5], [7, 8, 6]]

goal_board = [[1, 2, 3], [4, 5, 6], [7, 8, 0]]

solution = solve_puzzle(initial_board, goal_board)
if solution:
Expand Down
Loading
Loading