Question

In: Computer Science

PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of...

PYTHON QUESTION:

- Write the body of a function most_ending_digit(L) that consumes a non-empty list of natural numbers L and return the single digit that occurs most frequently at the end of the numbers in the list. The function returns the smallest digit in the case of a tie. Your function should run in O(n) time. Do not mutate the passed parameter.

def most_ending_digit(L)

'''
Returns the single digit that occurs most frequently
as the last digit of numbers in L. Returns the
smallest in the case of a tie.
  
most_ending_digit: (listof Nat) -> Nat
Requires: len(L) > 0
  
Examples:
most_ending_digit([1,2,3]) => 1
most_ending_digit([105, 201, 333,
995, 9, 87, 10]) => 5
'''

## CODE

- Write the body of a function __eq__ method for this class that returns True if the two objects compared are Movie objects each having the same fields and False otherwise.

class Movie:
'''
Fields:
name (Str),
year (Nat),
rating (Nat)
'''
def __init__(self, n, y, r):
self.name = n
self.year = y
self.rating = r
  
def __eq__(self, other):
'''
Returns True if the movies are equal
  
__eq__: Movie Any -> Bool
'''
##CODE

Solutions

Expert Solution

def most_ending_digit(l):
res=[]
for i in l:
res.append(i%10)#get the last digits
s=list(set(res))
s.sort()#sort list to return the least value digit in case of tie
sc=[]
for i in s:
sc.append(res.count(i))#get the count of each digit
return s[sc.index(max(sc))]#return the value of the maximum count digit
  
print(most_ending_digit([1,2,3]))#call the function and print output.
print(most_ending_digit([105,201,333,995,9,87,10]))

Screenshots:

The screenshots are attached below for reference.

Please follow them for proper indentation.

2)

class Movie:
def __init__(self, n, y, r):
self.name = n
self.year = y
self.rating = r
  
def __eq__(self, other):
if other.name==self.name and other.year==self.year and other.rating==self.rating:#compare the fields
return True#return true if fields are same
else:
return False
  
m1=Movie("n1",2019,5)
m2=Movie("n1",2019,5)
print(m1.__eq__(m2))

Screenshots:

The screenshots are attached below for reference.

Please follow them for proper indentation.

Please upvote my answer. Thank you.


Related Solutions

In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. sort function is not allowed as well as list comprehensions math is the only library that can be imported Example: int([4, 13,...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT