Saturday, August 15, 2020

Railway Oriented Programming In CSharp

 Railway Oriented Programming My Implementation In CSharp





Error handling using ROP



Implementation of Either Monad using Interface

Keeping all our errors in a single enum

Github Repo

Example taken from Railway Oriented Programming



Tuesday, October 1, 2019

Parsing escape backslashes invalid JSON

let data = jsonData.replace(/(^\{.*"Data":")(.*)("\}$)/,"$2")
let x = JSON.parse(data)



Monday, October 22, 2018

Image convert to Base64 (from file to string)


Demo:


Thursday, March 1, 2018

Random Restart Hill Climbing For N Queen

Hill climbing is an optimization technique which is a local search algorithm. It is an algorithm that starts with a random solution to a problem. By changing a single element of the solution it attempts to find a better solution. Hill climbing is sometimes called greedy local search because it grabs a good neighbor state without thinking ahead about where to go next.
To find a global maxima random restart hill climbing is used. Random restart hill climbing is a series of hill climbing searches. From randomly generated initial state until a goal is found.

board setup: n queens, one queen per column
successors : The successors of a state are all possible states generated by moving a single queen to another square in the same column. Meaning, there are n queens on the board and each queen can move to n-1 defined positions. So, each state has n*(n-1) successors.
heuristic cost function h : h is the number of pairs of queens that are attacking each other
Basic hill climbing creates a random state of the board. Then it finds the highest value successor of the current state(for n queen the lowest valued successor), which is called the neighbor. If the heuristic cost value of the neighbor is less than the heuristic cost value of the current state, then the neighbor becomes the current state. If not then the current solution is the minima of the current hill.
What random restart does is, it runs the hill climbing until the global minima is found. In each hill climbing, it creates a new initial state of the board then runs the hill climbing algorithm to find the minima of the hill. It stops when the global minima is reached.
h(board)
1  returns the number of attacking pair on board

highest_valued_successor_of_current(current)
1  best_h  
2  best_successor  NULL
3  while(new successor can be created)
4      successor  new_successor(current)
5      if h(successor) < best_h 
6           best_h  h(successor)
7           best_successor  successor
8  return successor


hill_climbing()
1  current  A random state of the board
2  while(1)
3      neighbor  highest_valued_successor_of_current(current)
4      if h(neighbor) >= h(current) 
5           return current
6      neighbor  current

random_restart_hill_climbing()
1  final_h  
2  best_board  NULL
3  while(final_h != 0)
4       result  hill_climbing()
5       now_h  h(result)
6       if final_h > now_h
7            final_h  now_h
8            best_board  result
9  return best_board

Saturday, January 27, 2018

Use of CFGs for Parsing OPEN

We can think of using CFGs to detect various language constructs in the token streams freed from simple
syntactic and semantic errors, as it is easier to describe the constructs with CFGs. But CFGs are hard to
apply practically. In this session we first exercise on simpler implementations of CFGs, and then
implement the FIRST and FOLLOW functions that facilitate efficient parsing algorithms for practical
problems.

Session 5 by Nafis Islam on Scribd





Friday, January 5, 2018

Detecting Simple Syntax Errors

Syntax errors are very common in source programs. Suppose, a given C source program has been scanned, filtered, lexically analyzed and tokenized as that were done in earlier sessions. In addition, line numbers have been assigned to the source code lines for generating proper error messages. As the first step to Syntax Analysis, we now perform detection of simple syntax errors like duplication of tokens except parentheses or braces, Unbalanced braces or parentheses problem, unmatched ‘else’ problem, etc


Session 4 by Nafis Islam on Scribd




Symbol Table Generation

A given C source program has been scanned, filtered and then lexically analyzed as it was done in Session 2. We have all the lexemes marked as different types of tokens like keywords, identifiers, operators, separators, parentheses, numbers, etc. We now get corrected the unknown lexemes first, and then generate a Symbol Table describing the features of the identifiers. Finally, we generate a modified token stream in accordance with the Symbol Table for processing by the next phase, that is, Syntactic Analysis


Session 3 by Nafis Islam on Scribd


Lexical Analysis

we have a C source program scanned and filtered as it was done in Session 1. We now take that modified file as input, and separate the lexemes first. We further recognize and mark the lexemes as different types of tokens like keywords, identifiers, operators, separators, parenthesis, numbers, etc.

