AndreiL
Welcome to AndreiL.
Enjoy your time.
Please log in.

Max number from a list div et imp method - Python

Go down

Max number from a list div et imp method - Python

Post by Lucaci Andrei on Mon Feb 13, 2012 9:37 pm

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
Created on Feb 14, 2012

@author: sin
'''
n = int(raw_input("Insert number: "))#the number of the range in which the max will be applied.
x = []#empty list
import random
for i in range(0, n):
x.append(random.randint(0, random.randint(0, random.randint(0, random.randint(0, n))))) #randoming as much as possible
print x#generating a random list
j = x[:]#making a copy of that list
j.sort(reverse=True)#sorting reversed so that the maximum number is at the first of the list, so that we can verify if the
#algorithm is working or not.
print j
def div_imp(p, q):#applying divide et impera method to solve this problem
if p == q:
return x[p] #if the max number is in the middle, then it will return that number
else:
mij = (p + q) / 2
max1 = div_imp(p, mij)
max2 = div_imp(mij + 1, q)
if max1 > max2:
return max1
return max2

print "The maximum number from the list is: ", div_imp(0, n - 1)
raw_input("Press any key to exit...")

avatar
Lucaci Andrei
"Tata Lor"


Number of messages : 222
Points : 2266743
Reputation : 1007
Registration date : 2008-08-15
Age : 25
Location : Cluj-Napoca

View user profile http://www.andreil.wgz.ro

Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum