Given a binary number in the form of string, the task is to check whether the decimal representation of the given binary number is divisible by 10 or not. The number could be very large and may not fit even in long long int.
Example 1:
Input:
bin = "1010"
Output:
1
Explanation:
The decimal representation of given 
string is 10 which is divisible by 10.
So, answer is 1.
Example 2:
Input:
bin = "10"
Output:
0
Explanation:
The decimal representation of bin is 
2, which is not divisible by 10, so 
answer is 0.
Your Task:
You don't need to read input or print anything.Your Task is to complete the function isDivisibleBy10() which takes a binary string bin as input parameter and returns 0 if the decimal representaion of bin is not divisible by 10.Otherwise it returns 1.
Expected Time Complexity:O(N)
Expected Auxillary Space:O(1)
Constraints:
1 <= |bin| <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def isDivisibleBy10(self,bin):

        #code here

Solutions:
class Solution:

	def isDivisibleBy10(self, bin):
		n = int(bin, 2)
		if n % 10 == 0:
			return 1
		return 0


class Solution:

	def isDivisibleBy10(self, bin):
		x = int(bin, 2)
		return 1 if x % 10 == 0 else 0


import math

class Solution:

	def isDivisibleBy10(self, bin):
		num = int(bin, 2)
		if num % 10 == 0:
			return 1
		return 0


class Solution:

	def isDivisibleBy10(self, bin):
		dec = str(int(bin, 2))
		return 1 if dec[-1] == '0' else 0


class Solution:

	def isDivisibleBy10(self, n):
		item = int(n, 2)
		return int(item % 10 == 0)


class Solution:

	def isDivisibleBy10(self, b):
		a = int(b, 2)
		if a % 10 == 0:
			return 1
		return 0


class Solution:

	def isDivisibleBy10(self, bin):
		if eval('0b' + bin) % 10 == 0:
			return 1
		else:
			return 0


class Solution:

	def isDivisibleBy10(self, s):
		if int(s, 2) % 10 == 0:
			return 1
		return 0


class Solution:

	def isDivisibleBy10(self, bin):
		if bin[-1] == '1':
			return 0
		last_digit_sum = 0
		k = 1
		n = len(bin)
		for i in range(n - 2, -1, -1):
			if bin[i] == '1':
				v = 0
				if k % 4 == 0:
					v = 6
				elif k % 4 == 1:
					v = 2
				elif k % 4 == 2:
					v = 4
				else:
					v = 8
				last_digit_sum += v
			k += 1
		if last_digit_sum % 10 == 0:
			return 1
		return 0


class Solution:

	def isDivisibleBy10(self, bin):
		n = len(bin)
		if bin[n - 1] == '1':
			return 0
		sum = 0
		for i in range(n - 2, -1, -1):
			if bin[i] == '1':
				posFromRight = n - i - 1
				if posFromRight % 4 == 1:
					sum = sum + 2
				elif posFromRight % 4 == 2:
					sum = sum + 4
				elif posFromRight % 4 == 3:
					sum = sum + 8
				else:
					sum = sum + 6
		if sum % 10 == 0:
			return 1
		return 0