karthiga.m1988
BAN USERLearner..
#include <stdio.h>
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)
{
int i;
for (i = 0; i < n; i++)
printf(" %.2x", start[i]);
printf("\n");
}
/*Main function to call above function for 0x01234567*/
int main()
{
int i = 0x01234567;
show_mem_rep((char *)&i, sizeof(i));
getchar();
return 0;
}
little endian machine, gives “67 45 23 01″ as output , big endian machine, gives “01 23 45 67″ as output.
Working C++ code :
It will print only 8 numbers , coz this operation s done based on integer. The 9 th number does not falls under the integer limit
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void LockAndSaySequence(int num);
int power(int n,int i);
int reversenum(int num);
int _tmain(int argc, _TCHAR* argv[])
{
int num = 0;
LockAndSaySequence(1);
getch();
return 0;
}
void LockAndSaySequence(int num)
{
int prev = num%10, count = 0, rem = 0, newnum = 0 , limit = 0 ;
printf("%d\n",num);
while(num!= 0 && limit < 7 )
{
rem = num % 10;
num = num / 10;
if(prev == rem)
{
count++;
if(num==0)
{
newnum = (newnum *10 + count)*10 + prev;
num = newnum;
printf("%d\n", num);
limit++;
num = reversenum(num);
prev = num%10;
newnum = 0;
count = 0;
}
}
else
{
newnum = (newnum *10 + count)*10 + prev;
prev = rem;
count = 1;
if(num==0)
{
newnum = (newnum *10 + count)*10 + prev;
num = newnum;
newnum = 0;
printf("%d\n", num);
limit++;
num = reversenum(num);
prev = num%10;
count = 0;
}
}
}
}
int reversenum(int num)
{
int rem = 0, newnum = 0;
while(num!= 0)
{
rem = num % 10;
num = num / 10;
newnum = newnum * 10 + rem;
}
return newnum;
}
Can be better solved using strnings operations
1 ] Reverse the two lists using this function :
struct node* Recurse_binary_Add(struct node* resultlist ,struct node* list1,struct node* list2)
{
struct node* newnode = createNode(0);
if(list1 != NULL || list2 != NULL)
{
if(list1 != NULL && list2 != NULL)
Recurse_binary_Add(newnode,list1->next, list2->next);
if(list1 != NULL)
Recurse_binary_Add(newnode,list1->next,NULL);
else
Recurse_binary_Add(newnode,NULL,list2);
}
2 ] Then find binary addition using this :
struct node* Iterative_binary_Add(struct node** resultlist ,struct node* list1,struct node* list2)
{
int reminder = 0, data = 0;
while(list1 != NULL || list2 != NULL)
{
if(list1 != NULL && list2 != NULL)
{
data = (list1->data + list2->data + reminder ) % 2;
list1 = list1->next;
list2 = list2->next;
}
else
{
if(list1 != NULL)
{
data = (list1->data + reminder ) %2;
list1 = list1->next;
}
else
{
data = (list2->data + reminder ) % 2;
list2 = list2->next;
}
}
reminder = (data == 0 )? 1 : 0 ;
struct node* newnode = createNode(data);
if(*resultlist == NULL)
*resultlist = newnode;
else
{
struct node* tempResultPointer = *resultlist;
while(tempResultPointer->next!=NULL)
tempResultPointer = tempResultPointer->next;
tempResultPointer->next = newnode;
}
}
return *resultlist;
}
Then reverse the result list
STATIC METHODS VS Instance Method
SM : When a method declaration includes a static modifier, that method is said to be a static method.
IM : When no static modifier is present, the method is said to be an instance method.
SM : Static method is the only one. All the instances will be accessing the same method
IM : Every instance of the class will have its own instance methods
static members as belonging to classes
Instance members as belonging to objects (instances of classes).
SM : When a static member M is referenced in a member-access of the form E.M, E must denote a type containing M.It is a compile-time error for E to denote an instance.
IM : When an instance member M is referenced in a member-accessof the form E.M, E must denote an instance of a type containing M. It is a compile-time error for E to denote a type.
SM : Static methods just operate on input parameters and cannot access internal instance fields.
IM : Instance method can acces the instance fields
SM : The static method is callable on a class even when no instance of the class has been created.
IM : Cannot access instance method with out the instance
SM : Static methods cannot access non-static fields and events in their containing type, and they cannot access an instance variable of any object unless it is explicitly passed in a method parameter.
IM : Instance methods can access static fields
SM : Static methods can be overloaded but not overridden
IM : Instance methods can be overriden when the class containing the instance method is inherited and having virtual keyword in the base class method and overriden key word in the derived class method
SM : A call to a static method generates a call instruction in Microsoft intermediate language (MSIL)
IM : call to an instance method generates a callvirt instruction, which also checks for a null object references
Example :
class Test
{ static void Main() {
int x; Test t = new Test();
static int y; t.x = 1; // Ok
void F() { t.y = 1; // Error, cannot access static member through instance
x = 1; // Ok, same as this.x = 1 Test.x = 1; // Error, cannot access instance member through type
y = 1; // Ok, same as Test.y = 1 Test.y = 1; // Ok
} }
static void G() { }
x = 1; // Error, cannot access this.x
y = 1; // Ok, same as Test.y = 1
}
Source : msdn
It is a C# solution for the question
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileHierarchy_Csharp
{
class Program
{
static void Main(string[] args)
{
string traverseDirectory ="..\\..\\..\\..\\";
string extension = ".txt";
string[] keyword = new string[] { "file", "value" };
string[] lines,elements;
char[] seperator = new Char[]{':','\n'};
int index = 0;
string currentDirectory = Directory.GetCurrentDirectory();
string filePath = traverseDirectory+"a.abc"+extension;
string fileName = null;
bool found = false;
if (File.Exists(filePath))
{
lines = File.ReadAllLines(filePath);
for(int i = 0 ; found==false && i<lines.Count();i++)
{
elements = lines[i].Split(seperator);
foreach (string element in elements)
{
if (element.ToString() == keyword[0])
{
index = element.IndexOf(element);
fileName = elements[index+1];
found = true;
break;
}
}
}
found = false;
if(fileName!=null)
{
if (File.Exists(traverseDirectory + fileName + extension))
{
lines = File.ReadAllLines(filePath);
elements = lines[0].Split(seperator);
for (int i = 0; found == false && i < lines.Count(); i++)
{
elements = lines[i].Split(seperator);
foreach (string element in elements)
{
if (element.ToString() == keyword[1])
{
index = element.IndexOf(element);
fileName = elements[index+1];
found = true;
break;
}
}
}
}
}
Console.WriteLine(fileName);
}
string s = Console.ReadLine();
}
}
}
1. all the subsequences where number of 0s = number of 1s
- karthiga.m1988 March 06, 20132. max length subsequence where number of 0s = number of 1s
example : 0 1 1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
these are the steps of dry running
1] 0 1 1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
01, 011101011010011000
2] 1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
nil
3]1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
1 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0
4] 1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
1 0 1 0 1 1 0 1 0 0
1 0 1 0 1 1 0 1 0 0 1 1 0 0
1 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
5] 0 1 0 1 1 0 1 0 0 1 1 0 0 0 1
0 1 0 1 1 0
0 1 0 1 1 0 1 0 0 1
0 1 0 1 1 0 1 0 0 1 1 0
6] 1 0 1 1 0 1 0 0 1 1 0 0 0 1
1 0 1 1 0 1 0 0
1 0 1 1 0 1 0 0 1 1 0
void findtheLargestSequence()
{
int a[] = {0, 1, 1, 1, 0, 1, 0 ,1, 1, 0 ,1, 0, 0 ,1, 1, 0, 0 ,0 ,1 };
int length = sizeof(a)/sizeof(a[0]);
int maxcount = 0,Zeros_count,OnesCount,start = 0,end = 0;
for(int i = 0; i < length; i ++)
{
Zeros_count = 0; OnesCount= 0;
for(int j = i ; j < length; j++)
{
if(a[j]== 0) Zeros_count++;
if(a[j]==1) OnesCount++;
if(Zeros_count== OnesCount)
{
int count = j-i ;
if(count>maxcount)
{
maxcount = count; start = i; end = j;
}
}
}
}
printf("the longest o's and 1's sequence is :" );
for(int i = start ; i < end ; i++)
{
printf("%d",a[i]);
}
}