Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- The mystery function is defined over the non-negative integers. The more common name of this function is concealed in order to not tempt you to search the Web for help in solving this kata, which most definitely would be a very dishonorable thing to do.
- Assume num has n bits. Then mystery(num) is the number whose binary representation is the numth entry in the table T(n), where T(n) is defined recursively as follows:
- T(1) = [0, 1]
- T(n + 1) is obtained by taking two copies of T(n), reversing the second copy, prepending each entry of the first copy with 0 and each entry of the reversed copy with 1, and then concatenating the two. For example:
- T(2) = [ 00, 01, 11, 10 ]
- and
- T(3) = [ 000, 001, 011, 010, 110, 111, 101, 100 ]
- mystery(6) is the 6th entry in T(3) ( with indexing starting at 0 ), i.e., 101 interpreted as a binary number. So, mystery(6) returns 5.
- Your mission is to implement the function mystery, where the argument may have up to 63 bits. Note that T(63) is far too large to compute and store, so you'll have to find an alternative way of implementing mystery. You are also asked to implement mystery_inv ( or mysteryInv ), the inverse of mystery. Finally, you are asked to implement a function name_of_mystery ( or nameOfMystery ), which shall return the name that mystery is more commonly known as. After passing all tests you are free to learn more about this function on Wikipedia or another place.
- Hint: If you don't know the name of mystery, remember there is information in passing as well as failing a test.
- '''
- import math
- # 1. Display the vector
- def displayVector(v):
- for i in range(0, len(v)):
- print(v[i])
- # 2. Decimal To Binary
- def decimalToBinary(num):
- num = int(abs(num))
- digits = []
- if num == 0:
- digits.append(0)
- return digits
- numOfDigits = (int)(math.log2(num)) + 1
- for i in range(numOfDigits-1, -1, -1):
- if num >= pow(2, i):
- digits.append(1)
- num -= pow(2, i)
- else:
- digits.append(0)
- return digits
- # 3. Binary to Decimal
- def binaryToDecimal(v):
- sum = 0
- # If i have input = [1, 0, 1, 1, 0] ----> 1 * 2^4 + 0 * 2^3 + 1 * 2^2 + 2 * 2^1 + 0 * 1^0
- for i in range(0, len(v)):
- sum += (v[i] * 2**(len(v)-1-i))
- return sum
- # 4. Create T-Map
- def TMap(n):
- TMap1 = ['0', '1']
- if n == 1:
- return TMap1
- elif n>1:
- firstCopy = TMap(n-1)
- secondCopy = firstCopy.copy()
- for i in range(0, len(firstCopy)):
- firstCopy[i] = '0' + firstCopy[i]
- secondCopy[i] = '1' + secondCopy[i]
- secondCopy.reverse()
- result = firstCopy + secondCopy
- return result
- # 5. Our mystery function
- def mystery(number):
- binary = decimalToBinary(number)
- n = len(binary)
- tmap = TMap(n)
- stringInPositionNumber = tmap[number-1]
- # Convert the string into a list of integers
- myList = []
- for i in range(0, len(stringInPositionNumber)):
- if stringInPositionNumber[i] == '0' or stringInPositionNumber[i] == '1':
- myList.append((int)(stringInPositionNumber[i]))
- decimal = binaryToDecimal(myList)
- print()
- print("mystery(" + str(number) +") = " + str(decimal) + ", because: ")
- print("1: The binary equal for " + str(number) + " is: " + ' '.join(map(str, binary)))
- print("2: " + ' '.join(map(str, binary)) + " has " + str(n) + " digits")
- print("3. " + "So, we will create the map TMap(" + str(n) + ") = " + str(tmap))
- print("4. " + "We are going to select the element in position " + str(number) + " of TMap(" + str(n) + "), which is " + stringInPositionNumber)
- print("5. " + "Finally, we will convert " + stringInPositionNumber + " into decimal, which is: " + str(decimal))
- # **********************************************************************************************************************
- # **********************************************************************************************************************
- # **********************************************************************************************************************
- # **********************************************************************************************************************
- # MAIN FUNCTION
- print()
- print("***********************************************************************")
- number = int(input("Write down a number to pass it as an argument in my mystery function: "))
- print("***********************************************************************")
- mystery(number)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement