Monday, 23 October 2017
Capturing console stream input for processing
Some good articles below found interesting in the web.
The simplest
The sample Host and Client implementation here
My Own test
class Program
{
static public void test()
{
System.Diagnostics.Process cmd = new System.Diagnostics.Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.Flush();
cmd.StandardInput.WriteLine("echo 5 > input.txt");
cmd.StandardInput.WriteLine("echo 3 1 2 3 >> input.txt");
cmd.StandardInput.WriteLine("echo 2 1 1 >> input.txt");
cmd.StandardInput.WriteLine("echo 4 10 5 5 1 >> input.txt");
cmd.StandardInput.WriteLine("echo 0 >> input.txt");
cmd.StandardInput.WriteLine("echo 1 2 >> input.txt");
cmd.StandardInput.WriteLine("PrizeDraw < input.txt");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
string line;
int i = 0;
do
{
line = cmd.StandardOutput.ReadLine();
i++;
if (line != null)
{
Console.WriteLine(line);
//Console.WriteLine("Line " + i.ToString() + " -- " + line);
}
} while (line != null);
}
static void Main(string[] args)
{
test();
Console.ReadLine();
}
}
Wednesday, 5 July 2017
c# coding OOPs
Code on using Char
using System;
using System.Text;
public class TextInput {
protected string _value;
public virtual void Add(char c)
{
_value
+= c.ToString();
}
public virtual string GetValue()
{
return _value;
}
}
public class NumericInput : TextInput
{
public override void Add(char n)
{
if(Char.IsNumber(n))
_value
+= n.ToString();
}
}
public class UserInput
{
public static void Main(string[] args)
{
TextInput input = new NumericInput();
input.Add('1');
input.Add('a');
input.Add('0');
Console.WriteLine(input.GetValue());
Console.ReadLine();
}
}
RESULT: 10
Code 2 : IndexOf and Substring
using System;
public class Path
{
public string CurrentPath { get; private set; }
public Path(string path)
{
this.CurrentPath = path;
}
public void Cd(string newPath)
{
string[] folders = CurrentPath.Split('/');
if(newPath.IndexOf("..")<0)
CurrentPath += "/" + newPath;
else
{
CurrentPath = CurrentPath.Substring(0, CurrentPath.LastIndexOf("/"));
CurrentPath += newPath.Substring(2, newPath.Length -2);
}
}
public static void Main(string[] args)
{
Path path = new Path("/a/b/c/d");
path.Cd("../x");
Console.WriteLine(path.CurrentPath);
}
}
Unresolved - Binary Tree Search
using System;
public class Node
{
public int Value { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public Node(int value, Node left, Node right)
{
Value
= value;
Left
= left;
Right
= right;
}
}
public class BinarySearchTree
{
public static bool Contains(Node root, int value)
{
if (root == null) return false;
if (root.Value == value)
return true;
else if (root.Value <
value)
return Contains(root.Right,
value);
else
return Contains(root.Left,
value);
}
public static void Main(string[] args)
{
Node n1 = new Node(1, null, null);
Node n3 = new Node(3, null, null);
Node n2 = new Node(2, n1, n3);
Console.WriteLine(Contains(n2,
3));
Console.ReadLine();
}
}
Palindrome Test
public class Palindrome
{
public static bool IsPalindrome(string word)
{
if (word.ToLower() == ReverseString(word.ToLower()))
return true;
return false;
}
public static void Main(string[] args)
{
Console.WriteLine(Palindrome.IsPalindrome("Deleveled"));
Console.ReadLine();
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
-----------------------------
Another String Manipulation
-- imcomplete
using System;
public class Path
{
public string CurrentPath { get; private set; }
public Path(string path)
{
this.CurrentPath = path;
}
public void Cd(string newPath)
{
if (newPath == "/") CurrentPath = "/";
string[] cmd =
newPath.Split('/');
foreach (string cd in cmd)
switch (cd)
{
case "..":
{
CurrentPath
= "/" +
CurrentPath.Substring(1, CurrentPath.LastIndexOf('/') - 1);
break;
}
case "":
{
CurrentPath
= ""; //to root this can
only happen some cd(/x);
break;
}
default:
{
CurrentPath
= CurrentPath + "/" + cd;
break;
}
}
}
public static void Main(string[] args)
{
Path path = new Path("/a/b/c/d");
path.Cd("/x");
Console.WriteLine(path.CurrentPath);
Console.ReadLine();
}
}
-------------
-- Smallest number left to be used ..
input = { 1, 3, 4, 5, 7 }; the answer = 2
input = { 1, 2, 3, 4, 7,9 }; the answer = 5
class Program
{
static void Main(string[] args)
{
int[] input = { 1, 3, 4, 5, 7 };
int result = solution(input);
Console.WriteLine("small number to be used is {0}", result);
Console.ReadLine();
}
public static int solution(int[] A)
{
int smallGuy = 1;
// write your code in Java SE 8
if (A.GetUpperBound(0) == 0) return smallGuy;
if (A == null) return smallGuy;
var aList = A.ToList();
foreach (var number in A)
{
if (aList.IndexOf(smallGuy) >= 0)
++smallGuy;
}
return smallGuy;
}
}
-- Smallest number left to be used ..
input = { 1, 3, 4, 5, 7 }; the answer = 2
input = { 1, 2, 3, 4, 7,9 }; the answer = 5
class Program
{
static void Main(string[] args)
{
int[] input = { 1, 3, 4, 5, 7 };
int result = solution(input);
Console.WriteLine("small number to be used is {0}", result);
Console.ReadLine();
}
public static int solution(int[] A)
{
int smallGuy = 1;
// write your code in Java SE 8
if (A.GetUpperBound(0) == 0) return smallGuy;
if (A == null) return smallGuy;
var aList = A.ToList();
foreach (var number in A)
{
if (aList.IndexOf(smallGuy) >= 0)
++smallGuy;
}
return smallGuy;
}
}
Monday, 30 June 2014
LINQ
Populating data-rows into a collection list (Class)
public class TaskStep
{
public int stepId {get; set;}
public string stepName {get; set;}
public string connectionString {get; set;}
}
-------
List<TaskStep> taskSteps = steps.Rows
.Cast<DataRow>()
.Select(row => new MyDomain.Client.Data.TaskStep
{
stepId = (int)row[0],
stepName = (string)row[1],
connectionString = (string)row[2]
})
.ToList();
Wednesday, 18 June 2014
Tic Tac Toe
---------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication3.Models
{
public class TicTacToe
{
public char[] ticBoard;
public int Player = 1; // player 1 is X; player 2
public static int turns { get; set; }
public TicTacToe()
{
ticBoard = new char[10];
for (int iSquare = 1; iSquare <= 9; iSquare++) //program wil ignor the 0 index
{
//Player = 2;
ticBoard[iSquare] = '-'; //empty char
}
}
public void makeATic()
{
//// for winning for testing
//if (turns == 0)
//{
// ticBoard[1] = 'O';
// ticBoard[2] = 'O';
// ticBoard[3] = 'O';
// ticBoard[4] = 'X';
// ticBoard[8] = 'X';
// ticBoard[9] = 'X';
// turns = 5;
//}
turns++;
// bool validPos = false;
Random _r = new Random();
//int iPos = _r.Next(1, 10); // no ze index
List<char> listTicBoard = new List<char>();
listTicBoard = ticBoard.ToList();
int iSquare = listTicBoard.IndexOf('-');
do // (int iSquare = listTicBoard.IndexOf('-'); iSquare <= 9; iSquare++) //program wil ignor the 0 index
{
if (ticBoard[iSquare] == '-') // this square is yet to play
{
//bool playThis = _r.Next(0, 1) == 0 ? false : true; // you wanna play this
if (((_r.Next(1, 100) % 2) == 1) || (turns == 9)) // if odd then play
{
ticBoard[iSquare] = Player == 1 ? 'X' : 'O'; //place the tick
Player = Player == 1 ? 2 : 1; //switch the player
break; //played
}
}
iSquare = iSquare < 9 ? iSquare + 1 : iSquare;
iSquare = listTicBoard.IndexOf('-', iSquare);
} while (iSquare != -1); // all being palyed
}
public int doWeHaveAWinner()
{
if (hasAWinningCombination() == false && turns <= 9)
{
return 0; //draw
}
else
return Player == 1 ? 2 : 1; //here is the winnder
}
public bool hasAWinningCombination()
{
if (turns <= 4) // cannot have a winner
return false;
List<string> winningCombination = new List<string>();
winningCombination.Add("123");
winningCombination.Add("456");
winningCombination.Add("789");
winningCombination.Add("147");
winningCombination.Add("258");
winningCombination.Add("369");
winningCombination.Add("357");
winningCombination.Add("159");
for (int iCounter = 0; iCounter <= winningCombination.Count - 1; iCounter++)
{
bool hasAWinner = true;
List<char> t = winningCombination[0].ToList();
foreach (char pos in t)
{
char PlayerChar = Player == 2 ? 'X' : 'O'; //player is switched at this point
if (ticBoard[Convert.ToInt16(pos.ToString())] != PlayerChar)
{
hasAWinner = false; //not this combination
break;
}
}
if (hasAWinner == true)
return true;
}
return false;
}
}
}
-----------------------------------
--Home Controller About Action Method
--------------------------------
public ActionResult About()
{
TicTacToe ticboard = new TicTacToe();
int hasAWinner = 0;
TicTacToe.turns = 0;
ViewBag.WinningPlayer = ", OOPS It's a draw";
while (hasAWinner == 0 && TicTacToe.turns < 9)
{
ticboard.makeATic();
if (TicTacToe.turns > 4) // cannot have a winner
hasAWinner = ticboard.doWeHaveAWinner();
if (hasAWinner != 0)
{
ViewBag.WinningPlayer = "Player " + hasAWinner;
break;
}
}
var Lines = ticboard.ticBoard;
foreach(char n in Lines)
{
break;
}
ViewBag.Line1 = ticboard.ticBoard[1].ToString() + ticboard.ticBoard[2].ToString() + ticboard.ticBoard[3].ToString();
ViewBag.Line2 = ticboard.ticBoard[4].ToString() + ticboard.ticBoard[5].ToString() + ticboard.ticBoard[6].ToString();
ViewBag.Line3 = ticboard.ticBoard[7].ToString() + ticboard.ticBoard[8].ToString() + ticboard.ticBoard[9].ToString();
ViewBag.Message = "Play tic tac toe.";
return View();
}
-------------------------------------------
--About.chtml
------------------------------------------
@{
ViewBag.Title = "Tic Tac Toe";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
<article>
<p> @ViewBag.Line1</p>
<p> @ViewBag.Line2</p>
<p> @ViewBag.Line3</p>
<p><b>Winner is @ViewBag.WinningPlayer</b></p>
</article>
<aside>
<h3>Aside Title</h3>
<p>
Use this area to provide additional information.
</p>
<ul>
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>
</aside>
Thursday, 13 September 2012
Wednesday, 25 April 2012
Search and replace exact string value with regex
Using regex to search for exactly matching string value in a input string. You can immediately replace the string with replacement. Click here
Thursday, 14 July 2011
Using stored procedures
SqlConnection mConnection = new SqlConnection(mConnectionString);
SqlCommand mCommand = new SqlCommand();
try
{
//Update with Web Response
mCommand = new SqlCommand("spi_Error");
mCommand.CommandType = CommandType.StoredProcedure;
mCommand.Parameters.Add(new SqlParameter("@Message", ErrorString));
mCommand.Parameters.Add(new SqlParameter("@RowNumber", ParameterDirection.Output));
//mCommand.Transaction = mTransaction
mConnection.Open();
mCommand.Connection = mConnection;
mCommand.ExecuteNonQuery();
SqlCommand mCommand = new SqlCommand();
try
{
//Update with Web Response
mCommand = new SqlCommand("spi_Error");
mCommand.CommandType = CommandType.StoredProcedure;
mCommand.Parameters.Add(new SqlParameter("@Message", ErrorString));
mCommand.Parameters.Add(new SqlParameter("@RowNumber", ParameterDirection.Output));
//mCommand.Transaction = mTransaction
mConnection.Open();
mCommand.Connection = mConnection;
mCommand.ExecuteNonQuery();
Subscribe to:
Posts (Atom)