1,105,546 Community Members

Max num in list file

Member Avatar
june.pierre.311
Newbie Poster
2 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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()
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

I do not see any recursion.

Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

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

'''
Member Avatar
june.pierre.311
Newbie Poster
2 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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()
Member Avatar
sneekula
Nearly a Posting Maven
2,496 posts since Oct 2006
Reputation Points: 917 [?]
Q&As Helped to Solve: 263 [?]
Skill Endorsements: 5 [?]
 
0
 

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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: