for loop printing last number in sequence [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 4 years ago.
Improve this question
How can you print out the last number in the sequence and the amount of times it has ran in a for loop?
int numOne = 50;
for (int i = 0; i < numOne; i++)
{
Console.WriteLine(i);
}

var list = new List<int> { 1, 2, 3, 6, 10, 1, 5, 1 };
var lastItem = list.Last();
Console.WriteLine("{0} {1}", lastItem, list.Count(item => item == lastItem));

Related

How to limit console input [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am trying to check user input which supposes to be a four-digit account number only. if the user enters more or less input. it would say "invalid entry". I am not sure how to check var length in c#.
Console.WriteLine("Enter a four-digit account number");
int acc = Convert.ToInt32(Console.ReadLine());
int d = acc % 10;
acc = acc / 10;
if (acc % 7 == d)
Console.WriteLine("Valid Account Number");
else
Console.WriteLine("Invalid Account Number");
Updated
var inputText = Console.ReadLine();
var inputNumber = 0;
// try to parse - if success, then parsingFlag = True
// and parsed number is stored to inputNumber
var parsingFlag = Int32.TryParse(inputText, out inputNumber);
if ((inputNumber >= 1000) && (inputNumber <= 9999))
{
// do your algorithm
}
else
Console.WriteLine("Invalid entry");

how to separate per 8 digit from string in c#? [closed]

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 3 years ago.
Improve this question
string txte = save.ToString();
for (int i = 7; i < save.Length; i= i+8)
{
outs.Append(txte.Substring(i-7,i)+ " ");
}
Given
public static class Extension
{
public static IEnumerable<string> Chunks(this string input, int size)
// select with index
=> input?.Select((x, i) => i)
// filter by chunk
.Where(i => i % size == 0)
// substring out the chunk, or part thereof
.Select(i => input.Substring(i, Math.Min(size,input.Length - i)));
}
Usage
var s = "11111111222222223333333344444444";
foreach (var result in s.Chunks(8))
Console.WriteLine(result);
Console.WriteLine("---");
// or add space
Console.WriteLine(string.Join(" ", s.Chunks(8)));
Results
11111111
22222222
33333333
44444444
---
11111111 22222222 33333333 44444444
As long as you are sure your input has a length which is the exact multiple of 8 characters then this works easily:
var output =
String.Join(
" ",
Enumerable.Range(0, txt.Length / 8).Select(x => txt.Substring(x * 8, 8)));
If you need to ensure that the input has a length which is the multiple of 8 then do this first:
txt = txt.PadRight(txt.Length + txt.Length % 8 == 0 ? 0 : 8 - txt.Length % 8);

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;
}

How to Zip two numbers using c# [closed]

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 6 years ago.
Improve this question
string A = "1234"
string B = "567890"
I want to zip the numbers. Out put should display as "1526374890"
what is the best way to achieve this using C# code.
You can do that with the following code. It does not make any assumptions about which string is longer.
string A = "1234";
string B = "567890";
char[] chars = new char[A.Length + B.Length];
int charsIndex = 0;
for (int i = 0; i < A.Length || i < B.Length; i++)
{
if(i < A.Length)
chars[charsIndex++] = A[i];
if(i < B.Length)
chars[charsIndex++] = B[i];
}
string result = new string(chars);
Console.WriteLine(result);

C# how to repeat an action [closed]

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 6 years ago.
Improve this question
Basically trying to shorten this:
octree[nodeIndices[0]].nodes[nodeIndices[1]]
.nodes[nodeIndices[2]].nodes[nodeIndices[3]].nodes[nodeIndices[4]]
I thought something like this could work, but not sure exactly how:
octree[nodeIndices[0]].(Enumerable.Repeat(node[nodeIndices[i]], 4)
edit: here's more code
OctreeNode[] nodes = octree;
ushort subdivisions = 1;
while (true)
{
ushort nodeIndex = 0;
OctreeNode node = nodes[nodeIndex];
if (node == null)
{
node = new OctreeNode();
node.nodes = new OctreeNode[8];
nodes = node.nodes;
}
subdivisions++;
if (subdivisions > 4)
break;
}
// the actual octree variable is unchanged
var result = octree[nodeIndices[0]];
for (int i = 1; i <= 4; i++)
result = result.node[nodeIndices[i]];
// do something with result;

Categories