Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value
Question:
Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array
def max_end3(nums):
if nums[0:] > nums[:-1]:
# this is where I am lost
return print(nums)
elif nums[:-1] > nums[0:]:
# this is where I am lost
return print(nums)
max_end3([1, 2, 3])
max_end3([11, 5, 9])
max_end3([2, 11, 3])
I am missing something and I cant seem to remember it. there is a +1 somewhere that allows me to iterate through each element and modify them as it goes. I cannot seem to remember how to assign the larger value to every item in the list.
Here is a link to the problem https://codingbat.com/prob/p135290
I appreciate any assistance.
Thank You.
Answers:
You can find the maximum value using in-built max
and form a list of the maximum values:
def max_end3(nums):
m = max(nums[0], nums[-1])
print([m for _ in nums])
You could do a list comprehension to change all the values, it would be something like
list = [value = list[:0] for value in list]
also the return statements aren’t indented
Check this out
def max_end3(nums):
return 3 * [max(nums[0], nums[-1])]
Given an array of ints length 3, figure out which is larger, the first or last element in the array, and set all the other elements to be that value. Return the changed array
def max_end3(nums):
if nums[0:] > nums[:-1]:
# this is where I am lost
return print(nums)
elif nums[:-1] > nums[0:]:
# this is where I am lost
return print(nums)
max_end3([1, 2, 3])
max_end3([11, 5, 9])
max_end3([2, 11, 3])
I am missing something and I cant seem to remember it. there is a +1 somewhere that allows me to iterate through each element and modify them as it goes. I cannot seem to remember how to assign the larger value to every item in the list.
Here is a link to the problem https://codingbat.com/prob/p135290
I appreciate any assistance.
Thank You.
You can find the maximum value using in-built max
and form a list of the maximum values:
def max_end3(nums):
m = max(nums[0], nums[-1])
print([m for _ in nums])
You could do a list comprehension to change all the values, it would be something like
list = [value = list[:0] for value in list]
also the return statements aren’t indented
Check this out
def max_end3(nums):
return 3 * [max(nums[0], nums[-1])]