Road to Great Code, Challenge #1 — Shuffle Playlist
Hone your craft with this Java coding challenge!
Join the DZone community and get the full member experience.
Join For FreeNo matter what your profession is, it’s important to hone your craft. Uncle Bob (Rob Martin) in his book titled The Clean Coder suggests that us programmers should regularly practice writing code and solve common problems. Once we get good at solving them, he goes as far as recommending that we program to the rhythm of the music. Not to mention that we shouldn’t only stick to one programming language that we’ve mastered. We should constantly expose ourselves to different languages to get a better understanding of the programming universe.
You may also like: Clean Code Principles
By doing this consistently, you'll be able to instantly make a decision about which programming language is the most suitable to solve a problem at hand.
Road to Great Code will, therefore, be a series of programming challenges to help you work on your craft by tackling a different programming challenge once a week.
The Problem
It’s October 2001 and Apple is releasing the first iPod, which will revolutionize the portable media player market. Your job is to implement the song shuffle feature. Using your favorite programming language (Java), you need to implement this feature.
Naturally, you could use the Collections.shuffle
method; however, that wouldn’t help us improve all that much, so let's implement the shuffle method from scratch.
Here’s the starting point that contains a library of sorted songs.
public static void main(String[] args) {
List<Song> library = new ArrayList<>() {
{
add(new Song("Basket Case"));
add(new Song("Blood On The Leaves"));
add(new Song("Creep"));
add(new Song("Cut Your Hair"));
add(new Song("Don't Look Back in Anger"));
add(new Song("Fields Of Gold"));
add(new Song("Juicy"));
add(new Song("Mysterious Ways"));
add(new Song("Rocket Man"));
add(new Song("Smells Like Teen Spirit"));
add(new Song("Video Games"));
add(new Song("Wannabe"));
}
};
myShuffle(library);
library.forEach(System.out::println);
}
private static void myShuffle(List<Song> songs) {
// type solution here
}
The Task
Your job is to implement the myShuffle
method and then print out the shuffled library.
A Solution
This is an example solution that I came up with. Compare it with your own and let me know your observations.
xxxxxxxxxx
private static void myShuffle(List<Song> songs) {
Random random = new Random();
for (int index = 0; index < songs.size(); index++) {
int secondIndex = random.nextInt(songs.size());
swap(index, secondIndex, songs);
}
}
private static void swap(int firstIndex, int secondIndex, List<Song> songs) {
System.out.println(String.format("Replacing %s with %s", firstIndex, secondIndex));
Song firstSong = songs.get(firstIndex);
Song secondSong = songs.get(secondIndex);
songs.set(secondIndex, firstSong);
songs.set(firstIndex, secondSong);
}
Further Reading
Clean Code Principles7 Tips to Write Better Java Code You Should Know
Clean Architecture Is ScreamingOpinions expressed by DZone contributors are their own.
Trending
-
Never Use Credentials in a CI/CD Pipeline Again
-
Scaling Site Reliability Engineering (SRE) Teams the Right Way
-
Design Patterns for Microservices: Ambassador, Anti-Corruption Layer, and Backends for Frontends
-
MLOps: Definition, Importance, and Implementation
Comments