Coding challenges #2 🧩
Tags: javascript, python, challenge, beginners
Published: 2021-01-04 11:07:09

Greetings fellow problem solvers! 🤓

As I said already, problem-solving is just like a muscle and it is necessary to practice often in order to improve and grow as a developer.

In this second episode, I picked some easy problems to solve from my favorite algorithms website. 

<h2>Table of Contents</h2>

<ul>
<li><a href="#1">Can you get the loop ?</a></li>
<li><a href="#2">Morse Code</a></li>
<li><a href="#3">Rectangle into Squares</a></li>
<li><a href="#4">Meeting</a></li>
<li><a href="#5">Playing with digits</a></li>
</ul>

<h2 id="1">Can you get the loop ?</h2>
*From [Codewars](https://www.codewars.com/kata/52a89c2ea8ddc5547a000863)*

**The problem:**

You are given a node that is the beginning of a linked list. This list always contains a tail and a loop.

Your objective is to determine the length of the loop.

For example in the following picture the tail's size is 3 and the loop size is 11.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/hzyxdp7rgi5xplp8o71q.png)

Use the "next" attribute to get the following node.

*Note*: do NOT mutate the nodes!

**My solution (JavaScript):**

```javascript
function loop_size(node){
  let turtle = node;
  let rabbit = node;
  
  /* We need the turtle and the rabbit to start at the same 
  place. (The rabbit being faster will catch up the turtle at 
  some point) */
  do {
    turtle = turtle.getNext();
    rabbit = rabbit.getNext().getNext();
  } while (turtle !== rabbit)
  
  /* The rabbit goes for a run and we know that ends when he 
  reaches the turtle position again. */
  let counter = 0;
  do {
    rabbit = rabbit.getNext();
    counter++;
  } while (turtle !== rabbit)
    
    return counter;
} 
 
```
 

<h2 id="2">Morse Code</h2>
*From [Codewars](https://www.codewars.com/kata/54b724efac3d5402db00065e)*

**The problem:**

In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superseded by voice and digital data communication channels, it still has its use in some applications around the world.
The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words.

NOTE: Extra spaces before or after the code have no meaning and should be ignored.

In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words.

Your task is to implement a function that would take the morse code as input and return a decoded human-readable string.

For example:
```
decodeMorse('.... . -.--   .--- ..- -.. .')
#should return "HEY JUDE"
```

*NOTE*: For coding purposes you have to use ASCII characters . and -, not Unicode characters.

The Morse code table is preloaded for you as a dictionary, feel free to use it:

JavaScript/TypeScript: MORSE_CODE['.--']

**My solution (JavaScript):**
```javascript
const decodeMorse = (morseCode) => {
  const response = [];
  
  const words = morseCode.trim().split('   ');
  
  for (const word of words) {
    const wordArr = [];
    for (const letter of word.split(' ')) {
      wordArr.push(MORSE_CODE[letter]);
    }
    response.push(wordArr.join(''))
  }
  
  return response.join(' ');
  
}
```

<h2 id="3">Rectangle into Squares</h2>
*From [Codewars](https://www.codewars.com/kata/55466989aeecab5aac00003e)*

**The problem:**

The drawing below gives an idea of how to cut a given "true" rectangle into squares ("true" rectangle meaning that the two dimensions are different).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/izsg23eginaqpc5y4iln.jpg)

Can you translate this drawing into an algorithm?

You will be given two dimensions

- a positive integer length (parameter named lng)
- a positive integer width (parameter named wdth)

You will return a collection or a string (depending on the language; Shell bash, PowerShell, Pascal and Fortran return a string) with the size of each of the squares.

Example:
```
  sqInRect(5, 3) should return "3 2 1 1"
  sqInRect(3, 5) should return "3 2 1 1"
```
**My solution (JavaScript):**
```javascript
function sqInRect(lng, wdth){
  console.log(lng, wdth);
  
  if (lng === wdth) { return null; }
  
  let lngx = lng;
  let wdthx = wdth;
  
  let area = lng * wdth;
  let result = [];


  
  while (area > 0) {
    if (lngx > wdthx) {
      area -= Math.pow(wdthx, 2);
      result.push(wdthx);
      lngx =  lngx - wdthx;
    } else {
      area -= Math.pow(lngx, 2);
      result.push(lngx);
      wdthx = wdthx - lngx;
    }
  }
  
  return result;  
}
```

<h2 id="4">Meeting</h2>
*From [Codewars](https://www.codewars.com/kata/59df2f8f08c6cec835000012/train/python)*

**The problem:**

John has invited some friends. His list is:

```
s = "Fred:Corwill;Wilfred:Corwill;Barney:Tornbull;Betty:Tornbull;Bjon:Tornbull;Raphael:Corwill;Alfred:Corwill";
```
Could you make a program that

- makes this string uppercase
- gives it sorted in alphabetical order by last name.

When the last names are the same, sort them by first name. Last name and first name of a guest come in the result between parentheses separated by a comma.

So the result of function meeting(s) will be:

```
"(CORWILL, ALFRED)(CORWILL, FRED)(CORWILL, RAPHAEL)(CORWILL, WILFRED)(TORNBULL, BARNEY)(TORNBULL, BETTY)(TORNBULL, BJON)"
```

It can happen that in two distinct families with the same family name two people have the same first name too.

**My solution (Python):**
```python
def meeting(s):
    result = ""
    
    # Convert string to list
    names_list = s.upper().split(";")
    
    # Create a dictionnary and bind a list of first names to every last names
    names_dic = {}
    for name in names_list:
        first_name = name.split(":")[0]
        last_name = name.split(":")[1]
        
        if last_name in names_dic: names_dic[last_name].append(first_name)
        else: names_dic[last_name] = [first_name]
            
    # Sort and add every entry to the result string
    for key in sorted(names_dic):
        for first_name in sorted(names_dic[key]):
            result = result + f"({key}, {first_name})"
    
    return result
```

<h2 id="5">Playing with digits</h2>
*From [Codewars](https://www.codewars.com/kata/5552101f47fc5178b1000050)*

**The problem:**

Some numbers have funny properties. For example:

89 --> 8¹ + 9² = 89 * 1

695 --> 6² + 9³ + 5⁴= 1390 = 695 * 2

46288 --> 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51

Given a positive integer n written as abcd... (a, b, c, d... being digits) and a positive integer p

we want to find a positive integer k, if it exists, such as the sum of the digits of n taken to the successive powers of p is equal to k * n.
In other words:

Is there an integer k such as : (a ^ p + b ^ (p+1) + c ^(p+2) + d ^ (p+3) + ...) = n * k

If it is the case we will return k, if not return -1.

*Note*: n and p will always be given as strictly positive integers.

```
dig_pow(89, 1) should return 1 since 8¹ + 9² = 89 = 89 * 1
dig_pow(92, 1) should return -1 since there is no k such as 9¹ + 2² equals 92 * k
dig_pow(695, 2) should return 2 since 6² + 9³ + 5⁴= 1390 = 695 * 2
dig_pow(46288, 3) should return 51 since 4³ + 6⁴+ 2⁵ + 8⁶ + 8⁷ = 2360688 = 46288 * 51
```

**My solution (JavaScript):**
```javascript
function digPow(n, p){
  if(!n || !p){
    return -1;
  }
  let digitArray = n.toString().split("");
  let sun = 0;
  for(let i = 0; i<digitArray.length; i++){
    sun += Math.pow(digitArray[i], p+i);
    
  }
  if(parseInt(sun/n) === sun/n){
  return sun/n;
  }
  else{
    return -1;
  }
}
```