1
\$\begingroup\$

This is the first time I tried to solve a problem and wrote code for it, rather than learning some code and trying out to see what I might be able to do with it. As such, it's not much, but I'd like any tips and help about how I wrote this. Basically, I tried to write a program to find the biggest number in a list of numbers.

def main():
    print("Enter a series of numbers separated only by spaces:", end='\t')
    n = input().split()
    n = [int(i) for i in n]
    while len(n) != 1:
        if n[0] > n[1]:
            n.remove(n[1])
        else:
            n.remove(n[0])
    print("the biggest number is: ", n[0])
main()

Feedback? Did I do anything kinda noobish?

\$\endgroup\$

migrated from stackoverflow.com May 23 '14 at 18:14

This question came from our site for professional and enthusiast programmers.

3
\$\begingroup\$

The most obvious thing I can see is that you wrote a loop when one was not needed.

largest = max(n)

Also, check your boundary conditions. What happens if no numbers are entered?

\$\endgroup\$
  • \$\begingroup\$ hm. I had not learned the max function yet. Are loops something to be avoided whenever you can? I considered lots of if statement, but the downside was the you couldn't enter as many items as you wanted to and it'd contain a lot of nesting. \$\endgroup\$ – user3471004 May 21 '14 at 3:54
  • \$\begingroup\$ Loops are a common source of program errors. You won't be able to avoid them entirely, but definitely look for places where you don't need to write a loop yourself. Python has quite a number of features that help with this; you already found list comprehensions which is great. The itertools standard module has all kinds of fun things in it. \$\endgroup\$ – Greg Hewgill May 21 '14 at 3:56
  • \$\begingroup\$ Besides being a source of program errors, for loops written in pure Python are typically slower than list comprehensions, and functions implemented in C, like max and those provided by itertools. With large datasets this can make a big difference in performance. \$\endgroup\$ – dano May 21 '14 at 4:17
2
\$\begingroup\$

Your algorithm for finding the maximum value is rather inefficient, as list.remove requires O(N) time to remove an item from near the start of a list of length N. That means that your overall algorithm (which needs to remove N-1 items from the list) will take O(N^2) time. A better algorithm needs only to look at each item once, comparing it to the largest item seen so far, and will take O(N) time.

The built in max function will do that for you, but if you wanted to implement it yourself anyway, you could do:

def my_max(lst):
    max_val = lst[0]       # initialize with the first value from the list
    for item in lst:       # loop over the items in the list
        if item > max_val: # compare current item with the maximum seen so far
            max_val = item # set new max
    return max_val         # return the max
\$\endgroup\$
  • \$\begingroup\$ Many python solutions are similar, in traditional programming we need this kind of code, for a thing, we do in the easiest way like swapping two variables. a, b = b, a \$\endgroup\$ – SergioAraujo Feb 8 '17 at 22:27
1
\$\begingroup\$

These lines:

n = input().split()
n = [int(i) for i in n]

can be replaced with:

n = list( map(int, input().split()) )

Also, n is not a great name for a list, it is generally used for numbers.

\$\endgroup\$
  • 1
    \$\begingroup\$ As the print() function is used, it can be inferred that this is Python 3, so wrapping list(map(...)) is needed here since we're using list.remove and list indexing. \$\endgroup\$ – SimonT May 21 '14 at 3:55