Answer to Question #295624 in Python for Michael

Question #295624

Let A=[4, 5, 6, 5, 3, 2, 8, 0, 4, 6, 7, 8, 4, 5, 8, 9, 8, 6, 8, 5, 5, 4, 2, 1, 9, 3, 3, 4, 6, 4] be a list. You need to write a python function Create_List(A) to create a new two-dimensional list B=[[ , ], [ , ], ...., [ , ]] such that for an element [x, y] in B, x denote a value of an element in A and y denotes how many times x appeared in list A. The function Create_List (A) will take list A as input and return list B as an output




NOTE:




Do not use the input() function for taking input from the keyboard. Specify the input in fixed form but function must be generalized that can work with values.



you are not allowed to use extra lists other than A and B. Further, no predefined functions are allowed except len(), and append().

1
Expert's answer
2022-02-09T10:40:51-0500
def Create_List(A):
    B = []
    n = len(A)
    sort_list = [0] * n
    for i in range(n):
        sort_list[i] = A[i]
    for i in range(n - 1):
        for j in range(i, n):
            if sort_list[i] > sort_list[j]:
                sort_list[i], sort_list[j] = sort_list[j], sort_list[i]
    i = 0
    while i < n - 1:
        count = 0
        item = sort_list[i]
        while (sort_list[i] == item):
            i += 1
            count += 1
            if i == n:
                break
        B.append([item, count])
    return B

A = [4, 5, 6, 5, 3, 2, 8, 0, 4, 6, 7, 8, 4, 5, 8, 9, 8, 6, 8, 5, 5, 4, 2, 1, 9, 3, 3, 4, 6, 4]

print(Create_List(A))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS