redM0nk
BAN USERPython implementation:
'''
outputs an array such that when given
arr = [...]
for an index i, it gives you the number of elements in the arr[i+1:] which are smaller than arr[i]
PS: this code doesn't handle duplicates
'''
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert(self, data):
if self is None:
self = Node(data)
elif data <= self.data:
# left insert
if self.left is None:
self.left = Node(data)
else:
self.left.insert(data)
else:
# right insert
if self.right is None:
self.right = Node(data)
else:
self.right.insert(data)
def findNode(self, data):
if self is None:
return None
else:
if self.data == data:
return self
elif self.data > data and self.left is not None:
return self.left.findNode(data)
elif self.right is not None:
return self.right.findNode(data)
def calcLeftCount(self, data):
orderVisit = []
count = 0
node = self.findNode(data)
if node is not None and node.left is not None:
orderVisit.append(node.left)
while len(orderVisit) != 0:
node = orderVisit.pop()
count += 1
if node.right is not None:
orderVisit.append(node.right)
if node.left is not None:
orderVisit.append(node.left)
return count
if __name__ == "__main__":
root = None
_inpArr = []
_inp = " "
while True:
_inp = input('')
if _inp == "": break
_inpArr.append(int(_inp))
if root is None: root = Node(int(_inp))
else: root.insert(int(_inp))
print("Input:", _inpArr)
_output = []
for val in _inpArr:
_output.append(root.calcLeftCount(val))
print("Output:", _output)
'''Python3.3:
Removing duplicates on the fly
E.g.:
a
v
a
then answer:
a, v
'''
if __name__ == "__main__":
name_space = {}
ordered_list = []
while True:
_name = input("")
if(_name == ""): break
if _name not in name_space:
name_space[_name] = 1
ordered_list.append(_name)
print(list(name_space.keys()))
print("ordered list: ", ordered_list)
'''
Question:
A ... Z are coded in (1,26)
AA ... ZZ are coded in (27, 702)
AAA ... ZZZ are coded in (703,...)
'''
import string,math
charMap = list(string.ascii_lowercase)
def numToCode(_num):
#INPUT: INTEGER, OUTPUT = STRING
_out_code = []
_dist = 1
while _num != 0:
_dist = int(_num % 26)
_out_code.append(charMap[_dist-1])
_num = math.floor(_num/26)
return ''.join(_out_code[::-1])
if __name__ == "__main__":
#TODO take input from user
_inp = ""
while _inp == "":
_inp = input("Enter code:")
_inp_num = int(_inp)
_out_code = numToCode(_inp_num)
print(_out_code)
Hope this helps:
- redM0nk June 30, 2014