Programming Challenges - A List


 
Thread Tools Search this Thread
Top Forums Programming Programming Challenges - A List
# 1  
Old 05-23-2007
Programming Challenges - A List

Lets start a list of programming challenges, and include a difficulty level for each program(easy, medium, or hard). Basiclly, you need to say what the program needs to do, any special features, whatever. Please post your files as an attachment, or host it and put a link to it, so to not spoil the answers to other people. Anyone can compete, and you could use any programming language you want, but since this is a C Programming Forum (well, it is Unix, but whatever), preferably C.

This should not be an epic failure,
Octal.
# 2  
Old 05-25-2007
If no one is willing to post anything, I'll start off with something to get us started:

Challenge: Convert English to Morse-Code and Morse-Code to English (the user can decide which). Each Morse-Coded letter should have one space seperating them, and three spaces seperating each word.

Difficulty: Easy

Helpful Information: http://upload.wikimedia.org/wikipedi...92/Intcode.png

Bonus: Make sounds according to the actual beeps that morse code would make. Remember that '.' is a short beep, and '-' is a long beep.

I'll have my solution up in a short amount of time.
# 3  
Old 05-26-2007
In the interest of laziness (and the fact that I am busy at work right now), here's a python version.
Code:
#!/usr/local/bin/python

morseDict=dict([('A','.-'),('B','-...'),('C','-.-.'),('D','-..'),('E','.'),('F','..-.'),('G','--.'),('H','....'),('I','..'),('J','.---'),('K','-.-'),('L','.-..'),('M','--'),('N','-.'),('O','---'),('P','.--.'),('Q','--.-'),('R','.-.'),('S','...'),('T','-'),('U','..-'),('V','...-'),('W','.--'),('X','-..-'),('Y','-.--'),('Z','--..'),('0','-----'),('1','.----'),('2','..---'),('3','...--'),('4','....-'),('5','.....'),('6','-....'),('7','--...'),('8','---..'),('9','----.')])

inputLine=raw_input()
inputLine=inputLine.upper()

for letter in inputLine:
        if letter == " ":
                print "  ",
        else:
                print morseDict[letter],

This is written on a server, so I'm not going to try for the sounds bit (there's no way for me to verify if my Solaris box is beeping away or not).

-edit
Currently handles only letters and numbers

Last edited by blowtorch; 05-26-2007 at 11:52 PM..
# 4  
Old 05-27-2007
Quote:
Originally Posted by blowtorch
Currently handles only letters and numbers
It didn't have to handle anything else...

I can't get the array to work...I have everything else worked out. If someone wants to look at my code and help, I would appretiate it.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char a[][] = { /* morse.c:5: error: array type has incomplete element type */
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
	'Y', 'Z',
	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
	' ', 
	".- ", "-.. ", "-.- ", "-.. ", ". ", "..-. ", "--. ", "....",
	"..", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ",
	"--.- ", ".-. ", "... ", "- ", "..- ", "...- ", "...- ",
	".-- ", "-..- ", "-.-- ", "--.. ",
	".---- ", "..--- ", "...-- ", "....- ", "..... ", "-.... ",
	"--... ", "---.. ", "----. ", "----- ",
	"   "
};

void convert(char [], int);
int set_ary_num(int, int);
void help(char []);

main(int argc, char *argv[]) {
	if (argc < 3 || strstr(argv[1], "-h") != NULL) {
		help(argv[0]);
		exit(1);
	}
	if (strstr(argv[1], "-e") != NULL) {
		convert(argv[2], 0);
	} else if (strstr(argv[1], "-m") != NULL) {
		convert(argv[2], 1);
	} else {
		fprintf(stderr, "unkown option: %s\n\n", argv[1]);
		help(argv[0]);
		exit(1);
	}
	
}
void convert(char s[], int mode) {
	int i = 0, j;

	while (s[i] != '\0') {
		j = 0;
		while (s[i] != a[j]) {
			j++;
		}
		j = set_ary_num(j, mode);
		printf("%s", a[j+=35]);
		i++;
	}
}
int set_ary_num(int n, int mode) {
	return (mode == 0) ? (n += 35) : (n -= 35);
}
void help(char prog[]) {
	fprintf(stderr, "usage: %s [options] [string]\n"
		"options:\n\t-e\tenglish to morse\n"
		"\t-m\tmorse to english\n\t-h\tprint this\n",
		prog);
}

I've tried a few things for that, and I can't get anything to work...
# 5  
Old 05-27-2007
That array definition is crazy. The closest legal definition to something like that might be something like:

[code]const char a[][5] = {
{ 'A', 'B', 'C', 'D', 'E' },
{ 'a', 'b', 'c', 'd', 'e' }
};

But here everything is a character. You are trying to mix characters and strings in one array. A one dimensional array of structures with each structure containing one element which is the character and a second element which is a string with the morse code might work better for you.
# 6  
Old 05-28-2007
Here's the C code implementation. It is working, but with a wierd output:
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>

struct morse {
        char letter;
        char morseCode[6];
};

