We help IT Professionals succeed at work.

Simple Python help

on
245 Views
I'm new to python and trying to figure out how to do this.

i want a function that takes in an array of numbers: fxn([1, 2, 3, 3, 3]) and if one of the element values occurs more than arraySize/2 times, then return that number (in this case 3 works).  If not, just return the string "can't" or something.

Also what do you think the running time of it would be?

Thanks.
Comment
Watch Question

View Solution Only

Top Expert 2006

Commented:
1.  Sort the array
2.  For each element within the array
3.      If Count > ArraySize / 2
4.          Return Value
5.      End If
6.      If Current_Element == Next_Element
7.          If Value != Current_Element
8.              Count = 0
9.          End If
10.        Value = Current_Element
11.        Count = Count + 1
12.      End If
13.      Return "Can't"
14.  Loop

I don't do Python, but that's the general algorithm you'd use. Generally, this process should perform very quickly.
Top Expert 2006

Commented:
[Swap lines 13. and 14. around]

Commented:
I'm familiar with the algorithm but was hoping to get python help.  Thanks.

Commented:
I'm a python coder, not a real pro; let me the time to download the interpreter and try out myself some tests; I'll tell the results ASAP. Luigi
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)

Commented:
what running time order do you think that is?  (n)?  faster?

Commented:
It's a very simple function, therefore I think that its running time is almost instant.

Commented:
Thanks for the points :)
Unlock the solution to this question.

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.