Skip to content

Commit

Permalink
day 7 file handling program
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Oct 30, 2020
1 parent 2cde209 commit a2d0435
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
25 changes: 25 additions & 0 deletions day7_file_handling_debt_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Required concepts: https://www.w3schools.com/python/python_file_handling.asp file handling
import os

print('Question: Find money owed to friends')

def update_file(name, amount):
path = 'tmp/' + name + '.txt'
# get current amount
reader = open(path, 'r')
current_amount = int('0'+reader.read())
# write the new amount
writer = open(path, 'w')
writer.write(str(current_amount + amount))

names = ['raj', 'riya', 'raj']
amounts = [1000, 200, 300]

# # first make empty files for all :)
for name in names:
open('tmp/'+name+'.txt','w')

for name, amount in zip(names, amounts):
update_file(name, amount)

print('Made the files with total debts')
37 changes: 22 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp map, filter, lambda
# Required concepts: https://www.w3schools.com/python/python_file_handling.asp file handling
import os

print('Question: prime numbers in list,')
print('Question: Find money owed to friends')

def check_prime(num):
for i in range(2,num):
if num == 1:
return False
if num%i == 0:
# you found a factor of num!
return False
# you didn't find any multiple, you're prime
return True

li = [2,3,4,5,6,7,8,9]
print(li)
def update_file(name, amount):
path = 'tmp/' + name + '.txt'
# get current amount
reader = open(path, 'r')
current_amount = int('0'+reader.read())
# write the new amount
writer = open(path, 'w')
writer.write(str(current_amount + amount))

primes = filter(check_prime,li)

print(list(primes))
names = ['raj', 'riya', 'raj']
amounts = [1000, 200, 300]

# # first make empty files for all :)
for name in names:
open('tmp/'+name+'.txt','w')

for name, amount in zip(names, amounts):
update_file(name, amount)

print('Made the files with total debts')
1 change: 1 addition & 0 deletions tmp/raj.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1300
1 change: 1 addition & 0 deletions tmp/riya.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
200

0 comments on commit a2d0435

Please sign in to comment.