Related
I'm trying to understand arrays better and I've found this piece of code on this website that fills an array with a load of random numbers. I was wondering how you would go about, say, extracting a range of numbers. So if i wanted to find out how many of these random numbers inside of the array, were between 25 and 50, how would i go about doing this? I've heard about the Array.FindAll<> however i have no clue on how to use it.
Thank you in advance.
Random r = new Random();
int count = 100;
// Create an array with count elements.
int[] numbers = new int[count];
// Loop over each index
for (int i = 0; i < count; i++)
{
// Generate and store a random number at current index
numbers[i] = r.Next(1, 100);
}
Using LINQ you can try the following:
var numbersInRange = numbers.Where(number => number > 25 && number < 60)
.ToArray();
The above will filter out all the numbers in the range (25,60). If you want to get also the numbers that are equal to either 25 or 60, you just have to use >= and <= repsectively.
If you don't want to use LINQ (which I couldn't explain), you could try the following:
var numbersInRange = new List<int>();
foreach(var number in numbers)
{
if(number > 25 && number < 60)
{
numbersInRange.Add(number);
}
}
So at the end you would have a list that would contains your numbers.
Comparing this to the LINQ version, I think that LINQ is the winner in simplicity and readability.
Using Linq it is a single line
int[] inRange = numbers.Where(x => x >= 25 && x <= 50).ToArray();
The Enumerable methods are a collection of methods that operates on sequences of values and apply a predicate expression (x => 25 && x <= 50) to each member (x) of the sequence (numbers). In your particular case the Where method filters the sequence based on the result of the boolean expression (the return includes the members where the expression returns true).
We can use Linq. First have using System.Linq in your namespaces so the extension methods are available.
To find the numbers:
numbers.Where(x => x >= 25 & x <= 50)
This takes a lambda expression (in this case x => x >= 25 & x <= 50) that takes on object of the type in the sequence (in this case int) returns bool and then evaluates for that expression for all contained items, filtering out those that do not.
You can use ToArray() on the result to get it back into an array, or just work on the results directly.
To get which numbers, that is to say, which indices the matches where at, you can use:
numbers.Select((x, i) => new {El = x, Idx = i}).Where(x => x.EL >= 25 & x.El <= 50).Select(x => x.Idx)
This first creates a new anonymous object for each item where El is the value held and Idx is the index it was at. Then it does the same Where as before, but on the El property of the object we created in the first step. Finally it extracts out the Idx property of the survivors so we have all of the matching indices.
Although "Linq" can do the job as shown by other answers ; there is also, as you heard about, Array.FindAll which behaves the same but already returns an array (and so doesn't need a call to ToArray)
int[] matchedNumbers = Array.FindAll (currentNumber => 25 <= currentNumber && currentNumber <= 50);
As for the "strange" arrow notation it's a lambda expression
I've been trying to solve this interview problem which asks to shuffle a string so that no two adjacent letters are identical
For example,
ABCC -> ACBC
The approach I'm thinking of is to
1) Iterate over the input string and store the (letter, frequency)
pairs in some collection
2) Now build a result string by pulling the highest frequency (that is > 0) letter that we didn't just pull
3) Update (decrement) the frequency whenever we pull a letter
4) return the result string if all letters have zero frequency
5) return error if we're left with only one letter with frequency greater than 1
With this approach we can save the more precious (less frequent) letters for last. But for this to work, we need a collection that lets us efficiently query a key and at the same time efficiently sort it by values. Something like this would work except we need to keep the collection sorted after every letter retrieval.
I'm assuming Unicode characters.
Any ideas on what collection to use? Or an alternative approach?
You can sort the letters by frequency, split the sorted list in half, and construct the output by taking letters from the two halves in turn. This takes a single sort.
Example:
Initial string: ACABBACAB
Sort: AAAABBBCC
Split: AAAA+BBBCC
Combine: ABABABCAC
If the number of letters of highest frequency exceeds half the length of the string, the problem has no solution.
Why not use two Data Structures: One for sorting (Like a Heap) and one for key retrieval, like a Dictionary?
The accepted answer may produce a correct result, but is likely not the 'correct' answer to this interview brain teaser, nor the most efficient algorithm.
The simple answer is to take the premise of a basic sorting algorithm and alter the looping predicate to check for adjacency rather than magnitude. This ensures that the 'sorting' operation is the only step required, and (like all good sorting algorithms) does the least amount of work possible.
Below is a c# example akin to insertion sort for simplicity (though many sorting algorithm could be similarly adjusted):
string NonAdjacencySort(string stringInput)
{
var input = stringInput.ToCharArray();
for(var i = 0; i < input.Length; i++)
{
var j = i;
while(j > 0 && j < input.Length - 1 &&
(input[j+1] == input[j] || input[j-1] == input[j]))
{
var tmp = input[j];
input[j] = input[j-1];
input[j-1] = tmp;
j--;
}
if(input[1] == input[0])
{
var tmp = input[0];
input[0] = input[input.Length-1];
input[input.Length-1] = tmp;
}
}
return new string(input);
}
The major change to standard insertion sort is that the function has to both look ahead and behind, and therefore needs to wrap around to the last index.
A final point is that this type of algorithm fails gracefully, providing a result with the fewest consecutive characters (grouped at the front).
Since I somehow got convinced to expand an off-hand comment into a full algorithm, I'll write it out as an answer, which must be more readable than a series of uneditable comments.
The algorithm is pretty simple, actually. It's based on the observation that if we sort the string and then divide it into two equal-length halves, plus the middle character if the string has odd length, then corresponding positions in the two halves must differ from each other, unless there is no solution. That's easy to see: if the two characters are the same, then so are all the characters between them, which totals ⌈n/2⌉+1 characters. But a solution is only possible if there are no more than ⌈n/2⌉ instances of any single character.
So we can proceed as follows:
Sort the string.
If the string's length is odd, output the middle character.
Divide the string (minus its middle character if the length is odd) into two equal-length halves, and interleave the two halves.
At each point in the interleaving, since the pair of characters differ from each other (see above), at least one of them must differ from the last character output. So we first output that character and then the corresponding one from the other half.
The sample code below is in C++, since I don't have a C# environment handy to test with. It's also simplified in two ways, both of which would be easy enough to fix at the cost of obscuring the algorithm:
If at some point in the interleaving, the algorithm encounters a pair of identical characters, it should stop and report failure. But in the sample implementation below, which has an overly simple interface, there's no way to report failure. If there is no solution, the function below returns an incorrect solution.
The OP suggests that the algorithm should work with Unicode characters, but the complexity of correctly handling multibyte encodings didn't seem to add anything useful to explain the algorithm. So I just used single-byte characters. (In C# and certain implementations of C++, there is no character type wide enough to hold a Unicode code point, so astral plane characters must be represented with a surrogate pair.)
#include <algorithm>
#include <iostream>
#include <string>
// If possible, rearranges 'in' so that there are no two consecutive
// instances of the same character.
std::string rearrange(std::string in) {
// Sort the input. The function is call-by-value,
// so the argument itself isn't changed.
std::string out;
size_t len = in.size();
if (in.size()) {
out.reserve(len);
std::sort(in.begin(), in.end());
size_t mid = len / 2;
size_t tail = len - mid;
char prev = in[mid];
// For odd-length strings, start with the middle character.
if (len & 1) out.push_back(prev);
for (size_t head = 0; head < mid; ++head, ++tail)
// See explanatory text
if (in[tail] != prev) {
out.push_back(in[tail]);
out.push_back(prev = in[head]);
}
else {
out.push_back(in[head]);
out.push_back(prev = in[tail]);
}
}
}
return out;
}
you can do that by using a priority queue.
Please find the below explanation.
https://iq.opengenus.org/rearrange-string-no-same-adjacent-characters/
Here is a probabilistic approach. The algorithm is:
10) Select a random char from the input string.
20) Try to insert the selected char in a random position in the output string.
30) If it can't be inserted because of proximity with the same char, go to 10.
40) Remove the selected char from the input string and go to 10.
50) Continue until there are no more chars in the input string, or the failed attempts are too many.
public static string ShuffleNoSameAdjacent(string input, Random random = null)
{
if (input == null) return null;
if (random == null) random = new Random();
string output = "";
int maxAttempts = input.Length * input.Length * 2;
int attempts = 0;
while (input.Length > 0)
{
while (attempts < maxAttempts)
{
int inputPos = random.Next(0, input.Length);
var outputPos = random.Next(0, output.Length + 1);
var c = input[inputPos];
if (outputPos > 0 && output[outputPos - 1] == c)
{
attempts++; continue;
}
if (outputPos < output.Length && output[outputPos] == c)
{
attempts++; continue;
}
input = input.Remove(inputPos, 1);
output = output.Insert(outputPos, c.ToString());
break;
}
if (attempts >= maxAttempts) throw new InvalidOperationException(
$"Shuffle failed to complete after {attempts} attempts.");
}
return output;
}
Not suitable for strings longer than 1,000 chars!
Update: And here is a more complicated deterministic approach. The algorithm is:
Group the elements and sort the groups by length.
Create three empty piles of elements.
Insert each group to a separate pile, inserting always the largest group to the smallest pile, so that the piles differ in length as little as possible.
Check that there is no pile with more than half the total elements, in which case satisfying the condition of not having same adjacent elements is impossible.
Shuffle the piles.
Start yielding elements from the piles, selecting a different pile each time.
When the piles that are eligible for selection are more than one, select randomly, weighting by the size of each pile. Piles containing near half of the remaining elements should be much preferred. For example if the remaining elements are 100 and the two eligible piles have 49 and 40 elements respectively, then the first pile should be 10 times more preferable than the second (because 50 - 49 = 1 and 50 - 40 = 10).
public static IEnumerable<T> ShuffleNoSameAdjacent<T>(IEnumerable<T> source,
Random random = null, IEqualityComparer<T> comparer = null)
{
if (source == null) yield break;
if (random == null) random = new Random();
if (comparer == null) comparer = EqualityComparer<T>.Default;
var grouped = source
.GroupBy(i => i, comparer)
.OrderByDescending(g => g.Count());
var piles = Enumerable.Range(0, 3).Select(i => new Pile<T>()).ToArray();
foreach (var group in grouped)
{
GetSmallestPile().AddRange(group);
}
int totalCount = piles.Select(e => e.Count).Sum();
if (piles.Any(pile => pile.Count > (totalCount + 1) / 2))
{
throw new InvalidOperationException("Shuffle is impossible.");
}
piles.ForEach(pile => Shuffle(pile));
Pile<T> previouslySelectedPile = null;
while (totalCount > 0)
{
var selectedPile = GetRandomPile_WeightedByLength();
yield return selectedPile[selectedPile.Count - 1];
selectedPile.RemoveAt(selectedPile.Count - 1);
totalCount--;
previouslySelectedPile = selectedPile;
}
List<T> GetSmallestPile()
{
List<T> smallestPile = null;
int smallestCount = Int32.MaxValue;
foreach (var pile in piles)
{
if (pile.Count < smallestCount)
{
smallestPile = pile;
smallestCount = pile.Count;
}
}
return smallestPile;
}
void Shuffle(List<T> pile)
{
for (int i = 0; i < pile.Count; i++)
{
int j = random.Next(i, pile.Count);
if (i == j) continue;
var temp = pile[i];
pile[i] = pile[j];
pile[j] = temp;
}
}
Pile<T> GetRandomPile_WeightedByLength()
{
var eligiblePiles = piles
.Where(pile => pile.Count > 0 && pile != previouslySelectedPile)
.ToArray();
Debug.Assert(eligiblePiles.Length > 0, "No eligible pile.");
eligiblePiles.ForEach(pile =>
{
pile.Proximity = ((totalCount + 1) / 2) - pile.Count;
pile.Score = 1;
});
Debug.Assert(eligiblePiles.All(pile => pile.Proximity >= 0),
"A pile has negative proximity.");
foreach (var pile in eligiblePiles)
{
foreach (var otherPile in eligiblePiles)
{
if (otherPile == pile) continue;
pile.Score *= otherPile.Proximity;
}
}
var sumScore = eligiblePiles.Select(p => p.Score).Sum();
while (sumScore > Int32.MaxValue)
{
eligiblePiles.ForEach(pile => pile.Score /= 100);
sumScore = eligiblePiles.Select(p => p.Score).Sum();
}
if (sumScore == 0)
{
return eligiblePiles[random.Next(0, eligiblePiles.Length)];
}
var randomScore = random.Next(0, (int)sumScore);
int accumulatedScore = 0;
foreach (var pile in eligiblePiles)
{
accumulatedScore += (int)pile.Score;
if (randomScore < accumulatedScore) return pile;
}
Debug.Fail("Could not select a pile randomly by weight.");
return null;
}
}
private class Pile<T> : List<T>
{
public int Proximity { get; set; }
public long Score { get; set; }
}
This implementation can suffle millions of elements. I am not completely convinced that the quality of the suffling is as perfect as the previous probabilistic implementation, but should be close.
func shuffle(str:String)-> String{
var shuffleArray = [Character](str)
//Sorting
shuffleArray.sort()
var shuffle1 = [Character]()
var shuffle2 = [Character]()
var adjacentStr = ""
//Split
for i in 0..<shuffleArray.count{
if i > shuffleArray.count/2 {
shuffle2.append(shuffleArray[i])
}else{
shuffle1.append(shuffleArray[i])
}
}
let count = shuffle1.count > shuffle2.count ? shuffle1.count:shuffle2.count
//Merge with adjacent element
for i in 0..<count {
if i < shuffle1.count{
adjacentStr.append(shuffle1[i])
}
if i < shuffle2.count{
adjacentStr.append(shuffle2[i])
}
}
return adjacentStr
}
let s = shuffle(str: "AABC")
print(s)
I want to easily pre-populate a single dimensional string array which I am calling "letters" with the values:
AAAAAA
AAAAAB
AAAAAC
AAAAAD
..
..
ZZZZZX
ZZZZZY
ZZZZZZ
Thats 165 million combinations in order.
The idea being I need to then be able to ask for any particular combination of 6 characters such as BBCHHJ and use Array.Index to return the element of the array it is in.
I have the second bit fine:
String searchFor;
Console.Write("Enter a string value to search for: ");
searchFor = Console.ReadLine();
int indexValue = Array.IndexOf(letters, searchFor);
Console.WriteLine("The value you are after is in element index: " + indexValue);
Console.ReadLine();
But I have no idea how to easily initialise the letters array with all those combinations, in order!
A variation on Jakub's answer which should be a bit more efficient:
int result = s
.Select(c => c - 'A') // map 'A'-'Z' to 0-25
.Aggregate(0, (total, next) => total * 26 + next); // calculate the base 26 value
This has the advantage of avoiding the Reverse and the separate Sum, and the powers of 26 don't have to be calculated from scratch in each iteration.
Storing 308 million elements in array and searching them is not the best solution, rather calculate the index at runtime. I have created a code sample:
string input = "ZZZZZZ";
//default values
string alphabets_s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char[] alphabets = alphabets_s.ToCharArray();
int result = 1; //starting with "one" because zero will make everything zero
//calculating index
for (int i = 0; i < input.Length; i++)
{
//get character index and add "1" to avoid multiplication with "0"
int index = Array.IndexOf(alphabets, input[i]) + 1;
//multiply it with the current result
result *= index;
}
//subtract 1 from final result, because we started it with 1
result--;
PS: I did just basic testing, please inform me if you find anything wrong in it.
As I wrote in a comment, what you're trying to achieve is basically conversion from base 26 number.
The first step is to convert the string to a list of digits. Then just multiply by powers of 26 and add together:
var s = "AAAABB";
var result = s
.Select(c => c - 'A') //map characters to numbers: A -> 0, B -> 1 etc
.Reverse() //reverse the sequence to have the least significant digit first
.Select((d, i) => d * Math.Pow(26, i))
.Sum();
Panagiotis Kanavos introduced the following clever solution to produce LetterNumberNumber pattern in this SOF question: For loop when the values of the loop variable is a string of the pattern LetterNumberNumber?
var maxLetters=3; // Take 26 for all letters
var maxNumbers=3; // Take 99 for all the required numbers
var values=from char c in Enumerable.Range('A',maxLetters).Select(c=>(char)c)
from int i in Enumerable.Range(1,maxNumbers)
select String.Format("{0}{1:d2}",(char)c,i);
foreach(var value in values)
{
Console.WriteLine(value);
}
A01
A02
A03
B01
B02
B03
C01
C02
C03
D01
D02
D03
Is there way to instruct irregular course in Enumerable stuff? "Enumerable.Range(1, maxNumbers)" leads 01, 02, ...,99 (for maxNumbers 99).
Restriction Examples:
1. Restrict (01,02,...,99) only to (01,03,05,07,09,11,13)
2. Restrict (01,02,...,99) only to (02,04,06,08,10)
3. Restrict (01,02,...,99) only to (01,04,09,10)
What I did:
I worked "Enumarable", tried its methods like: Enumerable.Contains(1,3,5,7,9,13) gave big error, and I could not achieve to reach:
A01, A03, A05, ....,Z09, Z11, Z13.
If Enumarable is not suitable for this type of job, what do you offer to handle the problem?
This isn't a direct feature in C#, while it is in F#.
F# example:
[1..2..10]
will produce a list of [1,3,5,7,9].
You first example, "Restrict (01,02,...,99) only to (01,03,05,07,09,11,13)" can be achieved with
Enumerable.Range(1,99).Where(x => x % 2 == 1).Take(7);
The second example, "Restrict (01,02,...,99) only to (02,04,06,08,10)" can be achieved with
Enumerable.Range(1,99).Where(x => x % 2 == 0).Take(5);
And your third example, "Restrict (01,02,...,99) only to (01,04,09,10)" seems odd. I'm not sure what the pattern here is. If the last element isn't a typo, then starting at one and incrementing by 3, then 5, then 1 seems unclear, but here's a method that can accomplish it.
public static IEnumerable<int> GetOddMutation(int start, int max, int count, List<int> increments) {
int counter = 0;
int reset = increments.Count - 1;
int index = 0;
int incremented = start;
while(counter < count) {
var previous = incremented;
incremented += increments[index];
index = index == reset ? 0 : index + 1;
counter++;
if(previous != incremented) //Avoid duplicates if 0 is part of the incrementation strategy. Alternatively, call .Distinct() on the method.
yield return incremented;
}
}
called with
GetOddMutation(1,99,4, new List<int> {0,3,5,1})
will result in [1,4,9,10]
It sounds to me like you want the Enumerable.Range(1, maxNumbers) to be restricted by a certain condition rather than having all of the integers. Since Enumerable.Range() produces an IEnumerable<int>, you can chain any LINQ filtering method calls, namely the Enumerable.Where() method. For example, Enumerable.Range(1, 99).Where(x => x % 3 == 0) would yield (3,6,9,...99).
If you only wanted the specific case you specified where the list only contains (1,3,5,7,9,13), you could simply make a list with the desired numbers: new List<int> {1,3,5,7,9,13}; you could also use Enumerable.Range(1, maxNumbers).Where(x => x % 2 == 1) and maxNumbers = 13.
Use this, Rx generates a series of random numbers between 0 and 99.
var R = new Random();
var ints = Observable.Interval(TimeSpan.FromSeconds(1));
var RandomNos = ints.Select(i=> R.Next(100)); // was new Random().Next(100)
RandomNos.Subscribe(r=> Console.Write(r+ ","));
1,75,49,23,97,71,45,19,93,66,40,14,88,62,36,10,84
I want to capture/detect when I get 6 more-than-50 numbers in a row. Can Rx do it?
RandomNos.?????()
.Subscribe(l=> Console.WriteLine ("You got 6 more-than-50 numbers in a row"));
One way to do this is with the Buffer method.
var random = new Random();
var result = Observable.Interval(TimeSpan.FromSeconds(1))
.Select(i => random.Next(100))
.Buffer(6, 1)
.Where(buffer => buffer.All(n => n > 50))
If instead of 6-in-a-row you were trying to detect K-in-a-row, where K was really really huge, then you'd probably want to do something using Window instead, but since K = 6 here it's easiest to just do what I suggested.
Also, be aware that the probability of a number drawn uniformly from {0, 1, ..., 99} being greater than 50 is 49/100, not 1/2.
Just for fun, here's one that only uses a single counter for any "n" in a row - it keeps a running count of numbers over 50 - the Take(1) completes the stream at the first occurrence.
RandomNos.Scan(0, (a,x) => x > 50 ? ++a : 0)
.Where(x => x == 6)
.Take(1)
.Subscribe(_ => Console.WriteLine("You got 6 more-than-50 numbers in a row"));