'Replace With Next Largest Number
I need to write a function that replaces each number with the next highest number in the list.
Say we have the list[5, 7, 3, 2, 8].
The 5 gets replaced by the 7 and the list becomes [7, 5, 3, 2, 8].
After that, we switch the 5 and the 8 so it becomes [7, 8, 3, 2, 5]. Eventually the max of the entire list gets replaced by -1.
By the end this example should output [7, 8, 5, 3, -1]. I wrote this code:
def replace_next_largest(lst):
maxes = []
for i in range(len(lst)):
check = lst[i]
next_mx = [i for i in lst if i > check and not i in maxes]
if next_mx:
maxes.append(next_mx[0])
else:
if maxes.insert(lst.index(max(lst)),-1 )
return maxes
but it does not work with the input [4, 1, 6, -7, -8, 2], but it works for the other list mentioned above and all the other tests. My output is [6, 4, -1, -1, 1, -7] and it should be [6, 2, -1, 1, -7, 4]
[6, 4, -1, -1, 1, -7] <--- My Answer
[6, 2, -1, 1, -7, 4] <--- correct answer
It works with [5, 7, 3, 2, 8] , [2, 3, 4, 5], and [1, 0, -1, 8, -72]
Solution 1:[1]
What the problem is
Hmmm... To be honest - I'm not sure. I can't really find any bad part of what you're posted.
Maybe it's a caching error.
Solution suggestions
You say that you don't have php artisan working. Hmm... How do you clear the cache then? The command: php artisan cache:clear
None the less... The first command that comes to mind is to try and clear the cache and generates composer-files, with this command:
php artisan cache:clear && composer dump-autoload
But if you can't do php artisan, then maybe just do: composer dump-autoload
Further debugging
If it was me, then I would start at the routes, changing this:
Route::get('/login','LoginController@index');
to this:
dd('hey');
Route::get('/login','LoginController@index');
And then reloading the page. You should then see it say 'Hey' on the screen.
... If that works, then change it to this:
Route::get('/login','LoginController@index');
dd('hey');
Then it _SHOULDN'T say 'Hey' on the screen.
... Then I would work my way through the controller as well.
And afterwards through the view.
To find the place where it deviates what you expect should happen and what really happens.
Fun fact:
I have tried once, where I did this - and moved the dd-statement aaaaaall the way through my code. And in the end, when I simply removed the dd-statement then the unexpected behaviour was gone. It must have been some caching or something.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zeth |
