18 April 2012

Quadractic equation

This equation I like more than the "beatiful" equation () mainly becaus it's not that mysterious. There is certain propoties of the quadractic equation that can be looked at and tested.
1st one is what shape will the equation be:

x^2x
Is the shape of:



And The second property you can get is directly from the quadractic equation, whether a equation has 2 roots, 1 root or it has imaginary roots

x^2x
b^2-4ac=
How many roots has this equation got?



Okay that was fun hope you enjoy.

18 March 2012

CodeEval FizzBuzz

CodeEval is a site where programmers can compete for jobs or in my case hopefully learn something more. I have small experience in Java and Visual Basic. I have slightly more experience in HTML, JavaScript and PHP. So I use this site for practise.
The 1st problem they put on there is FizzBuzz, the problem is below:

Description

Players generally sit in a circle. The player designated to go first says the number "1", and each player thenceforth counts one number in turn. However, any number divisible by 'A' e.g. three is replaced by the word fizz and any divisible by 'B' e.g. five by the word buzz. Numbers divisible by both become fizz buzz. A player who hesitates or makes a mistake is either eliminated.

Write a program that prints out the the pattern generated by such a scenario given the values of 'A'/'B' and 'N' which are read from an input text file. The input text file contains three space delimited numbers i.e. A, B, N. The program should then print out the final series of numbers using 'F' for fizz, 'B' for 'buzz' and 'FB' for fizz buzz.

Input sample:

Your program should read an input file (provided on the command line) which contains multiple newline separated lines. Each line will contain 3 numbers which are space delimited. The first number is first number to divide by ('A' in this example), the second number is the second number to divide by ('B' in this example) and the third number is where you should count till ('N' in this example). You may assume that the input file is formatted correctly and is the numbers are valid positive integers.e.g.

3 5 10
2 7 15

Output sample:

Print out the series 1 through N replacing numbers divisible by 'A' by F, numbers divisible by 'B' by B and numbers divisible by both as 'FB'. Since the input file contains multiple sets of values, your output will print out one line per set. Ensure that there are no trailing empty spaces on each line you print.e.g.

1 2 F 4 B F 7 8 F B
1 F 3 F 5 F B F 9 F 11 F 13 FB 15

Unfortunately when I did the Java solution I couldn't do it, so I searched for the solution online. DO NOT CLICK LOOK UNLESS YOU HAVE TRIED IT FIRST
/*Sample code to read in test cases:
public class fizzbuzz {
    public static void main (String[] args) {
    ...
    File file = new File(args[0]);
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line;
    while ((line = in.readLine()) != null) {
        String[] lineArray = line.split("\s");
        if (lineArray.length > 0) {
            //Process line of input Here
        }
    }
  }
}
*/
import java.io.*;

import java.util.*;

public class fizzbuzz {

    public static void main(String[] args) throws IOException{

        Scanner console = new Scanner(new FileReader(args[0]));
        while(console.hasNext()){
            int first=console.nextInt();
            int second =console.nextInt();
            int third =console.nextInt();

            for(int i=1;i<=third;i++){
                if(i%first==0 && i%second==0 ){
                    System.out.printf("FB ");
                }
                else if(i%first==0)
                    System.out.printf("F ");
                else if(i%second==0)
                    System.out.printf("B " );
                else
                    System.out.printf("%d ",i);
            }
            System.out.println();
        }
        console.close();
    }
}
The last bit of the code I can do, it's just I couldn't read a file.
I also decided to see if I could do it in PHP as well
<?php
 /*Sample code to read in test cases:
 $fh = fopen($argv[1], "r");
 while (true) {
 $test = fgets($fh);
 # break loop if $test is an empty line
 # $test represents the test case, do something with it
 }
 */
    $file_get_contents=fopen($argv[1],"r");
     while ( ($line = fgets($file_get_contents)) !== false) {
     $k=0;
           foreach(preg_split("/[\s]/", $line) as $space){
              if($k==0){
                  $f=$space;
               }
                if($k==1){
                  $b=$space;
                }
               if($k==2){
                   $n=$space;
                }
                $k=$k+1;
            }
           for($i=1;$i<=$n;$i++){
                if(($i % $f==0)&&($i % $b==0)){
                 echo "FB ";
                }
                elseif($i % $f==0){
              echo "F ";
             }
                elseif($i % $b==0){
                 echo "B ";
                }
                else{
                 echo $i." ";
                }
            }
            echo "\n";
        }
 ?>
Good tip they do provide comments to help you through it, which is rather handy. Anyway I also tried to do it in JavaScript:
/*Sample code to read in test cases:
function codeEvalExecute(line) 
{ //your code goes here. line is your test case 
//return 
}*/
function codeEvalExecute(line){
 //an array with 3 values
 var Byline=line.split(" ");
 //fizz
 var f=Byline[0];
 //buzz
 var b=Byline[1];
 //number run to
 var n=Byline[2];
 var string="";
 
 for(var i=1;i<=n;i++){
  if((i % f==0)&&(i % b==0)){
          string=string+"FB ";
        }
        else if(i % f==0){
           string=string+"F ";
       }
        else if(i % b==0){
            string=string+"B ";
        }
        else{
           string=string+i+" ";
       }
 }
 return string;
}
Update 18/03/2012 11.59am
I should point out if you want to learn JavaScript or PHP I suggest w3 school as for Java I don't know any sites so put some in the comments if you have a suggestion

22 January 2012

Physica website testing week 1

The website is finished (sort of Scalar module isn't done yet), all that is left to do is testing. I have received all the Physica test cases and as of Monday I have begun testing the website. Unfortunately some of the test cases are 6 years old so they may need to be updated and isn't exactly helpful.
On the plus side I will be able to test any silly mistakes I have made whilst making the website. Silly mistakes I have made so far and have corrected:
  1. In the Geometry module, gravity couldn't be set below zero.
  2. Also in Geometry module gravity Z wouldn't display
  3. In the Monitor module OUTPUT_INTERVAL was misnamed
  4. In Generic module the code doesn't include "TRANSIENT_RUN"
  5. Also in Generic module>transient>Delta_t>Cfl_Based CFL_LIMIT misnamed
  6. under Generic module>Transient END_TIME default is 1.0E+20
  7. In Inform Option New doesn't reset everything. Also need to add a confirm button for option New
  8. Material properties module doesn't display the code after refresh
  9. not put a confirm new button on inform page
  10. not put in the extra JavaScript needed to display content when clicked on
  11. put an option for FALSE_TIMESTEP and UNDER_RELAXATION to use
Some mistakes that need further investigation:
  1. In Free Surface module>SOLVE_FREESURF CONVECTION_METHOD is missing, one of the options is VAN_LEER
  2. under Free surface module>Level_set_method LSM_WIDTH not found when running Physica
  3. test cases>apps>fill04 run-time error M6201:MATH
Note to self
  1. in the test cases under apps fill01, fill02, fill03, fill04 removed the extension ".gz". gz extension is a file compression
  2. fill04 Inform file solidification module a lot of the code can be set in material properties module
  3. mesh builder page has been updated to included everything, does need checking though
  4. Generic module USER_INTERVAL_SAVE produces images if you have the plotpar file, if not use USER_MODULE
  5. have to put Geometry file first for some reason otherwise it produces errors

8 December 2011

OR careers, MSc project, Discrete Event Simulation

Apologies first. I have not been blogging in a long time mainly because I was caught up doing my MSc project website in which there is a heck of a lot of modules to go through and then check to make sure they display correctly. At the moment I have made all 27 modules of the website, now all I need to is sort out display, repeating JavaScript and the most labour-some task Testing. Or as I like to call it "Testing to destruction". The idea of making it fail is more harsh but sounds more fun.

Okay apologies done. On the 16th November 2011, it was a Wednesday (by the way) I attended a Operational Research Careers Open day. This was in Birmingham and started at 10:00 and went on to 15:30. There were a lot of stalls there so to name as many as I can: After browsing the stalls they had a lot of talks from a lot of different companies on what exactly they do. There was talks from Martin Slaughter from Hartley McMaster Ltd on what they did with Vodafone. It was on opening hours and rotas for staff i.e. whether to open on a Sunday or not? Also a talk from Mike Nicholson from IBM UK talking about a case study they did. Next up was GORS or Government OR Services. They talked about how the work is very varied, job security and an amazing pension (better than the average). Then Andrew Long from British Airways Where he talked about the code on an airway ticket and all the different sections which is put together to form British Airways. Then there was a Company I never heard of before that day Tata Steel I presume this is for engineers who describe themselves as mathematicians but I could be wrong. And the very last talk of the day was Tom Hibbert from Tesco who talks about the different problems Tesco faces like perishable good, how much to order etc.

The Careers day was very informative the only problem was travel costs for me to get there by 10.00 i had to pay £70 roughly and to get home i got the cheapest ticket which was around £18 pounds mark. If however they moved the starting time to nearly 12.00 then my forward journey would only cost £18 roughly. Thats the only thing that annoyed me, apart from that well done the OR Society.

For an assessment centre I have to study up on one of four Operational Research Topics namely "System Dynamics", "Discrete Event Simulation", "Bayes net or other decision tree type approach", "Multi- criteria Decision Analysis (or a specific technique from within this group of techniques)".

Having looked at the wiki pages of these I found out Discrete Event Simulation looks mainly like Queueing theory. So I have to brush up on this, luckily I did actually do this in my BSc whilst at Greenwich University with my favourite lecturer Professor Vitaly Strusevich. I have a lot of man love for this guy, a person who actually got me into OR and got me to join the OR society.

UPDATE 13th December
Some links I forgot about. Whilst at the career fair the first talk was from the chairman of the OR Society and he mentioned some projects he was working on they were Green Logistics and ITIS Holding.

10 October 2011

Job hunting-feel out this questionaire...

If your in my position (finished university)you will be now job hunting. Depending on what sites you go to, instead of wanting your CV, they want to ask you questions about certain criteria's that they will be judging you on. These questions will be different on every website you do, but the type of questions will be near enough the same. Instead of typing out your answer on every site you go to, I would recommend you make a document which has these type of questions on there, so when you go to a website which asks a question you have previously answered, you can copy and paste your answer and then tweak it to fit what that website requires of you.

Here is some questions to get you thinking about:
  1. Describe a situation when you identified a new way of doing something, which affected other people. How did you convince the others that it was the right thing to do?
  2. Describe a recent situation when you have had to overcome a challenge. What difficulties did you face and how did you resolve these?
  3. Describe a situation which demonstrates how you applied your analytical skills to solve a problem
  4. Describe a time when you have worked within a team. What attributes did you bring to the team? What did you learn about your own behavior in this situation and how would you change your behavior in a similar situation in the future?
  5. What are your spare time interests? Please explain why these are important to you and include any areas of responsibility

Specific questions

Why have you chosen to join <insert company name>? What has influenced your choice of business area and what particularly interests you in the <insert company scheme>?

Modelling/problem question

  1. Quantitative Analysis This Refers to a situation where the analytical questions have been established and you need to produce a concrete and quantified solution using numerical analysis and /or modelling. Please describe:
    1. Your role
    2. Any analytical techniques you used and how you used them.
    3. How you used available data or produced it.
    4. How you assessed the accuracy and usefulness of your results
  2. Drive for results It is important that OR analysts plan their work clearly and can respond to clients requests within deadlines, being proactive and using initiative when problems arise. Please describe:
    1. Your role in the project;
    2. Your objective and the steps you took to plan and monitor the project;
    3. How you dealt with challenges, both foreseen and unexpected.

You may not get these exact question, but it will be along the same lines as these. If you are job hunting good luck out there, and be careful of spam website and spam job posting.

7 September 2011

php open, write and download a file

I have spend today and yesterday trying to make html write into a file and then download that file. The first objective was to open a file.

<?php
$filename="LinkTestText.txt";
$file=fopen($filename,'w+');
?>

Seems straight forward but i got an error

fopen(LinkTestText.txt) [function.fopen]: failed to open stream: Permission denied.

Okay okay calm down all I have to do is change permission of the file. <long time doing a google search> (many frustrating clicking and shouting at the computer (glad it doesn't have emotions)). On all <exaggeration> the forums no one mentioned a php function called "chmod"!!! Why this is what i'm looking for, okay so lets code it.

<?php
$filename="LinkTestText.txt";
chmod($filename, 0777);
$file=fopen($filename,'w+');
?>

Error appears:

chmod(): Operation not permitted in /file/path/to/that/ruddy/file.php

Rubbish, lets look on php.net to see if they have an answer <Quick look> ooh they have an answer. What one person says is use File Transfer Protocol (ftp) to change the permission, Huzzah an answer finally.

<?php
$filename="LinkTestText.txt";
$row['username']="Joe Bloggs";
$row['password']="passw0rd";
$ftp_details['ftp_user_name'] = $row['username'];
$ftp_details['ftp_user_pass'] = $row['password'];
$ftp_details['ftp_root'] = '/file/path/to/that/ruddy/';
//website address
$ftp_details['ftp_server'] = '10.0.1.200';
function chmod_11oo10($path, $mod, $ftp_details){
// extract ftp details (array keys as variable names)
extract ($ftp_details);
// set up basic connection
$conn_id = ftp_connect($ftp_server);
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// try to chmod $path directory
if (ftp_site($conn_id, 'CHMOD '.$mod.' '.$ftp_root.$path) !== false){
$success=TRUE;
}
else {
$success=FALSE;
}
// close the connection
ftp_close($conn_id);
return $success;
}
chmod_11oo10($filename, "0777", $ftp_details);
$file=fopen($filename,'w+');
fwrite($file,"Writing a lovely string to a file");
fclose($file);
$file=fopen($filename,'w+');
?>

Good so all we need to do now is activate ftp and then work out a way to force a download. Here is a link i found earlier

<?php
//http://php.net/manual/en/function.readfile.php
header('Content-Description: File Transfer');
header('Content-Type: application/download');
//it will be called
header('Content-Disposition: attachment; filename="Link test test.txt"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_clean();
flush();
readfile($filename);
exit;
?>

Done. One last thing the whole of it HAS to appear before "any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.". The only thing that is missing is why i put 0777 as the permission, to be honest i have no idea, just saw the number keep on popping up on a couple of forums.

1 September 2011

Film reviews rankings (no maths here)

I manage to see a film in the cinema nearly every Tuesday, so i do see a lot of film. When I rank film, i normally base it on how much i want someone to see the film in the full cinema experience, this is normally confusing as some bad films get high reviews because they look incredible.

So I have come up with a different system, this is based on DVD collection:
  1. Absolutely rubbish film, if it comes on the TV, change the channel.
  2. Don't waste your money on buying the DVD, Just watch it on the TV.
  3. I don't think you should own the DVD, so just rent it.
  4. The film is lacking something, but the DVD is still worth owning.
  5. Watch in the cinema and definitely buy the DVD.
Unfortunately I do own at least one film that belongs to each of the different ranks. <sigh>