## What is the dumbest code ever made that has become famous?

BogoSort is a moderately well-known algorithm to sort a list. Here’s how it works:

- Put the elements of the list in a random order.
- Check if the list is sorted. If not, start over.

BogoSort has an average running time of **O((n+1)!)**, which is not very good. It is also the rare algorithm which has **NO worst-case running time**; if the input has at least two elements, it is possible for the algorithm to run for any amount of time.

## CodeEval: Penultimate Word

**Challenge Description:**

Write a program which finds the next-to-last word in a string.

**Input Sample:**

Your program should accept as its first argument a path to a filename. Input example is the following

some line with text another line

Each line has more than one word.

**Output Sample:**

Print the next-to-last word in the following way.

with another

**Solution:**

## CodeEval: Shortest Repetition

### Challenge Description:

Write a program to determine the shortest repetition in a string.

A string is said to have period **p** if it can be formed by concatenating one or more repetitions of another string of length **p**. For example, the string “xyzxyzxyzxyz” has period 3, since it is formed by 4 repetitions of the string “xyz”. It also has periods 6 (two repetitions of “xyzxyz”) and 12 (one repetition of “xyzxyzxyzxyz”).

### Input Sample:

Your program should accept as its first argument a path to a filename. Each line will contain a string of up to 80 non-blank characters. E.g.

abcabcabcabc bcbcbcbcbcbcbcbcbcbcbcbcbcbc dddddddddddddddddddd adcdefg

### Output Sample:

Print out the smallest period of the input string. E.g.

3 2 1 7

### Solution:

## CodeEval: PASS TRIANGLE

**CHALLENGE DESCRIPTION:**

By starting at the top of the triangle and moving to **adjacent** numbers on the row below, the maximum total from top to bottom is **27**.

596 468 071 5

5 + 9 + 6 + 7 = 27

### INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. Input example is the following:

5 9 6 4 6 8 0 7 1 5

You make also check full input file which will be used for your code evaluation.

### OUTPUT SAMPLE:

The correct output is the maximum sum for the triangle. So for the given example the correct answer would beĀ **27**

**Solution**

## HackerRank: Gemstones

**Problem**

John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lower-case Latin letter from ‘a’ to ‘z’. An element can be present multiple times in a rock. An element is called a *gem-element* if it occurs at least once in each of the rocks.

Given the list of N rocks with their compositions, display the number of gem-elements that exist in those rocks.

**Input Format**

The first line consists of an integer, N, the number of rocks.

Each of the next N lines contains a rock’s composition. Each composition consists of lower-case letters of English alphabet.

**Constraints**

1ā¤Nā¤100

Each composition consists of only lower-case Latin letters (‘a’-‘z’).

1ā¤ *length of each composition* ā¤100

**Output Format**

Print the number of gem-elements that are common in these rocks. If there are none, print 0.

**Sample Input**

```
3
abcdde
baccd
eeabg
```

**Sample Output**

```
2
```

**Explanation**

Only “a” and “b” are the two kinds of gem-elements, since these are the only characters that occur in every rock’s composition.

**Solution**

## HackerRank: The Love-Letter Mystery

**Problem**

James found a love letter his friend Harry has written for his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter into palindromes.

To do this, he follows two rules:

- He can reduce the value of a letter, e.g. he can change
*d*to*c*, but he cannot change*c*to*d*. - In order to form a palindrome, if he has to repeatedly reduce the value of a letter, he can do it until the letter becomes
*a*. Once a letter has been changed to*a*, it can no longer be changed.

Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.

**Input Format**

The first line contains an integer T, i.e., the number of test cases.

The next TĀ lines will contain a string each. The strings do not contain any spaces.

**Constraints**

1ā¤Tā¤10

1ā¤ *length of string* ā¤10^4

All characters are lower case English letters.

**Output Format**

A single line containing the number of minimum operations corresponding to each test case.

**Sample Input**

```
4
abc
abcba
abcd
cba
```

**Sample Output**

```
2
0
4
2
```

**Explanation**

- For the first test case, ab
**c**-> ab**b**-> aba. - For the second test case,
*abcba*is already a palindromic string. - For the third test case,
*abc*.**d**-> abc**c**-> abc**b**-> abc**a**= ab**c**a -> ab**b**a - For the fourth test case,
.**c**ba ->**b**ba -> aba

**Solution**

## HackerRank: Alternating Characters

**Problem**

Shashank likes strings in which consecutive characters are different. For example, he likes *ABABA*, while he doesn’t like *ABAA*. Given a string containing characters A and B only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string.

Your task is to find the minimum number of required deletions.

**Input Format**

The first line contains an integer T, i.e. the number of test cases.

The next T lines contain a string each.

**Output Format**

For each test case, print the minimum number of deletions required.

**Constraints**

1ā¤Tā¤10

1ā¤ *length of string* ā¤10^5

**Sample Input**

```
5
AAAA
BBBBB
ABABABAB
BABABA
AAABBB
```

**Sample Output**

```
3
4
0
0
4
```

**Explanation**

*AAAA* ā¹ā¹ *A*, *3* deletions

*BBBBB* ā¹ā¹ *B*, *4* deletions

*ABABABAB* ā¹ā¹ *ABABABAB*, *0* deletions

*BABABA* ā¹ā¹ *BABABA*, *0* deletions

*AAABBB* ā¹ā¹ *AB*, *4* deletions because to convert it to AB we need to delete 2 A’s and 2 B’s

**Solution**

## HackerRank: Modified Kaprekar Numbers

**Problem**:

A modified *Kaprekar number* is a positive whole number n with d digits, such that when we split its square into two pieces – a right hand piece r with d digits and a left hand piece l that contains the remaining dĀ or dā1Ā digits, the sum of the pieces is equal to the original number (i.e. lĀ + r = n).

**Note:** r may have leading zeros.

Here’s an explanation from Wikipedia about the **ORIGINAL** Kaprekar Number (spot the difference!): *In mathematics, a Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 45Ā² = 2025 and 20+25 = 45.*

**The Task**

You are given the two positive integers pĀ and q, where pĀ is lower than q. Write a program to determine how many Kaprekar numbers are there in the range between p and q (both inclusive) and display them all.

**Input Format**

There will be two lines of input: p, lowest value q, highest value

**Constraints**:

**0<p<q<100000**

**Output Format**

Output each Kaprekar number in the given range, space-separated on a single line. If no Kaprekar numbers exist in the given range, print ** INVALID RANGE**.

**Sample Input**

```
1
100
```

**Sample Output**

1 9 45 55 99

**Explanation**

1, 9, 45, 55, and 99 are the Kaprekar Numbers in the given range.

**Solution**

## HackerRank: Paint The Tiles

Nikita has a line of **N** tiles indexed from **0**Ā to **Nā1**. She wants to paint them to match a color configuration, **C**, which is comprised of 2 colors: **Red(R) and Blue(B)**

In one stroke, Nikita can paint 1Ā or more adjacent tiles a single color. After she finishes painting, each **tile(i)**Ā should be painted color **C(i)**.

It should be noted that it is not allowed to apply more than 1Ā stroke on a tile.

Given the required color configuration, find and print the *minimum* number of strokes required for Nikita to paint all NĀ tiles.

**Note:** In a line of tiles, 2Ā tiles with the indices **i**Ā and **j** are considered adjacent only if **|jāi|=1**.

**Input Format**

The first line contains a single integer, **N**, denoting the number of tiles to be painted.

The second line contains a string, **C**, denoting the desired color configuration.

For each character **C(i)**Ā in **C**:

- If
**C(i)=“R”**, it means the**i**tile must be painted^{th}.*red* - If
**C(i)=“B”**, it means the**i**tile must be painted^{th}.*blue*

**Constraints**

**1ā¤Nā¤1000****C(i)ā{“R”, “B”}**

**Output Format**

Print the minimum number of strokes required to paint all NĀ tiles in the desired color configuration.

**Sample Input 0**

```
5
RRRRR
```

**Sample Output 0**

```
1
```

**Sample Input 1**

```
5
RRBRR
```

**Sample Output 1**

```
3
```

**Sample Input 2**

```
5
BRBRB
```

**Sample Output 2**

```
5
```

**Explanation**

*Sample Case 0:*

Nikita will paint all 5Ā consecutive tiles red in a single stroke:

*Sample Case 1:*

Nikita will need 3 strokes to paint all 5Ā tiles:

**Solution:Ā **