Trying to solve animal lifetimes in intermediate python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Trying to solve animal lifetimes in intermediate python

This is my code: ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] num=int(input()) res=len(list(filter(lambda x:x>num==0,ages))) print(res) Only one test case seem to come out write

25th Mar 2021, 11:03 AM
Simisola Osinowo
Simisola Osinowo - avatar
17 Answers
+ 4
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] num=int(input()) res=len(list(filter(lambda x:x>num,ages))) print(res) this is the right answer
8th Aug 2021, 6:10 PM
ANDREW TSEGAYE
ANDREW TSEGAYE - avatar
+ 1
What is the problem?
25th Mar 2021, 11:14 AM
Slick
Slick - avatar
+ 1
Ya, like I said your lambda is incorrect. Remove the ==0 part from the return value of your lambda.
25th Mar 2021, 11:31 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Slick The object for this practice is to use map and or filter.
25th Mar 2021, 11:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Slick Ya I just looked where the practice was in the course. Lol
25th Mar 2021, 11:35 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] a=int(input()) result = len(list(filter(lambda x : x>a,ages))) print(result)
15th Apr 2021, 3:08 PM
Veena Tirmal
Veena Tirmal - avatar
+ 1
# You have two ways: ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] det = int(input()) def older(x): return x > det filter_data = list(filter(older, ages)) print(len(filter_data)) filters = list(filter(lambda x: x > det, ages)) print(len(filters))
16th Dec 2021, 10:31 PM
Mohammad Jamal Mahmoud Al Jadallah
Mohammad Jamal Mahmoud Al Jadallah - avatar
0
While I can't see the practice challenge as it is for pro subscription, I can see that the lambda in your filter is most likely incorrect. x>num==0 This is the same as x > num and num == 0 Is this what you intended?
25th Mar 2021, 11:16 AM
ChaoticDawg
ChaoticDawg - avatar
0
The problem is that only one test case comes out write
25th Mar 2021, 11:25 AM
Simisola Osinowo
Simisola Osinowo - avatar
0
No, explain the problem you are trying to solve with code. You have an output so it's right to me. Unless we know some expected inputs and their cooresponding outputs, it will be hard to help.
25th Mar 2021, 11:27 AM
Slick
Slick - avatar
0
You are analyzing a data set of animals. The data is a list of numbers, which represent the ages of animals. You need to take a number as input and output how many of the animals are older than the given number. This is the problem
25th Mar 2021, 11:29 AM
Simisola Osinowo
Simisola Osinowo - avatar
0
Thanks it worked
25th Mar 2021, 11:33 AM
Simisola Osinowo
Simisola Osinowo - avatar
0
ChaoticDawg gotcha, that wasn't explained
25th Mar 2021, 11:34 AM
Slick
Slick - avatar
0
This is what I have tried and it works: 1. you create an number input which has to be an integer 2. you filter out the number of animals over x whose age is greater than your input using lambda that is (lambda x: x> number, age) as shown below: NB. remember to find the length of the list you have created using len(lits()) function. ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] number=int(input()) animal_number=len(list(filter(lambda x: x>number, ages))) print(animal_number )
6th Apr 2021, 5:15 PM
Allan 🔥STORMER🔥🔥🔥🔥
Allan 🔥STORMER🔥🔥🔥🔥 - avatar
0
s,i=[3,1,9,0.4,7,12,2,1.7,5.7,42,6.7,14.5,21],int(input()) print(len((list(filter(lambda x:x>i,s)))))
10th Aug 2021, 6:19 AM
Nyatro
Nyatro - avatar
- 1
oh okay. - sort the list. - find index of searched number. - then get the length of the list AFTER the index
25th Mar 2021, 11:31 AM
Slick
Slick - avatar
- 1
def animals(ages): return number = int(input()) ages = [3, 1, 9, 0.4, 7, 12, 2, 1.7, 5.7, 42, 6.7, 14.5, 21] older = list(filter(lambda x: x>number, ages)) print(len(older))
30th Jul 2021, 12:00 PM
emmanuel omofuma