Hi, I need help writing a code for this. The language is python 3, and we cannot use things like break, continue, exit(), lambda, map, filter, raise, try, except, and assert in our code. Thank you!
Implement one of the sorting algorithms from class, either bubble, selection or quick sort with the following modification. Call the function "color_sort(the_list)"
Each element is now a list (or tuple) with two elements. The first element is the number value that you need to sort. The second is a "color" which is really a string.
In case of a tie, sort the highest priority color forward of the lower priority colors.
Heres the Color Hierarchy:
|
Gold - Highest |
|
Red - Second |
|
Blue - Third |
|
Green - Fourth |
A sample list to be sorted will be for instance:
[(3, 'red'), (2, 'blue'), (2, 'gold'), (5, 'green'), (17, 'blue')]
Sorted it will be:
[(2, 'gold'), (2, 'blue'), (3, 'red'), (5, 'green'), (17, 'blue')]
If there are all the same color, then it sorts just as a usual list would:
[(27, 'red'), (10, 'red'), (21, 'red'), (46, 'red'), (13, 'red')]
Sorted it is:
[(10, 'red'), (13, 'red'), (21, 'red'), (27, 'red'), (46, 'red')]
Colors will only be in those four categories, and in the list, will always be in lower case.
We CANNOT use .sort(), because it results in a zero for the problem.
PLEASE GIVE IT A THUMBS UP

color = ["gold","red","blue","green"]
def color_sort(the_list):
global color
i=0
while(i<len(the_list)):
j=0
while(j<len(the_list)):
if(the_list[i][0] < the_list[j][0]):
t = the_list[i]
the_list[i] = the_list[j]
the_list[j] = t
elif(the_list[i][0] == the_list[j][0]):
if(the_list[i][1] != the_list[j][1]):
if((the_list[i][1]==color[0]
and the_list[j][1]==color[1]) or (the_list[i][1]==color[0] and
the_list[j][1]==color[2]) or (the_list[i][1]==color[0] and
the_list[j][1]==color[3])):
t =
the_list[i]
the_list[i] = the_list[j]
the_list[j] = t
elif((the_list[i][1]==color[1] and the_list[j][1]==color[2]) or
(the_list[i][1]==color[1] and the_list[j][1]==color[3])):
t =
the_list[i]
the_list[i] = the_list[j]
the_list[j] = t
elif((the_list[i][1]==color[2] and
the_list[j][1]==color[3])):
t =
the_list[i]
the_list[i] = the_list[j]
the_list[j] = t
j+=1
i+=1
return the_list
l = [(3, 'red'), (2, 'blue'), (2, 'gold'), (5, 'green'), (17,
'blue')]
l1 = [(27, 'red'), (10, 'red'), (21, 'red'), (46, 'red'), (13,
'red')]
print(color_sort(l))
print(color_sort(l1))

Hi, I need help writing a code for this. The language is python 3, and we...
Hi, I need help writing a code for this. The language is python 3, and we cannot use things like break, continue, exit(), lambda, map, filter, raise, try, except, and assert in our code. Thank you! We must write a function called "binary_to_decimal(binary_number)" that takes a string for a binary number and output the decimal integer for that number. The solution, aka the code, MUST contain a for loop of this form (power is an integer variable you define earlier):...
Python 3 Coding Functions:
Here is any code required to help code what is below:
def pokemon_by_types(db, types):
new_db = {}
for pokemon_type in types:
for key in db: # iterate through all the type in types list
if db[key][1] == pokemon_type or db[key][2] == pokemon_type:
if key not in new_db:
new_db[key] = db[key]
return new_db
I need help coding the functions listed below in the image:
Thank you
get types(db): Given a database db, this function determines all the...
the following python code edits BMP image file to negative but I need help with modifying this code so it turns it the same BMP image file into a black and white instead of a negative. heres python code I have so far [pasted below] ## # This program processes a digital image by creating a negative of a BMP image. # from io import SEEK_CUR from sys import exit def main() : filename = input("Please enter the file name:...
Please help me with this question! It is due by midnight! I'm
doing this in Visual Studio 2019. But any others are fine as long
as it can compile.
I also wanted to see the results if it is compiled and QT
comments as well. Please respond immediately!
Description: For this homework we will use classes to model something in the real world. In this case a resistor. The colored bands on the top-most resistor shown in the photo below...
Python Code help needed. Based on the file (2nd picture) a
dictionary like the third picture is to be made.
def load_locations(filename): (str) -> dict Read data from the file with the given fi lename and create and return a dictionary with location data, structured as descr ibed in the ass ignment handout, Part 1 Section I1 [BEGIN DESCRIPTION There is nothing interesting here [END DESCRIPTION] BEGIN DESCRIPTION] You wake up lost in some unfamiliar place. All you know is...
I need a python 3 help. Please help me with this question Part 2. Linked Lists You start working with the class LinkNode that represents a single node of a linked list. It has two instance attributes: value and next. class LinkNode: def __init__(self,value,nxt=None): assert isinstance(nxt, LinkNode) or nxt is None self.value = value self.next = nxt Before you start with the coding questions, answer the following questions about the constructor Valid Constructor or Not? LinkNode(1, 3) LinkNode(1, None) LinkNode(1,...
All those points need to be in the code
Project 3 Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort Class River describes river's name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles long and returns false otherwise. Class CTRivers describes collection of CT rivers. It has no data, and it provides the...
Hello, I need help with my code. The code needs to display random number from 1 to 50 every time the program runs but the program displays the same random numbers every time. Thanks #include #include using namespace std; void dynAlloc(int size, int *&arr); void displayArray(int *arr, int n); void insertionSort(int *arr, int n, int *temp); void linear_search(int *arr, int n, int key); void binary_search(int *arr, int n, int key); int main() { const int n = 50; int *arr,...
PRE-LABORATORY QUESTIONS (For full credit show your calculations) 1. List the expected color of light and the corresponding wavelength (nm) associated with each cation (use Table 1) Rubidium, Rb Cesium, Cs 2. Calculate the energy associated with the light emitted by each of the elements above (sue equations 1 and 2) Rubidium, Rb Cesium, Cs 11:17 $3.us-east-1.amazonaws.com Flame Tests&Elecrn Ceniguaion 1 of 7 FLAME TESTS & ELECTRON CONFIGURATION INTRODUCTION AND BACKGROUND The chemistry of an element strongly depends on the...
PYTHON 3 node Node ADT Question.
Question 3 (18 points): Purpose: To practice working with node chains created using the Node ADT to implement slightly harder functionality Degree of Difficulty: Moderate to Tricky In this question you'll implement merge sort for node chains! Recall, merge sort is a divide-and-conquer technique that uses recursion to sort a given sequence (in this case a node chain). A overview of the algorithm is given below. Algorithm mergeSort (NC) Borts node chain NC using...