Programming Challenges - A List


 
Thread Tools Search this Thread
Top Forums Programming Programming Challenges - A List
# 22  
Old 06-06-2007
Another one - no laughing at the following Smilie :

Earth has made contact with a number of extraterrestrial civilisations. Space tourism is flourishing, but many of the civilisations work with number systems that are different from our commonly used base 10, base 2, base 8 or even base 16! This understandably causes problems when dealing in all things monetary. You work for one of the space tour companies and are often required to work with these number systems. So you decide to write a base n converter, that will convert your cost quotes from base 10 to any of the bases that the civilisations use.

Input:
Two command line arguments that will be passed in to your program.
i.e. "base_conv m n"
where 0 < m < 4294967295 is the number that you want to convert
and 2 <= n <= 36 is the base to convert to.

Output:
Input number represented in the base specified. You can use A-Z to represent numbers greater than 9.

Example:
./a.out 42 8
52
./a.out 42 16
2A
./a.out 4294967291 36
1Z141YZ
# 23  
Old 06-06-2007
Quote:
Originally Posted by blowtorch
Challenge: DRAM

Difficulty: Easy to Medium in C, Easy in perl/python

As the problem says, the value of the palindrome may exceed your system's defined integer size in C/C++.
My solution:
Code:
#include <stdio.h>
#include <stdlib.h>

long dram(long);
int palin(long);
long reverse(long);

main(int argc, char *argv[]) {
	long n;
	if (argc < 2) {
		fprintf(stderr, "usage: %s [number]\n", argv[0]);
		exit(1);
	}
	if (argc == 2) {
		n = atoi(argv[1]);
		printf("%ld\n", dram(n));
	} else {
		while (--argc > 0) {
			n = atoi(*++argv);
			printf("%ld\n", dram(n));
		}
	}
}
long dram(long n) {
	long n2;
	while (palin(n) != 1) {
		n2 = reverse(n);
		n += n2;
	}
	return n;
}
int palin(long n) {
	long n2 = reverse(n);

	if (n == n2) {
		return 1;
	}
	return 0;
}
long reverse(long n) {
	long rev = 0;

	while (n > 0) {
		rev *= 10;
		rev += n%10;
		n /= 10;
	}
	return rev;
}

I'll work on the bases challenge later.
# 24  
Old 06-07-2007
Quote:
Originally Posted by blowtorch
Challenge: DRAM

Difficulty: Easy to Medium in C, Easy in perl/python

As the problem says, the value of the palindrome may exceed your system's defined integer size in C/C++.
Here is my shot! Smilie

Code:
#include <stdio.h>

int reverseCheck(unsigned long);

int main()
{
  unsigned long number = 2468;
  unsigned long tmp = 0;

  while ( tmp != number ) {
    tmp = reverseCheck(number);
    if( tmp == number ) {
      tmp = number;
    }
    else {
      number += tmp;
    }
  }

  printf("Number:%d\n", number);

  return 0;
}

int reverseCheck(unsigned long number) {
  unsigned int i;
  unsigned long num;
  unsigned long sum;

  for( num = number, i=1; num / 10; i *= 10, num /= 10 );
  for( sum = 0; number > 0; sum += (number % 10) * i, number /= 10, i /= 10);

  return sum;
}

# 25  
Old 06-07-2007
For the base code problem !

Smilie

Code:
#include <stdio.h>

#define OUR_BASE 10

int main(int argc, char *argv[]) {
  unsigned long m;
  unsigned int n;
  unsigned int r;
  unsigned long d;

  if ( argc < 3 ) {
    fprintf(stderr, "USAGE: binary number base\n");
    exit(1);
  }

  m = atol(argv[1]);
  n = atoi(argv[2]);
  d = m / n;
  r = m % n;
  if ( r > (OUR_BASE - 1) ) {
    r += 7;
  }
  r += '0';
  printf("%d%c\n", d, r);

  return 0;
}

