Generate Alpha Numeric Sequence in C# - c#

I want to generate a string sequence in c#.And this sequence starts with 1 and ends on ZZ. for e.g.:-
1-99
A0-A9
AA-AZ
B0-B9
BA-BZ
.....
....
Z0-Z9
ZA-ZZ
So this is a sequence that i wants to generate and the the length not more then 2 characters means the end of sequence is ZZ.
So Please help me out and if possible can we also do it in oracle.
Thanks in Advance.
Regards
Anil

You want a string like "123456789101112...99A0A1...ZZ", right?
This should fit:
string sequenceStr = "";
for (int i = 1; i < 100; i++)
sequenceStr += i.ToString();
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < 36; j++)
{
sequenceStr += Encoding.UTF8.GetString(new byte[] { (byte)(i + 65) }); // A=65, B=66, ...
if (j < 10)
{
sequenceStr += j.ToString();
}
if (j > 9)
{
sequenceStr += Encoding.UTF8.GetString(new byte[] { (byte)(j + 55) });
}
}
}

Related

C#, Windows application, draw a square with side N

for school practice I have to make a windows application in C# that takes a input number and draws a square of X's with side N. I have to do it with a loops and i can't use any preset commands. (For example math.pow i cannot use) (I've included a picture of the assignment.) I've already mode this program in a console application and there it worked fine.
I think that i'm very close of solving it but can't figure out what the last step is. I would love to know what i'm missing and how i should solve this.
See the assignment
This is my code now:
int n;
n = int.Parse(txt_input.Text);
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text = "X";
lbl_output.Text = "\n";
}
//middel part
for (int i = 0; i < n - 2; i++)
{
lbl_output.Text = "X";
for (int j = 0; j < n - 2; j++) lbl_output.Text = " ";
lbl_output.Text = "X";
lbl_output.Text = "\n";
}
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text = "X";
lbl_output.Text = "\n";
}
Try this:
int n = int.Parse(txt_input.Text);
var sb = new StringBuilder();
for (int j = 0; j < n; j++)
{
sb.Append('X');
}
sb.AppendLine();
for (int i = 0; i < n - 2; i++)
{
sb.Append('X');
for (int j = 0; j < n - 2; j++)
{
sb.Append(' ');
}
sb.Append('X');
sb.AppendLine();
}
for (int j = 0; j < n; j++)
{
sb.Append('X');
}
lbl_output.Text = sb.ToString();
Try this;
int n = int.Parse(txt_input.Text);
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text += "X";
}
lbl_output.Text += "\n";
//middel part
for (int i = 0; i < n - 2; i++)
{
lbl_output.Text += "X";
for (int j = 0; j < n - 2; j++)
lbl_output.Text += " ";
lbl_output.Text += "X";
lbl_output.Text += "\n";
}
//upper part
for (int i = 0; i < n; i++)
{
lbl_output.Text += "X";
}
You forget to use += which will append the text with the previous assigned text. Also you had some unnecessary new lines in your code.
Need to append to the string. There's a couple ways of doing this. Tho '+=' should work fine. += is short for variable = variable + newValue
int n = int.Parse(txt_input.Text);
string output = "";
for(int x = 0; x < n; x++) //rows
{
if (x == 0 || x == n-1) //first / last row all x
for(int y = 0; y < n; y++)
{
output += "x";
}
else //other rows
for(int y = 0; y < n; y++)
{
output += (y == 0 || y == n - 1) ? "x" : " "; //if first or last column "X" else " "
}
output += "\n"; //at the end of each row a return
}
lbl_output.Text = output;
You can see it run in the browser here: https://dotnetfiddle.net/wiYfjX
Use two loops. One for the width of the square and one for the height of the square.
Give this a try and replace the parameter from what is in the txt_input control. (Just place the function whereever you want in your code (button_click for example) instead of the form load.
private void Form1_Load(object sender, EventArgs e)
{
lblOutput.Text = GenerateSquare(5);
}
private string GenerateSquare(int n)
{
string square = "";
for (int w = 0; w < n; w++)
{
for (int h = 0; h < n; h++)
{
// top or bottom line
if (w == 0 || w == n - 1)
{
square += "x";
}
else // sides
{
if (h == 0 || h == n - 1)
{
square += "x";
}
else square += " ";
}
// change line
if (h == n - 1)
square += "\n";
}
}
return square;
}

Why do I get the error 'unreachable code detected'?

using System;
namespace G09 {
class Reverse {
static void Main()
{
Console.WriteLine(ReverseText(24));
Console.ReadKey();
}
static string ReverseText(int n) {
if (n < 1)
{
return "";
}
string index = "1";
string revIndex = "";
int count = 1;
string[] arr = new string[n];
for (int i = 0; i < n; i++)
{
arr[i] = index + revIndex;
for (int j = 0; j <= i; j++)
{
if (count > 8)
{
index = index + 0;
count = 0;
break;
}
index = index + (count++ + 1).ToString();
break;
}
revIndex = "";
for (int k = index.Length - 1; k >= 0; k--)
{
if (k == index.Length - 1)
{
continue;
}
revIndex += index[k];
}
}
return string.Join("\n", arr);
}
}
}
/tmp/csharp117013-18-5s54om.vh6mt2o6r/code.cs(10,41): warning CS0162:
Unreachable code detected
Can anyone tell me why this code is unreachable on line 23??
I can not understand... In visual studio it works fine, but in other shows error.
The j++ is pointless because of the break at the end, remove one of both
for (int j = 0; j <= i; j++) {
...
break;
}
You will never increment j because you break the iteration after the first loop
for (int j = 0; j <= i; j++) //<- Error here, j++ will never be reached
{
if (count > 8)
{
index = index + 0;
count = 0;
break;
}
index = index + (count++ + 1).ToString();
break; //you leave the for loop here
}
Your issue is with this for loop:
for (int j = 0; j <= i; j++) {
if (count > 8) {
index = index + 0;
count = 0;
break;
}
index = index + (count++ + 1).ToString();
break;
}
I'm guessing the for loop starts on line 23. Because you have all paths inside the for loop containing break statements the j++ part of the for loop will never be hit. The loop will execute once and break out before ever hitting the increment for variable j.

Chess board positions

How can i make this so the user types positions in console (table[4,5]) i want that user types that?
int[,] table = new int[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if ((i + j) % 2 == 0)
{
table[i, j] = 0;
}
else
{
table[i, j] = 1;
}
}
}
Console.WriteLine("4 - king");
Console.WriteLine("3 - queen");
Console.WriteLine("4 - hunter");
table[4,5] = 2;
table[6,7] = 3;
table[2,2] = 4;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Console.Write(table[i, j] + " ");
}
Console.WriteLine();
}
What should i do to make this work?And if i type this it doesnt work:
Thats why i have to put figures in different rows or columns how do i fix this
table[4,5] = 2;
table[4,7] = 3;
table[2,2] = 4;
You are confusing the assignment operator "=" with the logical comparison operator "==". Your second line is just comparing table[4,5] with 2 and probably returning false.
Change it to:
table[4,5] = 2;
Also, even if you manage to assign a value to table[4,5], you will overwrite it in the next lines. You should move that line to the end of the first nested loop. Just before the second "for (int i = 0; i < 8; i++)"

Neural network [ocr]

I come looking for general tips about the program I'm writing now.
The goal is:
Use neural network program to recognize 3 letters [D,O,M] (or display "nothing is recognized" if i input anything other than those 3).
Here's what I have so far:
A class for my single neuron
public class neuron
{
double[] weights;
public neuron()
{
weights = null;
}
public neuron(int size)
{
weights = new double[size + 1];
Random r = new Random();
for (int i = 0; i <= size; i++)
{
weights[i] = r.NextDouble() / 5 - 0.1;
}
}
public double output(double[] wej)
{
double s = 0.0;
for (int i = 0; i < weights.Length; i++) s += weights[i] * wej[i];
s = 1 / (1 + Math.Exp(s));
return s;
}
}
A class for a layer:
public class layer
{
neuron[] tab;
public layer()
{
tab = null;
}
public layer(int numNeurons, int numInputs)
{
tab = new neuron[numNeurons];
for (int i = 0; i < numNeurons; i++)
{
tab[i] = new neuron(numInputs);
}
}
public double[] compute(double[] wejscia)
{
double[] output = new double[tab.Length + 1];
output[0] = 1;
for (int i = 1; i <= tab.Length; i++)
{
output[i] = tab[i - 1].output(wejscia);
}
return output;
}
}
And finally a class for a network
public class network
{
layer[] layers = null;
public network(int numLayers, int numInputs, int[] npl)
{
layers = new layer[numLayers];
for (int i = 0; i < numLayers; i++)
{
layers[i] = new layer(npl[i], (i == 0) ? numInputs : (npl[i - 1]));
}
}
double[] compute(double[] inputs)
{
double[] output = layers[0].compute(inputs);
for (int i = 1; i < layers.Length; i++)
{
output = layers[i].compute(output);
}
return output;
}
}
Now for the algorythm I chose:
I have a picture box, size 200x200, where you can draw a letter (or read one from jpg file).
I then convert it to my first array(get the whole picture) and 2nd one(cut the non relevant background around it) like so:
Bitmap bmp2 = new Bitmap(this.pictureBox1.Image);
int[,] binaryfrom = new int[bmp2.Width, bmp2.Height];
int minrow=0, maxrow=0, mincol=0, maxcol=0;
for (int i = 0; i < bmp2.Height; i++)
{
for (int j = 0; j < bmp2.Width; j++)
{
if (bmp2.GetPixel(j, i).R == 0)
{
binaryfrom[i, j] = 1;
if (minrow == 0) minrow = i;
if (maxrow < i) maxrow = i;
if (mincol == 0) mincol = j;
else if (mincol > j) mincol = j;
if (maxcol < j) maxcol = j;
}
else
{
binaryfrom[i, j] = 0;
}
}
}
int[,] boundaries = new int[binaryfrom.GetLength(0)-minrow-(binaryfrom.GetLength(0)-(maxrow+1)),binaryfrom.GetLength(1)-mincol-(binaryfrom.GetLength(1)-(maxcol+1))];
for(int i = 0; i < boundaries.GetLength(0); i++)
{
for(int j = 0; j < boundaries.GetLength(1); j++)
{
boundaries[i, j] = binaryfrom[i + minrow, j + mincol];
}
}
And convert it to my final array of 12x8 like so (i know I could shorten this a fair bit, but wanted to have every step in different loop so I can see what went wrong easier[if anything did]):
int[,] finalnet = new int[12, 8];
int k = 1;
int l = 1;
for (int i = 0; i < finalnet.GetLength(0); i++)
{
for (int j = 0; j < finalnet.GetLength(1); j++)
{
finalnet[i, j] = 0;
}
}
while (k <= finalnet.GetLength(0))
{
while (l <= finalnet.GetLength(1))
{
for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * (k - 1); i < (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * k; i++)
{
for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * (l - 1); j < (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * l; j++)
{
if (boundaries[i, j] == 1) finalnet[k-1, l-1] = 1;
}
}
l++;
}
l = 1;
k++;
}
int a = boundaries.GetLength(0);
int b = finalnet.GetLength(1);
if((a%b) != 0){
k = 1;
while (k <= finalnet.GetLength(1))
{
for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * finalnet.GetLength(0); i < boundaries.GetLength(0); i++)
{
for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * (k - 1); j < (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * k; j++)
{
if (boundaries[i, j] == 1) finalnet[finalnet.GetLength(0) - 1, k - 1] = 1;
}
}
k++;
}
}
if (boundaries.GetLength(1) % finalnet.GetLength(1) != 0)
{
k = 1;
while (k <= finalnet.GetLength(0))
{
for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * (k - 1); i < (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * k; i++)
{
for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * finalnet.GetLength(1); j < boundaries.GetLength(1); j++)
{
if (boundaries[i, j] == 1) finalnet[k - 1, finalnet.GetLength(1) - 1] = 1;
}
}
k++;
}
for (int i = (int)(boundaries.GetLength(0) / finalnet.GetLength(0)) * finalnet.GetLength(0); i < boundaries.GetLength(0); i++)
{
for (int j = (int)(boundaries.GetLength(1) / finalnet.GetLength(1)) * finalnet.GetLength(1); j < boundaries.GetLength(1); j++)
{
if (boundaries[i, j] == 1) finalnet[finalnet.GetLength(0) - 1, finalnet.GetLength(1) - 1] = 1;
}
}
}
The result is a 12x8 (I can change it in the code to get it from some form controls) array of 0 and 1, where 1 form the rough shape of a letter you drawn.
Now my questions are:
Is this a correct algorythm?
Is my function
1/(1+Math.Exp(x))
good one to use here?
What should be the topology? 2 or 3 layers, and if 3, how many neurons in hidden layer? I have 96 inputs (every field of the finalnet array), so should I also take 96 neurons in the first layer? Should I have 3 neurons in the final layer or 4(to take into account the "not recognized" case), or is it not necessary?
Thank you for your help.
EDIT: Oh, and I forgot to add, I'm gonna train my network using Backpropagation algorythm.
You may need 4 layers at least to get accurate results using back propagation method. 1 input, 2 middle layers, and an output layer.
12 * 8 matrix is too small(and you may end up in data loss which will result in total failure) - try some thing 16 * 16. If you want to reduce the size then you have to peel out the outer layers of black pixels further.
Think about training the network with your reference characters.
Remember that you have to feed back the output back to the input layer again and iterate it multiple times.
A while back I created a neural net to recognize digits 0-9 (python, sorry), so based on my (short) experience, 3 layers are ok and 96/50/3 topology will probably do a good job. As for the output layer, it's your choice; you can either backpropagate all 0s when the input image is not a D, O or M or use the fourth output neuron to indicate that the letter was not recognized. I think that the first option would be the best one because it's simpler (shorter training time, less problems debugging the net...), you just need to apply a threshold under which you classify the image as 'not recognized'.
I also used the sigmoid as activation function, I didn't try others but it worked :)

How to create a loop for the following case?

/html/body/table/tr[1]/td[2]
/html/body/table/tr[1]/td[4]
/html/body/table/tr[3]/td[2]
/html/body/table/tr[3]/td[4]
/html/body/table/tr[5]/td[2]
/html/body/table/tr[5]/td[4]
So, the index of tr[ ] would be odd numbers, and td[ ] would always be either 2 or 4.
for(int i = 1; i < bound; i += 2) {
for(int j = 2; j <= 4; j += 2) {
Console.WriteLine(
String.Format("/html/body/table/tr[{0}]/td[{1}]", i, j)
);
}
Console.WriteLine();
}
You could do something as simple as
for(tr = 1; tr < maxodd+1; tr += 2;)
{
//pseudoimplementation
/html/bod/table/tr[tr]/td[2]
/html/bod/table/tr[tr]/td[4]
}
The most naive case:
for(int i = 1; i < 6; i += 2) {
Console.WriteLine("html/body/table/tr[" + i + "]/td[2]");
Console.WriteLine("html/body/table/tr[" + i + "]/td[4]");
}

Categories

Resources