Session 2 by Nafis Islam on Scribd


Thursday, January 4, 2018

Scanning and Filtering a Source Program

You are given a C source program with single and multiple line comments. As the first step toward compilation you need to remove the comments and white space (extra spaces, tabs and newline characters). Develop a program that takes as input file the given source program and produces a filtered file as stated above. The program must also display both the files.

Session 1 by Nafis Islam on Scribd




Wednesday, November 22, 2017

Into to Meta-Heuristics

Meta-heuristic হল একটি primary field of stochastic optimization.

Stochastic optimization হল the general class of algorithm and techniques যা randomness use করে optimal বা optimal এর কাছাকাছি solution বের করে আনে।

•যে সকল problem এ আমরা আগে থেকে  বুঝতে পারি না যে, optimal solution কেমন হতে পারে।

•যে সকল problem এ আমরা জানি না, principal way তে  কিভাবে solution এর দিকে আগাব।

•যে সকল problem এ, brute-force search impossible কারন search space অনেক বড়।


        কিন্তু  আমাদের কাছে ঐ  problem এর কোন candidate solution দিলে   test করে দেখতে পাড়ব solution তা কত ভাল।সে সকল problem এ meta-heuristic use করা হয়।

“Meta-heuristics are applied to I know it when I see it problem”

        এরকম কোন problem এর সামনে পরলে সবচেয়ে simplest thing যা করতে পারি তা হল  random search, আমরা random solution বের করব যতক্ষণ আমাদের কাছে  time আছে, সবচেয়ে best solution return করব।

কিন্তু random search এর আগে একটা  alternative পথ দেখতে পারি।

      আমরা প্রথমে random solution নেই। এই solution একটা ছোট  এবং random modification করি। এখন নতুন solution test করি।
যদি দেখি current solution better হয় then old solution ফেলে দিব। যদি না হয় তাহলে current solution ফেলে দিব।
     এখন বর্তমান হাতে যে solution আসে  তার উপর ছোট ও random modification করব। এভাবে যতক্ষণ পারা যায় repeat করতে থাকব।
     এই পধতি কে বলে hill-climbing.এটি হল একটি simple meta-heuristic algorithm.

    "It exploits a heuristic belief about your space of candidate solutions which is usually true for many problems: that similar solutions tend to behave similarly (and tend to have similar quality), so small modifications will generally result in small, well-behaved changes in quality, allowing us to “climb the hill” of quality up to good solutions. This heuristic belief is one of the central defining features of meta-heuristics: indeed, nearly all meta-heuristics are essentially elaborate combinations of hill-climbing and random search."

     Meta-heuristic দারা যে সকল problem tackle করা যায় সে সকল problem হল sub-class of inverse problem। 

       Inverse problem কে আমারা এভাবে কল্পনা করতে পাড়ি, মনে করি আমাদের কাছে একটি test function আছে  f() যেটা parameter হিসেবে একটি candidate solution কে নিবে এবং ওই solutionটির একটি assessment বা fitness return করবে।

       কিন্তু এটা মোটামোট impossible উপরের functionটির inverse function f-1() তৈরি করা যা assessment বা fitness parameter হিসেবে নেয় এবং এমন একটি candidate solution return করবে যার ঐ assessment or fitness থাকবে।

      Optimization methods(Meta-heuristic) design করা হয়েছে  inverse problem কে overcome করতে।

Wednesday, August 30, 2017

Bishops

Problem Statement:

                        আমাদের একটি বিশপ এর current position দিয়ে দেয়া হয়েছে (r1,c1)। এবং আমাদের destination position (r2,c2) ও দেয়া হয়েছে। বিশপটি একটি ইনফিনিট সাইজ chess board এ আসে।আমাদের বলতে হবে বিশপটি তার destination এ পোষাইতে পাড়বে কি না? আর পোষাইতে পাড়লে মিনিমাম কত move এ পোষাইতে পাড়বে।

Solution:

এ প্রব্লেম আমি বহুদিন আগে দেখেছি LOJ এ। কিন্তু অত দিন কোন solution মাথায় আসে নাই।
আজকে same প্রবলেম পাইলাম সিএস আচাডেমীতে। 

