Given a string of characters, I want the function `findMiddle()`/`find_middle()` to return the middle number in the product of each digit in the string.

Example: 's7d8jd9' -> 7, 8, 9 -> 7\*8\*9=504, thus 0 should be returned as an integer.

Not all strings will contain digits. In this case and the case for any non-strings, return -1.

If the product has an even number of digits, return the middle two digits

Example: 1563 -> 56

NOTE: Remove leading zeros if product is even and the first digit of the two is a zero.
Example 2016 -> 1

Difficulty: EASY
Tags: String algorithms

Fundamentals

Starter code:
def find_middle(string):
	

Solutions:
from operator import mul
from functools import reduce

def find_middle(s):
	if not s or not isinstance(s, str):
		return -1
	lstDig = [int(c) for c in s if c.isnumeric()]
	if not lstDig:
		return -1
	prod = str(reduce(mul, lstDig))
	i = (len(prod) - 1) // 2
	return int(prod[i:-i or len(prod)])


def find_middle(string):
	if string is None:
		return -1
	if type(string) is int:
		return -1
	if type(string) is list:
		return -1
	spisok = []
	for i in string:
		if i.isdigit():
			spisok.append(i)
	if len(spisok) == 0:
		return -1
	spisok_ans = 1
	for i in spisok:
		spisok_ans *= int(i)
	spisok = str(spisok_ans)
	if len(spisok) % 2 == 0:
		if len(spisok) >= 2:
			spisok[:1].replace('0', '')
		test = int(len(spisok) / 2)
		answer = spisok[test - 1:test + 1]
	else:
		test = int(len(spisok) / 2)
		answer = spisok[test:test + 1]
	answer = ''.join(answer)
	return int(answer)


from functools import reduce
from operator import mul

def find_middle(string):
	try:
		s = str(reduce(mul, map(int, filter(str.isdigit, string))))
		(q, r) = divmod(len(s), 2)
		return int(s[q + r - 1:q + 1])
	except TypeError:
		return -1


from functools import reduce
import re

def find_middle(string):
	try:
		nums = re.findall('\\d', string)
		result = str(reduce(lambda x, y: x * y, (int(x) for x in nums)))
		return int(result[(len(result) - 1) // 2:len(result) // 2 + 1])
	except:
		return -1


from functools import reduce

def find_middle(stg):
	if not isinstance(stg, str):
		return -1
	dig = [int(c) for c in stg if c.isdecimal()]
	if not dig:
		return -1
	prod = str(reduce(int.__mul__, dig))
	l = len(prod)
	(i, j) = ((l - 1) // 2, l // 2 + 1)
	return int(prod[i:j])


def find_middle(string):
	try:
		lst = []
		for i in string:
			if i.isdigit():
				lst.append(i)
		x = str(eval('*'.join(lst)))
		return int(str(x)[len(x) // 2]) if len(x) % 2 == 1 else int(str(x)[len(x) // 2 - 1] + str(x)[len(x) // 2])
	except:
		return -1


from functools import reduce
from math import ceil
from operator import mul

def find_middle(string):
	try:
		n = str(reduce(mul, list(map(int, list(filter(str.isdigit, string))))))
		size = len(n)
		return int(n[ceil(size / 2 - 1):size // 2 + 1])
	except TypeError:
		return -1


def find_middle(string):
	if not type(string) == str:
		return -1
	numbers = [int(i) for i in string if i.isdigit()]
	if not numbers:
		return -1
	prod = 1
	for i in numbers:
		prod *= i
	prod_str = str(prod)
	return int(prod_str[len(prod_str) // 2 - 1:len(prod_str) // 2 + 1]) if len(prod_str) % 2 == 0 else int(prod_str[len(prod_str) // 2])


def find_middle(string):
	if type(string) != str:
		return -1
	result = 1
	count = 0
	for c in string:
		if c.isdigit():
			count += 1
			result *= int(c)
	if count:
		if len(str(result)) % 2 == 0:
			final = str(result)[len(str(result)) // 2 - 1:len(str(result)) // 2 + 1]
			if final[0] == '0':
				return int(final[1])
			else:
				return int(final)
		else:
			return int(str(result)[len(str(result)) // 2])
	else:
		return -1


from numpy import prod

def find_middle(string):
	return int(findMiddle(str(prod(list(map(int, filter(type(string).isdigit, string))))))) if type(string) is str and any((char.isdigit() for char in string)) else -1

def findMiddle(s):
	return s[int(len(s) / 2) - 1:int(len(s) / 2) + 1] if not len(s) % 2 else s[int(len(s) / 2):int(len(s) / 2) + 1]