int main(int argc, char *argv[]) {
        struct morse morse[36];
        char input;

        morse[0].letter='0';
        strcpy(morse[0].morseCode,"-----");
        morse[1].letter='1';
        strcpy(morse[1].morseCode,".----");
        morse[2].letter='2';
        strcpy(morse[2].morseCode,"..---");
        morse[3].letter='3';
        strcpy(morse[3].morseCode,"...--");
        morse[4].letter='4';
        strcpy(morse[4].morseCode,"....-");
        morse[5].letter='5';
        strcpy(morse[5].morseCode,".....");
        morse[6].letter='6';
        strcpy(morse[6].morseCode,"-....");
        morse[7].letter='7';
        strcpy(morse[7].morseCode,"--...");
        morse[8].letter='8';
        strcpy(morse[8].morseCode,"---..");
        morse[9].letter='9';
        strcpy(morse[9].morseCode,"----.");
        morse[10].letter='A';
        strcpy(morse[10].morseCode,".-");
        morse[11].letter='B';
        strcpy(morse[11].morseCode,"-...");
        morse[12].letter='C';
        strcpy(morse[12].morseCode,"-.-.");
        morse[13].letter='D';
        strcpy(morse[13].morseCode,"-..");
        morse[14].letter='E';
        strcpy(morse[14].morseCode,".");
        morse[15].letter='F';
        strcpy(morse[15].morseCode,"..-.");
        morse[16].letter='G';
        strcpy(morse[16].morseCode,"--.");
        morse[17].letter='H';
        strcpy(morse[17].morseCode,"....");
        morse[18].letter='I';
        strcpy(morse[18].morseCode,"..");
        morse[19].letter='J';
        strcpy(morse[19].morseCode,".---");
        morse[20].letter='K';
        strcpy(morse[20].morseCode,"-.-");
        morse[21].letter='L';
        strcpy(morse[21].morseCode,".-..");
        morse[22].letter='M';
        strcpy(morse[22].morseCode,"--");
        morse[23].letter='N';
        strcpy(morse[23].morseCode,"-.");
        morse[24].letter='O';
        strcpy(morse[24].morseCode,"---");
        morse[25].letter='P';
        strcpy(morse[25].morseCode,".--.");
        morse[26].letter='Q';
        strcpy(morse[26].morseCode,"--.-");
        morse[27].letter='R';
        strcpy(morse[27].morseCode,".-.");
        morse[28].letter='S';
        strcpy(morse[28].morseCode,"...");
        morse[29].letter='T';
        strcpy(morse[29].morseCode,"-");
        morse[30].letter='U';
        strcpy(morse[30].morseCode,"..-");
        morse[31].letter='V';
        strcpy(morse[31].morseCode,"...-");
        morse[32].letter='W';
        strcpy(morse[32].morseCode,".--");
        morse[33].letter='X';
        strcpy(morse[33].morseCode,"-..-");
        morse[34].letter='Y';
        strcpy(morse[34].morseCode,"-.--");
        morse[35].letter='Z';
        strcpy(morse[35].morseCode,"--..");

        while((input=fgetc(stdin))!=EOF) {
                if(input=='\n') fprintf(stdout,"\n");
                if(input==' ') {
                        fprintf(stdout,"   ");
                }
                if(input>47&&input<58) {
                        fprintf(stdout,"%s ",morse[input-48].morseCode);
                        continue;
                }
                input=toupper(input);
                fprintf(stdout,"%s ",morse[input-55].morseCode);
        }

}

The letter in the struct is not used because I am manipulating the array index, but if you try to add symbols, it may be necessary to actually run through the entire array every time and compare the input value with the letter and print the corresponding morse code value. And I can't understand the output that I'm getting either.

Code:
# ./a.out < test
- .... .. ...    ÿ¿ü .. ...    ÿ¿ü .-    ÿ¿ü - . ... -    ÿ¿ü ..-. .. .-.. .    ÿ¿ü - . ... - .. -. --.    ÿ¿ü ..-. --- .-.    ÿ¿ü -- --- .-. ... .    ÿ¿ü -.-. --- -.. .    ÿ¿ü - .... .- -    ÿ¿ü .. ...    ÿ¿ü -- --- .-. ... .    ÿ¿ü .- -. -..    ÿ¿ü -. --- -    ÿ¿ü -- --- .-. --.. .

Why are those symbols popping up after every " " that is encountered?
# 7  
Old 05-28-2007
Perhaps this ?
Code:
                input=toupper(input);
                fprintf(stdout,"%s ",morse[input-55].morseCode);

becomes

Code:
        else if ((input >= 65 && input <= 90) || (input >= 97 && input <= 122))
            {
                input=toupper(input);
                fprintf(stdout,"%s ",morse[input-55].morseCode);
                continue;
            }
        if (input)
            fprintf (stdout, " %c ", input);

You did not handle characters outside the set [A-Za-z].
Code:
[/tmp]$ ./morse
www.unix.com
.-- .-- .--  . ..- -. .. -..-  . -.-. --- --


Last edited by vino; 05-28-2007 at 01:35 AM..
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