Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 35 additions & 7 deletions functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ def use_of_underscore():
source : https://hackernoon.com/understanding-the-underscore-of-python-309d1a029edc
"""

def _print_x_y(): # single leading inderscore - private
def _print_x_y(): # single leading inderscore - private
print('x and y : {x}, {y}'.format(x=x, y=y))

# ignore value when unpacking
x, _, y = (1,2,3)
x, _, y = (1, 2, 3)
_print_x_y()

# ignore multiple values, "Extended upacking not
# available in < 3.x
x, *_, y = (1,2,3,4,5,5,6,7)
x, *_, y = (1, 2, 3, 4, 5, 5, 6, 7)
_print_x_y()

# ignore index
for _ in range(10): print('s', end=' ',sep=' ')
for _ in range(10): print('s', end=' ', sep=' ')

print()

list_ = [(1,2), (3,4)]
list_ = [(1, 2), (3, 4)]
# single trailing underscore - avoiding conflict
# with builtins names

Expand Down Expand Up @@ -85,11 +86,38 @@ def unpack_a_list(input_list: list) -> list:
output_list = list()
for item in input_list:
if isinstance(item, list):
output_list += item
output_list.extend(item)
else:
output_list.append(item)
return output_list

"""
Its one liner
[output_list.extend(item) if isinstance(item, list) else output_list.append(item) for item in input_list]
return output_list
"""

# Eg

# Example
print("Unpacked list of [1,2,[3,4],5] is {}".format(unpack_a_list([1, 2, [3, 4], 5])))


def find_min_max_in_list(input_list: list) -> tuple:
"""
Find min and max number in list of numbers
Expect the list contains int only
:param input_list:
:return:
"""
min = max = input_list[0]
for number in input_list:
if number > max:
max = number
if number < min:
min = number

return (max, min)


# Example
print("Max and min in [1,2,3,6,4,5] is {}".format(find_min_max_in_list([1, 2, 3, 6, 4, 5])))