-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_operation.py
34 lines (28 loc) · 1.13 KB
/
list_operation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import sys
class InvalidListSize(Exception):
"Raised when the input string length is not a multiple of 10"
pass
def process_user_list(input):
try:
user_list = input.split()
size = len(user_list)
if size % 10 != 0 or size == 0:
raise InvalidListSize
else:
for i in range(size):
# convert each item to int type
user_list[i] = int(user_list[i])
# remove values from indices which are multiple of 2 or 3
new_list = [value for index, value in enumerate(user_list) if (index + 1) % 2 != 0 and (index + 1) % 3 != 0]
print("New list :", new_list)
except InvalidListSize:
print("List size is not a multiple of 10")
except ValueError:
print("The input was not a valid integer.")
# Get input from environment variable
input_string = os.environ.get('INPUT_STRING', '')
# If input is not provided through environment variable, wait for user input
if not input_string and sys.stdin.isatty():
input_string = input('Enter elements of a list separated by space:\n')
process_user_list(input_string)