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...

  • ​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]...

  • 1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...

    1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true if the digit i appears in the positive (base 10) integer n, false if it does not in c++. ex. Enter a positive integer: 54321 Enter a digit: 7 The digit 7 does NOT appear in the integer 54321. 2. IS AN INPUT STRING A PALINDROME? A palindrome is a string in which the letters are the same in both directions, disregarding capitalization and...

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

    Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position. Check for the insert_position value exceeding the list (my_list) bounds. if the insert_position is greater than the length of the list, insert the value at the end of the list. if the insert_position is less than or equal...

  • Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a...

    Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: The number parameter must be a default argument. -      If the default argument for number is given in the function call, only the first number of items are reversed. -      If the default argument for number is not provided in the function call, then the...

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
ADVERTISEMENT