Import: Datetime Is - Valid - Date (Date - STR)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Q1.

To validate the input and ensure that the date is in the correct format, we would need
to use Python's datetime module. The datetime module provides classes for working with
dates and times. We can use the strptime() method to parse the input string into a datetime
object and then check if the input is a valid date.
import datetime

def is_valid_date(date_str):
"""
Checks if the input string is a valid date in the format yyyy-mm-
dd.

Args:
date_str (str): The input string to be validated.

Returns:
bool: True if the input is a valid date, False otherwise.
"""
try:
datetime.datetime.strptime(date_str, '%Y-%m-%d')
return True
except ValueError:
return False

print(is_valid_date('2023-04-28'))
print(is_valid_date('2023-04-31'))
print(is_valid_date('2023/04/28'))
print(is_valid_date('23-04-28'))

True
False
False
False

Q2. The function first checks if the list is empty. If it is, it returns a tuple of None for both
highest and lowest. Otherwise, it sets both the highest and lowest variables to the first
element of the list. It then loops through the remaining elements of the list, updating the
highest and lowest variables accordingly.
def find_high_low(lst):
"""
This function takes in a list of integers and returns the highest
and lowest values in the list.
Args:
- lst: A list of integers
Returns:
- A tuple containing the highest and lowest values in the list,
respectively.
"""
if not lst:
return None, None
else:
highest = lst[0]
lowest = lst[0]
for num in lst:
if num > highest:
highest = num
if num < lowest:
lowest = num
return highest, lowest

# Test cases
print(find_high_low([1, 2, 3, 4, 5]))
print(find_high_low([-1, -2, -3, -4, -5]))
print(find_high_low([10, 20, 30, 40, -50]))
print(find_high_low([5]))
print(find_high_low([]))

(5, 1)
(-1, -5)
(40, -50)
(5, 5)
(None, None)

Q3. The function first splits the input string by commas using the split() method, which
returns a list of words as strings. It then uses a list comprehension to strip any leading or
trailing whitespace from each word using the strip() method.
def string_to_list(string):
"""
This function takes in a string of words separated by commas and
returns a list of words.
Args:
- string: A string of words separated by commas
Returns:
- A list of words
"""
split_string = string.split(',')
stripped_list = [word.strip() for word in split_string]
return stripped_list

# Test cases
print(string_to_list('apple, banana, cherry, durian'))
print(string_to_list(' cat, dog , fish , bird '))
print(string_to_list('one'))
print(string_to_list(''))
Q4.
def is_valid_credit_card_number(number):
"""
This function takes in a string representing a credit card number
and checks whether it is valid.
Args:
- number: A string representing a credit card number
Returns:
- True if the credit card number is valid, False otherwise
"""
number = number.replace(' ', '').replace('-', '')
if not number.isdigit():
return False
if not 13 <= len(number) <= 16:
return False

total = 0
for i in range(len(number)-1, -1, -1):
digit = int(number[i])
if i % 2 == len(number) % 2:
digit *= 2
if digit > 9:
digit -= 9
total += digit

return total % 10 == 0

# Test cases
print(is_valid_credit_card_number('4111-1111-1111-1111'))
print(is_valid_credit_card_number('6011-1111-1111-1117'))
print(is_valid_credit_card_number('1234567890123456'))
print(is_valid_credit_card_number('4111111111111111'))
print(is_valid_credit_card_number('4111 1111 1111 1111'))
print(is_valid_credit_card_number('4111-1111-1111-1112'))

True
True
False
True
True
False

Q5.
def sort_list_alphabetically(lst):
"""
This function takes in a list of strings and sorts the list in
alphabetical order.
Args:
- lst: A list of strings
Returns:
- A new list containing the strings in lst sorted in alphabetical
order
"""
return sorted(lst)

# Test cases
lst1 = ['banana', 'apple', 'cherry', 'durian']
sorted_lst1 = sort_list_alphabetically(lst1)
print(sorted_lst1)

lst2 = ['Python', 'Java', 'C++', 'JavaScript']


sorted_lst2 = sort_list_alphabetically(lst2)
print(sorted_lst2)

lst3 = ['Dog', 'cat', 'bird', 'Fish']


sorted_lst3 = sort_list_alphabetically(lst3)
print(sorted_lst3)

['apple', 'banana', 'cherry', 'durian']


['C++', 'Java', 'JavaScript', 'Python']
['Dog', 'Fish', 'bird', 'cat']

You might also like