Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fix Error and clenup code
#4
I suggest this
import bisect
tab_list = [0, 4, 11, 18, 26, 33, 40, 47, 54, 62, 69, 77, 84, 92, 99, 107,
            114, 122, 130, 138, 146, 154, 163, 171, 180, 189, 198, 207, 216,
            226, 236, 246, 257, 268, 279, 291, 303, 316, 329, 345, 358, 375,
            392, 412, 433, 457, 485, 518, 560, 620, 735]


def get_rst(value):
    """computes the largest number in tab_list <= value. Works if tab_list[0] <= value"""
    return tab_list[bisect.bisect_right(tab_list, value) - 1]
 

# test this
for i in range(20):
    print(i, get_rst(i))
Output:
0 0 1 0 2 0 3 0 4 4 5 4 6 4 7 4 8 4 9 4 10 4 11 11 12 11 13 11 14 11 15 11 16 11 17 11 18 18 19 18
Reply


Messages In This Thread
Fix Error and clenup code - by zinho - Dec-19-2018, 09:40 PM
RE: Fix Error and clenup code - by Gribouillis - Dec-19-2018, 10:24 PM
RE: Fix Error and clenup code - by zinho - Dec-19-2018, 11:13 PM
RE: Fix Error and clenup code - by Gribouillis - Dec-20-2018, 12:29 AM
RE: Fix Error and clenup code - by Gribouillis - Dec-20-2018, 12:31 AM
RE: Fix Error and clenup code - by zinho - Dec-20-2018, 01:14 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020