-2

So I need to find the smallest number for each row in this file which contains these numbers:

6,3,5
4,4,8
3,7,2
1,8,9
9,0,6

How would I be able to sort through each row to find the smallest number without using the built in min() or importing anything? This is what I have so far:

for line in input_file:
    lines = line.split(' ')
    lines = line.replace('\n',"").replace(',',"")

    for i in range(len(lines)):
        total = int(lines[i])

        print total
        if total > maximum:
            maximum = total
            print 'hey', maximum
print 'HI', maximum
HEY
  • 23
  • 6

2 Answers2

2

I don't use python, but here is general aproach:

for line in input_file:
    numbers = line.replace('\n',"").split(',')

    min = int(numbers[0])
    for num in numbers:
        n = int(num)
        if n < min:
            min = n
    print min

this will print minimal value for each row

Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
  • 1
    It's a bit more Pythonic (and readable) to just say `for num in numbers` rather than use the `range(len())` formulation and index them. If you don't need the indices, it's preferable to iterate over the objects directly. – Two-Bit Alchemist Nov 12 '15 at 22:39
1

Python Function

Python has a min() function built in:

min([7,5,3,4])

=> 3

Sort

This isn't recommended, but you could sort a list and then pick the first number. This is more computationally intensive, but doesn't use the min() function, if you want this for some reason. But really, the only reason you should use this is if you get a assignment for a class or something.

list = [7,5,3,4]
list.sort()
list[0] #=> 3

Yourself

If you need to implement this yourself, the following should work:

for line in input_file:
    numbers = [int(i) for i in line.replace('\n',"").split(',')]
    min = numbers[0]
    for num in numbers:
        if num < min:
            min = num
    print min
Ben Aubin
  • 5,542
  • 2
  • 34
  • 54
  • Eh, the list comprehension is not very readable here. It would be cleaner to leave the `numbers = line.replace('\n', '').split(',')` and iterate `for num in map(int, numbers):`. Alternately (stylistically less preferable IMO but w/e), `numbers = map(int, line.replace('\n', '').split(',')`. – Two-Bit Alchemist Nov 12 '15 at 22:45
  • @HEY, if an answer worked for you, click that check mark (Lashane did it in a bit of a different way, if you like that one better). This helps other people who stumble upon this question know which is best. – Ben Aubin Nov 12 '15 at 22:46
  • @Two-Bit Alchemist: I'm a big believer in using as few lines as possible - not sure why :D – Ben Aubin Nov 12 '15 at 22:47
  • @penne12 On Python 3 at least, `for num in map(int, numbers)` is both more efficient than your code an an exactly equal number of [lines](https://en.wikipedia.org/wiki/Source_lines_of_code). You're hardcoding two iterations of each line. Probably not _that_ big of a deal (how many numbers are on each line really?) but still technically doubling the work. – Two-Bit Alchemist Nov 12 '15 at 22:49
  • @Two-BitAlchemist That makes sense. – Ben Aubin Nov 12 '15 at 22:51