Python – Index Frequency Alphabet List
Create a String list with each character repeated as much as its position number.
Method #1 : Using ascii_lowercase + ord() + loop
In this, task of getting index is done using ord(), and ascii lowercase() is used for extracting alphabets. Loop is used to perform task for each letter.
Python3
# Python3 code to demonstrate working of # Index Frequency Alphabet List # Using ascii_lowercase + ord() + loop from string import ascii_lowercase # extracting start index strt_idx = ord ( 'a' ) - 1 res = [] for ele in ascii_lowercase: # multiplying Frequency res.append(ele * ( ord (ele) - strt_idx)) # printing result print ( "The constructed list : " + str (res)) |
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time complexity: O(n), where n is the number of elements in the input list ‘test_list’.
Auxiliary space: O(n), as the code creates a new list ‘res’ with the same number of elements as the input list ‘test_list’, so the amount of memory used is proportional to the size of the input.
Method #2 : Using list comprehension + ascii_lowercase + ord()
In this, list comprehension is used to solve this problem. This is shorthand of above method.
Python3
# Python3 code to demonstrate working of # Index Frequency Alphabet List # Using list comprehension + ascii_lowercase + ord() from string import ascii_lowercase # extracting start index strt_idx = ord ( 'a' ) - 1 # list comprehension to solve as one liner res = [ele * ( ord (ele) - strt_idx) for ele in ascii_lowercase] # printing result print ( "The constructed list : " + str (res)) |
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time complexity: O(1) since it only iterates 26 times (once for each lowercase letter of the alphabet).
Auxiliary space: O(1) since it creates a list with 26 elements, one for each lowercase letter of the alphabet.
Method#3 : Using map() +ascii lower + enumerate
In this method we will use map function to iterate over the lower ascii values and use enumerate to access the value with corresponding index of alphabet. With the alphabet and index value generate frequency list.
Python3
# Python3 code to demonstrate working of # Index Frequency Alphabet List # Using enumerate + ascii_lowercase + map() from string import ascii_lowercase # Using map + enumertate to generate resultant list res = map ( lambda i: i[ 1 ] * (i[ 0 ] + 1 ), enumerate (ascii_lowercase)) # printing result print ( "The constructed list is :" , list (res)) |
The constructed list is : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4 : Using for loop
Python3
# Python3 code to demonstrate working of # Index Frequency Alphabet List loweralpha = "abcdefghijklmnopqrstuvwxyz" res = [] h = 1 for i in loweralpha: res.append(i * h) h + = 1 # printing result print ( "The constructed list : " + str (res)) |
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using a dictionary and a for loop
This method creates an empty dictionary and loops through the lowercase alphabet, calculates the frequency value for each letter, and adds the letter and its frequency value to the dictionary. Finally, it gets a list of all frequency values from the dictionary and prints the resulting list.
Python3
from string import ascii_lowercase # create an empty dictionary to store the frequency values for each letter freq_dict = {} # loop through the lowercase alphabet and its corresponding index # enumerate() function returns a tuple of index and corresponding value from an iterable for idx, letter in enumerate (ascii_lowercase): # calculate the frequency value for the current letter freq_value = (idx + 1 ) * letter # add the letter and its frequency value to the dictionary freq_dict[letter] = freq_value # get a list of all frequency values from the dictionary res = list (freq_dict.values()) # print the resulting list print ( "The constructed list : " + str (res)) |
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time Complexity:
- The time complexity of the program is O(N), where N is the number of letters in the lowercase alphabet (i.e., 26).
- This is because the program loops through the alphabet once and performs a constant-time operation for each letter.
Auxiliary Space:
- The auxiliary space used by the program is O(N), where N is the number of letters in the lowercase alphabet (i.e., 26).
- This is because the program creates a dictionary that contains one key-value pair for each letter in the alphabet, and the size of the dictionary is proportional to N.
Additionally, the program creates a list that contains all frequency values, which also has a size proportional to N.
Method #6: Using zip() function and list comprehension.
Creates a list of tuples containing the letters and their corresponding index using a list comprehension and the enumerate() function. It then uses another list comprehension to calculate the frequency value for each letter. Finally, it creates a dictionary from the list of tuples using the zip() function and the dict() constructor. The rest of the code is the same as the original implementation.
Python3
from string import ascii_lowercase # create a list of tuples containing the letters and their corresponding index letter_index_pairs = [(letter, idx) for idx, letter in enumerate (ascii_lowercase)] # calculate the frequency value for each letter using a list comprehension freq_values = [(idx + 1 ) * letter for letter, idx in letter_index_pairs] # create a dictionary from the list of tuples freq_dict = dict ( zip (ascii_lowercase, freq_values)) # get a list of all frequency values from the dictionary res = list (freq_dict.values()) # print the resulting list print ( "The constructed list : " + str (res)) |
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time complexity: O(n), where n is the number of lowercase letters in the alphabet. This is because the code loops through the alphabet once to create the list of tuples and once again to create the list of frequency values.
Auxiliary space: O(n), as the code creates two lists and one dictionary to store the tuples, frequency values, and final results. However, since the size of the input (the lowercase alphabet) is fixed, the space complexity can be considered constant.
Method #7:Using reduce() and a lambda function (requires importing the functools module):
1. Import the reduce function from functools module and the ascii_lowercase string from the string module.
2. Extract the start index by subtracting 1 from the Unicode value of ‘a’.
3. For each alphabet in the ascii_lowercase string, associate it with its position in the string starting from 1 using the enumerate function.
4. Use the reduce function to multiply each alphabet with its corresponding position and concatenate it with the previous elements of the list.
5. Print the final list.
Python3
from functools import reduce from string import ascii_lowercase # Extracting start index strt_idx = ord ( 'a' ) - 1 # Using reduce() to create the list res = reduce ( lambda a, b: a + [b[ 0 ] * b[ 1 ]], enumerate (ascii_lowercase, 1 ), []) # Printing the result print ( "The constructed list : " + str (res)) #This code is contributed by Jyothi Pinjala. |
The constructed list : ['a', 'bb', 'ccc', 'dddd', 'eeeee', 'ffffff', 'ggggggg', 'hhhhhhhh', 'iiiiiiiii', 'jjjjjjjjjj', 'kkkkkkkkkkk', 'llllllllllll', 'mmmmmmmmmmmmm', 'nnnnnnnnnnnnnn', 'ooooooooooooooo', 'pppppppppppppppp', 'qqqqqqqqqqqqqqqqq', 'rrrrrrrrrrrrrrrrrr', 'sssssssssssssssssss', 'tttttttttttttttttttt', 'uuuuuuuuuuuuuuuuuuuuu', 'vvvvvvvvvvvvvvvvvvvvvv', 'wwwwwwwwwwwwwwwwwwwwwww', 'xxxxxxxxxxxxxxxxxxxxxxxx', 'yyyyyyyyyyyyyyyyyyyyyyyyy', 'zzzzzzzzzzzzzzzzzzzzzzzzzz']
Time complexity:
The enumerate() function takes O(n) time, where n is the number of elements in the ascii_lowercase string.
The lambda function passed to reduce() takes O(1) time to execute for each iteration.
The reduce() function takes O(n) time to iterate over the enumerate() function.
Therefore, the overall time complexity of the code is O(n).
Space complexity:
The space required to store the ascii_lowercase string is O(1) as it has a fixed size.
The space required to store the output list res is proportional to the size of the input string, as each alphabet is repeated a number of times equal to its position in the English alphabet.
Therefore, the overall space complexity of the code is O(n).
Please Login to comment...