class
Rational:
def
__init__(
self
, a
=
0
, b
=
0
):
self
.p
=
a
self
.q
=
b
def
compare(a: Rational, b: Rational)
-
>
int
:
if
(a.p
*
b.q
=
=
a.q
*
b.p):
return
0
if
(a.p
*
b.q > a.q
*
b.p):
return
1
return
-
1
def
binarySearch(arr:
list
, l:
int
,
r:
int
, x: Rational)
-
>
int
:
if
(r >
=
l):
mid
=
l
+
(r
-
l)
/
/
2
if
(compare(arr[mid], x)
=
=
0
):
return
mid
if
(compare(arr[mid], x) >
0
):
return
binarySearch(arr, l, mid
-
1
, x)
return
binarySearch(arr, mid
+
1
, r, x)
return
-
1
if
__name__
=
=
"__main__"
:
arr
=
[ Rational(
1
,
5
), Rational(
2
,
3
),
Rational(
3
,
2
), Rational(
13
,
2
) ]
x
=
Rational(
3
,
2
)
n
=
len
(arr)
print
(
"Element found at index %d"
%
(
binarySearch(arr,
0
, n
-
1
, x)))