 Programming Challenges - Page 15

1. Bad Speller ^^
Join Date
May 2001
Location
Uh, I'm somewhere where I don't know where I am.
Posts
1,228
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;
}
}
}
}```

2. Bad Speller ^^
Join Date
May 2001
Location
Uh, I'm somewhere where I don't know where I am.
Posts
1,228
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.

3. 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) == 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\$```

4. Bad Speller ^^
Join Date
May 2001
Location
Uh, I'm somewhere where I don't know where I am.
Posts
1,228
The problem is to reverse every other word. like

whats eht matter htiw kansas. I like seip. Chips era good oot.

5. 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) == 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\$```

6. 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;```

7. 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 . 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.
Last edited by tony_yum; 06-14-2005 at 07:13 AM.

8. Bad Speller ^^
Join Date
May 2001
Location
Uh, I'm somewhere where I don't know where I am.
Posts
1,228
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.

9. 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;

}```

10. Registered User
Join Date
Apr 2001
Location
SF Bay Area, CA
Posts
14,947 Originally Posted by tony_yum
Code:
`   unsigned char p = (unsigned char)pow(2.0, (double)i);`
*involuntary shudder* Instead of using a floating point math function, here's what I'd do:

Code:
`   unsigned char p = (unsigned char)(1 << i);` Last edited by bwkaz; 07-26-2005 at 08:11 PM.

11. unsigned char p = (unsigned char)(1 << i);
Oh yeah that's for the correction

12. Registered User
Join Date
May 2005
Location
in the Bash shell.
Posts
193
write a program that can access protected memory without help from the linux kernel. :-)
Last edited by zeroth; 09-20-2005 at 04:05 PM.

13. Registered User
Join Date
Apr 2001
Location
SF Bay Area, CA
Posts
14,947
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. 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.

14. Registered User
Join Date
May 2005
Location
in the Bash shell.
Posts
193 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. 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

15. Registered User
Join Date
Oct 2005
Posts
19 Originally Posted by grady
swap two integers without using a temporary variable.
It's easy !
Code:
```void swap(int *_a, int *_b)
{
*_a=*_a+*_b;
*_b=*_a-*_b;
*_a=*_a-*_b;
}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•