Thursday, November 26, 2015

[Prolog] Airline Path Finder

Summary: A path finder for finding all flights in Prolog.


%Airports
airport(aus, "Austin-Bergstrom International").
airport(ewr, "Newark Liberty International").
airport(lax, "Los Angeles International").
airport(phl, "Philadelphia International").
airport(sfo, "San Francisco International").
airport(sjc, "Norman Y. Mineta San Jose International").

% From, To, DepartTime, ArriveTime, Price
flight(sfo, lax, 8:00, 9:20, 86.31).
flight(sfo, lax, 12:40, 14:00, 89.42).
flight(sfo, lax, 20:30, 21:50, 69.97).
flight(lax, sfo, 9:10, 10:25, 90.32).
flight(lax, sfo, 15:55, 17:10, 82.05).
flight(lax, sjc, 9:55, 11:05, 151.19).
flight(lax, sjc, 18:05, 19:15, 149.80).
flight(sjc, lax, 9:50, 11:10, 162.00).
flight(sjc, lax, 15:31, 16:50, 205.06).
flight(sfo, aus, 6:05, 13:33, 399.20).
flight(aus, sfo, 7:00, 11:29, 399.20).
flight(aus, sfo, 12:44, 16:25, 399.20).
flight(lax, aus, 7:00, 16:00, 297.20).
flight(lax, aus, 14:30, 19:25, 297.20).
flight(aus, lax, 8:12, 9:44, 472.20).
flight(aus, lax, 12:44, 14:20, 472.20).
flight(sfo, phl, 8:20, 16:37, 890.71).
flight(phl, sfo, 6:00, 9:19, 787.47).
flight(phl, sfo, 16:24, 19:40, 787.47).
flight(aus, phl, 7:45, 11:56, 427.47).
flight(phl, aus, 17:14, 20:16, 427.47).
flight(aus, ewr, 6:29, 11:11, 240.15).
flight(aus, ewr, 13:19, 18:00, 240.15).
flight(ewr, aus, 9:00, 12:22, 240.15).
flight(ewr, aus, 19:15, 22:46, 240.15).
flight(lax, ewr, 8:30, 16:49, 292.47).
flight(lax, ewr, 12:50, 20:59, 292.47).
flight(ewr, lax, 9:05, 12:20, 292.47).
flight(ewr, lax, 5:25, 20:40, 292.47).
flight(phl, ewr, 9:55, 10:48, 415.96).
flight(phl, ewr, 14:40, 15:31, 415.96).
flight(ewr, phl, 6:59, 8:00, 415.96).
flight(ewr, phl, 11:35, 12:28, 415.96).
flight(ewr, phl, 14:29, 15:23, 415.96).
flight(sfo, ewr, 7:25, 15:56, 313.40).
flight(sfo, ewr, 14:50, 23:14, 313.40).
flight(ewr, sfo, 7:30, 10:55, 313.40).
flight(ewr, sfo, 10:30, 13:55, 313.40).
flight(ewr, sfo, 16:11, 19:40, 313.40).

isEarlier(H1:M1, H2:M2) :-
((H2 == H1 , M1 < M2) ;
(H1 < H2 );  (H2 = H1 , M2 = M1)).

append([], X, X).
append([H|T], L2,[H|L3]) :-
append(T, L2, L3).

flyshort(Departing, Arriving, Visited, DT, AT, Flights, TotalCost):-
flight(Departing, Arriving, Start, End, Cost),
TotalCost = Cost,
DT = Start,
AT = End,
append([Departing, Arriving],Visited , Flights).

flylong(Departing, Arriving, Visited, DT, AT, Flights, TotalCost):-
flight(Departing, Next, Start_temp, End_temp, Cost_temp),
flight(Next, To, Start_next, End_next, Cost_next),
Next \= Arriving,
isEarlier(End_temp, Start_next),
flight(To, Arriving, Start_to, End_to, Cost_to),
isEarlier(End_next, Start_to),
DT = Start_temp,
AT = End_to,
TotalCost is Cost_temp + Cost_next + Cost_to,
append([Departing, Next, To, Arriving], Visited, Flights).

flyok(Departing, Arriving, Visited, DT, AT, Flights, TotalCost) :-
flight(Departing, To, Start_temp,End_temp, Cost_temp),
flight(To, Arriving,Start_to,End_to, Cost_to),
isEarlier(End_temp, Start_to),
DT = Start_temp,
AT = End_to,
TotalCost is Cost_temp + Cost_to,
append([Departing, To, Arriving], Visited, Flights).

route(Departing, Arriving, Visited, DT, AT, Flights, TotalCost) :-
 flyshort(Departing, Arriving, Visited, DT, AT, Flights, TotalCost);
 flylong(Departing, Arriving, Visited, DT, AT, Flights, TotalCost);
 flyok(Departing, Arriving, Visited, DT, AT, Flights, TotalCost).



[Prolog] Fun Murder Mystery

Instructions: You will use Prolog to solve a murder mystery.

There are 5 victims:
1) Mr. Boddy, the host of the party.  Killed with a candlestick in the hall.
2) The cook.  Killed with a knife in the kitchen.
3) A motorist who stopped by the house.  Killed with a wrench in the lounge.
4) A police officer who was searching for the motorist.  Killed with a lead pipe in the library.
5) Yvette, the house maid.  Strangled by a rope in the billiards room.
6) A singing telegram.  Shot with the revolver in the hall.

The suspects are:
1) Professor Plum
2) Mrs. Peacock
3) Mrs. White
4) Miss Scarlet, a Madam operating a brothel in Washington DC.
5) Colonel Mustard
6) Mr. Green
7) Wadsworth, the butler

You are given the following clues regarding motive:
*Everyone except Wadsworth had a motive to kill Mr. Boddy.
*The cook had worked for Mrs. Peacock, and had stolen money from her.
*The motorist was blackmailing Colonel Mustard.
*Yvette had worked for Miss Scarlet's brothel before, and left on bad terms.
*Yvette and Colonel Mustard had formerly had an affair, and she was threatening to tell his wife.
*Mrs. White knew that Yvette was also having an affair with her husband.
*Miss Scarlet had been bribing the police officer to keep him from arresting her.
*Professor Plum had lost his license as a psychiatrist after having an affair with his patient, who turns out to be the singing telegram.
*Wadsworth has an unnatural hatred for singing telegrams.

There are some additional clues that may help you solve the case:
*Colonel Mustard is bad with knots, and so could not have used the rope.
*Professor Plum hates guns.
*Mrs. Peacock would never risk damaging a fine candlestick by using it as a weapon.
*Miss Scarlet was never in the billiards room.
*Professor Plum was never in the kitchen.
*Colonel Mustard never visited the room where Mr. Boddy was murdered.
*Mrs. White has an alibi for the murder of Mr. Boddy.
*Mr. Green has an alibi for every murder.
*Miss Scarlet has an alibi for every murder committed in the same room where the revolver was used.

Notes: This is not correct so the murderer got away.

victim(mr_boddy).
victim(cook).
victim(motorist).
victim(police_officer).
victim(yvette).
victim(singing_telegram).

suspect(professor_plum).
suspect(mrs_peacock).
suspect(mrs_white).
suspect(miss_scarlet).
suspect(colonel_mustard).
suspect(mr_green).
suspect(wadsworth).

weapon(wrench).
weapon(candlestick).
weapon(lead_pipe).
weapon(knife).
weapon(revolver).
weapon(rope).

room(hall).
room(kitchen).
room(lounge).
room(library).
room(billiard_room).

murder(mr_boddy,candlestick,hall).
murder(cook,knife,kitchen).
murder(motorist,wrench,lounge).
murder(police_officer,lead_pipe,library).
murder(singing_telegram,revolver,hall).
murder(yvette,rope,billiard_room).

motive(professor_plum,mr_boddy).
motive(mrs_peacock,mr_boddy).
motive(mrs_white,mr_boddy).
motive(miss_scarlet,mr_boddy).
motive(colonel_mustard,mr_boddy).
motive(mr_green,mr_boddy).
motive(mrs_peacock,cook).
motive(colonel_mustard,motorist).
motive(molonel_mustard,yvette).
motive(mrs_white,yvette).
motive(miss_scarlet,police_officer).
motive(professor_plum,singing_telegram).
motive(wadsworth,singing_telegram).

alibis(colonel_mustard, mr_boddy, candlestick).
alibis(mrs_white, mr_boddy, candlestick).

alibir(colonel_mustard, billiard_room, rope).
alibir(professor_plum, hall, revolver).
alibir(mrs_peacock, hall, candlestick).
alibir(miss_scarlet, hall, revolver).

alibiv(mr_green, X).

% Update accuse to solve the murders.
% Add more facts and rules as needed.
accuse(V,S) :- murder(V,W,R), suspect(S), motive(S, V), not(alibis(S, V, W)), not(alibir(S, R, W)), not(alibiv(S, V)).

[Javascript] Fun Chat Server

Instructions: 
Run the code from the command line:
$ node chatServer.js
Open two more command line prompts, and telnet to port 9000 on each.
Whatever you type in one window will be displayed in the other.


var net = require('net');
var eol = require('os').EOL;

var srvr = net.createServer();
var clientList = [];

srvr.on('connection', function(client) {
  client.name = client.remoteAddress + ':' + client.remotePort;
  client.write('Welcome, ' + client.name + eol);
  clientList.push(client);

  client.on('data', function(data) {
    broadcast(data, client);

    if(data.toString().trim() === "\\list")
    {
      for(var i in clientList)
      {
        if(client === clientList[i])
        {
          for(var j in clientList)
          {
            clientList[i].write(clientList[j].name + "\n");
          }
        }
      }
    }

    if(data.indexOf("\\rename") === 0)
    {
      var namae = data.toString().trim().replace("\\rename", "");

      client.name = namae;
    }

    if(data.indexOf("\\private") === 0)
    {
      var namae = data.toString().trim().replace("\\private", "");
      var realname = [];

      for(var i = 0; i < namae.length; i++)
      {
        var words = namae[i].split(" ");
        realname.push(words[0]);
      }

      //console.log(realname);
      var okname = " ";
      var n = 0;
      for(var i = 1; i < realname.length; i++)
      {
        if(realname[i] !== "")
        {
          okname += realname[i];
        }
        else{
          n = i;
          break;
        }
      }

      var speech = "";
      for(var i = n; i < realname.length; i++)
      {
        if(realname[i] !== "")
        {
          speech += realname[i];
        }
        else{
          speech += " ";
        }
      }

      console.log(okname); //ok got a name 
      for(var i in clientList)
      {
        if(clientList[i].name === okname)
        {
          //console.log(okname + " is in lol");
          clientList[i].write(client.name + " says " + speech + "\n");
        }
      }
    }

  });
});

function broadcast(data, client) {
  for (var i in clientList) {
    if (client !== clientList[i] && data.indexOf("\\private") !== 0 && data.indexOf("\\rename") !== 0 && data.toString().trim() !== "\\list") {
      clientList[i].write(client.name + " says " + data);
    }
  }
}

srvr.listen(9000);

Wednesday, November 25, 2015

[One Year Later] Re-introduction

It has been more than a year since I started blogging about my journey into the computer science field. It has also been two years since I started coding. So much has changed. I still believe my introductory classes were the most challenging because I came into it without any idea about it.

I've been tutoring students, like me, with programming at San Jose State University tutoring center.
I made new friends and expanded my network in the field.
I gained internship and research experience at another state!
I landed an interview as a candidate for Google software engineer internship next summer.

Wish me luck!

[Android App Development] Nanodegree Popular Movies I and II

On October 13, 2015, I started the nanodegree on Udacity that is co-created by Google. Currently, I am taking a leave for the month of December because that month is filled with finals, family get-togethers, and a nerve-inducing interview (I am nervous. I should study.). Thus, having no time to think and do anything about the nanodegree.

For the nanodegree, I completed project one in less than three days. Project one was an app that lists current, relevant movies from the movie database onto a grid view. I can also select a movie and see information about it.

I did not run into too much problems. On the other hand, I learned about api keys, custom image adapters and Genymotion. The rest of the topics, on that project, I have already know how to implement because I came into the program with prior experience.

I am almost finished with project two. My last step is to make the UI interface tablet friendly. However, I am really stuck on fragments. However, there is an event that Udacity is holding next Thursday, where other Udacity students meet. I hope that someone will help.