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

This is a python question. Write a function mult_table(n) that consumes a natural number n and...
This is a python question. Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion. def mult_table(n) ''' Returns the n+1 by n+1 multiplication table    mult_table: Nat => (listof (listof Nat))    Examples:...
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...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
(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()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT