Add Scroogle to your search area in Firefox 2.0 Install the 'Scroogle Scraper' search plugin.

Here’s a quick example of weighted round robin in php.

//weighted round robin
 
define("START_WEIGHT", 1);
class printer
{
  private $name;
  private $weight;
  private $normalizedWeight;
  public function printer($name,$weight=START_WEIGHT)
  {
    $this->name=$name;
    $this->weight=$weight;
  }
  public function getName() { return $this->name; }
  public function getWeight() { return $this->weight; }
  public function setWeight($weight) { $this->weight=$weight; }
  public function getNormalizedWeight(){ return $this->normalizedWeight; }
  public function setNormalizedWeight($weight) { $this->normalizedWeight=$weight; }
 
}
 
$printerList=array();
$printerList[]=new printer("printer1",1);
$printerList[]=new printer("printer2",2);
$printerList[]=new printer("printer3",4);
$printerList[]=new printer("printer4",1);
 
$jobList=array(‘job1′,‘job2′,‘job3′,‘job4′,‘job5′,‘job6′,‘job7′,‘job8′,‘job9′,‘job10′,‘job11′,‘job12′,‘job13′,‘job14′,‘job15′,‘job16′);
 
$min=2147483647; //a large number
foreach($printerList as $printer)
{
  $min=($printer->getWeight()<$min)?$printer->getWeight():$min;
}
foreach($printerList as $printer)
{
  $printer->setNormalizedWeight($printer->getWeight()/$min);
}
 
$jobCount=count($jobList);
while($jobCount>0)
{
  foreach($printerList as $printer)
  {
    $jobCount=count($jobList);
    if ($jobCount>0)
    {
      $jobList=jobPush($printer,$jobList,$jobCount);
    }
  }
}
 
function jobPush($printer,$jobList,$jobCount)
{
  for($x=0;$x<$printer->getNormalizedWeight();$x++)
  {
    if ($jobCount>0)
    {
      $job=array_shift($jobList);
      $jobCount–;
      echo($printer->getName()." getting ".$job."n");
    }
  }
  echo("n");
  return $jobList;
}

Recently I helped direct and produce a biographical documentary. Here’s the equipment list I used:

  • Canon XHA1 video camera
  • Camera tripod
  • Boom pole
  • Directional mic with windscreen (dead cat)
  • XLR cables
  • MiniDV tapes
  • Macbook Pro w/ iMovie and iDVD
  • Dell Laptop with photoshop
  • Epson flatbed scanner
  • Radio Shack 3 channel audio mixer
  • Sennheiser condenser microphone with stand (for narration)
  • External 1TB drive
  • Mediawiki
  • Royalty free music (http://incompetech.com/m/c/royalty-free/)

Do I know anything about film making? Not really. I did read a bunch of books. And I watched an awful lot of documentaries to get ideas. Special thanks to Tyler T. for the technical advice!

I had a friend send me a list of these questions today. some are repeats from previous posts but there are several new ones. Enjoy!

Logic Problems

a) 8 Steel balls, 1 heavier than all the rest. Use a Balance Scale to determine the heaviest ball in the shortest number of tries.

b) Find how many trailing zeroes are in the product of 100! (100 Factorial).

c) Replace each letter below with a corresponding number from 0 to 8. Each Letter can only be matched to one number (ie, all of the E’s would be a 3, and that would mean that no other Letter could be a 3). The following addition problem should work after you replace the letters.

   S  E  V  E  N
   S  E  V  E  N
+         S   I   X
-----------------------
 T W E N  T   Y

Programming Problems

a) Write a function that takes an array of integers from 1 to 1 million, with one number being duplicated. Determine which integer is a duplicate, accounting for speed and efficiency.

b) Write a function that takes an array of integers from 1 to 1 million, with one number being missing from the range. Determine the missing integer, account for speed and efficiency.

c) Write a function that takes an array of an number of integers. Determine the largest contiguous sum, meaning given any two integers in the array, what is the highest sum? Given an array such as Array(1, -5, 3, -3, -9, 2) the highest contiguous sum would be 5.

d) Write a function that takes a string as a parameter and determine the first unique character in the string. In other words, given the string “sissy” the answer would be “i”. Afterwards, how would you optimize the function.

e) Pseudo-code the Ball Clock problem, determining how many cycles it takes for the balls to end up in the same position in the queue tray. Afterwards, how would you optimize the process if it were to be hit thousands of times on a given interface by thousands of users. If you have come up with a pure brute-force method, how could you optimize it further to shorten the processing time?

Google just released their new browser, Chrome. You can read all about it, comic-style, on Google Books.

I just downloaded it. I’ll let you know how it goes

it’s not as difficult as it first looks:

Given two water faucets, one hot and one cold, and one bucket. The hot water fills the empty bucket in eight minutes. The cold water fills the bucket in seven minutes. Then I make a hole in the bucket such that the full bucket drains in four minutes. Now take the empty bucket, with the hole, and place under both faucets together. Turn on the hot and cold water at the same time. How long does it take to fill the bucket halfway?

reversing a string is a classic tech interview question. of course you’re not allowed to call any built-in reverse() function. A solution might look like, in my pseudocode:

someString = new string("732432424");
char temp;
int end=someString.length()-1;

for (int x=0;x<end;x++)
{
	temp=someString[end];
	someString[x]=someString[end];
	someString[end]=temp;
	end--;
}

but sometimes the interviewer asks you to reverse the string in place, without using other memory, and for that, we’ll need our XOR friend

A B  XOR
----+---
0 0 | 0
0 1 | 1
1 0 | 1
1 1 | 0
someString = new string("732432424");
char temp;
int end=someString.length()-1; //assume zero based char arrays

for (int x=0;x<end;x++)
{
	someString[x]=someString[x] XOR someString[end];
        // first loop: someString[x]=3
	someString[end]=someString[end] XOR someString[x];
        //first loop: someString[end]=7
	someString[x] = someString[x] XOR someString[end];
       //first loop: someString[x]=4
	end--;
}

ME: did you install xampp?
ME: and, in the xampp control panel, did you start apache?
ME: and, in a browser, did you go to http://localhost
ME: and, once, there, did you see a xampp welcome message?
SOMEGUY: yes, yes and yes
ME: when you go to http://www.whatismyip.com what does it say for your ip address?
SOMEGUY: [address ]
ME: is that computer running microsoft’s firewall? if so, can you shut it off?
SOMEGUY: ok
ME: are you using a local router/firewall? if so, you need to let/forward port 80 through to that “server”
ME: the point is that port 80 is not responding externally, so we need to fix it. to get into your router, you just need to know your default gateway.
ME: start->run
ME: type “cmd”
ME: then type “ipconfig” (no quotes) and press enter
ME: you’re looking for the Default Gateway IP address
SOMEGUY: it is 192.168.1.1
ME: ok. go to that in a browser
ME: if you use default linksys settings, there will be no username and the password is admin
ME: ok, so there should be some provision for forwarding ports.
ME: sometimes it’s under firewall->single port forwarding
ME: you’ll need to forward port 80 to the ip address of that server computer
ME: it says prot range forward
ME: yeah, so you can forward 80 to 80 to the ip of the server at 80 to 80
ME: you can find the ip of that server by doing the start->run, cmd, ipconfig command again
ME: go to http://www.[sitename].com/ (i set up DNS for you)
ME: does it work for you?
SOMEGUY: it brings up xampp for windows
ME: yup, that means it’s working
ME: ok, now we need to edit us some files
ME: step 1
ME: in that folder create a file called index.html, and in that file put “hello world”
ME: you could use notepad to create a new text file, but when you save it, be sure it doesn’t append a .txt to the end of index.html
SOMEGUY: can i use word
ME: if word allows you save in plain text format, then yes
ME: again, make sure the file name is index.html and not index.html.txt
ME: when you browse that folder (c:\xampp\htdocs\[sitename]) can you now see a little browser file icon called index, and if you double click it, does ie open a window with the text you typed in?
SOMEGUY: yes
ME: now time to edit another file
ME: go to: C:\xampp\apache\conf\extra
ME: and use notepad to edit httpd-vhosts.conf
ME: delete everything out of that file, and paste in the following:
ME: NameVirtualHost *:80

DocumentRoot “C:\xampp\htdocs\[sitename]”
ServerName *.[sitename].com

ME: then save that file
ME: and then stop and start xampp
ME: from the control panel
ME: start and stop apache, i mean, from the control panel
ME: go to http://www.[sitename].com/
SOMEGUY: i stopped it and started it
ME: what do you see?
SOMEGUY: HELLO WORLD
ME: that’s what I see, too. you’re hosting a webpage now for the ENTIRE WORLD!!!
ME: you’ll probably need dreamweaver (which you can get a trial version free download) to start pimping out your site
ME: the point is that anything you place in c:\xampp\htdocs\[sitename] will be accessable to the world on www.[sitename].com
ME: so, if you had an mp3 called “mysong.mp3″ and you copied it into c:\xampp\htdocs\[sitename] your friends could download it by going to http://www.[sitename].com/mysong.mp3
ME: the html files are for your webpages, but the server will serve up any file you place in that folder
SOMEGUY: how do i make links
ME: dreamweaver is a good editor and you can get lots of cool templates free.
ME: in dreamweaver it’s easy. if you’re editing raw html text, a link looks like this
SOMEGUY: i now have an mp3 in that folder
ME: obviously, you don’t want the mpaa to know you’re hosting mp3s. it’s illegal and all. i was just providing you an example
ME: that index file is the root file that apache/xampp will serve up if the filename is not explicited stated in the URL. you should just point dreamweaver to that file to edit it.
ME: you can, obviously, add more files to that folder, for other pages on your site. maybe a songs.html, for a list of your songs. then you just link to songs.html from your index.html
ME: as well, because apache/xampp uses index.html as the default name, if you wanted www.[sitename].com/songs to work (instead of songs.html), you simply need to create a subfolder in c:\xampp\htdocs\[sitename] called songs and put an index.html file in that subfolder with your song list

1. http://www.php.net/manual/en/control-structures.alternative-syntax.php shows an alternative syntax for loops:

    foreach($list as $item)
    {
        echo $item;
    } 

can be done as:

    foreach($list as $item):
        echo $item;
    endforeach

2. if your server has short tags enabled, you can do this:

<?

$somevar="hello";

?>

<?=$somevar?>

this script checks to see if a program is running and starts it if it is not. if a certain time has passed the script will kill the running program

#!/bin/bash
 
#location of script to monitor
PROGRAM=‘myscript.php’
COUNTER=0
 
#!/bin/bash
 
date2stamp () {
    date –utc –date "$1" +%s
}
 
# convert a date into a UNIX timestamp
# time afterwhich script should not run
finishtime=$(date2stamp "Thu Nov  1 15:22:00 MDT 2007")
#echo $finishtime
 
while [ 1 ];
do
 
nowtime=`date –utc +%s`
#echo $nowtime
diffTime=$((finishtime-nowtime))
if ((diffTime > 0));
then
echo "still time to go"
else
echo "TIME UP!: checking if running:"
PROG_CHECK=`ps aux|grep $PROGRAM|wc -l`
if [ $PROG_CHECK -gt 1 ];
then
echo "running after time up"
PROG_ID=`ps uax|grep $PROGRAM |head -n 1|awk ‘{print $2}’`
#echo "KILLING"
#kill -9 $PROG_ID
else
echo "not running after time up"
fi
exit 65
fi
 
 
PROG_CHECK=`ps aux|grep $PROGRAM|wc -l`
if [ $PROG_CHECK -gt 1 ];
then
echo "running"
else
let COUNTER=COUNTER+1
echo "not running: starting: new log file: $COUNTER"
/usr/local/bin/php /root/$PROGRAM &
fi
 
#time in seconds to sleep
sleep 5
 
done

We recently upgraded from MySQL 4 to MySQL 5 and we noticed strange characters in some of the varchar and text fields.

step 1: dump out the contents of the bad field in hex
mysql> select hex(myfieldname) from mytablename where myid=’something’;

step 2: run a php program to print out the chars one at a time

function hex2asc($temp) {
  $len = strlen($temp);
  for ($i=0;$i<$len;$i+=2) {
  $data[$i]=chr(hexdec(substr($temp,$i,2)))."  ".substr($temp,$i,2);
 
  }
  return $data;
}
$str="HEX GOES HERE";
$data=hex2asc($str);
print_r($data);

step 3: once you’ve identified the bad chars and their hex values, change them in the database

here are some bad chars I found:

– fix apostrophe
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’92′),”‘”);

– fix left single quote
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’93′),”‘”);

– fix right single quote
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’94′),”‘”);

– fix bullets
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’95′),”& #8226;”);

– fix double dash
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’96′),”–”);

– fix triple dash
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’97′),”—”);

— fix supscripted TM
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’99′),”<sup>TM</sup>”);

– replace jacked up apostrophe/single quote
– delete EFs
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’EF’),”");
– replace BFs with single quote
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’BF’),”‘”);
– delete BDs
update mytablename set myfieldname=REPLACE(myfieldname,UNHEX(’BD’),”");

see also: http://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html

Next Page »

Send to a friend * Print this page * Join the club * Talk with my robot * Advertise here * Search this Site * Donate * Link to me


Web hosting by Utah Hub *  Powered by CreativeTap *  In association with Segomo
Unless otherwise noted, Copyright 2004-2006, Ryan Byrd. All Rights Reserved.
Ryan Byrd dot net -- probably the coolest site in Utah