Given a number S, check if it is divisible by 8. 
Example 1:
Input:
S=16
Output:
1
Explanation:
The given number is divisible by 8.
Example 2:
Input:
S=54141111648421214584416464555
Output:
-1
Explanation:
Given Number is not divisible by 8.
Your Task:
You don't need to read input or print anything.Your task is to complete the function DivisibleByEight() which takes a string S as input and returns 1 if the number is divisible by 8,else it returns -1.
Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)
Constraints:
1 <= digits in S < 10^6

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:
    def DivisibleByEight(self,S):
        #code here

Solutions:
class Solution:

	def check(self, num):
		if num % 8 == 0:
			return 1
		else:
			return -1

	def DivisibleByEight(self, S):
		if len(S) < 4:
			return self.check(int(S))
		else:
			num = S[len(S) - 3:]
			return self.check(int(num))


class Solution:

	def DivisibleByEight(self, S):
		x = S[-3:]
		n = int(x)
		if n % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		s = S[-3:]
		if int(s) % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		st = str(S)
		if len(st) > 3:
			a = len(st)
			b = len(st) - 3
			c = st[b:a]
			if int(c) % 8 == 0:
				return 1
			else:
				return -1
		elif int(S) % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) == 1 and int(S[-1]) % 8 == 0:
			return 1
		elif len(S) == 2 and int(S[-2::1]) % 8 == 0:
			return 1
		elif len(S) >= 3 and int(S[-3::1]) % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		l = len(S)
		if l > 2:
			S = S[l - 3:]
		S = int(S)
		if S % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		n = len(S)
		if n == 1:
			if int(S) % 8 == 0:
				return 1
			else:
				return -1
		if n == 2:
			if int(S) % 8 == 0:
				return 1
			else:
				return -1
		last_three_digits = int(S[-3:])
		if last_three_digits % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		n = bin(int(S[-3:]))
		return 1 if n[-3:] == '000' or n == '0b0' else -1


class Solution:

	def DivisibleByEight(self, S):
		a = int(S[-3:])
		if a & 7:
			return -1
		return 1


class Solution:

	def DivisibleByEight(self, n):
		if int(n[-3:]) % 8 == 0:
			return 1
		return -1


class Solution:

	def DivisibleByEight(self, n):
		a = str(n)
		b = a[-3:]
		if int(b) % 8 == 0:
			return 1
		return -1


class Solution:

	def DivisibleByEight(self, s):
		n = len(s)
		if n < 3:
			return 1 if int(s) % 8 == 0 else -1
		else:
			return 1 if int(s[n - 3:n + 1]) % 8 == 0 else -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) > 3:
			if int(S[len(S) - 3:]) % 8 == 0:
				return 1
			return -1
		if int(S) % 8 == 0:
			return 1
		return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(str(S)) <= 3:
			if int(S) % 8 == 0:
				return 1
			else:
				return -1
		elif int(str(S)[len(S) - 3:len(S)]) % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) == 1:
			if int(S[0]) % 8 == 0:
				return 1
			return -1
		if len(S) == 2:
			if (int(S[-2]) * 10 + int(S[-1])) % 8 == 0:
				return 1
			return -1
		if (int(S[-3]) * 100 + int(S[-2]) * 10 + int(S[-1])) % 8 == 0:
			return 1
		return -1


class Solution:

	def DivisibleByEight(self, s):
		if len(s) == 1:
			if int(s[0]) % 8:
				return -1
			else:
				return 1
		elif len(s) == 2:
			if (int(s[0]) * 10 + int(s[1])) % 8:
				return -1
			else:
				return 1
		elif (int(s[-3]) * 100 + int(s[-2]) * 10 + int(s[-1])) % 8:
			return -1
		else:
			return 1


class Solution:

	def DivisibleByEight(self, s):
		if len(s) <= 3:
			if int(s) % 8 == 0:
				return 1
			else:
				return -1
		else:
			s1 = len(s) - 3
			k = s[s1:]
			if int(k) % 8 == 0:
				return 1
			else:
				return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) >= 4:
			S = S[len(S) - 3:]
		a = int(S)
		if a % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) >= 3:
			k = int(S[len(S) - 3:])
		else:
			k = int(S)
		c = k % 8
		if c == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, s):
		if len(s) < 3:
			if int(s) % 8 == 0:
				return 1
			else:
				return -1
		else:
			x = int(s[len(s) - 3] + s[len(s) - 2] + s[len(s) - 1])
			if x % 8 == 0:
				return 1
			else:
				return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) < 4:
			if int(S) % 8 == 0:
				return 1
			return -1
		else:
			x = S[len(S) - 4:]
			if int(x) % 8 == 0:
				return 1
			else:
				return -1


class Solution:

	def DivisibleByEight(self, S):
		m = S[-3:]
		k = int(m)
		if k % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		if len(S) > 2:
			x = int(S[len(S) - 3:])
		else:
			x = int(S)
		return 1 if x % (1 << 3) == 0 else -1


class Solution:

	def divisible(self, n):
		if n % 8 == 0:
			return 1
		else:
			return -1

	def DivisibleByEight(self, S):
		if len(S) < 4:
			return self.divisible(int(S))
		else:
			n = S[len(S) - 3:]
			return self.divisible(int(n))


class Solution:

	def DivisibleByEight(self, S):
		P = str(S)
		n = len(P)
		if n == 0:
			return False
		if n == 1:
			if int(P[0]) % 8 == 0:
				return 1
			else:
				return -1
		if n == 2:
			if (int(P[n - 2]) * 10 + int(P[n - 1])) % 8 == 0:
				return 1
			else:
				return -1
		last = int(P[n - 1])
		second_last = int(P[n - 2])
		third_last = int(P[n - 3])
		if (third_last * 100 + second_last * 10 + last) % 8 == 0:
			return 1
		else:
			return -1


class Solution:

	def DivisibleByEight(self, S):
		g = len(S)
		if g < 5:
			f = int(S)
			if f % 8 == 0:
				return 1
			return -1
		S = S[g - 3:]
		if int(S) % 8 == 0:
			return 1
		return -1