Implemet Stack in python

 

class Stack:
def __init__(self, data ):
self.stack = []
if(data):
self.stack.append(data)
def push(self, data ):
self.stack.append(data)
def pop(self):
popped_elemnt = self.stack.pop()
def show(self):
for i in self.stack:
print(i)
s = Stack(45)
s.push(23)
s.push(29)
s.push(695)
s.show()
s.pop()
s.show()

Arrays In Javascript.

Consider this small code sample in Javascript.


var arrayOfNumbers=[1,2,3,4,5,6,78];
for(var index in arrayOfNumbers){
    console.log(index+1);
};

Output of this snippet will be

01
11
21
31
41
51
61

Now this is because Arrays are treated as Objects in Javascript, so index is iterating over keys in an object. In this case the object will be represented as follows

arrayOfNumbers={ "0":1,
                 "1":2,
                 "2":3,
                 "3":4,
                 "4":5,
                 "5":6,
                 "6":78}

Write a program to check whether two strings are anagram or not.


import java.util.*;
public class anagrams {
    public static void main(String[] args) {
        int count=0,i;
        Scanner sc= new Scanner(System.in);
        System.out.println("enter first string");
        String a=sc.nextLine();
        System.out.println("enter second string");
        String b=sc.nextLine();
        a=a.toLowerCase();
        b=b.toLowerCase();
        char a1[]=a.toCharArray();
        char a2[]=b.toCharArray();
        Arrays.sort(a1);
        Arrays.sort(a2);
        if(a1.length!=a2.length)
            count++;
        if(count==0)
        for(i=0;i<a1.length;i++)
            if(a1[i]!=a2[i])

                count++;

        if(count==0)
            System.out.println("anagrams");
        else
            System.out.println("not anagrams");
    }
}

Solution for SAMER08F problem in SPOJ.

problem link-  
http://www.spoj.com/problems/SAMER08F/

#include<stdio.h>
#include<math.h>
int main()
{
int n,i,sum=0;
while(1)
{
scanf("%d",&n);
if(n==0)
break;
for(i=1;i<=n;i++)
sum=sum+ pow(i, 2);
printf("%d\n",sum);
sum=0;
}
return 0;
}


this problem can be done without  using loop. 

wap to check whether an integer is a power of 2..



#include<stdio.h>
main()
{
int n,i;
printf("\nenter the number\n");
scanf("%d",&n);
i=n-1;
if((n&i)==0)
   printf("\ninteger is a power of 2\n");
else
   printf("\ninteger is not a power of 2\n");
}




Implemet Stack in python

  class Stack : def __init__ ( self , data ): self . stack = [] if ( data ): self . stack . append ( da...