Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,221,096 members, 8,047,687 topics. Date: Wednesday, 08 January 2025 at 02:55 PM |
Nairaland Forum / Science/Technology / Programming / Write An Algorithm To Shuffle An Array (1585 Views)
How Important Is Data Structures And Algorithm To Pro Developers / How To Empty An Array In Javascript / Is It Compulsory To Write An Algorithm Before Coding (2) (3) (4)
Write An Algorithm To Shuffle An Array by Deicide: 8:32am On Jul 23, 2022 |
Was able to come up with this using rust. static mut TEMP_V: Vec<i32> = Vec::new(); 1 Like |
Re: Write An Algorithm To Shuffle An Array by operAhdam: 9:11am On Jul 23, 2022 |
that's new,,why do you choose rust boss of all this other languages 1 Like |
Re: Write An Algorithm To Shuffle An Array by Deicide: 9:18am On Jul 23, 2022 |
operAhdam:Cause it's one of the best programming languages ever, honestly you should try it. |
Re: Write An Algorithm To Shuffle An Array by operAhdam: 9:24am On Jul 23, 2022 |
Deicide:e easy pass python |
Re: Write An Algorithm To Shuffle An Array by Nobody: 9:39am On Jul 23, 2022 |
Explain the question well, shuffle array as in how? |
Re: Write An Algorithm To Shuffle An Array by Deicide: 9:54am On Jul 23, 2022 |
Think4Myself:You never play WHAT before? How them day take shuffle cards. Let's say you have input [1,2,3] you can shuffle it to be [1, 2, 3] or [1, 3, 2] or [3, 1, 2] etc |
Re: Write An Algorithm To Shuffle An Array by Deicide: 9:54am On Jul 23, 2022 |
operAhdam:No but way more faster than python |
Re: Write An Algorithm To Shuffle An Array by Ikechukwu183: 10:12am On Jul 23, 2022 |
Deicide:As a C++ and Python programmer, I can tell you that this code is not 'readable' by human eyes... is this even a HLL? |
Re: Write An Algorithm To Shuffle An Array by Deicide: 10:15am On Jul 23, 2022 |
Ikechukwu183:That's because Nairaland code formatting is useless. |
Re: Write An Algorithm To Shuffle An Array by Nobody: 10:16am On Jul 23, 2022 |
Deicide: Ok |
Re: Write An Algorithm To Shuffle An Array by parkervero(m): 10:26am On Jul 23, 2022 |
As a Java dev I can't read the code The while loop is not in addendum to Java way of implementing while loop. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 10:32am On Jul 23, 2022 |
Deicide: from random import randrange Here's something pretty simple, time complexity should be O (n log n), since the time complexity of the randrange function is O (log n). It's being quite a while since I did anything in rust, I don't want to fight the borrow checker right now Rust used to be my favorite language but it's just too painful to do anything substantial with it, try implementing a doubly linkeldlist in Rust and you'll agree that C++ is easy compared to Rust. 3 Likes 1 Share |
Re: Write An Algorithm To Shuffle An Array by Ikechukwu183: 10:36am On Jul 23, 2022 |
namikaze:Brilliant onefrom random import randrange 1 Like |
Re: Write An Algorithm To Shuffle An Array by Deicide: 10:38am On Jul 23, 2022 |
namikaze:Nice onefrom random import randrange 1 Like |
Re: Write An Algorithm To Shuffle An Array by Deicide: 10:42am On Jul 23, 2022 |
parkervero:You just have to get used to it to love it |
Re: Write An Algorithm To Shuffle An Array by namikaze: 10:42am On Jul 23, 2022 |
Ikechukwu183:Python for the win |
Re: Write An Algorithm To Shuffle An Array by namikaze: 10:44am On Jul 23, 2022 |
Deicide:I modified my post, check it out. |
Re: Write An Algorithm To Shuffle An Array by Deicide: 10:53am On Jul 23, 2022 |
namikaze:Yeah I agree there are something that are Impossible in rust without using the unsafe key work like i probably did in my code to reduce time & space complexity. But I love the language it makes you as a programmer to be very careful with what you write. Can you guess the complexity of my code since lookup in a hashmap is O(1) so am thing O(n^2 + random(n)) I don't even know what that means |
Re: Write An Algorithm To Shuffle An Array by TastyFriedPussy: 11:08am On Jul 23, 2022 |
I'll like to see someone try this with JS 1 Like |
Re: Write An Algorithm To Shuffle An Array by Deicide: 11:22am On Jul 23, 2022 |
TastyFriedPussy:It'll be easy with Js |
Re: Write An Algorithm To Shuffle An Array by namikaze: 11:24am On Jul 23, 2022 |
Deicide:My guy even with the unsafe keyword it gets complex real quick, I know what I faced when I tried implementing a doubly linkedlist in Rust, it was crazy. Memory management in C/C++ is much simpler than in Rust. I agree though learning rust will make you better developer overall. Guy you're brave, using recursion inside a while loop, I hail thee loolz, well let me see if I can figure out the complexity... |
Re: Write An Algorithm To Shuffle An Array by namikaze: 11:29am On Jul 23, 2022 |
Deicide:Sorry, what's the role of the count parameter? |
Re: Write An Algorithm To Shuffle An Array by Deicide: 11:31am On Jul 23, 2022 |
namikaze:Even on reddit they would advise you not to implement a double linked list or even a Linked list in rust exexpt you wanna run mad But you can use Box<T> to make the running mad slower My code actually runs fast despite the recursion, it mostly just depends on if it gets the random number right. Best case would be O(n) but that's impossible but it can happen just that the chance are small especially if the input is large. I'll rewrite this code in C++ so I can test the complexity properly. The lazy_load for HasSet I did up there I don't have the crate for it |
Re: Write An Algorithm To Shuffle An Array by Deicide: 11:32am On Jul 23, 2022 |
namikaze:That's the base case of the recursion; if that's not there the code would run forever |
Re: Write An Algorithm To Shuffle An Array by namikaze: 11:37am On Jul 23, 2022 |
Deicide:Ok, say for instance I called the function on a vector, [1,2,3,4,5]; what would pass as argument for the count parameter? |
Re: Write An Algorithm To Shuffle An Array by Deicide: 11:43am On Jul 23, 2022 |
namikaze:like this shuffle([1,2,3,4,5], 5); 5 here is the length of the vector. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 11:44am On Jul 23, 2022 |
Deicide:I remember the Box<T> construct lol, and to think I was able to implement a linkedlist (single) in it I don't think I have the code anymore but I'll check my GitHub later. Ofcourse it would run fast, rust if fast af. I do think it's not that great to let your algorithm depend on "picking the right number" but I get you. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 11:46am On Jul 23, 2022 |
Deicide:Ok sure. |
Re: Write An Algorithm To Shuffle An Array by Deicide: 11:54am On Jul 23, 2022 |
namikaze:That's why this code wouldn't work if they we duplicate in the input array. Who knows maybe if I stroll out later the solution might come to my head |
Re: Write An Algorithm To Shuffle An Array by namikaze: 12:05pm On Jul 23, 2022 |
Deicide:You should definitely go for walk You used the hashset to reduce lookup from O(n) to O(1) didn't you? nice one. Also an instance where random() keeps returning an index to a value that's already in the hashset continuously till count gets to zero would yield undesired results. |
Re: Write An Algorithm To Shuffle An Array by Deicide: 12:15pm On Jul 23, 2022 |
namikaze: Yeah, Things I learnt on leetcode Yes the complexity is dependent on the random number. So it can be infinite ): so don't try on large dataset. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 12:34pm On Jul 23, 2022 |
Deicide:Another way to express your solution without the recursion would be; function shuffle(Array arr) This I think follows the same idea, but would still depend on the random function, so not recommended. |
Asm. / Can You Or Do You Know Someone That Can Copycat 2go Java Chatting Software. / Does Anyone Know Itex Integrated Services In VI
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2025 Oluwaseun Osewa. All rights reserved. See How To Advertise. 49 |