Interview Question
Country: United States
def get_average_marks():
with open('my_file.txt', 'r') as f:
file_content = f.read()
lines = file_content.split('\n')
students = {}
lowest_id = 0
for line in lines:
row = line.split(",")
row = [x.strip() for x in row]
id = int(row[0])
if lowest_id == 0 or id < lowest_id:
lowest_id = id
marks = int(row[2])
if id not in students:
students[id] = { "marks": 0, "count": 0 }
students[id]['marks'] += marks
students[id]['count'] += 1
return str(round(students[lowest_id]['marks'] / students[lowest_id]['count']))
print(get_average_marks())
def get_average_marks():
with open('my_file.txt', 'r') as f:
file_content = f.read()
lines = file_content.split('\n')
students = {}
lowest_id = 0
for line in lines:
row = line.split(",")
row = [x.strip() for x in row]
id = int(row[0])
if lowest_id == 0 or id < lowest_id:
lowest_id = id
marks = int(row[2])
if id not in students:
students[id] = { "marks": 0, "count": 0 }
students[id]['marks'] += marks
students[id]['count'] += 1
return str(round(students[lowest_id]['marks'] / students[lowest_id]['count']))
print(get_average_marks())
def get_average_marks():
with open('my_file.txt', 'r') as f:
file_content = f.read()
lines = file_content.split('\n')
students = {}
lowest_id = 0
for line in lines:
row = line.split(",")
row = [x.strip() for x in row]
id = int(row[0])
if lowest_id == 0 or id < lowest_id:
lowest_id = id
marks = int(row[2])
if id not in students:
students[id] = { "marks": 0, "count": 0 }
students[id]['marks'] += marks
students[id]['count'] += 1
return str(round(students[lowest_id]['marks'] / students[lowest_id]['count']))
print(get_average_marks())
let getMarkOfLowerId2 = function(data) {
let map = new Map();
let minId = Infinity;
while (data.length > 0) {
let [ id, className, mark ] = data.pop().split(',');
minId = id < minId ? id : minId;
if (!map.has(id)) {
map.set(id, { total: 0, count: 0 });
}
let tempo = map.get(id);
tempo.total += +mark;
tempo.count++;
}
let smallStudent = map.get(minId);
return Math.floor(smallStudent.total / smallStudent.count);
};
It has enough hint - and you can easily convert it to Python.
- NoOne May 28, 2021