Skip to content
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions Data_processing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
---
title: Process Student Scores
tags: ['python', 'functions']
---
# Problem Statement
Write a function process_student_scores(students: list) -> dict that processes a list of students' scores and returns a dictionary containing the following information for each subject:

The average score for the subject.
The highest score for the subject.
The lowest score for the subject.

Each student in the input list is represented as a dictionary with the following structure:
{
'name': <string>,
'scores': {
'subject1': <score>,
'subject2': <score>,
...
}
}

The function should return a dictionary with the subject names as keys and another dictionary as the value containing the average, highest, and lowest scores for each subject.
Function Signature:
def process_student_scores(students: list) -> dict:
**Sample Input**
```
students = [
{'name': 'Alice', 'scores': {'Math': 85, 'Science': 90}},
{'name': 'Bob', 'scores': {'Math': 75, 'Science': 80}},
{'name': 'Charlie', 'scores': {'Math': 95, 'Science': 85}}
]
process_student_scores(students)
```
**Sample Output**
```
{
'Math': {'average': 85.0, 'highest': 95, 'lowest': 75},
'Science': {'average': 85.0, 'highest': 90, 'lowest': 80}
}
```
# Solution
```py test.py -r 'python3 test.py'
<prefix>
def process_student_scores(students: list) -> dict:
"""
Processes the list of student scores and returns a dictionary containing:
- The average score for each subject.
- The highest score for each subject.
- The lowest score for each subject.

Arguments:
students: list of dictionaries, where each dictionary contains student name and their scores in subjects.

Returns:
dict: A dictionary where keys are subject names and values are dictionaries with 'average', 'highest', and 'lowest' scores.
"""
</prefix>

# Initialize an empty dictionary to store results
subject_stats = {}

<sol>
# Iterate through each student
for student in students:
# Iterate through each subject score
for subject, score in student['scores'].items():
# If subject is not in the subject_stats dictionary, initialize it
if subject not in subject_stats:
subject_stats[subject] = {'scores': [], 'average': 0, 'highest': float('-inf'), 'lowest': float('inf')}

# Add the score to the list of scores for the subject
subject_stats[subject]['scores'].append(score)

# Update the highest and lowest scores for the subject
if score > subject_stats[subject]['highest']:
subject_stats[subject]['highest'] = score
if score < subject_stats[subject]['lowest']:
subject_stats[subject]['lowest'] = score

# Calculate the average score for each subject
for subject, stats in subject_stats.items():
stats['average'] = sum(stats['scores']) / len(stats['scores'])
# Remove the scores list as it's no longer needed
del stats['scores']
</sol>

return subject_stats

```
# Public Test Cases
## Input 1
```
students = [
{'name': 'Alice', 'scores': {'Math': 85, 'Science': 90}},
{'name': 'Bob', 'scores': {'Math': 75, 'Science': 80}},
{'name': 'Charlie', 'scores': {'Math': 95, 'Science': 85}}
]

process_student_scores(students)
```
## Output 1
```
{
'Math': {'average': 85.0, 'highest': 95, 'lowest': 75},
'Science': {'average': 85.0, 'highest': 90, 'lowest': 80}
}
```
## Input 2
```
students = [
{'name': 'Alice', 'scores': {'Math': 70, 'Science': 60}},
{'name': 'Bob', 'scores': {'Math': 80, 'Science': 70}},
{'name': 'Charlie', 'scores': {'Math': 90, 'Science': 80}}
]

process_student_scores(students)
```
## Output 2
```
{
'Math': {'average': 80.0, 'highest': 90, 'lowest': 70},
'Science': {'average': 70.0, 'highest': 80, 'lowest': 60}
}
```
## Input 3
```
students = [
{'name': 'Alice', 'scores': {'Math': 100}},
{'name': 'Bob', 'scores': {'Math': 95}},
{'name': 'Charlie', 'scores': {'Math': 90}}
]

process_student_scores(students)
```
## Output 3
```
{
'Math': {'average': 95.0, 'highest': 100, 'lowest': 90}
}
```

# Private Test Cases
## Input 1
```
students = [
{'name': 'John', 'scores': {'English': 85, 'History': 75}},
{'name': 'Jane', 'scores': {'English': 90, 'History': 80}},
{'name': 'Alex', 'scores': {'English': 80, 'History': 85}}
]

process_student_scores(students)
```
## Output 1
```
{
'English': {'average': 85.0, 'highest': 90, 'lowest': 80},
'History': {'average': 80.0, 'highest': 85, 'lowest': 75}
}
```
159 changes: 159 additions & 0 deletions data_processing_IO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
---
title: Data Processing I/O
---
# Problem Statement
Write a Python program that processes a list of student grades. The program should:

Take a list of student names and their respective grades in multiple subjects as input.
Compute and output:
The average grade for each student.
The highest grade for each student.
The lowest grade for each student.
**Sample Input**
```
3
Alice 85 90 92
Bob 78 88 82
Charlie 95 87 91
```
**Sample Output**
```
Name: Alice
Average Grade: 89.0
Highest Grade: 92
Lowest Grade: 85

Name: Bob
Average Grade: 82.67
Highest Grade: 88
Lowest Grade: 78

Name: Charlie
Average Grade: 91.0
Highest Grade: 95
Lowest Grade: 87
```
# Solution
```py test.py -r 'python3 test.py'
<template>
def process_studentgrades():
"""
Processes student grades to compute the average, highest, and lowest grades
for each student and outputs the results in a specific format.
"""
# Read the number of students
<sol>
n = int(input())

#Loop over the number of students
for in range(n):
# Read each student's data (name and grades)
data = input().split()

#First part is the name, the rest are the grades
name = data[0]
grades = list(map(int, data[1:]))

#Calculate average, highest, and lowest grades
average = sum(grades) / len(grades)
highest = max(grades)
lowest = min(grades)
</sol>

#Print the results for each student
print(f"Name: {name}")
print(f"Average Grade: {average:.2f}")
print(f"Highest Grade: {highest}")
print(f"Lowest Grade: {lowest}")
print() # Blank line between students

#Call the function to start processing
process_student_grades()
</template>
```
# Public Test Cases
## Input 1
```
3
Alice 85 90 92
Bob 78 88 82
Charlie 95 87 91
```
## Output 1
```
Name: Alice
Average Grade: 89.0
Highest Grade: 92
Lowest Grade: 85

Name: Bob
Average Grade: 82.67
Highest Grade: 88
Lowest Grade: 78

Name: Charlie
Average Grade: 91.0
Highest Grade: 95
Lowest Grade: 87
```
## Input 2
```
2
John 70 80 90 85
Emily 95 88 92 100
```
## Output 2
```
Name: John
Average Grade: 81.25
Highest Grade: 90
Lowest Grade: 70

Name: Emily
Average Grade: 93.75
Highest Grade: 100
Lowest Grade: 88
```
## Input 3
```
1
Zara 50 60 70 80
```
## Output 3
```
Name: Zara
Average Grade: 65.0
Highest Grade: 80
Lowest Grade: 50
```

# Private Test Cases
## Input 1
```
Adam 50 60 70
Eve 60 65 70 75
Sam 100 90 95 85
Lily 95 85 100
```
## Output 1
```
Name: Adam
Average Grade: 60.0
Highest Grade: 70
Lowest Grade: 50

Name: Eve
Average Grade: 70.0
Highest Grade: 75
Lowest Grade: 60

Name: Sam
Average Grade: 92.5
Highest Grade: 100
Lowest Grade: 85

Name: Lily
Average Grade: 93.33
Highest Grade: 100
Lowest Grade: 85
```
68 changes: 68 additions & 0 deletions data_types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
title: Key-Value Transformation in a Dictionary
tags: ['python', 'functions']
---
# Problem Statement
Write a function reverse_dict(d: dict) -> dict that reverses the keys and values of a dictionary. Assume values are unique and hashable.
**Sample Input**
```
{1: 'a', 2: 'b', 3: 'c'}
```
**Sample Output**
```
{'a': 1, 'b': 2, 'c': 3}
```
# Solution
```py test.py -r 'python3 test.py'
<prefix>
def reverse_dict(d: dict) -> dict:
"""
Reverses the keys and values of the given dictionary.

Arguments:
d: dict - a dictionary where the values are unique and hashable.

Returns:
dict - a new dictionary with the keys and values reversed.
"""
</prefix>
<template>
return <sol>{value: key for key, value in d.items()}</sol>
</template>

```
# Public Test Cases
## Input 1
```
{1: 'a', 2: 'b', 3: 'c'}
```
## Output 1
```
{'a': 1, 'b': 2, 'c': 3}
```
## Input 2
```
{'x': 10, 'y': 20, 'z': 30}
```
## Output 2
```
{10: 'x', 20: 'y', 30: 'z'}
```
## Input 3
```
{100: 'apple', 200: 'banana'}
```
## Output 3
```
{'apple': 100, 'banana': 200}
```

# Private Test Cases
## Input 1
```
{'cat': 1, 'dog': 2, 'mouse': 3}
```
## Output 1
```
{1: 'cat', 2: 'dog', 3: 'mouse'}
```
Loading