Python - Construct Grades List
Last Updated :
22 Apr, 2023
Given a number, construct a list having all the possible Grades combination for the first N characters.
Input : num = 3
Output : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-']
Explanation : All grades till C rendered in list.
Input : num = 5
Output : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-', 'E+', 'E', 'E-']
Explanation : 5 corresponds to E, hence all combinations.
Method #1 : Using list comprehension + ord()
The combination of the above functions can be used to solve this problem. In this, we perform the task of incrementing and extracting ASCII characters using ord() and list comprehension is used in character list creation.
Python3
# Python3 code to demonstrate working of
# Construct Grades List
# Using list comprehension + ord()
# initializing N
num = 4
# Using list comprehension + ord()
# each character paired to symbols and character incremented using idx
# conversion by chr + ord
res = [chr(ord('A') + idx) + sym for idx in range(num)
for sym in ['+', '', '-']]
# printing result
print("Grades List : " + str(res))
Output : Grades List : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-']
Time complexity:O(n^2), where n is the value of "num".
Auxiliary space: O(n^2), as well.
Method #2 : Using join() + map() + product() + ascii_uppercase
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting ascii characters using ascii_uppercase, product() and map(), is used to perform linkage with symbols, the result is created after perform join of all.
Python3
# Python3 code to demonstrate working of
# Construct Grades List
# Using join() + map() + product() + ascii_uppercase
from string import ascii_uppercase
from itertools import product
# initializing N
num = 4
# Using join() + map() + product() + ascii_uppercase
# pairing using product, map used to join characters with symbols.
res = [*map(''.join, product(ascii_uppercase[:num], ['+', '', '-']))]
# printing result
print("Grades List : " + str(res))
OutputGrades List : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-', 'D+', 'D', 'D-']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #3: Using loop and list methods
Python3
# Python3 code to demonstrate working of
# Construct Grades List
# initializing N
num = 3
x = ["+", "", "-"]
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res = []
for i in range(0, num):
b = []
for j in x:
s = alphabets[i]+j
b.append(s)
res.extend(b)
# printing result
print("Grades List : " + str(res))
OutputGrades List : ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-']
Time Complexity: O(N^2), where N is the value of the variable num. The nested for loops iterate N times each.
Auxiliary Space: O(N), as we are creating a list of length N, which is the variable res.
Method #4: Using itertools.product()
Step-by-step approach:
Import the itertools module to access the product() function.
Define the range of values for the first letter of the grade as a string.
Define the range of values for the second letter of the grade as a list.
Use the product() function to create a Cartesian product of the two ranges.
Flatten the list of tuples obtained from the Cartesian product to create the final grades list.
Python3
import itertools
num = 3
x = ["+", "", "-"]
alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
grades = list(itertools.product(alphabets[:num], x))
res = [''.join(grade) for grade in grades]
print("Grades List: " + str(res))
OutputGrades List: ['A+', 'A', 'A-', 'B+', 'B', 'B-', 'C+', 'C', 'C-']
Time complexity: O(NM), where N is the value of num and M is the length of the x list.
Auxiliary space: O(NM), as we are creating a list of tuples of size NxM and then flattening it to create the final list.
Similar Reads
Program to create grade calculator in Python Given different scored marks of students. We need to find a Grade Calculator in Python. The test score is an average of the respective marks scored in assignments, tests, and lab work. The final test score is assigned using the below formula. 10% of marks scored from submission of Assignments 70% of
4 min read
Create Dictionary from the List-Python The task of creating a dictionary from a list in Python involves mapping each element to a uniquely generated key, enabling structured data storage and quick lookups. For example, given a = ["gfg", "is", "best"] and prefix k = "def_key_", the goal is to generate {'def_key_gfg': 'gfg', 'def_key_is':
3 min read
Output of Python programs | Set 8 Prerequisite - Lists in Python Predict the output of the following Python programs. Program 1 Python list = [1, 2, 3, None, (1, 2, 3, 4, 5), ['Geeks', 'for', 'Geeks']] print len(list) Output: 6Explanation: The beauty of python list datatype is that within a list, a programmer can nest another list,
3 min read
Extract Dictionary Values as a Python List To extract dictionary values from a list, we iterate through each dictionary, check for the key's presence, and collect its value. The result is a list of values corresponding to the specified key across all dictionaries.For example, given data = {'a': 1, 'b': 2, 'c': 3}, the output will be [1, 2, 3
3 min read
Python - Ways to format elements of given list Formatting elements of a list is a common requirement especially when displaying or preparing data for further processing. Python provides multiple methods to format list elements concisely and efficiently. List comprehension combined with Python f-strings provides a simple and efficient way to form
2 min read