Skip to content

Commit

Permalink
Day 12 object constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
yozaam committed Nov 4, 2020
1 parent 9a2844e commit f663a5a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
13 changes: 12 additions & 1 deletion aptitude_quant.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,15 @@ Q. a,b fill in 20,30 mins c can drain full tank in x mins
sirs method:
`a = 15/20 = 75% `
`b = 15/30 = 50% `
`overflow = 25% in 15 mins, 100% in 15x4=60 mins :)`
`overflow = 25% in 15 mins, 100% in 15x4=60 mins :)`

## Day 5, Time Speed Distance

`ST = D`

Case: T is constant: `S is directly proportional to D`

Case: S is constant: `T is directly proportional to D`

Case D is constant: `S is inversely proportional to T`

19 changes: 19 additions & 0 deletions day12_list_of_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Required concepts: https://www.w3schools.com/python/python_intro.asp OOPS & list comprehension

print('Question: Make a list of objects with different properties')

class Bird:
def __init__(self,name,color):
# this constructor creates a Bird with name
self.name = name
self.color = color

all_names = ['parrot', 'ostrich']
all_colors = ['green', 'blue']
# n: no. of birds
n = len(all_names)

all_birds = [Bird(all_names[i],all_colors[i]) for i in range(n)]

for bird in all_birds:
print(bird.name, bird.color)
32 changes: 10 additions & 22 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
# Required concepts: https://www.w3schools.com/python/python_file_handling.asp file handling
import os
# Required concepts: https://www.w3schools.com/python/python_intro.asp OOPS & list comprehension

print('Question: Find money owed to friends')
print('Question: Make a list of objects with different names')

class Bird:
def __init__(self,name):
# this constructor creates a Bird with name
self.name = name

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))
all_names = ['parrot', 'ostrich']

all_birds = [Bird(name) for name in all_names]

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')
for bird in all_birds:
print(bird.name)

0 comments on commit f663a5a

Please sign in to comment.