Sunday, June 19, 2016

Artificial Intelligence using python

The below code is to reach a goal in a board game with robot at position with value 'r' and goal with value 'g' in the row* column board.

Below are the conventions for the code to solve the problem.


m - size of grid
grid - position of the board(in row * col)
g - goal position
r - robot current postion in the board


#!/bin/python
def find_m_position(grid):
for row in range(m):
for col in range(m):
if grid[row][col] == 'r':
return (row,col)

def find_p_postion(grid):
for row in range(m):
for col in range(m):
if grid[row][col] == 'g':
return (row, col)


def PathtoGoal(n,grid):
#print all the moves here
#pass
row_m, col_m = find_m_position(grid)
row_p, col_p = find_p_postion(grid)

while((row_m != row_p) and (col_m != col_p)):
diff_row = row_m - row_p
diff_col = col_m - col_p
if(diff_row < 0):
print "DOWN"
row_m = row_m + 1


else:
print "UP"
row_m = row_m - 1

if(diff_col > 0):
print "LEFT"
col_m = col_m - 1
else:
print "RIGHT"
col_m = col_m + 1


return None

m = input()

grid = []
for i in xrange(0, m):
grid.append(raw_input().strip())

#print grid

PathtoGoal(m,grid)



Sunday, March 6, 2016

Project Euler - Problem 2 - Sum of even Fibonaaci numbers

def euler2(n):

su = 0
lst = []
for i in xrange(1,n,3):
    if(i == 1):
        res = 2
    elif (i == 4):
        res = 8
    else:
        res = 4* lst[-1]+ lst[-2]


    if(res < n):
        lst.append(res)

    else:
        break


print sum(lst)

Count the Squares between any two numbers

import math

def get_squares(n1, n2): #Given n1, n2 are the numbers both inclusive

       print int(math.floor(math.sqrt(n2)) - math.ceil(math.sqrt(n1))) + 1

Sunday, February 21, 2016

Cashless transaction with local Transport - part 3


package JavaSMSProject;


import java.net.*;

public class CopyOfSMSclass {
public void sendSMS(Integer amount, Integer bal, String recipient) {
        try {
                //String recipient = "918888899999"; // Receipietnt mobile number
                String message = "Your account has been deducted with" + amount + "your current balance is" + bal;
                String username = "xxx";  //Username that you have registered with OzekiNG
                String password = "xxx"; // Password that you have registered with OzekiNG
                String originator = "918888866666"; // Sender mobile number

                String requestUrl  = "http://127.0.0.1:9501/api?action=sendmessage&" +
    "username=" + URLEncoder.encode(username, "UTF-8") +
    "&password=" + URLEncoder.encode(password, "UTF-8") +
    "&recipient=" + URLEncoder.encode(recipient, "UTF-8") +
    "&messagetype=SMS:TEXT" +
    "&messagedata=" + URLEncoder.encode(message, "UTF-8") +
    "&originator=" + URLEncoder.encode(originator, "UTF-8") +
    "&serviceprovider=HTTPServer0" +
    "&responseformat=html";
             
                //GSMModem1



                URL url = new URL(requestUrl);
                HttpURLConnection uc = (HttpURLConnection)url.openConnection();

                System.out.println(uc.getResponseMessage());

                uc.disconnect();

        } catch(Exception ex) {
                System.out.println(ex.getMessage());

        }
}


}


This class triggers SMS gateway and sends the information to the customer.

A basic application to work with SMS along with cashless transaction with local transport and it has few enhancements to work on.






Cashless transaction with local Transport - part 2

Readdatabase.class

package JavaSMSProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class ReadDatabase {

public void writeToExcel(String fileName, String uid, String amount) throws IOException {

Integer cellValue = 0;
Integer cellAmount = 0;
Integer bal = 0;
Integer phoneNumber = 0;

FileInputStream fsIP= new FileInputStream(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook(fsIP);
XSSFSheet sheet = workbook.getSheetAt(0);

int count = sheet.getPhysicalNumberOfRows();
System.out.println(count);

List<String> list = new ArrayList<String>();
/*int size = list.size(); */

  for(int i=0; i < count - 1; i++){

  XSSFRow row1 = sheet.getRow(i + 1);
  Cell cell1 = row1.getCell(0);

  cellValue = (int)Math.round(cell1.getNumericCellValue());

  System.out.println("UserId:" + cellValue);

  String matchuid = cellValue.toString();

  if(uid.equalsIgnoreCase(matchuid)){

  Cell cell2 = row1.getCell(1);

  cellAmount = (int)Math.round(cell2.getNumericCellValue());

  System.out.println("Amount:" + cellAmount);

  bal = cellAmount - Integer.parseInt(amount);

  System.out.println("Bal remaning:" + bal);

  Cell cell3 = row1.getCell(2);

  phoneNumber = (int)Math.round(cell3.getNumericCellValue());

  System.out.println("Phone number:" + phoneNumber);

  cell2.setCellValue(bal.doubleValue());

 
  CopyOfSMSclass sms = new CopyOfSMSclass();
  sms.sendSMS(Integer.parseInt(amount), bal, phoneNumber.toString());

  break;



  }//if


  //list.add(cell1.getStringCellValue());

  }//for

  System.out.println("Sorry no such user found");

  fsIP.close();
 try (FileOutputStream outputStream = new FileOutputStream(new File(fileName))) {
 workbook.write(outputStream);
}

catch (FileNotFoundException e) {
   
       e.printStackTrace();
   }


}

}