Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c02e03a

Browse files
Discussed with Ram, solution given to check both integer and float number
1 parent e437c96 commit c02e03a

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

‎Excersise/EvenNumEvenIndex_42.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
Given a list of N numbers, use a single list comprehension to produce a new list that only contains those values that are:
3+
(a) even numbers, and
4+
(b) from elements in the original list that had even indices
5+
For example, if list[2] contains a value that is even, that value should be included in the new list,
6+
since it is also at an even index (i.e., 2) in the original list.
7+
However, if list[3] contains an even number, that number should not be included in the new list since
8+
it is at an odd index (i.e., 3) in the original list.
9+
'''
10+
11+
def EvenNumEvenIndex(someList):
12+
if type(someList) is not list:
13+
print("Invalid user input")
14+
if len(someList) == 0:
15+
print("No value in the list")
16+
17+
dummyList = []
18+
for num in someList:
19+
if someList.index(num) %2 == 0 and num %2 == 0:
20+
dummyList.append(int(num))
21+
return dummyList
22+
23+
24+
def getUserInput():
25+
listOfIntNum = []
26+
listOfFloatNum = []
27+
28+
inputList = input("Enter list of nunmbers: ")
29+
inputList = inputList.split(" ")
30+
31+
if inputList:
32+
for value in inputList:
33+
# try:
34+
# if isinstance(int(value),int):
35+
# listOfNum.append(int(value))
36+
# continue
37+
# if isinstance(float(value),float):
38+
# listOfNum.append(float(value))
39+
# continue
40+
# # if type(int(value)) == int:
41+
# # listOfNum.append(int(value))
42+
# # continue
43+
# # if type(int(float(value))) == int:
44+
# # listOfNum.append(int(float(value)))
45+
# # continue
46+
# except ValueError:
47+
# pass
48+
49+
if '.' in value:
50+
# possible that, value can be a floating number or a sentence with full stop.
51+
try:
52+
if isinstance(float(value), float):
53+
listOfFloatNum.append(float(value))
54+
continue
55+
except ValueError as e:
56+
pass
57+
try:
58+
if isinstance(int(value), int):
59+
listOfIntNum.append(int(value))
60+
except ValueError as e:
61+
pass
62+
63+
64+
return listOfIntNum + listOfFloatNum
65+
66+
if __name__ == "__main__":
67+
numList = getUserInput()
68+
print(numList)
69+
print(EvenNumEvenIndex(numList))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /