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)) |
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)) |
Grades 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)) |
Grades 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)) |
Grades 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.