Thursday, March 2, 2017

Remove adjacent duplicates

Given a string, recursively remove adjacent duplicate characters from string. The output string should not have any adjacent duplicates.


Input:
geeeksforgeek

Output:
gksforgk


Check my geeksforgeeks submission: http://www.practice.geeksforgeeks.org/viewSol.php?subId=1579026&pid=1583

Answer: (Solution using both loops & recursion is present)

#include <stdio.h>
#include <string.h>
void duplicate_removal(char *s)
{
   static char *p, *q;
   static char flag = 0;

   if(flag == 1) {
      if(*p != *s) {
         q = s;
 flag = 2;
 return;
      }
      duplicate_removal(++s);
      s = p;
   }

   if((flag == 2) && (*q == '\0')) {
      *s = '\0';
      flag = 0;
      return;
   }

   if(flag == 2) {
      *s = *q++;
   }

   if(*s == '\0') {
      flag = 0;
      return;
   }
   else if ((flag == 0) && (*s == *(s+1))) {
      p = s;
      flag = 1;
      duplicate_removal(s);
      s = p;
   }

   duplicate_removal(++s);
}


int main(void) {
   char buff[32];
#if 0
   int i,j,k,l,len;
#endif

   printf("String: ");
   scanf("%s",buff);

#if 0
   len = strlen(buff);

   for(i=0; i<len; i++) {
      if(buff[i] == buff[i+1]) {
         for(k=i; k<len; k++) {
            if(buff[i] != buff[k]) {
               j = k;
               break;
            }
         }

         for(k=j,l=i; k<len; k++,l++) {
            buff[l] = buff[k];
         }
         buff[l] = '\0';
      }
      len = strlen(buff);
   }
#endif

   duplicate_removal(buff);
   printf("Output: %s\n",buff);
   return 0;
}



Wednesday, March 1, 2017

Path traversal

Given a matrix with 0’s and 1’s , you enter the matrix at cell (0,0) in left to right direction. whenever you encounter a 0 you retain in same direction , if you encounter a 1’s you have to change direction to right of current direction and change that 1 value to 0, you have to find out from which index you will leave the matrix at the end.

Input:
3 3
0 0 1 0 0 0 0 0 0

Output:
2 2 

Input:
2 4
0 0 0 1 0 0 1 1

Output:
0 2

Example: The matrix will be represented as
0 1 1 1 0 
1 0 1 0 1 
1 1 1 0 0

The path is 
(0,0) (0,1) (1,1) (2,1) (2,0) (1,0) (1,1) (1,2) (2,2) (2,1) (2,0)


Answer:

Check my geeksforgeeks submission: http://www.practice.geeksforgeeks.org/viewSol.php?subId=1574427&pid=1586

#include <stdio.h>

// RR = Row Right
// CD = Column Down
// RL = Row Left
// CU = Column Up
enum state { RR, CD, RL, CU};

void change_state(int *curstate)
{
if(*curstate == CU)
*curstate = RR;
else 
  *curstate = *curstate + 1;
}

void parse_state(int *curstate, int *i, int *j)
{
printf(" %d,%d\n",*i,*j);
switch(*curstate) {
case RR:
*j = *j + 1;
break;
case CD:
*i = *i + 1;
break;
case RL:
*j = *j - 1;
break;
case CU:
*i = *i - 1;
break;
default:
break;
}
}

void print_exitpath(int r, int c, int (*p)[10][10]) {
int i=0,j=0;
int curstate = RR;

 while((i>=0) && (i<x) && (j>=0) && (j<y)) {
if((*p)[i][j] == 1) {
(*p)[i][j] = 0;
change_state(&curstate);
}
parse_state(&curstate, &i, &j);
}
}

int main(void) {
int r, c,i,j;
int a[10][10];

printf("r: ");
scanf("%d",&r);
printf("c: ");
scanf("%d",&c);
for(i=0; i < r ; i++) {
for(j=0; j
printf("Value[%d][%d]: ",i,j);
scanf("%d",&a[i][j]);
}
}

print_exitpath(r,c,&a);
return 0;
}

Tuesday, February 28, 2017



Pattern count

Given a string, your task is to find the number of patterns of form 1[0]1 where [0] represents any number of zeroes (minimum requirement is one 0) there should not be any other character except 0 in the [0] sequence.

Example:
Input:
100001abc101
1001ab010abc01001
10101010

Output:
2
2
3

Check my geeksforgeeks submissionhttp://www.practice.geeksforgeeks.org/viewSol.php?subId=1574505&pid=1598

Answer 1: (Unoptimized)
#include <stdio.h>

int main(void)
{
char buff[32];
int count = 0, i = 0, zflag = 0,oflag = 0;

printf("String: ");
scanf("%s",buff);

while(buff[i] != '\0') {
if(buff[i] == '0') {
if(oflag == 1) {
oflag = 0;
zflag = 1;
}
}
else if(buff[i] == '1') {
oflag = 1;
if(zflag == 1) {
//oflag = 0;
zflag = 0;
count++;
}
}
else {
oflag = 0;
zflag = 0;
}
i++;
}
printf("Count = %d\n",count);

return 0;
}

Answer2: (Better version)
#include <stdio.h>
#include <string.h>

int patterncount(char *s)
{
int len = strlen(s), i, count = 0;

for(i = 0; i < len; i++) {
if(s[i] == '1' && s[i+1] == '0') {
i++;
for(; i < len; i++) {
if(s[i] == '1') {
count++;
i--;
break;
}
else if (s[i] != '0')
break;
}
}
}
return count;
}

int main(void)
{
char buff[32];

printf("String: ");
scanf("%s", buff);

printf("Count = %d\n",patterncount(buff));

return 0;
}

Sunday, November 1, 2015

custom sizeof implementation

