Consider a series of numbers composed of only digits 4 and 7. First few numbers in the series are 4, 7, 44, 47, 74, 77, 444, .. etc. Given a string N constructed by 4, 7 digit only, find position of this number in this series.
 
Example 1:
Input: N = "7"
Output: 2
Explanation: In the series 4 , 7 , 44 , 47...
7 is at second position.
 
Example 2:
Input: N = "444"
Output: 7
Explanation: In the series 4 , 7 , 44 ,
47 , 74 , 77 , 444 , 447... 444 is at 7th
position 
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function findpos() which accepts a string N as input parameter and returns the position of that string in the given series.
 
Expected Time Complexity: O(Length of given string)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= Length of String < 50

Source: geeksforgeeks | Difficulty: MEDIUM
Tags: String algorithms

Data structures

Mathematics

Starter code: #User function Template for python3

class Solution:

    def findpos(self, n):
        # code here

Solutions:
class Solution:

	def findpos(self, n):
		cnt = [0] * 51
		for i in range(1, 51):
			cnt[i] = 2 ** i
		total = cnt.copy()
		for i in range(1, 51):
			total[i] += total[i - 1]
		sz = len(n)
		ans = total[sz - 1]
		for i in range(sz - 1):
			if n[i] == '7':
				ans += cnt[sz - 1 - i]
		ans += 1 if n[-1] == '4' else 2
		return ans


import math

class Solution:

	def findpos(self, n):
		pos = 0
		for i in range(len(n) - 1):
			pos += 2 ** (i + 1)
		start = 1
		end = 2 ** len(n)
		for ele in n:
			mid = math.ceil((start + end) / 2)
			if ele == '4':
				end = mid
			else:
				start = mid
		pos += start
		return pos


class Solution:

	def findpos(self, n):
		j = len(n)
		i = 0
		c = 0
		while i < j:
			if n[i] == '7':
				c = c * 2 + 2
			if n[i] == '4':
				c = c * 2 + 1
			i += 1
		return c


class Solution:

	def findpos(self, n):
		ans = 2 * (pow(2, len(n) - 1) - 1)
		i = 0
		while i < len(n):
			if n[i] == '7':
				ans += pow(2, len(n) - 1 - i)
			i += 1
		return ans + 1


class Solution:

	def findpos(self, n):
		k = len(n)
		output = 0
		for j in range(k):
			output += 2 ** j
		num = ''
		for item in n:
			if item == '4':
				num += '0'
			else:
				num += '1'
		return output + int(num, 2)


class Solution:

	def findpos(self, n):
		i = 1
		j = 2 ** len(n)
		mid = (i + j) // 2
		for elm in n:
			if elm == '4':
				j = mid
			else:
				i = mid + 1
			mid = (i + j) // 2
		result = mid
		for i in range(1, len(n)):
			result += 2 ** i
		return result


class Solution:

	def findpos(self, n):
		last_n = len(n) - 1
		a = 2
		r = 2
		gp_sum = int(a * (r ** last_n - 1) / (r - 1))
		s = 1
		e = 2 ** len(n)
		for i in range(len(n)):
			diff = e - s + 1
			if n[i] == '4':
				e = e - diff // 2
			else:
				s = s + diff // 2
		return gp_sum + s


class Solution:

	def findpos(self, n):
		p = 0
		for i in range(len(n)):
			if n[i] == '4':
				p = p * 2 + 1
			else:
				p = p * 2 + 2
		return p


class Solution:

	def findpos(self, n):
		(s, p) = (0, 1)
		l = len(n)
		for i in range(l - 1, -1, -1):
			if n[i] == '7':
				s += p
			p *= 2
		return s + 2 ** l - 1


class Solution:

	def findpos(self, n):
		l = len(n)
		first = 0
		last = 2 ** l
		res = 0
		for i in range(l):
			res = res + 2 ** i
		i = 0
		while i < l - 1:
			if n[i] == '4':
				last = (first + last) // 2
			else:
				first = (first + last) // 2
			i += 1
		if n[i] == '4':
			return res + first
		return res + first + 1


class Solution:

	def findpos(self, n):
		length = len(n)
		ans = 2 ** length - 2
		string_length = 2 ** length
		for i in range(length):
			if n[i] == '7':
				ans += string_length / 2
			string_length /= 2
		return int(ans + 1)


class Solution:

	def findpos(self, n):
		pos = 0
		for i in n:
			if i == '4':
				pos = 2 * pos + 1
			else:
				pos = 2 * pos + 2
		return pos


class Solution:

	def findpos(self, n):
		if n == '7':
			return 2
		if n == '4':
			return 1
		cnt = len(n)
		ans = 0
		for i in range(1, cnt):
			ans += 2 ** i
		s = ''
		for i in n:
			if i == '4':
				s = s + '0'
			else:
				s = s + '1'
		hlp = 0
		exp = 0
		for i in range(len(s) - 1, -1, -1):
			if s[i] == '1':
				hlp += 2 ** exp
			exp += 1
		hlp += 1
		return ans + hlp


class Solution:

	def findpos(self, n):
		l = len(n)
		ans = 0
		for i in range(1, l):
			ans += pow(2, i)
		for i in range(len(n)):
			if n[i] == '7':
				ans += pow(2, l) / pow(2, i + 1)
		return int(ans + 1)