While surfing in web I found interesting math problem called "Always perfect". That means if you add 1 to the product of four consecutive numbers the answer is ALWAYS a perfect square.
For example we have: 1,2,3,4 and the product will be 1X2X3X4=24. If we add 1 to the product that would become 25, since the result number is a perfect square the square root of 25 would be 5.

So now lets write a function which takes numbers separated by commas in string format and returns the number which is a perfect square and the square root of that number.

If string contains other characters than number or it has more or less than 4 numbers separated by comma function returns "incorrect input".

If string contains 4 numbers but not consecutive it returns "not consecutive".

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Mathematics

Starter code: def check_root(string):
	

Solutions:
def check_root(string):
	try:
		(a, b, c, d) = [int(i) for i in string.split(',')]
		if not (a == b - 1 and a == c - 2 and (a == d - 3)):
			return 'not consecutive'
		s = a * b * c * d + 1
		return str(s) + ', ' + str(int(s ** 0.5))
	except:
		return 'incorrect input'


import re

def check_root(string):
	if not re.fullmatch('(-?\\d+,){3}-?\\d+', string):
		return 'incorrect input'
	(a, b, c, d) = list(map(int, string.split(',')))
	if not d == c + 1 == b + 2 == a + 3:
		return 'not consecutive'
	return f'{a * b * c * d + 1}, {abs(b ** 2 + a)}'


def check_root(string):
	try:
		(a, b, c, d) = map(int, string.split(','))
		if a + 3 == b + 2 == c + 1 == d:
			x = a + (a + 1) ** 2
			return '{}, {}'.format(x ** 2, abs(x))
		return 'not consecutive'
	except:
		return 'incorrect input'


from functools import reduce
from operator import mul

def check_root(string):
	l = string.split(',')
	if len(l) != 4:
		return 'incorrect input'
	try:
		l = list(map(int, l))
	except:
		return 'incorrect input'
	if not all((l[i] - l[i - 1] == 1 for i in range(1, len(l)))):
		return 'not consecutive'
	return '{}, {}'.format(reduce(mul, l) + 1, int((reduce(mul, l) + 1) ** 0.5))


def check_root(string):
	try:
		(a, b, c, d) = (int(d) for d in string.split(','))
	except ValueError:
		return 'incorrect input'
	if not a * d + 2 == b * c:
		return 'not consecutive'
	perfect = a * b * c * d + 1
	return '{}, {}'.format(perfect, int(perfect ** 0.5))