#include <stdio.h>

#define my_sizeof(type) (((type *)0)+1)

int main()
{
    printf("size of int = %d\n", sizeof(int));
    printf("My size of int = %d\n", my_sizeof(int));
    printf("My size of char = %d\n", my_sizeof(char));
    printf("My size of float = %d\n", my_sizeof(float));
    printf("My size of double = %d\n", my_sizeof(double));
    return 0;
}


Result:
   size of int = 4
   My size of int = 4
   My size of char = 1
   My size of float = 4
   My size of double = 8

BeagleBoard notes


============================================
How to load files(rd-ext2.bin & uImage) from mmc card and boot Linux?

In u-boot:
mmc rescan
fatload mmc 0:1 81600000 rd-ext2.bin

setenv bootargs mem=88M ip=172.16.9.111 console=ttyO2,19200n8 root=/dev/ram0 rw initrd=0x81600000,16M ramdisk_size=16384

fatload mmc 0:1 80000000 uImage
============================================


----------------------------------------
make uImage LOADADDR=0x80008000
make CONFIG_PREFIX=/home/neo/tgt install
make omap2plus_defconfig

-------------------------------------------
git status
  201  arm-linux-gnueabi-
  202  export ARCH=arm
  203  export CROSS_COMPILE=arm-linux-gnueabi-
  204  make uImage
  205  ls arch/arm/configs/ | grep omap
  206  make omap2plus_defconfig
  207  make uImage
  208  rm drivers/video/built-in.o
  209  rm drivers/video/.built-in.o.cmd
  210  make uImage
  211  rm drivers/video/console/built-in.o
  212  make uImage
  213  make distclean
  214  make omap2plus_defconfig
  215  make uImage
  216  make uImage LOADADDR=0x80008000
  217  cp arch/arm/boot/uImage /media/neo/boot/

Monday, December 30, 2013

Delete same data in single linked list


#include <stdio.h>
#include <malloc.h>

typedef struct slink {
int data;
struct slink *next;
}node;


int a[] = {1, 2, 3, 3, 1, 2, 2, 1, 8, 9};


void append(node **q, int num)
{
node *temp = *q, *r = NULL;

r = (node *)malloc(sizeof(node));
r->data = num;
r->next = NULL;

if(*q == NULL)
*q = r;
else {
while(NULL != temp->next)
temp = temp->next;
temp->next = r;
}
}

void display(node *temp)
{
printf("\nThe list: ");
while(temp) {
printf("%d ", temp->data);
temp = temp->next;
}
}


void red_del(node **q)
{
node *temp = *q, *new = NULL,  *old = NULL;
int data;

while (temp != NULL && temp->next != NULL) {
new = temp;
data = new->data;
while(new->next != NULL) {
if(data == new->next->data) {
old = new->next;
new->next = new->next->next;
free(old);
} else 
new = new->next;
}
temp = temp->next;
}
}

void revlist(node **q)
{
node *temp1 = *q, *temp2 = NULL, *temp3 = NULL;

if (!temp1) return;

while(temp1) {
temp2 = temp1->next;
temp1->next = temp3;
temp3 = temp1;
temp1 = temp2;
}
if(temp3) *q = temp3;
}

int main()
{
node *head = NULL;


for (i = 0; i < 10; i++)
append(&head, a[i]);
display(head);

revlist(&head);
display(head);

red_del(&head);
display(head);

return 0;
}


Test
The list: 1 2 3 3 1 2 2 1 8 9
The list: 9 8 1 2 2 1 3 3 2 1
The list: 9 8 1 2 3 

Best method for setting & reading a hardware register

#include <stdio.h>


char *basecalc(unsigned int num, unsigned int base)
{
        static char buff[32];
        char *s = &buff[31];
        *s = '\0';

        while(num) {
                *(--s) = "0123456789ABCDEF"[num%base];
                num /= base;
        }
        return s;
}

void setreg(unsigned int *reg, unsigned int nofbits, unsigned int bitpos, unsigned int value)
{
        unsigned int mask;
        mask  = (~(~0 << nofbits) << bitpos);
        value = value << bitpos;
        *reg  = (*reg & ~mask) | (value & mask);
}

unsigned int getreg(unsigned int *reg, unsigned int nofbits, unsigned int bitpos)
{
        return (*reg >> bitpos) & (( 1 << nofbits) - 1) ;
}

int main()
{
        int num, nofbits, bitpos, value;
        printf("Enter the num: ");
        scanf("%d", &num);
        printf("Enter the nofbits: ");
        scanf("%d", &nofbits);
        printf("Enter the bitpos: ");
        scanf("%d", &bitpos);
        printf("Enter the value: ");
        scanf("%d", &value);

        printf("Read reg: %s\n", basecalc(getreg(&num, nofbits, bitpos), 2));

        printf("The num: %s\n", basecalc(num, 2));
        setreg(&num, nofbits, bitpos, value);
        printf("The result: %s\n", basecalc(num, 2));
        return 0;

}

Linux style
static void setreg(void __iomem *reg, u32 nofbits, u32 bitpos, u32 value)
{
        u32 mask;
        mask = (~(~0 << nofbits) << bitpos);
        value <<= bitpos;
        iowrite32(((ioread32(reg) & ~mask) | (value & mask)), reg);
}

static u32 getreg(void __iomem *reg, u32 nofbits, u32 bitpos)
{
        return (ioread32(reg) >> bitpos) & (~(~0 << nofbits));
}


Test 1
Enter the num: 25
Enter the nofbits: 2
Enter the bitpos: 3
Enter the value: 1

Read reg: 11
The num: 11001
The result: 1001

Test 2
Enter the num: 153
Enter the nofbits: 2
Enter the bitpos: 3
Enter the value: 2
The num: 10011001
The result: 10010001