# Programming Challenges

Show 40 post(s) from this thread on one page
• 06-01-2005, 09:20 PM
tecknophreak
Find a six digit number that gives its digits reversed when multiplied by an integer between 2 and 9 inclusive.

Gotta keep the C++ people alive:

Code:

```int main() {         for (int i = 100000; i < 1000000; ++i) {                 for (int j = 2; j < 10; ++j) {                         int reverse((i % 10) * 100000 + ((i / 10) % 10) * 10000                                 + ((i / 100) % 10) * 1000 + ((i / 1000) % 10) * 100                                 + ((i / 10000) % 10) * 10 + i / 100000);                         if (i * j == reverse) {                                 cout << i << " + " << j << " = " << reverse << endl;                         }                 }         } }```
• 06-01-2005, 09:30 PM
tecknophreak
Here's one I stumbled across:

Consider a character set consisting of letters, a space, and a point. Words consist of one or more, but at most 20 letters. An input text consists of one or more words separated from each other by one or more spaces and terminated by 0 or more spaces followed by a point. Input should be read from, and including, the first letter of the first word, up to and including the terminating point. The output text is to be produced such that successive words are separated by a single space with the last word being terminated by a single point. Odd words are copied in reverse order while even words are merely echoed. For example, the input string

: whats the matter with kansas.

becomes

: whats eht matter htiw kansas.

Easy, but easy ones are fun once in a while too.
• 06-02-2005, 05:16 AM
mrBen
You forgot to reverse 'whats' in your example....

Here is a Python solution - it's a bit clunky, but it works, and it only took a short while to code.

Code:

```#!/usr/bin/python def isEven(number):         if divmod(number, 2)[1] == 0:                 return True         else:                 return False def reverse(mystring):         output = ""         for loop in range(len(mystring)-1, -1, -1):                 output += mystring[loop]         return output input = "whats the matter with kansas. I like pies. Chips are good too." data = input.split(' ') output = "" for word in data:         point = False         if word[-1] == ".":         point = False         if word[-1] == ".":                 word = word[:-1]                 point = True         if isEven(len(word)):                 output += word         else:                 output += reverse(word)         if point:                 output += ". "         else:                 output += " " print output```
And in action:
Code:

```mrben@hobbes:~/temp/script\$ python words.py stahw eht matter with kansas. I like pies. spihC era good oot. mrben@hobbes:~/temp/script\$```
• 06-02-2005, 09:46 AM
tecknophreak
The problem is to reverse every other word. like

whats eht matter htiw kansas. I like seip. Chips era good oot.
• 06-02-2005, 10:03 AM
mrBen
Doh! I read it as reversing every word that had an odd number of letters......

Amended script:

Code:

```#!/usr/bin/python def isEven(number):         if divmod(number, 2)[1] == 0:                 return True         else:                 return False def reverse(mystring):         output = ""         for loop in range(len(mystring)-1, -1, -1):                 output += mystring[loop]         return output input = "whats the matter with kansas. I like pies. Chips are good too." data = input.split(' ') output = "" count = len(data) if not isEven(count):         count -= 1 for index in range(0, count, 2):         output += data[index]+" "         if not data[index+1][-1] == ".":                 output += reverse(data[index+1])+" "         else:                 output += reverse(data[index+1][:-1])+". " if not isEven(len(data)):         output += data[-1] print output```
Output, using 2 sentences with odd and even numbers of words:
Code:

```mrben@hobbes:~/temp/script\$ python words2.py whats eht matter htiw kansas. I like seip. Chips era good oot. mrben@hobbes:~/temp/script\$ mrben@hobbes:~/temp/script\$ python words2.py whats eht matter htiw kansas. I like seip. Chips era really doog too. mrben@hobbes:~/temp/script\$```
• 06-14-2005, 05:31 AM
janet loves bill
Quote:

Originally posted by janet loves bill
Find a six digit number that gives its digits reversed when multiplied by an integer between 2 and 9 inclusive.
there are at least 3 ways this can be written in perl....
here is one............:)
Code:

```#!/usr/bin/perl -w         for(\$number=1e5; \$number <= 5e5; \$number++){         for(\$num=2; \$num <= 9; \$num++){         if(\$number*\$num==reverse(\$number)){         print("\$number times \$num\n");}}}         exit;```
• 06-14-2005, 06:03 AM
tony_yum
Quote:

Consider a character set consisting of letters, a space, and a point. Words consist of one or more, but at most 20 letters. An input text consists of one or more words separated from each other by one or more spaces and terminated by 0 or more spaces followed by a point. Input should be read from, and including, the first letter of the first word, up to and including the terminating point. The output text is to be produced such that successive words are separated by a single space with the last word being terminated by a single point. Odd words are copied in reverse order while even words are merely echoed. For example, the input string

A slightly cheating version in ruby :D . Doesn't take into account that there might be more then one space between words.

Code:

```#!/usr/bin/ruby sentences = 'whats the matter with kansas. I like pies. Chips are good too. '.split('. '); reverse = true sentences.each { | sentence |     words = sentence.split(' ')     (0..words.length).each { | i |         word = words[i]         next if word == nil         word.reverse! if reverse = !reverse         print word + (i == words.length - 1 ? '. ' : ' ')     } } print "\n"```
Output:

whats eht matter htiw kansas. I like seip. Chips era good oot.
• 07-25-2005, 09:43 AM
tecknophreak
How about having a function which takes two vars, an unsigned char pointer and an integer. The integer tells you what bit to flip on the unsigned char the pointer points to. Not a hard one, but a little fun challenge non-the-less.
• 07-26-2005, 04:30 AM
tony_yum
Quote:

How about having a function which takes two vars, an unsigned char pointer and an integer. The integer tells you what bit to flip on the unsigned char the pointer points to. Not a hard one, but a little fun challenge non-the-less.
Here's my little function :), assuming the integer 0 is the least significant bit in the unsigned char.

Code:

```void flip (unsigned char *c, int i) {   unsigned char p = (unsigned char)pow(2.0, (double)i);   *c = *c  ^ p; }```
• 07-26-2005, 07:09 PM
bwkaz
Quote:

Originally Posted by tony_yum
Code:

`  unsigned char p = (unsigned char)pow(2.0, (double)i);`

*involuntary shudder*

:p

Instead of using a floating point math function, here's what I'd do:

Code:

`  unsigned char p = (unsigned char)(1 << i);`
;)
• 07-27-2005, 04:37 AM
tony_yum
Quote:

unsigned char p = (unsigned char)(1 << i);
Oh yeah :) that's for the correction
• 09-20-2005, 03:03 PM
zeroth
write a program that can access protected memory without help from the linux kernel. :-)
• 09-20-2005, 07:06 PM
bwkaz
Code:

```fd = open("/dev/mem", O_RDWR); /* do stuff with fd */```
Alternately:

Code:

```char *ptr = malloc(however_much_you_need); /* do stuff with ptr */```
since all memory is "protected" as far as the Intel docs go. :p

But to be less pedantic, there's no other way to do it that I know of. Because while you do know where the paging tables are held in physical memory (you can get this from the PDBR, which is register CR3 on x86, IIRC, which user-mode code does have read access to), you don't know where in linear memory this physical address is mapped.

If you did know that piece of information, you have access to the entire 4GB linear address space, so you could maybe write to those addresses. (The page table must be mapped in, otherwise the processor wouldn't be able to do paging. So it's available somewhere, you just don't know where.) But those pages may also be marked supervisor-only (the U/S bit in the page table), which would mean writing to them would cause a segfault.
• 09-21-2005, 08:07 AM
zeroth
Quote:

Originally Posted by bwkaz
Code:

```fd = open("/dev/mem", O_RDWR); /* do stuff with fd */```
Alternately:

Code:

```char *ptr = malloc(however_much_you_need); /* do stuff with ptr */```
since all memory is "protected" as far as the Intel docs go. :p

But to be less pedantic, there's no other way to do it that I know of. Because while you do know where the paging tables are held in physical memory (you can get this from the PDBR, which is register CR3 on x86, IIRC, which user-mode code does have read access to), you don't know where in linear memory this physical address is mapped.

If you did know that piece of information, you have access to the entire 4GB linear address space, so you could maybe write to those addresses. (The page table must be mapped in, otherwise the processor wouldn't be able to do paging. So it's available somewhere, you just don't know where.) But those pages may also be marked supervisor-only (the U/S bit in the page table), which would mean writing to them would cause a segfault.

I tried my whack at a memory-editor for linux in C once, only to find out what "Protected Memory" meant :-p

SegFaults are a pain in my
• 11-01-2005, 10:43 PM
naihe2010
Quote:

```void swap(int *_a, int *_b) {     *_a=*_a+*_b;     *_b=*_a-*_b;     *_a=*_a-*_b; }```