# 26  
Old 06-07-2007
Quote:
Originally Posted by matrixmadhan
For the base code problem !
Hi matrixmadhan, this code does not work correctly. Try running it with the input as '255 16'. You should get FF, but you get 15F.
# 27  
Old 06-08-2007
Quote:
Originally Posted by blowtorch
Challenge: DRAM

Difficulty: Easy to Medium in C, Easy in perl/python

As the problem says, the value of the palindrome may exceed your system's defined integer size in C/C++.
My trial:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void palindrome(char *p, int value);

int main (int argc, char *argv[])
{
    int val[20];
    int i, n;
    char pal[20];

    n = 0;

    while (n < 20) {
        scanf("%d", &val[n]);
	if(val[n] == -1) {
	    break;
	}
	n++;
    }
    
    for(i = 0; i < n; i++) {
	pal[0] = '\0';
	palindrome(pal, val[i]);

	if(pal[0]) {
	    printf("Initial value: %d gives palindrome %s\n",val[i], pal);
        }
        else {
	    printf("Initial value: %d no palindrome found\n",val[i]);
        }
    }

    return(0);
}

void palindrome(char *p, int value)
{
    long pal;
    char str[20], rts[20];
    int i, j, len, nel; 

    pal = value;

    for(j = 0; j < 1000; j++) {
        sprintf(str, "%d", pal); 
	len = strlen(str);
        nel = len-1;

	for(i = 0; i < len; i++) {
	    rts[i] = str[nel];
	    nel--;
        }
	rts[len] = '\0';
        nel = len/2;

	if(!strncmp(str, rts, nel)) {
	    strcpy(p, str);    
	    return;
	}
	pal = pal + strtol(rts, 0, 0);
    }
    p[0] = '\0';
}

Regards
# 28  
Old 06-21-2007
Challenge: Write your own square root function.

Difficulty: Easy

My Implementation:
Code:
#include <stdio.h>
#include <stdlib.h>

float root(float);

main(int argc, char *argv[]) {
        if (argc < 2) {
                fprintf(stderr, "usage: %s [number]", argv[0]);
                exit(0);
        }

        while (*++argv) {
                printf("%f\n", root(atof(*argv)));
        }
}
float root(float o) {
        float r = 0.5*(o/2);

        while ((r*r) != o) {
                r = 0.5*(o/r+r);
        }
        return r;
}

Try entering a number like 10. It doesn't exactly work. So I made a new program:
Code:
#include <stdio.h>
#include <stdlib.h>

float root(float);

main(int argc, char *argv[]) {
	if (argc < 2) {
		fprintf(stderr, "usage: %s [number]", argv[0]);
		exit(0);
	}

	while (*++argv) {
		printf("%.2f\n", root(atof(*argv)));
	}
}
float root(float o) {
	float r = 0.5*(o/2);
	int i = 0;

	while ((r+1)*(r+1) <= o) {
		r++;
	}
	while (i < 4) {
		r = 0.5*(r+o/r);
		i++;
	}

	return r;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies

2. UNIX for Advanced & Expert Users

Challenges in finding and copying the block

Hi, I have a below challenging task where iam unable to find the block and copy the same into a file. I tried my luck,howver iam unable to reach the first and second step..Can anyone help me with a clue or with the commands so that i can give a try. 1. search the <number>9966993366</number>... (2 Replies)
Discussion started by: cskumar
2 Replies

3. AIX

AIX 6.1 IDSLDAP Installation Challenges

Please bare with me, since I am new to AIX and LDAP. I am attempting to install idsldap server on our AIX 6.1 NIM server. I installed the following packages: root@nim(/)# lslpp -l|grep ldap db2_08_01.ldap 8.1.1.80 COMMITTED DB2 LDAP Support idsldap.clt64bit61.rte 6.1.0.17 COMMITTED... (6 Replies)
Discussion started by: ecollins
6 Replies
Login or Register to Ask a Question