Jump to content

python finding largest number

xGear

Ah. Our computing teacher seems to swear by Python 2 so I have no clue about Python 3.

eww Python 2

 

You doing GCSE in it? I thought Py3 was mandatory for GCSE

Link to comment
Share on other sites

Link to post
Share on other sites

eww Python 2

 

You doing GCSE in it? I thought Py3 was mandatory for GCSE

We're doing Python 2 for whatever reason. For all I know we could end up switching to Python 3 but seeing how little some of us seem to grasp so far it seems unlikely.

Sig under construction.

Link to comment
Share on other sites

Link to post
Share on other sites

Could you use the built-in max() function?  It wouldn't tell you how many times the largest value appears, but it would tell you what the largest value is.  Same thing with min(), but it'll tell you the smallest value.  You could use this to get the number of times the largest/smallest value occurs, too, by checking to see if each value in the list equals the max/min value and doing something from there.  And with some more cleverness (like a dictionary or using the list.index() method) you could print which value (first/second/etc or A/B/etc) is the max/min.

Link to comment
Share on other sites

Link to post
Share on other sites

I guess, sorting seems better then having lots of ifs, simple options tend to be better.

 

Sure, using ifs is fine as a learning exercise but it's not a good way to solve the problem and shouldn't ever actually be put to use. As the array size grows, the amount of code needed to calculate it with ifs grows a lot and it's very ugly.

 

A sorted array can be very useful. You can get the max and min in O(1), you can apply a binary search, etc. However if all you want is the min/max value, then sorting is overkill. The best sorting algorithms run in O(nlogn) where as a simple min/max algorithm will run in O(n) on an unsorted array. Bubble sort isn't even the best sorting algorithm so it would be even slower.

 

Now none of this really matters on an array of 5 elements, but I figured I'd explain it anyway.

Link to comment
Share on other sites

Link to post
Share on other sites

Sure, using ifs is fine as a learning exercise but it's not a good way to solve the problem and shouldn't ever actually be put to use. As the array size grows, the amount of code needed to calculate it with ifs grows a lot and it's very ugly.

 

A sorted array can be very useful. You can get the max and min in O(1), you can apply a binary search, etc. However if all you want is the min/max value, then sorting is overkill. The best sorting algorithms run in O(nlogn) where as a simple min/max algorithm will run in O(n) on an unsorted array. Bubble sort isn't even the best sorting algorithm so it would be even slower.

 

Now none of this really matters on an array of 5 elements, but I figured I'd explain it anyway.

 

i know c# has array.sort() which I believe to be pretty fast. last year in college i did a whole unit on "data structures" which basically boiled down to creating a bubble search and doing one manually.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

i know c# has array.sort() which I believe to be pretty fast. last year in college i did a whole unit on "data structures" which basically boiled down to creating a bubble search and doing one manually.

 

Bubble sort is a common algorithm to start teaching sorting algorithms with, however it isn't the fastest. The further you go into data structures and algorithms, the more you'll learn about the different algorithms. It's a very interesting topic, and is very useful for programmers to learn about.

 

Pretty much every language has common algorithms, like sort, available in its library and they tend to be well optimized. They don't all use the same algorithms, although they all tend to be based around the fastest options.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×