Question

1.Write a function called high_point that takes a list of integers and returns True if there...

1.Write a function called high_point that takes a list of integers and returns True if there exists a value in the list that is bigger than the values immediately before and after it in the list. Your function must return False if no such value exists. The values in the beginning and the end of the list cannot be high points. (20 points)

Test Cases:

print(high_point([2,5,8,9,7,9])) True

print(high_point([2,5,6,6,3])) False

print(high_point([2,5,8,21,22])) False

2. Write a while loop to repeatedly ask for an integer between 0 and 9. Keep count of how many times each value occurs in a list and end the loop when any value reaches 3 occurrences, at which point you should print out the list of occurrences. You can assume that the user always enters an integer value, but must verify that the value lies within the correct range. If an invalid value is entered, simply ignore it and go to the next input.

Here is a sample run:

Enter a digit (0-9): 4

Enter a digit (0-9): 9

Enter a digit (0-9): 1

Enter a digit (0-9): 2

Enter a digit (0-9): -1

Enter a digit (0-9): 10

Enter a digit (0-9): 6

Enter a digit (0-9): 1

Enter a digit (0-9): 1

[0, 3, 1, 0, 1, 0, 1, 0, 0, 1]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
# 1st program
def high_point(num_list):
    # Iterate loop from index 1 to 1 less than length
    # First value and last value cannot be high point
    for i in range(1, len(num_list) - 1):
        # If a value at index i > value at index i-1 and i+1, return true
        if num_list[i] > num_list[i - 1] and num_list[i] > num_list[i + 1]:
            return True

    return False  # Return false after loop


# Test cases
print(high_point([2, 5, 8, 9, 7, 9]))
print(high_point([2, 5, 6, 6, 3]))
print(high_point([2, 5, 8, 21, 22]))
print()
# 2nd program
# Create a list to store occurence of digits from 0-9
# List indices starts from 0. So for list of length 10, index are 0-9
occurrence = [0] * 10

# Iterate loop till occurrence list doesn't have 3 in it
# which means no digit has appeared 3 times
while 3 not in occurrence:
    digit = int(input('Enter a digit (0-9): '))  # Ask to enter a digir
    if digit < 0 or digit > 9:  # If not valid, continue to ask another digit and do nothing
        continue
    occurrence[digit] += 1  # Increment value at index digit by 1

print(occurrence)  # Print occurrence list

SCREENSHOT

M HighPoint.py X # 1st program def high_point (num_list): # Iterate loop from index 1 to 1 less than length # First value and
OUTPUT
HighPoint E:\Practice Python\venv\Scripts\python.exe True False False Enter a digit (0-9): 4 Enter a digit (0-9): 9 Enter a d

Add a comment
Know the answer?
Add Answer to:
1.Write a function called high_point that takes a list of integers and returns True if there...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a function that takes a list and an integer v, and returns True if v...

    Create a function that takes a list and an integer v, and returns True if v is in the list (False otherwise). The function should be efficient and stop searching as soon as possible. •The main program must generate a very large list with random elements, call the function to search a value and display the result. Add in the function a variable Nsteps to count the number of steps used by the algorithm (number of times the loop is...

  • 1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if...

    1.1. Write a function named "areFirstTwoTheSame AsLast TwoChars" that accepts a string. It returns true if the first two characters and the last two characters of the string are the same. It returns false otherwise. In addition, if the string is empty or has only one character, it also returns false. For example, these are the strings with their expected return values false falsc "AB" true "ABA" false "ABAB" trus "ABBA" false "ABCABC false "ABCCAB" true 1.2 Write a function...

  • Implement function allEven() that takes a list of integers and returns True if all integers in...

    Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10]) False

  • Define a function in pycharm (isValid) that takes a password as a parameter and returns true...

    Define a function in pycharm (isValid) that takes a password as a parameter and returns true if the password is valid or false if the password is invalid. A valid password must be at least 8 characters long and contains $, _, or @. Invoke the function and print valid or invalid according to the returned Boolean value. Sample, if (isValid(password) == True): print(“Valid”) Or, result = isValid(password) If (result): print(“invalid”)

  • Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n...

    Define a function called get_n_largest(numbers, n) which takes a list of integers and a value n as parameters and returns a NEW list which contains the n largest values in the parameter list. The values in the returned list should be in increasing order. The returned list must always be of length n. If the number of values in the original list is less than n, the value None should be repeated at the end of the returned list to...

  • python Write a function called has_odd_even that takes three integers as parameters and that returns True...

    python Write a function called has_odd_even that takes three integers as parameters and that returns True if there is at least one odd and at least one even among the three numbers and that returns False otherwise. Below are some sample calls and the appropriate value to return. Function call Value returned has_odd_even(2, 4, 6) False has_odd_even(2, 3, 4) True has_odd_even(12, 4, 17) True has_odd_even(5, 17, 4) True has_odd_even(14, 7, 5) True has_odd_even(5, 4, 2) True has_odd_even(13, 20, 91) True...

  • ​Write a function that takes a list as an argument and returns true if the list...

    ​Write a function that takes a list as an argument and returns true if the list is already sorted in increasing order. (You should not sort the list, and make sure that you just iterate over the list once! Only one loop!) WRITE THE CODE IN PYTHON

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • Write a program to read two integer values and print true if both the numbers end...

    Write a program to read two integer values and print true if both the numbers end with the same digit, otherwise print false. Example: If 698 and 768 are given, program should print true as they both end with 8. [Hint: The % operator can be used to find the remainder.] At the time of execution, the program should print the message on the console as: Enter two integer values : For example, if the user gives the input as...

  • Using Python. Write a function clean2(aList) that takes a list of integers aList as argument, and...

    Using Python. Write a function clean2(aList) that takes a list of integers aList as argument, and modifies this list, such as multiple occurrences of values have been removed. The procedure does not return a list: it modifies its argument (reference aList). 3 For instance, the following statements: al = [1,2,3,4,4,4,5,1,2,1,5] print(al) clean2(al) print(al) print the following result (a different order of values is acceptable): [1,2,3,4,4,4,5,1,2,1,5] [1,2,3,4,5]

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT