+ 91
[ASSIGNMENT] Range number
A list has the property of having a "range number". A range number is calculated by finding the maximum possible total difference between any order of consecutive terms. Write a function rangenum(N) where N is a list. Test cases: rangenum([2,3,2]) >>> 2 ([2,3,2] is the case) rangenum([1,2,3]) >>> 3 ([1,3,2] is the arrangement) New test cases: rangenum([8,1,7]) >>> 13 ([8,1,7]) Good luck! Bonus: give the permutation of the list that returns its range number Thanks for Challenge of the Day! My first ever!
101 Answers
+ 40
My handpicked solutions per language:
https://code.sololearn.com/cLXlAL7sU5w0/?ref=app (ruby)
https://code.sololearn.com/ciTHGNXG7fCB/?ref=apphttps://code.sololearn.com/ciTHGNXG7fCB/?ref=app (python)
https://code.sololearn.com/cc2nKA6NKu9r/?ref=app (C++)
https://code.sololearn.com/c7lsmtE2dxAv/?ref=app (Java)
https://code.sololearn.com/cvDtzQpLd9k3/?ref=app (C#)
Other languages are welcome 😊😊
In simple English:
So basically for any array:
-take the difference of 2 consecutive elements.
-sum up such differences
Different permutations of the array produce different such numbers, hence find the maximum of such numbers
Hope you understand the challenge!
+ 38
congo for challenge of the day @vengat ☺
⬜🌹🌹🌹🌹🌹⬜
🌹⬜⬜⬜⬜⬜⬜
🌹⬜⬜⬜⬜⬜⬜
🌹⬜⬜⬜🌹🌹🌹
🌹⬜⬜⬜⬜⬜🌹
🌹⬜⬜⬜⬜⬜🌹
⬜🌹🌹🌹🌹🌹⬜
//i'll give it a try soon ☺ //v. busy
+ 24
I didn't get your requirements at all
+ 22
I don't understand
Я не понял
🤔🤔🤔
+ 16
https://code.sololearn.com/ciTHGNXG7fCB/?ref=app
Works as mentioned
+ 10
Pegasus my try, will add permutation later by God's grace
EDIT: Permutation added, It works but it is inefficient
https://code.sololearn.com/c576UXBQ4FXr/?ref=app
+ 10
Congrats for COTD 👍
Here's my try :
https://code.sololearn.com/c5SHhnm0awVB/?ref=app
+ 8
Heja... I found a clear rule to build the optimal sequence:
a) sort sequence
b) first number beginning,first number end, 2nd number begunning, 2nd number end...
Here my python solution:
https://code.sololearn.com/c4Rd5c0cKM0q/?ref=app
+ 7
Yes absolute difference.
If multiple, either retuen all or (recommended) return any one of them
+ 7
@Tommy take the difference of consecutive elements and sum them up. Find the maximum possible number that can be formed by any permutation of that list this way
+ 7
♻♠💻💻🎊 Thank you everyone for making SoloLearn this interesting. Enjoy!
+ 6
Reviewing different solutions I discovered that many of them are incorrect. E.g. for set of numbers [4, 2, 3, 3] many solutions answer 3, but the correct answer is 4 (sequence -> 3, 4, 2, 3). So, I decided to write my solution. ))
Enjoy!
https://code.sololearn.com/cvDtzQpLd9k3/#cs
+ 6
@andrej deserves every click!
+ 6
i think not. the idea of most algorithms is good but not complete.
the problems I see are identical values in the middle.
a sequence with two identical consecutive numbers cant be the ptimum..
one of them must be rearranged to one end.
@Andrej.. it is your turn!
+ 6
Hello Ale,
what you say is correct. It was more a mathematical challenge.
But at least on sololearn the correctness of tha algorithm was not the main point.
Many answeres ( also my first attempt) had a suboptimal solution.
But if that was implemeted correctly for me it was ok.
Very few learners found a better algorithm (w.o proof if it is right).
So we additionally had a mathematical discussion.
This challenge very clearly shew:
programming knowledge is one thing - algorithm knowledge a completely other one.
On sololearn the stress is on first one.
So the mathematical part even now is not completely solved.
For beginners a wake-up-call, for Math-Geeks (I am not) a fun.
+ 5
So basically for any array:
-take the difference of 2 consecutive elements.
-sum up such differences
Different permutations of the array produce different such numbers, hence find the maximum of such numbers
+ 5
Here's my try with JS. Solved, right? :)
https://code.sololearn.com/WSw7Tb273t1M/?ref=app
+ 5
@morpheus :
a man like you ... level12(!),
cool as ice
cant be stopped by this babyface.
I am right ..am I?