Rock 'n' Roll Problems
Tags: java, beginners, 100daysofcode, algorithms
Published: 2022-01-12 11:27:38

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.

```java
public class LoveBuzz {
    public static String loveBuzz(int a) {
        // Your code
        return "";
    }
}
```

```java
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));
    }
}
```

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.

```java
public class Palindrome {
    public static boolean isPalindrome(String song) {
        // Your code
       return false;
    }
}
```

```java
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"));
    }
}
```

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.

```java
public class InTheEnd {
    public static boolean checkEnding(String song1, String song2) {
        // Your code
        return false;
    }
}
```

```java
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"));
    }
}
```

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

```java
public class Reverse {
    public static String reverseSong(String song) {
        // Your code
        return song;
    }
}
```

```java
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"));
    }
}
```

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.

```java
public class TwoSum41 {
    public static int[] sum(int[] arr) {
        // Your code
        return arr;
    }
}
```

```java
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}));
    }
}
```

If you'd like to download any of the code, you can find these challenges on my [GitHub](https://github.com/nealeh/rock-n-roll-problems)