phython problem, please help me with the indent too i have difficulty with the i
ID: 3853710 • Letter: P
Question
phython problem, please help me with the indent too i have difficulty with the indent
Write a function that satisfies the following docstring: def largest_odd_times (L): """ Assumes L is a non-empty list of ints Returns the largest element of L that occurs an odd number of times in L. If no such element exists, returns None """ # Your code here For example, if largest_odd_times ([2, 2, 4, 4]) returns None. largest_odd_times ([3, 9, 5, 3, 5, 3]) returns 9 Paste your entire function, including the definition, in the box below. Do not leave any debugging print statements.Explanation / Answer
Since python code has to be indented, i am pasting the code link @ http://pasted.co/051397ba for your easy reference. All you need to do is copy and paste in your system. Also copied the same code below. Please check and let me know if you face any issues.
Code:
#!/usr/bin/python
def largest_odd_times(L):
""" Assumes L is a non-empty list of ints
Returns the largest element of L that occurs an odd number
of times in L. If no such element exists, returns None """
# declaring odd hash for storing the frequency of each element
oddHash = {}
# storing the frequency of each element
for each_elm in L:
if each_elm in oddHash:
oddHash[each_elm] += 1
else:
oddHash[each_elm] = 1
# assigning max variable to None
max = None
# checking if each element occurred odd number of times and if the element is largest
for each_elm in oddHash:
if oddHash[each_elm] % 2 and each_elm > max:
max = each_elm
# if max is defined that means we have found max number which occurred odd number of times else return None
if max:
return max
else:
return None
if __name__=='__main__':
print largest_odd_times([3,9,5,3,5,3])
print largest_odd_times([2,2,4,4])
print largest_odd_times([3, 3, 2, 0])
print largest_odd_times([6, 8, 6, 8, 6, 8, 6, 8, 6, 8])
print largest_odd_times([2, 4, 5, 4, 5, 4, 2, 2])
Execution and output:
Unix Terminal> python largest_odd.py
9
None
2
8
4
Unix Terminal>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.