📜  first_last6 - Python (1)

📅  最后修改于: 2023-12-03 15:00:46.162000             🧑  作者: Mango

first_last6 - Python

first_last6 is a Python function that takes in a list of integers and returns True if the first or last integer in the list is 6, and False otherwise.

Syntax
def first_last6(nums: List[int]) -> bool:
Parameters
  • nums: A list of integers. (required)
Return value
  • Returns True if the first or last integer in the list is 6, and False otherwise.
Example
from typing import List

def first_last6(nums: List[int]) -> bool:
    if nums[0] == 6 or nums[-1] == 6:
        return True
    else:
        return False

# Testing
print(first_last6([1, 2, 6])) # True
print(first_last6([6, 1, 2, 3])) # True
print(first_last6([13, 6, 1, 2, 3])) # False
Explanation
  • The first_last6 function takes a list of integers nums as input.
  • The function checks if the first or last integer in the list is 6.
  • If either the first or last integer is 6, the function returns True.
  • If neither the first nor the last integer is 6, the function returns False.
  • In the example above, the first and second tests both return True, because the lists contain a 6 in the appropriate position. The third test returns False because there is no 6 at the beginning or end of the list.