C# simple grid filling algorithm [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to find an efficient way to fill a gird (2 arrays) with numbers representing the area the cell in the grid occupies. For example:
The numbers correlates to the root area. The question is how can I randomly fill the matrix with these values that correlates to the root area
// Grid could look something like this
// 22333
// 22333
// 11333
// or
// 33311
// 33322
// 33322
// What i have currently
int numRows = 4;
int numColums = 5;
int cellSize = Random.Range(0,4);
for (int iRow = 0; iRow < numRows; iRow++) {
for (int iCol = 0; iCol <numColums ; iCol++) {
// Fill value
test.gridData.rows[iRow].colum[iCol] = cellSize;
}
}

int numRows = 4;
int numColums = 5;
int cellSize = Random.Range(0,4);
for (int iRow = 0; iRow < numRows; iRow++) {
for (int iCol = 0; iCol <numColums ; iCol++) {
// HERE
test.gridData[iRow][iCol].MyVal= val;
}
}
Assuming your actual code reflects the code in your question, the MyVal property of your Test matrix at position [row][column] is now equal to the value of cellSize.
You can access individual elements of a 2D array as such:
Assuming your grid is:
0 1 2 3 4
1 1 2 3 4
2 1 2 3 4
3 1 2 3 4
You can access individual cells using something like test.GridData[2][4] which will give you (assuming the above grid):
the value at row[2] => 2
⬇
0 1 2 3 4
1 1 2 3 4
2 1 2 3 4
3 1 2 3 4
and column[4] => 4:
0 1 2 3 4
1 1 2 3 4
2 1 2 3 4
3 1 2 3 4 ⬅

Related

C# Multiplication Table logic

I'm making a c# program that shows the multiplication table, by using my code I can print only complete table, how can I print random table as below.
my code bit:
using System;
public class Exer
{
public static void Main()
{
int j,n;
Console.Write("\n\n");
Console.Write("Display the multiplication table:\n");
Console.Write("-----------------------------------");
Console.Write("\n\n");
Console.Write("Input the number (Table to be calculated) : ");
n= Convert.ToInt32(Console.ReadLine());
Console.Write("\n");
for(j=1;j<=10;j++)
{
Console.Write("{0} X {1} = {2} \n",n,j,n*j);
}
}
}
Expected Output:
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
1 x 4 = 4
2 x 4 = 8
3 x 4 = 12
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
1 x 8 = 8
2 x 8 = 16
3 x 8 = 24
1 x 10 = 10
2 x 10 = 20
3 x 10 = 30
The tables in your picture are NOT random, though. There is a definite pattern. The function, DisplayMath(), is receiving an integer parameter; 3, 4, and 5 in the picture.
The output in the pictures is most likely produced by two NESTED for loops.
The integer parameter is controlling the INNER for loop, which goes from 1 to the parameter, incrementing by 1.
The OUTER loop goes from 2 to 10, incrementing by 2.
Possible code:
public static void Main() {
DisplayMath(4);
}
public static void DisplayMath(int x) {
for(int y=2; y<=10; y=y+2) {
for(int z=1; z<=x; z++) {
Console.WriteLine("{0} X {1} = {2}",z,y,z*y);
}
}
}
Output:
1 X 2 = 2
2 X 2 = 4
3 X 2 = 6
4 X 2 = 8
1 X 4 = 4
2 X 4 = 8
3 X 4 = 12
4 X 4 = 16
1 X 6 = 6
2 X 6 = 12
3 X 6 = 18
4 X 6 = 24
1 X 8 = 8
2 X 8 = 16
3 X 8 = 24
4 X 8 = 32
1 X 10 = 10
2 X 10 = 20
3 X 10 = 30
4 X 10 = 40
As per the required output, you need to change the for loop logic a little.
Because there is a logic in the expected result that you need to get before start coding, that isn't a 'random' table
for(j=2; j<=10; j=j+2 )
{
for (mathNumber = 1; mathNumber <= n; mathNumber++ )
{
Console.Write("{0} X {1} = {2} \n",mathNumber,j,mathNumber*j);
}
}
As you can see the result must be the result of the n first numbers (being n the parameter you get from Console, but perhaps is better that you get it as argument) multiplyed by the numbers 2,4,6,8,10

Show a pyramid based on number input

