Regular expression for value Range from 1 - 1440 (Integer) [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 1 year ago.
Improve this question
What will be the regular expression for value Range from 1 - 1440? (Integer)

I think this will do it. I'm running tests. I'm acting under the assumption you will not have numbers padded with zero like "0999"
"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"
breakdown:
[1-9] obvious
[1-9[0-9] allow 10 to 19
[1-9][0-9][0-9] allow 100 to 199
1[0-3][0-9][0-9] allow 1000 to 1399
14[0-3][0-9] allow 1400 to 1439
1440 obvious
Appears to work:
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
for(int i = 0;i < 10000;i++)
{
if(Regex.IsMatch(i.ToString(),"^([1-9]|[1-9][0-9]|[1-9][0-9][0-9]|1[0-3][0-9][0-9]|14[0-3][0-9]|1440)$"))
{
Console.WriteLine(i);
}
}
}
}

Related

Write program which count sum of numbers divided by 4 [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
I need to write a program which count sum of numbers divided by 4. I'm really beginner in C#.
Could you lead me to solve this problem?
I write something like this but I don't know what next and there is one problem because I put only two numbers but numbers could be a lot of.
Console.WriteLine("number a");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("number b");
int b = int.Parse(Console.ReadLine());
int sum = a + b;
int result = sum / 4 ;
Console.WriteLine ( "result is="+ result ) ;
use
double sum = Console.ReadLine().Split().Sum(x => int.Parse(x)) / 4.0;
Than you can enter any space separated array like
1 3 5 7

what is the most efficient data structure for matching specific numbers to ranges of percents? [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 5 years ago.
Improve this question
In my program, I want to be able to match certain numbers to ranges of percentages, like: 0 would be the match of 0%, 1 would be the match of less then 10%, 2 would be the match of 10%-20%... and so forth. What is the most efficient data structure/method to do it ?
I would like to perform it in c#.
A Dictionary for this purpose could be a decent solution. The keys of the Dictionary would be the numbers and the values could be Tuples with the corresponding min and max percentages. If you want to learn the range for a number you could retrieve it's range in O(1).
You could define it as:
var numbersPercentageRanges = new Dictionary<int, Tuple<double, double>>
{
{ 0, Tuple.Create(0,0) },
{ 1, Tuple.Create(0.1,0.2)}
};
and you could retrieve the corresponding range as:
if(numbersPercentageRanges.TryGetValue(1, out var range))
{
var min = range.Item1;
var max = range.Item2;
}

int permutations in C# [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 6 years ago.
Improve this question
I want to implement a method with the following signature:
public int Max(int number){ }
The largest number that can be created by a digits of a given number is obtained by ordering the digits from largest to smallest. See the following code for a possible implementation.
public int Max(int number)
{
var numberAsCharArray = number.ToString().OrderByDescending(c => c).ToArray();
var largestNumberAsString = new string(numberAsCharArray);
return Int32.Parse(str);
}
Maybe not most efficient way is to cast to string and order desc
var result = int.Parse(String.Join("", digit.ToString().OrderByDescending(x => x)))

Programmr "Reverse Array" 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 7 years ago.
Improve this question
I use Programmr.com to improve my skills. I have a problem with this exercise.
Question:
Write the program which takes input 10 elements in the array and
reverse the elements in the array without using any other array.
Example :
1.If user gives input 1,2,3,4,5,5,4,7,3,6 then output should be in this format: 6 3 7 4 5 5 4 3 2 1
2.If user gives input 25,23,,26,12,45,65,58,24,27,13 then output should be in this format: 13 27 24 58 65 45 12 26 23 25
Note : Make sure the output format is same as above given example.
My code:
using System;
class ReverseArray{
static void Main(string[] args){
int[] arr = new int[10];
for(int i =0;i<10;i++)
{
Console.WriteLine("Enter the array elements["+i+"]:");
arr[i]=int.Parse(Console.ReadLine());
}
//write your logic here
for(int i = arr.Length-1; i >= 0; i--)
{
Console.Write(arr[i] + " ");
}
//end
for(int i =0;i<10;i++)
{
Console.Write(arr[i]+" ");
}
}
}
Programmr doesn't pass task and write that there are undefined errors. I also tried method Array.Reverse.
Use Linq reverse method and string.join
You can:
Using System.Linq;
Console.WriteLine(String.Join(" ", arr.Reverse()));
That will reverse the array and print the list separated by spaces to the console

How to numerically sort an array of strings? [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 8 years ago.
Improve this question
I have an array of strings that has a number at the beginning, followed by a name. How can I sort the array numerically; ie
40 Mike, 25 Tyson, 9 Jackson, 5 Phillip, 3 Mitchell
as opposed to
9 Jackson, 5 Phillip, 40 Mike, 3 Mitchell, 25 Tyson
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
string[] items = {"10", "1", "30", "-5"};
Array.Sort(items, (x, y) =>
{
int xNum = int.Parse(x);
int yNum = int.Parse(y);
return Comparer<int>.Default.Compare(xNum, yNum);
});
foreach(var item in items)
Console.WriteLine(item);
}
}
var sorted = yourArray.OrderBy(x => int.Parse(x.Split()[0])).ToArray();
You can use SortedDictionary data structure to obtain an array sorted.

Categories

Resources