লাস্ট দাবা খেলেছি প্রায় ৬ মাস আগে। কিন্তু আজকে প্রব্লেমটা পরতেগিয়ে মাথায় একটা কথা আসল।
কোন বিশপ যদি কাল ঘরে থাকে তাহলে সে কখন সাদায় জেতে পারে না আবার বিশপ যদি সাদা ঘরে থাকে তাহলে সে কখন কাল জেতে পারে না।

এটা যখই মাথায় আসল তখনি বুঝে গেলাম destination এ পোষাইতে পাড়বে কি না তার solution পেয়ে গেসি।

আমি destination এ পোষাইতে পাড়ব যদি same color  হয় current position ও destination position।
এখন এটা check কিভাবে করব?

একটা ২ *৩ board আঁকলাম।



এখনে লক্ষ করলে দেখা যায় যে কোন cell এর r%2==c%2 হলে cellটি  white cell হবে। তা না হলে black cell হবে। 


আমরা পোষাইতে পাড়বে কি না তা পেয়ে গেসি। কিন্তু minimum move আমাদের লাগবে।

তখন আমি একটা ছবি আঁকলাম।



জিনিশটা লক্ষ করার মত হল আমরা যে রঙ এর ঘরে আছি বোর্ডে সেই রঙ এর অন্য কোন ঘর যেতে মাক্সিমাম ২টা move লাগবে। আর ঘরটি current cell er diagonal যদি হয় তাহলে আমরা ১ move এ যেতে পারি।

current cell (3,2) , destination cell (1,4) এ ২ cell diagonal তা হাতে একে দেখলেই বুঝা যায়। যেহেতু diagonal cell তাই ১ টা move চলে যেতে পারব।

current cell (3,2) , destination cell (3,4) এ ২ cell diagonal না। আমরা একটা move দিয়ে (2,3) cell এ চলে যেতে পারব , আর আরেকটা মভে দিয়ে destination cell (3,4) এ চলে যেতে পারব। সব non-diagonal same color cell এ আমরা একই ভাবে ২টা move এ পোষাইতে পারব।

এটাই আমাদের solution :v :v :v ।

এ question পরেছিলাম ১-১.৫ বছর আগে। এ ৩ লাইন এর observation আস্তে লাগে গেছে ১- ১.৫ বছর। জীবন বরই সুন্দর  :D ।



Sunday, August 27, 2017

DP & Recursion Lower Bound

DON'T KNOW:
Longest Run on a Snowboard
Wedding shopping       [DONE]
Cutting Sticks         [FAILED]
Unidirectional TSP
Getting in Line       [FAILED]
Distinct Subsequences
Longest Palindrome   [DONE]
Collecting Beepers
Optimal Array Multiplication Sequence
Maximum Sum
The Twin Towers
23 out of 5
Take the Land
Stacking Boxes
History Grading
Strategic Defense Initiative
Largest Submatrix

Non Classical (The Easier Ones):
Homer Simpson
How do you add? [DONE]

0-1 Knapsack (Subset Sum):
10819 - Trouble of 13-Dots [DONE]
990 - Diving for Gold
10261 - Ferry Loading
11003 - Boxes

Coin Change (CC):
166 - Making Change
11517 - Exact Change
10313 - Pay the Price
10306 - e-Coins

Longest Increasing Sub-sequence (LIS):
111 - History Grading
10131 - Is Bigger Smarter?
Testing the CATCHER
481 - What Goes Up
497 - Strategic Defense Initiative
10534 - Wavio Sequence
437 - The Tower of Babylon

Longest Common Sub-sequence (LCS):
Longest Common Sub-sequence
Compromise
Vacation

Bitmask DP:
(LOJ) Marriage Ceremonies (bitmask dp)
Free Candies (bitmask dp)

Forming Quiz Teams

Basic recursion:
524 - Prime Ring Problem [DONE]
750 - 8 Queens Chess Problem
729 - The Hamming Distance Problem
167 - The Sultan's Successors
574 - Sum It Up [DONE]
10098 - Generating Fast
10063 - Knuth's Permutation
193 - Graph Coloring

Football Player Transfer Prediction

Football Player Transfer Prediction Using Different Classifiers Project Report :  Football Player Transfer Prediction Report ...