pinn.chait
BAN USERMy Javascript code
function strcompe(s){
let i = 0;
let out = '';
let c = s[i];
out+= c;
let j =1;
while(i<s.length){
if(c===s[i+1]){
j++;
i++;
}else{
out+=j;
if(s[i+1]){
c = s[i+1];
out+= c;
}
j =1;
i++;
}
}
return out;
}
function strdecompreess(s){
let i = 0;
let out = '';
while(i<s.length){
for(let j=0; j<parseInt(s[i+1]);j++){
out+=s[i];
}
i = i+2;
}
return out;
}
let s = 'aaaabbebbccdddee';
s = strcompe(s);
console.log(s);
s = strdecompreess(s);
console.log(s)
(function(){
function node(data){
this.data = data;
this.next = null;
}
function list(){
this.head = null;
this.tail = null;
}
list.prototype.add = function(data){
let n = new node(data);
if(this.head === null){
this.head = n;
this.tail = n;
return
}
this.tail.next = n;
this.tail = n;
}
let l1 = new list();
l1.add('aaa');
l1.add('bbb');
l1.add('ddd');
l1.add('xyxz');
let l2 = new list();
l2.add('bbb');
l2.add('ccc');
l2.add('ccc');
l2.add('hkp');
let l3 = new list();
l3.add('ddd');
l3.add('eee');
l3.add('ffff');
l3.add('lmn');
function compare(l1,l2){
let n1 = l1.head;
let n2 = l2.head;
let out = new list();
while(n1 && n2){
if(n1.data<n2.data){
out.add(n1.data);
n1 = n1.next;
}else if(n1.data>n2.data){
out.add(n2.data);
n2 = n2.next;
}else if (n1.data === n2.data){
//out.add(n1.data);
n1 = n1.next;
n2 = n2.next;
}
}
while(n1){
out.add(n1.data);
n1 = n1.next;
}
while(n2){
out.add(n2.data);
n2 = n2.next;
}
return out
}
function combinenodes(){
let out = new list();
for(let i = 0; i<arguments.length;i++){
out = compare(out, arguments[i]);
}
// let list = compare(l1,l2);
// let list2 = compare(list,l3);
let n = out.head;
while(n){
console.log(n.data);
n = n.next;
}
}
combinenodes(l1,l2,l3);
}());
My Python solution
from copy import deepcopy
def stringcompare(haystack,needle):
output = []
needle_dict = {}
length = len(needle)
for c in needle:
if c in needle_dict.keys():
needle_dict[c] += 1
else:
needle_dict[c] = 1
for i in range(len(haystack)-len(needle)+1):
test = deepcopy(needle_dict)
if(compare(haystack[i:i+length],test)):
output.append(i)
print(output)
def compare(arr,dict):
for c in arr:
if(c in dict.keys()):
dict[c] -= 1
if dict[c] < 0:
return False
else:
return False
return True
def main():
haystack = 'abcdefghibcdajklmndcba'
needle = 'bcda'
stringcompare(haystack, needle)
if __name__ == '__main__':
main()
am I missing something in this question, my python solution, just pass root node
def longPath(self,node):
if node is None or (node.left is None and node.right is None):
return 0
else:
return 1+ max(self.longPath(node.left),self.longPath(node.right))
Python solution:
def arraymultiplication(input):
output = []
length = len(input)
mul = 1
for i in input:
mul*=i
for i in input:
if i == 0:
i = 1
output.append(mul/i)
print(output)
def main():
input = [1,2,3,4,5]
arraymultiplication(input)
if __name__ == "__main__":
main()
Repnancysimms14, Backend Developer at ASAPInfosystemsPvtLtd
I am Nancy from California,Handling project development and documentation is my job. Passionate determined.Looking for an open project ...
My Javascript solution, you can only iterate upto middle if you are maintaining size of list.
- pinn.chait February 19, 2017