I'm currently stuck on this problem the goal is to be able to print out a pyramid depending on the input number.
Ex: input = 3
It should look like this:
1
23
I'm able to make it work with some numbers but it doesn't work with other inputs.
The current logic that I follow is by dividing the input by 2 then add the remainder to the next one to be divided.
Example input: 10
10 / 2 = 5 remainder: 0
5 / 2 = 2 remainder: 1
2 + 1 (previous remainder)/ 2 = 1 remainder: 1
1
Whereas it would look like this:
5
2
2
1
The rules of the pyramid that I'm trying to make only needs to have one or two differences per row.
That's why I'll be needing to deduct 1 from 5 and add it to the next one:
4
3
2
1
Thus having pyramid like this: 10
****
***
**
*
The problem is this approach isn't applicable on other inputs and I'm having a hard time on finding a different approach for this.
This code works for your example, although it might be improved to detect whenever a number is not suitable to print a piramid.
static void Main(string[] args)
{
while (true)
{
Console.Write("Enter a number:");
int input = int.Parse(Console.ReadLine());
List<List<string>> piramid = new List<List<string>>();
int rowNumber = 1, dotsCount = 0;
while (dotsCount < input)
{
List<string> row = new List<string>();
for (int i = 1; i <= rowNumber && dotsCount < input; i++)
{
row.Add("*");
dotsCount++;
}
piramid.Add(row);
rowNumber++;
}
Console.WriteLine("Piramid");
Print(piramid);
Console.WriteLine("Piramid (inverted)");
PrintInverted(piramid);
}
}
static void Print(List<List<string>> piramid)
{
foreach (var item in piramid)
{
item.ForEach(Console.Write);
Console.Write("\n");
}
}
static void PrintInverted(List<List<string>> piramid)
{
for (int i = piramid.Count - 1; i >= 0; i--)
{
List<string> item = piramid[i];
item.ForEach(Console.Write);
Console.Write("\n");
}
}

Combination of two arrays [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a integer array that shows the nodes of a path : {1,2,3,4,5}
1 --> 2 --> 3 --> 4 --> 5
But some nodes of this path can be optional.
For example node 4 is optional.
So, I can use this path 1 --> 2 --> 3 --> 4 --> 5
or this path 1 --> 2 --> 3 --> 5 to reach my destination.
I want to produce all path combinations.
//ProduceCombinations(int[] path,int[] possibleNodes)
{1*,2,3,4*,5*}
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
12345
-2345
123-5
-23-5
1234-
-234-
123--
-23--
Here you go:
static void Main(string[] args)
{
int[] pathNodes = new int[] {1,2,3,4,5};
int[] optionalNodes = new int[] { 1, 4, 5 };
List<int[]> combies = ProduceCombinations(pathNodes, optionalNodes);
}
public static List<int[]> ProduceCombinations(int[] PathNodes, int[] OptionalNodes)
{
List<int[]> results = new List<int[]>();
results.Add((int[])PathNodes.Clone());
int index = 0;
for (int j = 0; j < OptionalNodes.Length; j++)
{
while (PathNodes[index] < OptionalNodes[j]) index++;
int lenght = results.Count;
for(int i = 0; i < lenght; i++)
{
var newSol = (int[])results[i].Clone();
newSol[index] = 0;
results.Add(newSol);
}
}
return results;
}

c# Hackerrank code terminated due to time out but there is no way to optimize this code further?

I was doing a hacker rank challenge in c# to try and bring some of my c skills over to c#. Now I know hacker rank is notoriously stupid with killing programs due to time out (in this case if it lasts more than 3 seconds). But I honestly cannot think of a way to optimize this code further.
Here are the instructions:
https://www.hackerrank.com/challenges/ctci-array-left-rotation
Basically the challenge is to shift an array of numbers some x amount of times over in the left direction inside the array.
To my knowledge this code is as minimal as it can get and still do the thing they requested. The only way I can think to optimize this code further is to merge the constraint "if(a[i] > 1000000 || a[i] < 1 )" with the writeline forloop at the end of the code, but I tried that and it didn't work.
To me this is literally the minimum number of operations to move the array over by an amount x. But the code fails in test case 7 and 8 (out of 8) due to time out. Am I missing something?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int i, j;
int temp = 0;
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
//constraints
if(n >= 100000 || n < 1 )
{
System.Environment.Exit(1);
}
if(k > n || n < 1 )
{
System.Environment.Exit(1);
}
for(i = 0; i< n; i++)
{
if(a[i] > 1000000 || a[i] < 1 )
{
System.Environment.Exit(1);
}
}
//double for loop. one adjust position by one. then repeat k number of times.
for(j = 0; j<k; j++)
{
for(i = 0; i< n-1; i++)
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
//view array
for(i = 0; i< n; i++)
{
Console.Write(a[i] + " " );
}
}
}
I used a queue mechanism to get this working. That way you don't have to do any array copy and just rotating the string to the end.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static int[] leftRotation(int[] arr, int rotation) {
Queue<int> queue = new Queue<int>(arr);
for (int i = 0; i < rotation; i++)
{
queue.Enqueue(queue.Dequeue());
}
return queue.ToArray();
}
static void Main(String[] args) {
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int d = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
int[] result = leftRotation(a, d);
Console.WriteLine(String.Join(" ", result));
}
}
Shuffling the values one at a time is very slow. There is no need to do this. One can think of the rotation as moving 2 blocks - the values to the left of the rotation point and the values including and to the right of the rotation point
1 2 3 4 5 6 7 8 9
Rotate this 3 times is
move 1-3 to a temp variable
move 4-9 to the start of the array
move 1-3 to the end of 4-9
Edit: Add a bit more detail
We want to rotate the array 3 places.
move 1 2 3 to a temporary array
1 2 3
1 2 3 4 5 6 7 8 9
move 4-9 to the start of the array
1 2 3
4 5 6 7 8 9 7 8 9
move 1-3 to the end of the array
1 2 3
4 5 6 7 8 9 1 2 3
We can get away without the temp array for the left hand block if we create a new target array and copy everything to it. The following passes all the tests for the problem
var result = new int[a.Length];
var block2Length = a.Length - k;
Array.Copy(a, k, result, 0, block2Length);
Array.Copy(a, 0, result, block2Length, k);
Console.WriteLine(string.Join(" ", result.Select(v => v.ToString())));
Other Points
The constraints in HackerRank are part of the problem definition - they are telling us what the values can/will do so that we don't have to worry about solving a more general problem
e.g.
1 <= a[i] < 10^6
tells us the numbers will all be within the range for standard integers, no need to use long or BigInteger. As part of the solution we don't need to confirm these. In a real world situation different rules apply but here we have as much code checking values that cannot be wrong as we have solving the problem.
This has already been answered but there is a different solution that is very fast:
The idea is that you use the periodicity of the shifting. Shifting an element n+1 times is the same as shifting it 1 time which boils down to k % n.
Thus, you can simply create a new array and move the "old" elements directly to the correct spot. See the sample code below:
static void Main(String[] args) {
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
// use the periodicity of the shifting creating a shift between 0 and n - 1.
int shifts = k % n;
// Create a new array to hold the elements at their new positions.
int[] newPositions = new int[a.Length];
// You only need to iterate of the array once assigning each element its new position.
for(int i= 0; i < a.Length; ++i)
{
// Here is the magic: (for clarification see table below)
int position = (i - shifts + n)%n;
newPositions[position] = a[i];
}
foreach(var element in newPositions)
Console.Write($"{element} ");
}
This is more intuitive once you write it down on paper. The table might prints more values than the array contains to show the actual positions in the array.
-4 -3 -2 -1 | 0 1 2 3 4 (array indices)
=============================
2 3 4 5 | 1 2 3 4 5 (array contents without shift)
3 4 5 1 | 2 3 4 5 1 (shifted 1 time)
4 5 1 2 | 3 4 5 1 2 (shifted 2 times)
5 1 2 3 | 4 5 1 2 3 (shifted 3 times)
1 2 3 4 | 5 1 2 3 4 (shifted 4 times)
formula: i - k + n
results:
i=0: 0 - 4 + 5 = 1
i=1: 1 - 4 + 5 = 2
i=2: 2 - 4 + 5 = 3
i=3: 3 - 4 + 5 = 4
i=4: 4 - 4 + 5 = 5
i=5: 5 - 4 + 5 = 6 => 0
EDIT: I skipped the parts to check the boundaries for the sake of simplicity.
Updated answer.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int i, j, z;
string[] tokens_n = Console.ReadLine().Split(' ');
int n = Convert.ToInt32(tokens_n[0]);
int k = Convert.ToInt32(tokens_n[1]);
string[] a_temp = Console.ReadLine().Split(' ');
int[] a = Array.ConvertAll(a_temp,Int32.Parse);
int[] temparray = new int[2*n];
//constraints
if(n >= 100000 || n < 1 )
{
System.Environment.Exit(1);
}
if(k > n || n < 1 )
{
System.Environment.Exit(1);
}
for(i = 0; i< n; i++)
{
if(a[i] > 1000000 || a[i] < 1 )
{
System.Environment.Exit(1);
}
}
for(j = 0; j<n; j++)
{
z = (j-k) %n;
if(z != 0)
{
z= (n+ z) %n;
}
temparray[z] = a[j];
}
//view array
for(i = 0; i< n; i++)
{
Console.Write(temparray[i] + " " );
}
}
}

