Rock 'n' Roll Problems

neale

Neale

Posted on January 12, 2022

Rock 'n' Roll Problems

No, not drugs, alcohol or "the man", but some classic programming brain-teasers with a musical twist.

First up, can you feel Nirvana's Love Buzz? Write the code that returns "Love" if the input is divisible by 3, "Buzz" if it is divisible by 5, "Love Buzz" if it is divisible by both. Otherwise return the input.

public class LoveBuzz {
    public static String loveBuzz(int a) {
        // Your code
        return "";
    }
}
Enter fullscreen mode Exit fullscreen mode
class LoveBuzzTest {
    @Test
    public void loveBuzz() {
        assertEquals("Love", LoveBuzz.loveBuzz(3));
        assertEquals("Buzz", LoveBuzz.loveBuzz(5));
        assertEquals("Love Buzz", LoveBuzz.loveBuzz(15));
        assertEquals("2", LoveBuzz.loveBuzz(2));
    }
}
Enter fullscreen mode Exit fullscreen mode

Next, find which of these songs are so devilishly rock 'n' roll you could play the record backwards and it would still make sense. Write the code to determine the palindromes. Ignore letter case and special characters.

public class Palindrome {
    public static boolean isPalindrome(String song) {
        // Your code
       return false;
    }
}
Enter fullscreen mode Exit fullscreen mode
class PalindromeTest {
    @Test
    public void testPalindrome() {
        assertEquals(true, Palindrome.isPalindrome("TNT"));
        assertEquals(true, Palindrome.isPalindrome("Live Evil"));
        assertEquals(false, Palindrome.isPalindrome("I Palindrome I"));
        assertEquals(false, Palindrome.isPalindrome("Hey Hey, My My"));
        assertEquals(true, Palindrome.isPalindrome("A Man, a Plan, a Canal, Panama"));
    }
}
Enter fullscreen mode Exit fullscreen mode

Next, In the End it does even matter, thank you very much Linkin Park. Write the code to determine whether these Linkin Park songs share the same 3-character ending.

public class InTheEnd {
    public static boolean checkEnding(String song1, String song2) {
        // Your code
        return false;
    }
}
Enter fullscreen mode Exit fullscreen mode
class InTheEndTest {
    @Test
    public void testCheckEnding() {
        assertEquals(true, InTheEnd.checkEnding("Halfway Right", "One More Light"));
        assertEquals(true, InTheEnd.checkEnding("Pushing Me Away", "Runaway"));
        assertEquals(false, InTheEnd.checkEnding("Shadow of the Day", "The Little Things Give You Away"));
        assertEquals(false, InTheEnd.checkEnding("Iridescent", "Crawling"));
        assertEquals(true, InTheEnd.checkEnding("Until It's Gone", "What I've Done"));
    }
}
Enter fullscreen mode Exit fullscreen mode

Next, these backwards-inspired songs have found themselves, well, back-to-front. Write the code to reverse them.

public class Reverse {
    public static String reverseSong(String song) {
        // Your code
        return song;
    }
}
Enter fullscreen mode Exit fullscreen mode
class ReverseTest {
    @Test
    public void testReverseSong() {
        assertEquals("Erase/Rewind", Reverse.reverseSong("dniweR/esarE"));
        assertEquals("Sideways in Reverse", Reverse.reverseSong("esreveR ni syawediS"));
        assertEquals("Robin Hood in Reverse", Reverse.reverseSong("esreveR ni dooH niboR"));
        assertEquals("Rewind the Film", Reverse.reverseSong("mliF eht dniweR"));
        assertEquals("Rearviewmirror", Reverse.reverseSong("rorrimweivraeR"));
    }
}
Enter fullscreen mode Exit fullscreen mode

Finally, TwoSum41. Write the code to return the two indices that add up to punk rock's favourite sum, 41. Otherwise return an empty array.

public class TwoSum41 {
    public static int[] sum(int[] arr) {
        // Your code
        return arr;
    }
}
Enter fullscreen mode Exit fullscreen mode
class TwoSum41Test {
    @Test
    public void testSum() {
        assertArrayEquals(new int[] {30,11}, TwoSum41.sum(new int[]{12,30,33,11,10}));
        assertArrayEquals(new int[] {}, TwoSum41.sum(new int[]{16,7,14,1,30}));
        assertArrayEquals(new int[] {}, TwoSum41.sum(new int[]{35,24,9,11,1}));
        assertArrayEquals(new int[] {36,5}, TwoSum41.sum(new int[]{36,4,5,20,10}));
        assertArrayEquals(new int[] {11,30}, TwoSum41.sum(new int[]{40,11,2,30,12}));
    }
}
Enter fullscreen mode Exit fullscreen mode

If you'd like to download any of the code, you can find these challenges on my GitHub

💖 💪 🙅 🚩
neale
Neale

Posted on January 12, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Rock 'n' Roll Problems
java Rock 'n' Roll Problems

January 12, 2022