Friday, April 15, 2011

String Permutation.

I Have Been Working On This Forever

First: String permutation is finding every possible combination of the letters in a string. For example:

string = "abc"



I have had a ton of fun with this, essentially I knew there were solutions available, and I'm sure there are a lot of better solutions as well. However, here's what I came up with.

Step 1. Write a function to swap two characters.
Step 2. Write a recursive function to return all permutations of a given string. It will call itself (minus the) first letter.


string getstringwithswappedchars(int first, int second, string toswap){ char buf = toswap[first]; toswap[first] = toswap[second]; toswap[second] = buf; return toswap; }


I was lying in bed, attempting to sleep, and *pop* I knew how to do it! My solution is essentially this. Take the previous call in the recursive loop (down to one letter which shall return itself) append the current letter to each, then add yourself to a vector after switching the current letter with every single letter in each permutation. beautiful
vector <string> permute(string toperm){ cout << "\npermuting " << toperm; vector toret; if (toperm.size() <= 1){ toret.push_back(toperm); return toret; } vector tvect = permute(toperm.substr(1,toperm.size()-1)); for (int b = 0; b < tvect.size(); b++){ for (int a = 0; a < toperm.size(); a++){ toret.push_back(getstringwithswappedchars(0,a,toperm[0] + tvect[b])); } } return toret; }

Thursday, April 14, 2011

First Post <(0.o)>

*** Well it's about time!!! *** (I can hear some of you yelling at your computer)

In this blog I hope to entertain you (oh great, another programmer without a sense of humor), and perhaps teach you some stuff. So lets start off with a little of both!


A Joke:

A man died and went to heaven. As he stood in front of St. Peter at the Pearly Gates, he saw a huge wall of clocks behind him. He asked, 'What are all those clocks?'

St. Peter answered, 'Those are Lie-Clocks. Everyone on Earth has a Lie-Clock. Every time you lie the hands on your clock will move.'

'Oh,' said the man, 'whose clock is that?'

'That's Mother Teresa's. The hands have never moved, indicating that she never told a lie.'

'Incredible,' said the man. 'And whose clock is that one?'

St. Peter responded, 'That's Abraham Lincoln's clock. The hands have moved twice, telling us that Abe told only two lies in his entire life.'

'So where's George Bush's clock?' asked the man.

'Bush's clock is in Jesus' office. He's using it as a ceiling fan.


And a little bit of programming:

As it's my first post, a small prank program! Don't expect much trivial stuff like this, I'll try to post useful things that I learn, and updates on projects, and keep stuff like this to a minimum.

#include < windows.h > using namespace std; void setKey(int key,bool down, bool scan = true){ BYTE keyState[256]; if (scan){ GetKeyboardState((LPBYTE)&keyState); if( down && !(keyState[key] & 1)){ keybd_event( VkKeyScan(key), /*MapVirtualKeyA(key,2)*/0, KEYEVENTF_EXTENDEDKEY | 0, 0 ); } else if ( !down && (keyState[key] & 1)){ // Simulate a key release keybd_event( VkKeyScanA( key), /*MapVirtualKeyA(key,2)*/0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } else{ GetKeyboardState((LPBYTE)&keyState); if( down && !(keyState[key] & 1)){ keybd_event( key, /*MapVirtualKeyA(key,2)*/0, KEYEVENTF_EXTENDEDKEY | 0, 0 ); } else if ( !down && (keyState[key] & 1)){ // Simulate a key release keybd_event( key, /*MapVirtualKeyA(key,2)*/0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); } } } int main(){ int time = GetTickCount(); int count = 0; while (count < 2){ for (int temp = 0; temp < 256; temp++){ setKey(temp,true,false); setKey(temp,false,false); Sleep(10); } if (GetTickCount() - time >= 10000){ count++; time = GetTickCount(); } } }

A note: the previous program is extremely annoying, and I wouldn't recommend using this on any computer. However it does demonstrate some useful functionality!

Here's the gist of simulating keypresses in windows- you have a couple options, the most predominate the keybd_event() function above. There are also other references you may wish to view:

Software Simulation Intro && MSDN

You also have the option of writing a driver to create the input, here are some possibly relevant links:

-WikiBook intro
-c++ vs asm
-MSDN Drivers
-Driver Interrupts