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

Binary search in a list of even than odd numbers - Python

Go down

Binary search in a list of even than odd numbers - Python

Post by Lucaci Andrei on Tue Feb 14, 2012 10:01 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
29
30
31
32
33
34
'''
Created on Feb 15, 2012

@author: sin
'''
n = int(raw_input("Insert range list number: "))
x = int(raw_input("Insert searched number: "))
a = range(0, n)
print "The raw list: \n", a
def prs(a):
s = []
c = []
for i in range(0, len(a)):
if a[i] % 2 == 0:
s.append(a[i])
else:
c.append(a[i])
s.extend(c)
return s

def bs(s, x):
p = 0; q = len(s) - 1
while 1:
if q < p:
return -1
m = (p + q) / 2
if s[m] < x:
p = m + 1
elif s[m] > x:
q = m - 1
else:
return m
print "The list divided into even and odd numbers: \n", prs(a)
print "Number", x, "is on position", bs(prs(a), x), "in the list."


If you want this code:
Code:
'''
Created on Feb 15, 2012

@author: sin
'''
n = int(raw_input("Insert range list number: "))
x = int(raw_input("Insert searched number: "))
a = range(0, n)
print "The raw list: \n", a
def prs(a):
    s = []
    c = []
    for i in range(0, len(a)):
        if a[i] % 2 == 0:
            s.append(a[i])
        else:
            c.append(a[i])
    s.extend(c)
    return s

def bs(s, x):
    p = 0; q = len(s) - 1
    while 1:
        if q < p:
            return -1
        m = (p + q) / 2
        if s[m] < x:
            p = m + 1
        elif s[m] > x:
            q = m - 1
        else:
            return m
print "The list divided into even and odd numbers: \n", prs(a)
print "Number", x, "is on position", bs(prs(a), x), "in the list."
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