Skip to content

Commit ff01be8

Browse files
committed
Assignment 5
1 parent 08b4139 commit ff01be8

21 files changed

+469
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Summary
2+
This code snippet allows the user to input a key and a value, and then adds the key-value pair to a dictionary. Finally, it prints the updated dictionary.
3+
4+
## Example Usage
5+
```python
6+
Enter key: 2
7+
Enter value: 30
8+
{0: 10, 1: 20, 2: 30}
9+
```
10+
11+
## Code Analysis
12+
### Inputs
13+
- `key`: an integer representing the key to be added to the dictionary.
14+
- `value`: an integer representing the value to be associated with the key in the dictionary.
15+
___
16+
### Flow
17+
1. Prompt the user to enter a key.
18+
2. Prompt the user to enter a value.
19+
3. Add the key-value pair to the dictionary.
20+
4. Print the updated dictionary.
21+
___
22+
### Outputs
23+
- `d`: the updated dictionary with the new key-value pair added.
24+
___
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
d = {0: 10, 1: 20}
2+
key = int(input("Enter key: "))
3+
value = int(input("Enter value: "))
4+
d[key] = value
5+
print(d)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Summary
2+
This code snippet checks if a given key exists in a dictionary.
3+
4+
## Example Usage
5+
```python
6+
Enter a key: 'a'
7+
Key already exists.
8+
```
9+
10+
## Code Analysis
11+
### Inputs
12+
- `key`: a string representing the key to be checked in the dictionary.
13+
___
14+
### Flow
15+
1. Prompt the user to enter a key.
16+
2. Check if the entered key exists in the dictionary.
17+
3. If the key exists, print 'Key already exists.'
18+
4. If the key does not exist, print 'Key does not exist.'
19+
___
20+
### Outputs
21+
- 'Key already exists.' if the entered key exists in the dictionary.
22+
- 'Key does not exist.' if the entered key does not exist in the dictionary.
23+
___
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
d = {'a': 1, 'b': 2, 'c': 3}
2+
key = input("Enter a key: ")
3+
if key in d:
4+
print('Key already exists.')
5+
else:
6+
print('Key does not exist.')
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## Summary
2+
The code snippet is a function called `combine_dicts_to_list_values` that takes in multiple dictionaries as arguments and combines them into a single dictionary where the values for each key are stored in a list.
3+
4+
## Example Usage
5+
```python
6+
dict1 = {"w": 50, "x": 100, "y": "Green", "z": 400}
7+
dict2 = {"x": 300, "y": "Red", "z": 600}
8+
combine_dicts_to_list_values(dict1, dict2)
9+
```
10+
Expected Output:
11+
```
12+
Original dictionaries:
13+
{'w': 50, 'x': 100, 'y': 'Green', 'z': 400}
14+
{'x': 300, 'y': 'Red', 'z': 600}
15+
16+
Combined dictionaries, creating a list of values for each key:
17+
{'w': [50], 'x': [100, 300], 'y': ['Green', 'Red'], 'z': [400, 600]}
18+
```
19+
20+
## Code Analysis
21+
### Inputs
22+
- `dict1`: A dictionary containing key-value pairs.
23+
- `*dict_list`: Variable number of dictionaries.
24+
___
25+
### Flow
26+
1. The function takes in a dictionary `dict1` and a variable number of dictionaries `*dict_list`.
27+
2. It prints the original dictionaries.
28+
3. It initializes an empty dictionary `combined_dict`.
29+
4. It iterates over each dictionary in `[dict1, *dict_list]`.
30+
5. For each key-value pair in the dictionary, it checks if the key already exists in `combined_dict`.
31+
6. If the key does not exist, it creates a new list with the value and assigns it to the key in `combined_dict`.
32+
7. If the key already exists, it appends the value to the existing list for that key in `combined_dict`.
33+
8. Finally, it prints the combined dictionary.
34+
___
35+
### Outputs
36+
- `combined_dict`: A dictionary where the values for each key are stored in a list.
37+
___
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def combine_dicts_to_list_values(dict1, *dict_list):
2+
print("Original dictionaries:\n")
3+
for dictionary in [dict1, *dict_list]:
4+
print(dictionary)
5+
print("\nCombined dictionaries, creating a list of values for each key:")
6+
combined_dict = {}
7+
for dictionary in [dict1, *dict_list]:
8+
for key, value in dictionary.items():
9+
if key not in combined_dict:
10+
combined_dict[key] = [value] # Create a new list for the key if it doesn't exist
11+
else:
12+
combined_dict[key].append(value) # Append the value to the existing list for the key
13+
print(combined_dict)
14+
15+
16+
if __name__ == "__main__":
17+
dict1 = {"w": 50, "x": 100, "y": "Green", "z": 400}
18+
dict2 = {"x": 300, "y": "Red", "z": 600}
19+
combine_dicts_to_list_values(dict1, dict2)
20+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def get_user_input():
2+
dict1 = {}
3+
dict2 = {}
4+
5+
# Get user input for dict1
6+
dict1_input = input("Enter key-value pairs for dict1 (separated by commas): ")
7+
dict1_pairs = dict1_input.split(',')
8+
for pair in dict1_pairs:
9+
key, value = pair.split(':')
10+
dict1[key.strip()] = int(value.strip())
11+
12+
# Get user input for dict2
13+
dict2_input = input("Enter key-value pairs for dict2 (separated by commas): ")
14+
dict2_pairs = dict2_input.split(',')
15+
for pair in dict2_pairs:
16+
key, value = pair.split(':')
17+
dict2[key.strip()] = int(value.strip())
18+
19+
return dict1, dict2
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Summary
2+
This code snippet prompts the user to enter the number of keys for a dictionary and then asks for the key-value pairs. It creates a dictionary with the entered key-value pairs and prints the resulting dictionary.
3+
4+
## Example Usage
5+
```python
6+
Enter the number of keys: 3
7+
Enter the key: 1
8+
Enter the value: 10
9+
Enter the key: 2
10+
Enter the value: 20
11+
Enter the key: 3
12+
Enter the value: 30
13+
{1: 10, 2: 20, 3: 30}
14+
```
15+
16+
## Code Analysis
17+
### Inputs
18+
- `num_keys`: an integer representing the number of keys for the dictionary
19+
- `key`: an integer representing the key for each key-value pair
20+
- `value`: an integer representing the value for each key-value pair
21+
___
22+
### Flow
23+
1. The code prompts the user to enter the number of keys for the dictionary.
24+
2. It then iterates over the range of keys.
25+
3. For each key, it prompts the user to enter the key and value.
26+
4. It adds the key-value pair to the `new_dict` dictionary.
27+
5. Finally, it prints the resulting dictionary.
28+
___
29+
### Outputs
30+
- `new_dict`: a dictionary containing the entered key-value pairs.
31+
___
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
new_dict = {}
2+
3+
# Prompt the user for input
4+
num_keys = int(input("Enter the number of keys: "))
5+
6+
# Iterate over the range of keys
7+
for i in range(num_keys):
8+
key = int(input("Enter the key: "))
9+
value = int(input("Enter the value: "))
10+
new_dict[key] = value
11+
12+
print(new_dict)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## Summary
2+
The code snippet is a function called `filter_data` that takes a dictionary of students' names as keys and tuples of their height and weight as values. It filters the students based on the condition that their height is greater than or equal to 6.0 and their weight is greater than or equal to 70. The function returns a new dictionary with the filtered students.
3+
4+
## Example Usage
5+
```python
6+
Original Dictionary:
7+
{
8+
'Cierra Vega': (6.2, 70),
9+
'Alden Cantrell': (5.9, 65),
10+
'Kierra Gentry': (6.0, 68),
11+
'Pierre Cox': (5.8, 66)
12+
}
13+
14+
Height > 6ft and Weight > 70kg:
15+
{
16+
'Cierra Vega': (6.2, 70)
17+
}
18+
```
19+
20+
## Code Analysis
21+
### Inputs
22+
- `students`: A dictionary where the keys are the names of the students and the values are tuples representing their height and weight.
23+
___
24+
### Flow
25+
1. The function `filter_data` takes the `students` dictionary as input.
26+
2. It uses a dictionary comprehension to iterate over the items in the `students` dictionary.
27+
3. For each key-value pair, it checks if the height (first element of the tuple) is greater than or equal to 6.0 and the weight (second element of the tuple) is greater than or equal to 70.
28+
4. If the condition is true, it adds the key-value pair to a new dictionary called `result`.
29+
5. Finally, it returns the `result` dictionary.
30+
___
31+
### Outputs
32+
- `result`: A dictionary containing the filtered students where the height is greater than or equal to 6.0 and the weight is greater than or equal to 70.
33+
___
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def filter_data(students):
2+
result = {k: s for k, s in students.items() if s[0] >= 6.0 and s[1] >= 70}
3+
return result
4+
5+
students = {
6+
'Cierra Vega': (6.2, 70),
7+
'Alden Cantrell': (5.9, 65),
8+
'Kierra Gentry': (6.0, 68),
9+
'Pierre Cox': (5.8, 66)
10+
}
11+
12+
print("Original Dictionary:")
13+
print(students)
14+
15+
print("\nHeight > 6ft and Weight > 70kg:")
16+
print(filter_data(students))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Summary
2+
The code snippet creates a dictionary called 'squares' that contains the squares of numbers from 1 to 15. It then prints the dictionary.
3+
4+
## Example Usage
5+
```python
6+
squares = {}
7+
for i in range(1, 16):
8+
squares[i] = i ** 2
9+
print(squares)
10+
```
11+
Expected Output:
12+
```
13+
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}
14+
```
15+
16+
## Code Analysis
17+
### Inputs
18+
- None
19+
___
20+
### Flow
21+
1. Initialize an empty dictionary called 'squares'.
22+
2. Iterate over the range from 1 to 15 (excluding 16).
23+
3. For each number in the range, calculate its square and assign it as the value in the 'squares' dictionary with the number as the key.
24+
4. Print the 'squares' dictionary.
25+
___
26+
### Outputs
27+
- The 'squares' dictionary containing the squares of numbers from 1 to 15.
28+
___
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
squares = {}
2+
for i in range(1, 16):
3+
squares[i] = i ** 2
4+
print(squares)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## Summary
2+
This code snippet defines a function called `group_pairs_into_dict_of_lists` that takes a list of key-value pairs and groups them into a dictionary of lists based on the keys. It then prompts the user to enter the number of pairs and the key-value pairs, and prints the original list and the grouped dictionary.
3+
4+
## Example Usage
5+
```python
6+
Enter the number of pairs: 5
7+
Enter the key: yellow
8+
Enter the value: 1
9+
Enter the key: blue
10+
Enter the value: 2
11+
Enter the key: yellow
12+
Enter the value: 3
13+
Enter the key: blue
14+
Enter the value: 4
15+
Enter the key: red
16+
Enter the value: 1
17+
Original list:
18+
[('yellow', '1'), ('blue', '2'), ('yellow', '3'), ('blue', '4'), ('red', '1')]
19+
20+
Grouping a sequence of key-value pairs into a dictionary of lists:
21+
{'yellow': ['1', '3'], 'blue': ['2', '4'], 'red': ['1']}
22+
```
23+
24+
## Code Analysis
25+
### Inputs
26+
- `pairs`: A list of key-value pairs.
27+
___
28+
### Flow
29+
1. The function `group_pairs_into_dict_of_lists` is defined.
30+
2. The function initializes an empty dictionary called `result`.
31+
3. The function iterates over each key-value pair in the `pairs` list.
32+
4. For each pair, the function checks if the key already exists in the `result` dictionary. If not, it adds the key to the dictionary with an empty list as the value.
33+
5. The function then appends the value to the list associated with the key in the `result` dictionary.
34+
6. After iterating through all the pairs, the function returns the `result` dictionary.
35+
7. The code prompts the user to enter the number of pairs.
36+
8. The code then prompts the user to enter the key and value for each pair and appends them to the `pairs` list.
37+
9. The code prints the original list of pairs.
38+
10. The code calls the `group_pairs_into_dict_of_lists` function with the `pairs` list and prints the resulting grouped dictionary.
39+
___
40+
### Outputs
41+
- The original list of key-value pairs.
42+
- The grouped dictionary where the keys are unique and the values are lists of corresponding values.
43+
___
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def group_pairs_into_dict_of_lists(pairs):
2+
result = {}
3+
for key, value in pairs:
4+
result.setdefault(key, []).append(value)
5+
return result
6+
7+
pairs = []
8+
9+
num_pairs = int(input("Enter the number of pairs: "))
10+
11+
for i in range(num_pairs):
12+
key = input("Enter the key: ")
13+
value = input("Enter the value: ")
14+
pairs.append((key, value))
15+
16+
print("Original list:")
17+
print(pairs)
18+
19+
print("\nGrouping a sequence of key-value pairs into a dictionary of lists:")
20+
print(group_pairs_into_dict_of_lists(pairs))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## Summary
2+
The code snippet creates a dictionary called `student_data` with information about students. It then iterates over the items in the dictionary and checks if the current value is already present in the `result` dictionary. If not, it adds the key-value pair to the `result` dictionary. Finally, it prints the `result` dictionary.
3+
4+
## Example Usage
5+
```python
6+
student_data = {
7+
'id1': {
8+
'name': ['Sara'],
9+
'class': ['V'],
10+
'subject_integration': ['english, math, science']
11+
},
12+
'id2': {
13+
'name': ['David'],
14+
'class': ['V'],
15+
'subject_integration': ['english, math, science']
16+
},
17+
'id3': {
18+
'name': ['Sara'],
19+
'class': ['V'],
20+
'subject_integration': ['english, math, science']
21+
},
22+
'id4': {
23+
'name': ['Surya'],
24+
'class': ['V'],
25+
'subject_integration': ['english, math, science']
26+
}
27+
}
28+
29+
result = {}
30+
for key, value in student_data.items():
31+
if value not in result.values():
32+
result[key] = value
33+
print(result)
34+
```
35+
36+
## Code Analysis
37+
### Inputs
38+
- `student_data`: a dictionary containing information about students. Each key represents a unique student ID, and each value is a dictionary containing the student's name, class, and subject integration.
39+
___
40+
### Flow
41+
1. Create an empty dictionary called `result`.
42+
2. Iterate over the items in the `student_data` dictionary.
43+
3. For each item, check if the value is already present in the `result` dictionary.
44+
4. If the value is not present, add the key-value pair to the `result` dictionary.
45+
5. Print the `result` dictionary.
46+
___
47+
### Outputs
48+
- `result`: a dictionary containing unique student data, where each key represents a unique student ID and each value is a dictionary containing the student's name, class, and subject integration.
49+
___

0 commit comments

Comments
 (0)