🔀 Shuffle a List in JavaScript and Python

You can build

👉 your own shuffled playlist like in Spotify
👉 a game of cards
👉 a lottery picker app
👉 and what not

How? 🧵 👇
1️⃣ What is Shuffle?

✪ Shuffle is like sorting where it reorders the original list of elements.

✪ But unlike sorting, shuffle doesn't define any specific rule how to order these elements. Output is always non-predictive.
2️⃣ How does Shuffle work?

✪ As output is non-predictive, hence there is some randomness.

✪ So the algorithm we will write need to have randomness included.
3️⃣ Shuffle in JavaScript

✪ Use Array.prototype.sort() which accepts a "comparator function"

✪ Comparator function takes any 2 elements from array and sorting order is defined by its output if positive or, negative or, 0.

See the shuffle technique 👇
✪ Math.random() returns a random floating point value between 0 and 1.

✪ So, Math.random() - 0.5 always returns a value in the range of -0.5 to 0.5
4️⃣ Shuffle in Python

Python in its "random" library provides this inbuilt function "shuffle" which in-place shuffles the list.
5️⃣ Final

There are many other shuffling techniques. E.g., "Fisher–Yates Shuffle Algorithm".

How do you prefer to implement shuffling in your application? Reply in the comments.

Share your feedbacks as well. Till we see again, 👋
And, if you are a React developer or aspiring to be, check the 👇 🧵

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Swapna Kumar Panda ✨

Swapna Kumar Panda ✨ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @swapnakpanda

1 Nov
🔥 Data Structures you should be knowing as a Web Developer

👉 HTML + CSS + JavaScript : 😢🥺
👉 + Data Structures : 💯 ✅ 💪

🧵 👇
We will be discussing about

👉 data structures
👉 their example use case in UI
👉 what operations to know

Data structures are,

1️⃣ Array
2️⃣ Set
3️⃣ Stack
4️⃣ Queue
5️⃣ Hash Table
6️⃣ Tree

✪ ✪ ✪
1️⃣ Array

Without an Array, no web development is possible. List of items are stored inside Array.

So you should be knowing

✪ Creating an Array
✪ Iterating
✪ Find an Element
✪ Insert Element(s)
✪ Delete Element(s)
✪ Filter an Array
✪ Fetch a Sub-Array
✪ Merging Arrays
Read 10 tweets
1 Nov
React Interview Questions

Series: 1️⃣
Level: Beginner
Topics:

1️⃣ Introduction
2️⃣ JSX
3️⃣ Virtual DOM
4️⃣ Component
5️⃣ States and Props
6️⃣ Data Binding
7️⃣ Component Rendering
8️⃣ Hooks Introduction
1️⃣ Introduction

✪ What is React?
✪ What is the current version of React?
✪ What are core features of React?
✪ What are advantages using React?
✪ Where shouldn't we use React?
✪ What is SPA?
✪ When will a SPA run slower/faster?
Read 12 tweets
31 Oct
💡 Why is below expression in Python evaluated to "True"?

→→ not False == True is not True == True ←←

Originally Asked By: @DataIlm

I will try to answer this one in the easiest possible way.

🧵 👇
2 important things we need to understand while solving this one.

1️⃣ Operator Precedence
2️⃣ Operator Associativity
1️⃣ Operator Precedence

✪ Logical 'not' operator has "lower precedence" than Comparison Operators (here '==') and Identity Operators (here 'is not').

✪ Comparison Operators and Identity Operators have same precedence.
Read 12 tweets
30 Oct
JavaScript Interview Questions

Series: 1️⃣
Level: Beginner
Topics:

1️⃣ Data Types
2️⃣ Boolean
3️⃣ Number
4️⃣ Logical Operators
5️⃣ Comparison Operators
6️⃣ Arithmetic Operators
1️⃣ Data Types

✪ What all data types does JavaScript provide?
✪ How to find out data type of a value? (which operator to use)
✪ What is type coercion?
✪ Any significant difference between "undefined" and "null"?
✪ What is the type of undefined?
✪ What is the type of null?
Read 14 tweets
30 Oct
🎉 🎊 Reached 2500 in this bird app 🥳

Thanks for all the 💙 and support.

👋 I am Swapna from India 🇮🇳

I create contents on 👇

🦕 DSA
🕸 JavaScript Concepts
🐍 Python Concepts
💻 Programming Tips

👇 are some awesome people you should consider to follow and support.
1️⃣ PS / DSA
@intelocode (Math/Psychology/Mentoring as well)
@Franc0Fernand0
@imdivi_jain
@sat_yam_d
@marioarrioladev
@DanielLeskosky
@akshayvarshney6

2️⃣ System Design
@manthanguptaa
@Franc0Fernand0
@BelloneDavide (.NET as well)
Read 17 tweets
29 Oct
🪜 Introduction to GraphQL

"FAANG might have changed to MANGA, but GraphQL is still GraphQL" 😉

🧵 👇
1️⃣ What is GraphQL?

GraphQL is

→ a query language for your API (explained in 6️⃣)

→ a server-side runtime for executing queries using a type system you define for your data (explained in 7️⃣)
2️⃣ What is an API?

✪ API stands for "Application Programming Interface".

✪ You have an application. Your application has several data. Users of your application want to fetch those data. How would they be able to do that?
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(