zeinabezz / DS502-2022-2023

8 stars 8 forks source link

Sheet 3 Solution 👍 #7

Open ahmedelgabalawy opened 1 year ago

ahmedelgabalawy commented 1 year ago

Q-1

file_content = ''
with open("news.txt", mode='r+', encoding='utf-8') as f:
    file_content = f.read()
# replace end line and remove trailing whitespace
file_content = file_content.strip().replace('\n', " ")

# remove '.'
file_content = file_content.replace('.', "")

counter = {}
for w in file_content.split(' '):
    if counter.get(w):
        counter[w] += 1
    else:
        counter[w] = 1

print(counter)

Q-2

vowels = ['a', 'e', 'o', 'i', 'u', 'y']
file_name = input('Enter file name : ') + ".txt"
file_content = ''
counter = {'vowels': 0, 'consonants': 0}
with open(file_name, mode='+r', encoding='utf-8') as f:
    file_content = f.read()

for letter in file_content:
    if letter in vowels:
        counter['vowels'] += 1
    else:
        counter['consonants'] += 1

print(counter)

Q-3

file_name = input('Enter file name : ') + ".txt"
file_content = ''
# do some remove end lines , convert to lower case , remove trailing whitespace
with open(file_name, mode='r+', encoding='utf-8') as f:
    file_content = f.read().replace('\n', ' ').lower().split(' ')

counter = {}
for w in file_content:
    if counter.get(w):
        counter[w] += 1
    else:
        counter[w] = 1

non_duplicated_words = [w for w, c in counter.items() if c == 1]
print(non_duplicated_words)

Q-4

file_name = input('Enter file name : ') + ".txt"
new_file_name = input('Enter the result file name : ') + ".txt"

with open(file_name, mode='r+', encoding='utf-8') as f1, open(new_file_name, mode='w', encoding='utf-8') as f2:
    f2.write(f1.read().replace('\n', ' '))

Q-5

lines = int(input('number of first lines to be read :'))

with open('news.txt', 'r+', encoding='utf-8') as f:
    for l in range(lines):
        print(f.readline().removesuffix('\n'))

Q-6

lines = int(input('number of last lines to be read :'))
result = []
with open('news.txt', 'r+', encoding='utf-8') as f:
    result = f.readlines()

for l in result[len(result) - lines:]:
    print(l.replace('\n', '').strip())

Q-7

file_name = "news.txt"
new_file_name = "new news.txt"
combined = []
with open(file_name, mode='r+', encoding='utf-8') as f1, open(new_file_name, mode='r+', encoding='utf-8') as f2:
    combined = list(zip(f1.readlines(), f2.readlines()))

print(combined)

Q-8

file_name = input('Enter file name : ') + ".txt"
file_content = ''
# do some remove end lines , convert to lower case , remove trailing whitespace
with open(file_name, mode='r+', encoding='utf-8') as f:
    file_content = f.read().replace('\n', '').replace(' ', '').lower()

counter = {}
for w in file_content:
    if counter.get(w):
        counter[w] += 1
    else:
        counter[w] = 1

for k, v in counter.items():
    print(f'{k} : {v}')

Q-9

file_name = input('Enter file name : ') + ".csv"
new_file_name = input('Enter the result file name : ') + ".csv"

with open(file_name, mode='r+', encoding='utf-8') as f1, open(new_file_name, mode='w', encoding='utf-8') as f2:
    f2.write(f1.read())

Q-10

import csv

file_name = 'employees.csv'
dataset = [["emp_name", "sal", "dept"],
           ['a', 100, 'cs'],
           ['b', 200, 'is'],
           ['c', 300, 'cs'],
           ['d', 400, 'ds'],
           ['e', 500, 'cs'],
           ['f', 6]]

with open(file_name, mode='w', newline="", encoding='utf-8') as csvfile:
    csv_writer = csv.writer(csvfile)
    csv_writer.writerows(dataset)

Q-11

import csv
file_name = 'employees.csv'
with open(file_name, mode='r+', newline="", encoding='utf-8') as csvfile:
    csv_reader = csv.reader(csvfile)

    for row in csv_reader:
        if 'cs' in row:
            print(row[0])
zeinabezz commented 1 year ago

Thanks recieved

zeinabezz commented 1 year ago

waiting Sheet 2 untill the end of this week