Concept explainers
python:
def character_gryffindor(character_list):
"""
Question 1
You are given a list of characters in Harry Potter.
Imagine you are Minerva McGonagall, and you need to pick the students from your
own house, which is Gryffindor, from the list.
To do so ...
- THIS MUST BE DONE IN ONE LINE
- First, remove the duplicate names in the list provided
- Then, remove the students that are not in Gryffindor
- Finally, sort the list of students by their first name
- Don't forget to return the resulting list of names!
Args:
character_list (list)
Returns:
list
>>> character_gryffindor(["Scorpius Malfoy, Slytherin", "Harry Potter,
Gryffindor", "Cedric Diggory, Hufflepuff", "Ronald Weasley, Gryffindor", "Luna
Lovegood, Ravenclaw"])
['Harry Potter, Gryffindor', 'Ronald Weasley, Gryffindor']
>>> character_gryffindor(["Hermione Granger, Gryffindor", "Hermione Granger,
Gryffindor", "Cedric Diggory, Hufflepuff", "Sirius Black, Gryffindor", "James
Potter, Gryffindor"])
['Hermione Granger, Gryffindor', 'James Potter, Gryffindor', 'Sirius Black,
Gryffindor']
"""
print(character_gryffindor(["Scorpius Malfoy, Slytherin", "Harry Potter,
Gryffindor", "Cedric Diggory, Hufflepuff", "Ronald Weasley, Gryffindor", "Luna
Lovegood, Ravenclaw"]))
print(character_gryffindor(["Hermione Granger, Gryffindor", "Hermione
Granger, Gryffindor", "Cedric Diggory, Hufflepuff", "Sirius Black, Gryffindor",
"James Potter, Gryffindor"]))
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images
- please use your own code for better explanationarrow_forward1 - Write a Python function that will do the following: 1. Ask the user to input an integer that will be appended to a list that was originally empty. This will be done 5 times, meaning that when the input is complete, the list will have five elements (all integers). a. Determine whether each element is an even number or an odd number b. Return a list of five string elements "odd" and "even" that map the indexes of the elements of the input list, according to whether these elements are even or odd numbers.For example: if the input sequence is 12, 13, 21, 51, and 30, the function will return the list ["even", "odd", "odd", "odd", "even"].arrow_forwardAssume, you have been given two lists. Your task is to multiply the first element of the first list, to the last element of the second list and store them in another list. Then, again multiply the second element of the first list, to the second last element of the second list, and so on. If any of your lists is out of element, and another list has some elements remaining, then just append the remaining elements to your final list and print the list.================================================ Given lists 1:list_one = [1, 4, 7, 5]list_two = [6, 1, 3, 9] Sample Output 1:[9, 12, 7, 30] Explanation 01: Here, from list_one’s 1st element, 1 has been multiplied with the last element of list_two 9. So, the product 1X9 =9 has been added to the answer. Then, from list_one’s 2nd element, 4 has been multiplied with the 2nd the last element of list_two 3. So, the product 3X4 =12 has been added to the answer. And so on. ================================================Given lists 2:list_one =…arrow_forward
- please code in python Create a program that uses list comprehension to take the following list and create a new list out of elements that have at least one ‘a’ character. Print out the new list.[‘Ford’, ‘Mazda’, ‘BMW’, ‘Nissan’, ‘Hyundai’, ‘Tesla’, ‘Honda’, ‘Subaru’, ‘Kia’, ‘Audi’, ‘Fiat’, ‘Bentley’, ‘Toyota’, ‘Lexus’]arrow_forwardThe function interleave_lists in python takes two parameters, L1 and L2, both lists. Notice that the lists may have different lengths. The function accumulates a new list by appending alternating items from L1 and L2 until one list has been exhausted. The remaining items from the other list are then appended to the end of the new list, and the new list is returned. For example, if L1 = ["hop", "skip", "jump", "rest"] and L2 = ["up", "down"], then the function would return the list: ["hop", "up", "skip", "down", "jump", "rest"]. HINT: Python has a built-in function min() which is helpful here. Initialize accumulator variable newlist to be an empty list Set min_length = min(len(L1), len(L2)), the smaller of the two list lengths Use a for loop to iterate k over range(min_length) to do the first part of this function's work. On each iteration, append to newlist the item from index k in L1, and then append the item from index k in L2 (two appends on each iteration). AFTER the loop…arrow_forwardWrite a function that will accept a list of integers and return the sum of the contents of the list(……if the list contains 1,2,3 it will return 6…..)arrow_forward
- Write a program that will simulate the NJ Cash 5 lottery. (python code) The program must pick 5 numbers from 1 to 38 - No duplicates allowed The numbers will be selected one at a time. It must print these numbers side by side (do not print the list as a list) on one line under the heading "These are the cash 5 numbers." You will need to "import random" at the beginning of your code. Each randomly selected number shall be stored in a list. No number should repeat in the list. After selecting the 5 numbers, ask the user if they want to play again and allow them to do so. This will require nested loops and lists (arrays). The outer loop should randomly select the the number between 1 and 38. It should then append that number to the empty list. (aNumbers.append(randomNumber). The inner loop should check each element in the list. Since the length of the list will grow, the loop should iterate to the length of he list.arrow_forwardIn python don't import librariesarrow_forwardCould you show me a different way to write this code in python? I’m trying to make the code simple and easy to understandarrow_forward
- mergeAndRemove(int[], int[]) This is a public static function that takes a int[] and int[] for the parameters and returns an int[]. Given two arrays of integers. Your job is to combine them into a single array and remove any duplicates, finally return the merged array.arrow_forwardpython LAB: Subtracting list elements from max When analyzing data sets, such as data for human heights or for human weights, a common step is to adjust the data. This can be done by normalizing to values between 0 and 1, or throwing away outliers. Write a program that adjusts a list of values by subtracting each value from the maximum value in the list. The input begins with an integer indicating the number of integers that follow.arrow_forwardin python Integer num_samples is read from input, representing the number of data samples to be read from input. List data_list contains the data samples read from the remaining input. For each element in data_list: If the element is greater than 25, output the element followed by ' at index ', the element's index in the list, and ' is flagged'. Otherwise, output the element followed by ' at index ', the element's index in the list, and ' is normal'.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education