- 3

Experts want your help to Optimize code:

def foo(start, end):    index = 0    is_lower = False    while index < (len(start)):        if is_lower and start[index] == '0':            break        if not is_lower and start[index] < end[index]:            first_lower = index            is_lower = True        index += 1    return index-1, first_lower start = '37282842000' end = '37282871999' result = [] while int(start) < int(end):    index, first_lower = foo(start, end)    range_end = index > first_lower and 10 or int(end[first_lower])    for x in range(int(start[index]), range_end):        result.append(start[:index] + str(x) )    if range_end == 10:        start = str(int(start[:index])+1)+'0'+start[index+1:]    else:        start = start[:index] + str(range_end) + start[index+1:] print (result) ~~~~ How to modify above code to give below output: 37282842 37282843 37282844 37282845 37282846 37282847 37282848 37282849 3728285 3728286 37282870 37282871

7th Aug 2021, 6:24 PM
Mohammad Jun
7 Antworten
0
That is the most unPythonic Python code I have ever seen. It's very hard to read...and decipher what it is supposed to do. Let''s see Input a start and end number, the start cannot be 0 or start with 0? the end should be bigger than the start. it will keep the first 8 digits? it will give a list of result with items incremented by 1, then by 10 until it less than the end number, then display the end 8 digit number... or I'm totally reading this wrong. It's C written in Python to me.. I'm sure you are a very experiened programmer to be able to write something this complex.
16th Aug 2021, 12:06 PM
Bob_Li
Bob_Li - avatar
0
I copied your code and it gives basically the same result, but continues on to 37282871778. So what is it supposed to be doing wrong? you want it to end early at 37282871? It would be nice if you could provide more info..
16th Aug 2021, 12:44 PM
Bob_Li
Bob_Li - avatar
0
There are some quirks in your code, don't know if it is intentional or not. Trying it with small numbers start = '0' end='10' gives an error because firs-lower is not assigned a value. So it seems like the number of digits in the start and end should be the same. start ='01 start ='10' gives a result of['01', '02'...'09'] So that seems to confirm that. But it works with start = '01' end = '9' which gives a result of ['01', '02',...'09] which is strange and might mean a bug in your condition under the first while loop. And it also ends in 09, which is strange because it normally stops before reaching the end number... Very weird.... end = '20' result = ['1'] start ='10' end = '25' result = ['1', '20', '21', '22', '23', '24'] So what am I looking for?
16th Aug 2021, 1:07 PM
Bob_Li
Bob_Li - avatar
0
what is foo() doing? it seems to be messing things up. try this: start='01' end = '2' and the crazy result is ['01', '02'...''09'] if it is to make sure the length of the inputs are the same, it is doing a very bad job at it.
16th Aug 2021, 1:22 PM
Bob_Li
Bob_Li - avatar
0
foo is buggy. do a trial on it first. start with small numbers, various combinations and see the behavior of your code. do something like test = (('1', '10'), ('1','9'), ('01','9'),('01','10')....) and loop through them with: for i in test: foo(*i) and see where it fails
16th Aug 2021, 1:31 PM
Bob_Li
Bob_Li - avatar
0
How about this. Your sample output is hard to understand (suddenly becoming smaller at the end, then getting bigger again.) I combined your code into a single function called baz(start, end). It returns a list. You use it like this: result = baz(start, end) print(result) it will print a list of the values. I added a missing variable and modified some grouping, corrected some conditional anomaly and did some testing. It seems more well behaved now, but it is not very stable and there are many ways to make it fail. I'm not sure if this is the result you want, since I'm not sure what this is supposed to do. But it seems to be working without errors now. https://code.sololearn.com/czE8Ht4PRPgR/?ref=app But this is surely not the way python code is meant to be written... Is the decrement by powers of 10 intentional? It makes the number become smaller and smaller after incrementing by 1. I did not alter it since I'm not sure if this is a feature or a bug.😁 I added a test case to show the effect.
16th Aug 2021, 3:26 PM
Bob_Li
Bob_Li - avatar
0
Here is something that was inspired by your post. https://code.sololearn.com/cyRfHNvrIEfj/?ref=app Maybe you wanted something like this?
17th Aug 2021, 9:05 AM
Bob_Li
Bob_Li - avatar