+ 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!

24th Oct 2017, 5:31 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
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!
25th Oct 2017, 3:21 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 38
congo for challenge of the day @vengat ☺ ⬜🌹🌹🌹🌹🌹⬜ 🌹⬜⬜⬜⬜⬜⬜ 🌹⬜⬜⬜⬜⬜⬜ 🌹⬜⬜⬜🌹🌹🌹 🌹⬜⬜⬜⬜⬜🌹 🌹⬜⬜⬜⬜⬜🌹 ⬜🌹🌹🌹🌹🌹⬜ //i'll give it a try soon ☺ //v. busy
25th Oct 2017, 3:24 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 24
I didn't get your requirements at all
25th Oct 2017, 3:24 PM
Bohdan Sakovych
Bohdan Sakovych - avatar
+ 22
I don't understand Я не понял 🤔🤔🤔
25th Oct 2017, 3:35 PM
<mr/>
<mr/> - avatar
24th Oct 2017, 10:38 AM
Kartikey Sahu
Kartikey Sahu - avatar
+ 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
25th Oct 2017, 7:50 PM
David Akhihiero
David Akhihiero - avatar
+ 10
Congrats for COTD 👍 Here's my try : https://code.sololearn.com/c5SHhnm0awVB/?ref=app
26th Oct 2017, 6:41 AM
LukArToDo
LukArToDo - avatar
+ 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
26th Oct 2017, 6:44 AM
Oma Falk
Oma Falk - avatar
+ 7
Yes absolute difference. If multiple, either retuen all or (recommended) return any one of them
24th Oct 2017, 8:32 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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
25th Oct 2017, 3:27 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 7
♻♠💻💻🎊 Thank you everyone for making SoloLearn this interesting. Enjoy!
25th Oct 2017, 3:48 PM
Kiprono Larry
Kiprono Larry - avatar
+ 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
26th Oct 2017, 1:15 PM
Andrei Zhulid
+ 6
@andrej deserves every click!
26th Oct 2017, 1:41 PM
Oma Falk
Oma Falk - avatar
+ 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!
26th Oct 2017, 1:52 PM
Oma Falk
Oma Falk - avatar
26th Oct 2017, 3:22 PM
Oma Falk
Oma Falk - avatar
+ 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.
30th Oct 2017, 12:20 PM
Oma Falk
Oma Falk - avatar
24th Oct 2017, 10:16 PM
Python
Python - avatar
+ 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
25th Oct 2017, 3:39 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
Here's my try with JS. Solved, right? :) https://code.sololearn.com/WSw7Tb273t1M/?ref=app
25th Oct 2017, 5:51 PM
Power'f GOD⚡⚡
Power'f GOD⚡⚡ - avatar
+ 5
@morpheus : a man like you ... level12(!), cool as ice cant be stopped by this babyface. I am right ..am I?
26th Oct 2017, 7:09 AM
Oma Falk
Oma Falk - avatar