Monday, March 18, 2019

Q:17 - Shortest distance to a character

Given a string S and a character C, return an array of integers representing the shortest distance from the current character in S to C.

Notes
All letters will be lowercase.
If the string is empty, return an empty array.
If the character is not present, return an empty array.
Examples
shortest_to_char("lovecodewars", "e") == [3, 2, 1, 0, 1, 2, 1, 0, 1, 2, 3, 4]
shortest_to_char("aaaabbbb", "b") == [4, 3, 2, 1, 0, 0, 0, 0]
shortest_to_char("", "b") == []
shortest_to_char("abcde", "") == []

Wednesday, February 20, 2019

Q:16 Get the expected output from the given dictionary

Input = [
{"subject" : "Hello", "message": "World"},
{"subject" : "Hello", "message": "Stranger!"},
{"subject" : "Hello", "message": "Stranger!"},
{"subject" : "Welcome", "message": "Python"}
]

Output:
{'Hello': ['World', 'Stranger!', 'Stranger!'], 'Welcome': ['Python']}

Monday, December 10, 2018

Q16: Find unique items from the below input

Question 16: Find unique items from the below input.
Input:
[{"name":"test2", "role":"admin","id":2},
{"name":"test1", "role":"guest","id":1},
{"name":"test3", "role":"admin","id":3},
{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2}]

Output:
[{"name":"test1", "role":"guest","id":1},
{"name":"test2", "role":"admin","id":2},
{"name":"test3", "role":"admin","id":3}]

Monday, November 19, 2018

Q15: Snail shell pattern

Question15: Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

array = [[1,2,3],
         [4,5,6],
         [7,8,9]]
snail(array) #=> [1,2,3,6,9,8,7,4,5]
For better understanding, please follow the numbers of the next array consecutively:

array = [[1,2,3],
         [8,9,4],
         [7,6,5]]
snail(array) #=> [1,2,3,4,5,6,7,8,9]
This image will illustrate things more clearly:


NOTE: The 0x0 (empty matrix) is represented as [[]]

Q14: lowest product of 4 consecutive digits in a number

Question 14: Create a function that returns the lowest product of 4 consecutive digits in a number given as a string.

This should only work if the number has 4 digits or more. If not, return "Number is too small".

##Example

lowest_product("123456789")--> 24 (1x2x3x4)
lowest_product("35") --> "Number is too small"