Question

IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList).

For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3, 2]) should return the list [ ["d", "c", "a", "b"], [2, 3, 5, 8] ] Notice that both the characters and the frequencies have been rearranged.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def sortCharacters(lst1,lst2):
    k = 1
    needNextPass = True
    while k < len(lst2) and needNextPass:
        # List may be sorted and next pass not needed
        needNextPass = False
        for i in range(len(lst2) - k):
            if lst2[i] > lst2[i + 1]:
                # swap list[i] with list[i + 1]
                temp = lst2[i]
                lst2[i] = lst2[i + 1]
                lst2[i + 1] = temp
                temp = lst1[i]
                lst1[i] = lst1[i + 1]
                lst1[i + 1] = temp
                needNextPass = True  # Next pass still needed
    return [lst1,lst2]

# Testing
print(sortCharacters(["a", "b", "c", "d"], [5, 8, 3, 2]))

Output

[ [d, c, a, b], [2, 3, 5, 8]] Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a function that takes two lists that are in ascending order as arguments. The function...

    Write a function that takes two lists that are in ascending order as arguments. The function should return a new list that has all the values from the two argument lists and is also in ascending order (e.g., [1,3,5] and [2,4,6] become [1,2,3,4,5,6]). Please use python

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • Using Python: write a function that takes two arguments: (1) a target item to find, and...

    Using Python: write a function that takes two arguments: (1) a target item to find, and (2) a list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and...

  • In python: ScoreFinder is a function that takes in two lists and a string. The first...

    In python: ScoreFinder is a function that takes in two lists and a string. The first list is a list of strings (player names), the second list is a list of floats (player scores), and the string is a name (player to find). If the player to find exists in the list of player names, then print the name of that player along with their associated score (which is in the second list at the same index). If the player...

  • Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount,...

    Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount, an integer specifying the number of “buckets” to return and numberLs, a list of numbers ) buckets should split the range of values in numberLs into bucketCount sub-ranges. Specifically buckets should return a list-of-lists of length bucketCount. Each list in the returned list-of-lists represents a “bucket”. Each bucket should contain the numbers from numberLs whose values are greater than or equal to min(numberLs) +...

  • In Python beginner code: Write a function named even_div, that takes two arguments (you can safely...

    In Python beginner code: Write a function named even_div, that takes two arguments (you can safely assume that both arguments will always be non-zero integers). When this function is called, it should return how many times the first argument can be divided by the second argument, if it is evenly divisible (no remainder). If it is not evenly divisible, the function should return 0. Examples: even_div(5, 2) will return 0 (5 divided by 2 is 2.5, not evenly divisible) even_div(10,...

  • PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments:...

    PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments: a list, and a number n. The program should generate 20 random numbers, in the range of 1 through 100, and assign each number to the list. The program should also ask the user to pick a number from 1 through a 100 and assign that number to n. The function should display all of the number in the list that are greater than...

  • PYTHON 4. Define a function that takes two arguments: a string called strText and a number...

    PYTHON 4. Define a function that takes two arguments: a string called strText and a number called intNumber. This function will use a repetition structure (a While or For loop) to print strText intNumber of times. Call this function. 5. Get an input from the user that is a file name with an extension (e.g., "myfile.ipynb" or "myfile.txt"). Print only the characters that follow the "." in the file name (e.g., "ipynb" and "txt"). 6. Ask the user for 5...

  • Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...

    Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...

  • PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns...

    PYTHON QUESTION PLEASE!! Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. Test case: the arguments are “apple@123” and “banana@#345”, your program should return “a@3”. Write a function named problem3 that accepts two strings as the arguments, returns the characters that occur in both strings. (20 pts) Test case: the arguments are "apple@123” and “banana@#345”, your program should return "a@3".

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT