Pages

Thursday 30 August 2012

ACM ICPC (3)

Some rights reserved by Kiewic


1st day
Our 1st day stay was an unofficial one. We reached there one day prior to the actual reporting day. There was actually nothing to do and a few teams like ours who had arrived a day before. Even though lunch and dinner arrangements were not done for the 1st day, we were directed to the college canteen, we dint go for lunch just had a light snack.
We had rest at noon, and evening we went to a seashore(cant call it a beach) nearby which is very much near to Amrita amma's ashram, as locals directed us there. Spent the evening near the sea shore, clicked a few pics and had fun. While returning to the hostels clicked a few pics near the huge college building which almost looked like a palace.
For dinner, we were told to go to the hostel mess, the dinner was decent enough and free although it was an official stay. The 1st day itself we got to know the rule for the college hostel students and i guess although few may not like it, but with a discipline point of view it was very much needed and good although. You are supposed to wash your own plates after the meal. This rule was only for the college students not for the guests.
After dinner had a nice walk and finally returned to the hostel, studied a bit and went to sleep
2nd Day (THE 1ST OFFICIAL DAY)
By the time we woke up, Many more students have had already arrived. We woke up late lazily since there was nothing to do as such, had bath and went to hotel Amma for breakfast. Had something new there called Omlette dosa..... :P Now we went for checking the college campus and building. The college building is a huge palace like structure pure white in color and looks highly royal. Looks sure like a ICPC centre. The classrooms and the campus was much like any other campus. thereafter went for registrations met few other students there, made a few friends there among the volunteers. Got to know few other colleges and school came from Maharashtra. There were certain documents to be presented, the college I-Card and other stuff. It all got wind up pretty soon. Just wanted to go in the rooms and have rest. Lunch was in the college canteen. The canteen was really big and was clean as well. The food was awesome. Volunteers to serve us the same. The coordination was splendid among the volunteers.

Monday 27 August 2012

Josephus Problem Ruby Code

Ruby code for solving josephus problem. Although the presentation isn't good enough. This was my first Ruby exercise so ignore the mistakes in it... :P

#! /usr/bin/ruby -w
class List
  attr_accessor :element,:next_element,:last_element
  def initialize(element)
      @element = element
      @next_element = nil
      @last_element = nil
  end
end
number = ARGV[0]
number=number.to_i
first = List.new(1)
temp = first
for i in 2..number
  temp=temp.next_element = List.new(i)
end
temp.next_element = temp.last_element
temp.next_element = first
temp = first
count=0
while temp.next_element.next_element != temp
  count = count + 2
  if count % 100 == 0
    puts "100 killed total count #{number+1}"
    temp.last_element= List.new(number = number + 1 )
  end
  temp = temp.next_element = temp.next_element.next_element = temp.next_element.next_element.next_element
end
STDOUT.puts "last one is #{temp.element.to_s}"

Ruby and Sinatra Example of Automatic Mutt Configuration

# these will include sinatra and erb gem(gem is a package in ruby it will have additional info like files required to be installed) in the code here we are including sinatra because sinatra is a DSL domain specific language(DSL is a language which is concerned with a specific domain problem, it helps us solve a particular problem and then advance accordingly there are other DSL's as well like HTML)sinatra is a DSL for quick development of webpages. ERB is embedded ruby erb gives a powerful way to embed ruby code to plain text document or an html file etc this helps in generation of documents etc. since we will b using WEBrick server which sinatra activates we will be requiring sinatra and since we will be creating a template with ruby embedded in it we will be requiring erb as well.
require 'sinatra'
require 'erb'

# get is a route which will map the get method of http to the controller action here /tryerb is a controller action.(A controller action is supplied with more info many a times for specific data e.g. GET /movies/3 will mean the route GET should map HTTP method to movies with id 3 and show the information of that movie with id 3)  information will be rendered from tryerb. get route will show the page. POST would create one. DELETE will remove and PUT will update.
get '/tryerb' do
    erb :tryerb
end

#muttconf_template will store the template info whenever a template encounters a <% %> it will consider it a ruby code and whenever it encounters a <%= %> it will consider that a statement there are few more tags as well.
muttconf_template = "set imap_user =\"<%= @usrname %>\"
set imap_pass =\"<%= @paswd %>\"
set spool_file =\"<%= @spool_file %>\"
set folder =\"<%= @folder %>\"
set postponed =\"<%= @postponed %>\"
set header_cache =\"<%= @header_cache %>\"
set message_cachedir =\"<%= @message_cache_dir %>\"
set certificate_file =\"<%= @certificate_file %>\""

#An object 'configure' of class ERB is created to which the above template is supplied
configure = ERB.new(muttconf_template)

# post route will create a new tryerb with the data that is attached to it.
post '/tryerb' do
  @usrname = params[:usrname]
  @paswd = params[:paswd]
  @spool_file = params[:spool_file]
  @folder = params[:folder]
  @postponed = params[:postponed]
  @header_cache = params[:header_cache]
  @message_cache_dir = params[:message_cache_dir]
  @certificate_file = params[:certificate_file]

#configure.result will give the data that was supplied to it. binding methods binds the global variables, i.e. retains the values of the variables that have been used earlier. FInally the mutrc file is opened and the output is written into it. aftr creation of the mutrc the msg of mutt being successfully configured is displayed.
  output = configure.result(binding)
  File.open('mutrc','w') do |f|
    f.write output
    f.close
  end
"MUTT CONFIGURED SUCCESSFULLY!!!"
end

The code was further Modified as below....

newfile.rb
# encoding: utf-8
#roshan again
require 'rubygems'
require 'sinatra'
require 'erb'

get '/newfile' do
    erb :newfile
end

file = File.open("template", "r")
muttconf_template = file.read
configure = ERB.new(muttconf_template)
post '/newfile' do
  @usrname = params[:usrname]
  @paswd = params[:paswd]
  output = configure.result(binding)
  user = @usrname
  passwd = @paswd
  pass = passwd
  system("(perl -e 'print crypt($ARGV[0], \"#{pass}\")' $#{pass})");
  system("sudo useradd #{user} -p '#{passwd.crypt("$1$password")}'")
  Dir.mkdir("/home/#{user}");
  File.open("/home/#{user}/.muttrc","w") do |f|
    f.write output
    f.close
    end
  erb :postfile
end

template file
set imap_user ="<%=@usrname%>@gmail.com"
set imap_pass ="<%=@paswd%>"
set folder ="imaps://imap.gmail.com:993"
set spoolfile ="+INBOX"
set header_cache ="~/.mutt/cache/headers"
set message_cachedir ="~/.mutt/cache/bodies"
set certificate_file ="~/.mutt/certificates"
set smtp_url ="smtp.googlemail.com:587"

views/newfile.erb
<form name="text_form" action="/newfile" method="post"
<H1>MUTT CONFIGURATION PAGE</H1>
Username: <input type="text" name="usrname" /><br/>
Password: <input type="password" name="paswd" /><br/>
<input type="submit">
</form>

views/postfile.erb
<html>
  <title>
  Success!!!
  </title>
    <head>
      <body>
        <h1>Successfully Configured Mutt!!!</h1>
         Open Terminal<br/>
         Type ssh [username]@[ip_address](ENTER)<br/>
         Type 'mutt' press enter and check you inbox
      </body>
    </head>
</html>

May be modified further as well....