Here is the problem:
And here is my solution(though it works but I am getting the following as an error):
Here is the problem:
And here is my solution(though it works but I am getting the following as an error):
Sweet, two problems here
First, your jdoodle assumes you are getting in int array, you are not. You are getting a string of numbers. So the first thing you need to do is take that string of numbers and split it into an array. Then you need to convert each item in the array to an int.
Here is what I came up with
def iq_test(input):
a = input.split(" ")
ct = 0
cf = 0
for i in a:
if int(i) % 2 ==0:
ct = ct + 1
else:
cf = cf + 1
if ct > cf:
for i in a:
if int(i) % 2 !=0:
y = a.index(i) + 1
return y
else:
for i in a:
if int(i) % 2 == 0:
y = a.index(i) + 1
return y
Note, how I split the input into an array as the first step, then everywhere you referenced i % 2
, I changed it to int(i) % 2
which permits it to do a modulus. Now this can fail, but the kata doesn’t, if the following input was provided, this would still blow up 3 5 8 9 N
, as ‘N’ can’t be converted to an int and we’d have to add isinstance(i, int) before running int(i) % 2 == 0
, which we talked about at
But as I said, the tests the kata is using will pass with the above code.
Thanks again ! But nowhere it was mentioned that I will be getting something like this [3 5 8 9 N] as an input. And apart from that what does the error mean?
The error is a bit cryptic but it is stating it can’t convert “1 3 5 4” in your for i in a
statement. Because for loops are meant to iterate over an enumerable object, such as, an array. It was unable to covnert the string to an array.
To figure out your input would be a string of numbers, I looked at the example tests.
Test.assert_equals(iq_test("2 4 7 8 10"),3)
Test.assert_equals(iq_test("1 2 2"), 1)
Those examples are your go to guide, as not only do they indicate the input you are likely to receive, but also give you the solution so you can test your code.
I got it. I never looked at the inputs carelessly. I thought I am getting integers in array.
Thanks man ! How did you get so good at python ?
Well, here is the funny thing, I actually know very little python (I just saw your question, saw your attempt and felt it was worth my time to help you out). I’ve just got a ton of experience and expertise in programming in general as I’ve been doing it for so long. Every language is the same when it comes to “how do you debug it?”, “how do you solve this problem?”, what differs is the syntax and the internal methods/functions that are available.
The one thing I will say is, it is refreshing to see someone ask questions about the answers I provide where the detail may not have struck home and left you pondering another question. No matter what, always do that. A programmer who stops asking questions to improve their own abilities is a programmer who isn’t worth anything anymore.
Oh as an FYI, my responses may be more delayed from here on out. I have a sound engineer engagement in a couple of hours, rehearsal start in about 3 hours from now and the event will finish a couple hours after that. Keep posting your attempts and your questions and I’ll do what I can when my time frees up
I found this site to be way better than stack overflow. Thanks once again and will be bothering you in near future for the upcoming katas
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.