Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,160,947 members, 7,845,045 topics. Date: Thursday, 30 May 2024 at 11:42 AM |
Nairaland Forum / Science/Technology / Programming / Write An Algorithm To Shuffle An Array (1298 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)
Re: Write An Algorithm To Shuffle An Array by Deicide: 12:48pm On Jul 23, 2022 |
namikaze:That's the thing about programming if your solution is not optimised you would know. So there is always room for growth till you move to the next thing. The randrange you used in python should also depend on random but it's abstracted though am not sure of this. |
Re: Write An Algorithm To Shuffle An Array by chukwuebuka65(m): 2:46pm On Jul 23, 2022 |
function shuffle( arr ) { Const seenArray = [ ] Const shuffledArray = [ ] for ( i=0,i<arr.length-1,i++) { let randomIndex = math.floor( math.random( ) *arr.length) If (!seenArray.include(randomIndex)) { shuffledArray [ randomIndex] = arr[i] seenArray[i] = randomIndex } else { for ( k=0,k<arr.length,k++) { If ( shuffledArray[k] == null) { shuffledArray[k] = arr[i] break; } } } } Return shuffledArray; } |
Re: Write An Algorithm To Shuffle An Array by chukwuebuka65(m): 2:50pm On Jul 23, 2022 |
namikaze: I think there is a chance this might go into infinite loop |
Re: Write An Algorithm To Shuffle An Array by qtguru(m): 3:18pm On Jul 23, 2022 |
Deicide: Everybody that has written it has sworn by it, but I don't know what Project to use it for, what do you use it for or just learning, is it straight forward ? 1 Like |
Re: Write An Algorithm To Shuffle An Array by Vicky2k(m): 3:52pm On Jul 23, 2022 |
chukwuebuka65:js things point. can someone implement this in c or typescript |
Re: Write An Algorithm To Shuffle An Array by Deicide: 4:07pm On Jul 23, 2022 |
qtguru:You can actually use it for anything. To be honest I was left with go or rust which to use for Sys admin stuff and I just picked rust all I use it for now is to write some CMD tool to solve admin task. And since it's cross platform it's also a plus though I still use go sometimes for that. Though I might no be using the full power but it suites my use case just fine. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 5:57pm On Jul 23, 2022 |
Deicide:No it's not like that. In my case, any value returned by the random() function would be used. In your case any value wouldn't do, it would have to return a new value not inside of the HashSet. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 6:00pm On Jul 23, 2022 |
chukwuebuka65:I know, this is not my solution, I was trying to remove the recursion from op's solution, mine's on the first page of the thread. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 6:20pm On Jul 23, 2022 |
chukwuebuka65:God of bugs In all seriousness lots of syntax errors and even after fixing the errors does not give the desired output. I bet you typed it on nairaland. |
Re: Write An Algorithm To Shuffle An Array by Deicide: 6:27pm On Jul 23, 2022 |
namikaze:He should have used typescript Seun need to fix code formatting on Nairaland even if it's only for this section, I mean see Quora see Reddit. 2 Likes |
Re: Write An Algorithm To Shuffle An Array by chukwuebuka65(m): 6:43pm On Jul 23, 2022 |
namikaze: Yea, but I think u didnt get the concept. If you get the concept u can fix it. |
Re: Write An Algorithm To Shuffle An Array by Deicide: 7:57pm On Jul 23, 2022 |
Re: Write An Algorithm To Shuffle An Array by Xedmark: 7:58pm On Jul 23, 2022 |
Deicide: is this what you call array? |
Re: Write An Algorithm To Shuffle An Array by Deicide: 8:08pm On Jul 23, 2022 |
namikaze:from random import randrange Using your python logic in rust, code translate to
This is fucking fast, cause it's using multiple thread and random results are cached Now I can finally rest and watch the sun set on a great universe |
Re: Write An Algorithm To Shuffle An Array by Deicide: 8:10pm On Jul 23, 2022 |
Xedmark:Yes, vectors are dynamic array |
Re: Write An Algorithm To Shuffle An Array by Nobody: 9:20pm On Jul 23, 2022 |
chukwuebuka65:Your code still has bugs after testing... it will be impressive to know u wrote it here on nairaland even before testing... sometimes it prints out empty in the array Nairaland reduced the quality of the image
|
Re: Write An Algorithm To Shuffle An Array by namikaze: 9:45pm On Jul 23, 2022 |
chukwuebuka65:What concept? unless you want me to debug your code lol. |
Re: Write An Algorithm To Shuffle An Array by namikaze: 9:48pm On Jul 23, 2022 |
Deicide:I see you rustecean, flexing Rust features anyhow You can't rest o, it's night here already |
Re: Write An Algorithm To Shuffle An Array by Nobody: 10:11pm On Jul 23, 2022 |
LET ME CONTRIBUTE WITH JS explanation. I am using while loop. while the length of shuffled array is not equal to given array the loop keeps running. now each time loops run a random value from given array is produced. now we push that value to the shuffled array provided it doesn't exist there previously, This way we get unique random values in shuffled array. code can be shortened further with ternary operators... for readability lets go with IFS i know it's neat. function shuffle(arr) { const shuffled = [] while(shuffled.length!==arr.length){ let randVal = Math.floor(Math.random()*arr.length) if (!shuffled.includes(arr[randVal])) { shuffled.push(arr[randVal]); } if (shuffled.length===arr.length) return shuffled } } shuffle(["a", "b", "c", "d", "e","g","h","i"]); 1 Like |
Re: Write An Algorithm To Shuffle An Array by Csami(m): 10:51pm On Jul 23, 2022 |
chukwuebuka65: This code is full of bed bugs and caterpillars Too many syntax errors ranging from ";" to "mixture of capital and small letters " const number = [23. 474, 74, 37, 27] function shuffleArray(array) { return array.sort( () => Math.random() - 0.5 ); } console.log(shuffleArray(number)) |
Re: Write An Algorithm To Shuffle An Array by Csami(m): 10:54pm On Jul 23, 2022 |
GREATIGBOMAN: me likey but it's too much code |
Re: Write An Algorithm To Shuffle An Array by Nobody: 10:59pm On Jul 23, 2022 |
Re: Write An Algorithm To Shuffle An Array by Nobody: 11:53pm On Jul 23, 2022 |
Csami: Nice.... but your solution is quite popular on google |
Re: Write An Algorithm To Shuffle An Array by chukwuebuka65(m): 1:44pm On Jul 24, 2022 |
Csami: I know because I wrote it off-hand here on nairaland while at the market. But you should try to avoid abstractions in your algorithms where necessary so we can determine the time and space complexity . You should have used array.sort(). |
I Made This Quadratic Solver With Python / Help Wit Project 'intelligent Data Analysis And Decision Support Systems' / Hardware Programming Using PIC PROGRAMMING
(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 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 41 |