We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,453 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Max num in list file

I'm writting a program that will take a file as input and go through the list of numbers in the file and return the max number uing recusions. I'm not very good with recursions here is what I have so far. Also I'm not allowed to use the max command.

def Mymax(lists):
    if (len(lists[1:])>0):
        return 


def main():
    fname = input("Enter filename: ")
    infile = open(fname, "r")
    data = infile.read()

    for line in data.splitlines():
        print(eval(line))

main()
5
Contributors
5
Replies
1 Day
Discussion Span
6 Months Ago
Last Updated
6
Views
june.pierre.311
Newbie Poster
2 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I do not see any recursion.

pyTony
pyMod
Moderator
6,330 posts since Apr 2010
Reputation Points: 879
Solved Threads: 989
Skill Endorsements: 27

Recursion calls the function from within the function with an adjusted parameter.
Example:

def remove_char_right(mystr, num, count=0):
    """
    remove num of characters to the right of a string
    """
    print("{} {}".format(count, mystr))  # test
    # exit condition, return the final result
    if count >= num:
        return mystr
    mystr = mystr[:-1]
    # needs return
    # function calls itself
    # notice the adjusted parameters mystr and count
    return remove_char_right(mystr, num, count+1)

# test it
mystr = 'parameter'
num = 5
newstr = remove_char_right(mystr, num)
print('-'*15)
print(newstr)

''' output -->

0 parameter
1 paramete
2 paramet
3 parame
4 param
5 para
---------------
para

'''
Lardmeister
Posting Virtuoso
1,940 posts since Mar 2007
Reputation Points: 465
Solved Threads: 73
Skill Endorsements: 5

Here the update to the code but it's still not giving me the maximum number in the lines for the code. I'm trying to pull the numbers out of the list one by one and then to compare them to the rest of the remaining numbers but I must be doing something out of order. In the end my list should be empty.

def numlist(s):
    first = (s[0:1])
    rest = (s[1:2])
    if first >= rest:
        return first
    else:
        return line.remove(s[0:1])

def main():
    fname = input("Enter filename: ")
    infile = open(fname, "r")
    data = infile.read()

    for line in data.splitlines():
        print(eval(line))
        numlist(line)
        print("The maximum number is: ", line)
main()
june.pierre.311
Newbie Poster
2 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Put in a few test print() to catch the many errors.

sneekula
Nearly a Posting Maven
2,483 posts since Oct 2006
Reputation Points: 1,000
Solved Threads: 231
Skill Endorsements: 2

Notice that a while loop mimics a recursive function rather closely. It should give you hints how to set it up ...

''' maxnum1.py
find the highest number in a list
not using Python's max(list) function
'''

mylist = [1, 2, 3, 4, 5 ,6, 7, 3, 5]

maxnum = 0
index = 0
while True:
    num = mylist[index]
    # update maxnum to be the highest number
    if num > maxnum:
        maxnum = num
    # go to next index
    index += 1
    # exit condition
    if index >= len(mylist):
        break

print(maxnum)  # 7
# test
print(max(mylist))  # 7

Actually, while loops are faster then the high overhead recursive functions.

vegaseat
DaniWeb's Hypocrite
Moderator
6,499 posts since Oct 2004
Reputation Points: 1,451
Solved Threads: 1,618
Skill Endorsements: 37

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1244 seconds using 2.67MB