Here's a simple programming challenge for all of you. For each challenge, you get 5 points for writing code that does what it's supposed to. 1 - 4 points for code that doesn't work, depending on how close it it. In a few days I'll write my own code for each challenge, and you get five points if you code runs faster than mine and 5 points if your code uses less memory than mine :D
You can use either C or C++ and any of the STL.
I think it'll give you guys some good practice as well as learn from a pro.
Spoiler for Challenge 1:
You have a path to a file, the problem is that it is a windows path and you need to use it on a *Nix machine. Windows uses forwards slashes for paths, *nix uses backslashes. You need to write a method that will convert
"..\MyDocuments\Programming\Tut1.cpp"
into
"../MyDocuments/Programming/Tut1.cpp"
Code:
#include <stdio.h>
const char * ConvertToUNIX(const char * str){
/*
This method returns a char string, which is a UNIX encoded path
The parameter is the original string
Your conversion goes here
*/
}
int main(void){
/*
There are two slashes for each one because \ is a reserved character
which tells the compiler that a special character is coming after it, \
is the id of that special character, in the string it will act like one
character.
*/
const char * path = "..\\MyDocuments\\Programming\\Tut1.cpp";
printf("Modified string: %s\n", ConvertToUNIX(path));
}
Spoiler for Challenge 2:
Write a method that returns the sum of all values from one to and including n. For example, if you pass the method the number 5, it should return 10 (5 + 4 + 3 + 2 + 1)
Code:
#include <stdio.h>
unsigned int Sum(unsigned int Value){
//Your code here
}
int main(){
unsigned int TestValue = 10000;
printf("Original string: %d\n", Sum(TestValue));
}
Spoiler for Challenge 3:
Write a method that parses a simple string and analyzes it. The string will have one of the basic math operations (* - + /) and two numbers. ie "12+4". It will always be two numbers and one operation.
Code:
#include <stdio.h>
int Analyze(const char * math){
//Your code goes here
}
int main(void){
char * test = "12+4";
printf("Answer: %d\n", Analyze(test));
char * test2 = "4*100";
printf("Answer: %d\n", Analyze(test2));
}
Spoiler for Challenge 4:
Write a method that counts how many time a certain character appears in a string.
Code:
#include <stdio.h>
int Count(const char * str, char search){
//Your code goes here
}
int main(void){
char * test = "Hello World, I'm a lil string";
printf("Answer: %d\n", Count(test, 'l'));
}
Spoiler for Challenge 5:
Write a method that counts how many time a certain character appears in a string.
Code:
#include <stdio.h>
#define NumberOfNumbers 15
void Sort(int * array){
//Your code goes here
}
int main(void){
int test[]= {10, 4, 65, 12, 43, 53, 23, 6, 13, 54, 12, 43, 45, 12, 44};
Sort(test);
unsigned int i;
for (i = 0; i < NumberOfNumbers; i++){
printf("Number %d: %d\n", i, test[i]);
}
}
06-11-2009, 02:10 AM
khh
I have a few questions:
May we search online for solutions to things we don't know how to do?
If so where is the limit? Like, it's ok to search for a function reference, but not for more complicated things, or?
Are we supposed to use everything you've written in your code, or can we change from type const char * to std::string if we wish?
When or if we complete the task, shall we post it here with code, post it here with link to code, or PM you with the code or a link to it?
Edit: Also, you've written the wrong description in challenge 5.
06-11-2009, 02:30 AM
ninja9578
Post your code, you can look up whatever you want from algorithms to function references.
06-11-2009, 05:23 AM
dsr
I'll take care of the two easiest and leave the rest for ye who hath more time on thy hands.
Spoiler for 1:
1. This isn't a task for a lower level language when you could just do
Code:
sed -e 's#\\#/#'
Wrap it in system() if you'd like. :P
Spoiler for 2:
2. The mathematical solution runs in constant time
Code:
int f(int n) { return (n + 1) * n / 2; }
06-11-2009, 11:30 AM
khh
Ok, I think I've solved the 4 first. I'm a bit confused by the fifth, though. Either there're 1 item too little in the array, or I'm missing something.
Spoiler for Challenge 1:
Code:
#include <stdio.h>
#include <string.h>
const char * ConvertToUNIX(const char * str){
unsigned int n = 0;
char* str2 = new char[strlen(str) + 1];
for ( ; str[n] ; n++) {
if (str[n] != '\\') {
str2[n] = str[n];
} else {
str2[n] = '/';
}
}
str2[n] = '\0';
return str2;
}
int Analyze(const char * math){
unsigned int n = 0, len = strlen(math);
int ans;
char *n1, *n2;
while (n < len) {
if (math[n] < 48 || math[n] > 57) {
break;
}
n++;
}
n1 = new char[n+1];
n2 = new char[len - n];
strncpy(n1, math, n);
strncpy(n2, math+n+1, (len - n));
n1[n] = '\0';
n2[len - n] = '\0';
switch (math[n]) {
case '*':
ans = (atoi(n1)*atoi(n2));
break;
case '/':
ans = (atoi(n1)/atoi(n2));
break;
case '+':
ans = (atoi(n1)+atoi(n2));
break;
case '-':
ans = (atoi(n1)-atoi(n2));
break;
default:
ans = 0;
}
delete[] n1;
delete[] n2;
return ans;
}
int main(void){
char * test = "12+4";
printf("Answer: %d\n", Analyze(test));
char * test2 = "4*100";
printf("Answer: %d\n", Analyze(test2));
}
Spoiler for challenge 4:
Code:
#include <stdio.h>
int Count(const char * str, char search){
unsigned int i = 0, n = 0;
while (str[n]) {
if (str[n] == search)
i++;
n++;
}
return i;
}
int main(void){
char * test = "Hello World, I'm a lil string";
printf("Answer: %d\n", Count(test, 'l'));
}
Also, it'd be nice if other people hide their code too. So that the ones who come here to try can find their own solutions, and don't get influenced by the ones that have been posted.
06-11-2009, 01:47 PM
ninja9578
I added spoiler tags around dsr's code. I also altered the 5th one, seems I forgot a value in the array, and some C compilers don't allow #defines inside of the main method
06-11-2009, 03:31 PM
khh
Thanks.
Here's my suggestion to the fifth
Spoiler for Challenge 5:
Code:
#include <stdio.h>
#define NumberOfNumbers 15
void Sort(int * array){
int tmp;
for (unsigned int i = 0 ; i < NumberOfNumbers ; i++) {
bool noChange = true;
for (unsigned int n = 0; n < (NumberOfNumbers - i - 1) ; n++) {
if (array[n] > array[n+1]) {
tmp = array[n];
array[n] = array[n+1];
array[n+1] = tmp;
noChange = false;
}
}
if (noChange) {
break;
}
}
}
int main(void){
int test[]= {10, 4, 65, 12, 43, 53, 23, 6, 13, 54, 12, 43, 45, 12, 44};
Sort(test);
unsigned int i;
for (i = 0; i < NumberOfNumbers; i++){
printf("Number %d: %d\n", i, test[i]);
}
}