Algorithm to represent a sequence of numbers

I have a sequence of numbers to generate, and I want to generate it using some sort of algorithm (iterative or recursive, doesn't matter).
Contextualizing: This numbers are indexes to iterative over a list of lists. I need to do a permutation (combination, i don't know exactly), but I need to generate all combinations of all positions of that list of lists.
The sequence and the output I am trying to get is:
1 1
2 1
3 1
4 1
5 1
1 2
2 1
3 1
4 1
5 1
1 3
2 1
3 1
4 1
5 1
1 4
2 1
3 1
4 1
5 1
1 5
2 1
3 1
4 1
5 1
1 1
2 2
3 1
4 1
5 1
1 2
2 2
3 1
4 1
5 1
1 3
2 2
3 1
4 1
5 1
1 4
2 2
3 1
4 1
5 1
1 5
2 2
3 1
4 1
5 1
1 1
2 3
3 1
4 1
5 1
1 2
2 3
3 1
4 1
5 1
1 3
2 3
3 1
4 1
5 1
1 4
2 3
3 1
4 1
5 1
1 5
2 3
3 1
4 1
5 1
1 1
2 4
3 1
4 1
5 1
and so on... the last state is:
1 5
2 5
3 5
4 5
5 5
Note that at each line break is a step of iteration or recursion. The algorithm must be generic. This code that i wrote can help, but it isn't what I want. :(
List<List<int>> lstDays = new List<List<int>>
{
new List<int>{1,2,3,4,5}, //day 18
new List<int>{1,2,3,4,5}, //day 19
new List<int>{1,2,3,4,5}, //day 22
new List<int>{1,2,3,4,5}, //day 23
new List<int>{1,2,3,4,5}, //day 24
};
for(int i=0;i<lstDays.Count;i++)
{
for(int j=0;j<lstDays[i].Count;j++)
{
for(int k=0;k<lstDays.Count;k++)
{
Console.Write(k+1);
//Console.Write(j+1);
Console.Write('\n');
}
Console.Write('\n');
}
}
I hope that you can help me ! (:
You can do it like this:
int[] second = new[] {0,0,0,0,0};
bool finish = false;
while (true) {
for (int i = 0 ; i != 5 ; i++) {
Console.WriteLine("{0} {1}", i+1, second[i]+1);
}
Console.WriteLine();
int p = 0;
do {
second[p]++;
if (second[p] == 5) {
second[p] = 0;
p++;
} else {
break;
}
} while (p != 5);
if (p == 5) break;
}
The sequence of the second digits is stored in the array "creatively" named second. The do/while loop "increments" this array as if it were a base-5 number stored as five separate digits.
Here is a demo on ideone.
Based on comments below by the venerable Eric Lippert, edits for the OPs original intent:
public void OutputSequence(int length){
Recurse(length-1, Enumerable.Range(1, length).ToArray(), new int[length]);
}
public void Recurse(int position, int[] arr, int[] state){
if (position == -1){
PrintState(state);
return;
}
for (int i = 0; i < arr.Length; i++)
{
state[position] = arr[i];
Recurse(position-1, arr, state);
}
}
public void PrintState(int[] state){
for (int i = 0; i < state.Length; i++)
Console.WriteLine ("{0} {1}",i+1, state[i]);
Console.WriteLine ();
}
OutputSequence(5); will give the output the OP originally asked for.
Old Answer
What you're looking for is called a Cartesian Product. LINQ is your friend:
var pairs = from i in Enumerable.Range(1, 5)
from j in Enumerable.Range(1, 5)
select new {i, j};
foreach(var p in pairs)
Console.WriteLine ("{0} {1}", p.i, p.j);
EDIT: Just for fun, here's a way to do N-Ary cartesian products.
public IEnumerable<IEnumerable<int>> NAryCartesianProduct(int upper, int times){
if (times == 0)
return Enumerable.Empty<IEnumerable<int>>();
var nums = Enumerable.Range(1, upper);
IEnumerable<IEnumerable<int>> products = nums.Select(i => new[]{i});
for (int i = 1; i < times; i++)
{
products = from p in products
from n in nums
select p.Concat(new [] {n});
}
return products;
}
And now you can get what you had before with:
var p = NAryCartesianProduct(5, 2);
foreach(var i in p)
Console.WriteLine (i);
I'm sure there's a more efficient way than creating new arrays all of the time but I just hacked this up quick :)
Here's a much more informative answer on this: Generating all Possible Combinations
EDIT2: Apparently the original link is the origination of the answer from that SO post. I didn't read through to the end until now.

Categories