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
I am trying to make a program to randomize entries for an event. I have the program working well enough for entering the entries but I'm stuck at randomizing it.
I have 2 lists, let's call one Head and the other one Heel. I have the lists as follows:
Head: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Heel: [1a, 2a, 3a, 4a, 5a, 6a, 7a, 8a, 9a, 10a]
I want each item in the Head to have 2 random partners from Heel, but no value from Heel can be matched more than twice at the end of it all. In the final output, each entry should be listed twice and only twice.
Hopefully someone understands and is able to help me, thanks.
I feel like I need to go take a long bath, I feel so dirty for having this piece of code see the light of day, but something about OP's logic was mezmerizing. Anyway, 3 AM me thinks this should work:
var head = new List<char>("abcdef");
var heel = new List<char>("123456");
heel = heel.Concat(heel);
var randomer = new Random();
foreach (var knownItem in head)
{
var idx1 = randomer.Next(heel.Count);
var pair1 = heel[idx1];
heel.RemoveAt(idx1);
char pair2='\0';
while (true)
{
var idx2 = randomer.Next(heel.Count);
pair2 = heel[idx2];
if (pair2 != pair1)
{
heel.RemoveAt(idx2);
break;
}
}
//DoTheDew
}
Next steps for tomorrow: dieharder test the results of this version vs #Arj
Here's a possible solution. Since there's no code to start with, I've done it in pseudocode for now.
Create two Lists, each having all of the values in heel; call them heel1 and heel2
For each element i in head:
Generate a random number j where 0 <= j < heel1.size. Remove the element at heel1[j] - that's your first pairing
Repeat with heel2 (for a different generated j). Remove that element at heel2[j] - that's the second pairing
Store i along with the two removed values
By the end you will have two numbers for each value in head, with no value from heel appearing more than twice. Since we are removing any used values each time, we don't need to check for your "more than two pairings" rule.
Related
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 months ago.
Improve this question
What is the differences between ways of creating ImmutableList?
List<int> numbers = new List<int>(){0, 1, 2, 3};
ImmutableList<int> immutableList = numbers.ToImmutableList();
ImmutableList<int> immutableList = ImmutableList.Create<int>(0, 1, 2, 3);
ImmutableList<int> immutableList = ImmutableList.CreateRange<int>(new List<int>() { 0, 1, 2, 3 });
ImmutableList<int>.Builder builder = ImmutableList.CreateBuilder<int>();
builder.AddRange(new List<int>() { 0, 1, 2, 3 });
ImmutableList<int> immutableList = builder.ToImmutableList();
Which is the faster and usabel?
Let's take a look at how each of those is implemented.
First off, the ToImmutableList() extension method involves first trying to cast to ImmutableList<T>, and then falling back to ImmutableList<T>.Empty.AddRange() otherwise. ImmutableList.Create() and ImmutableList.CreateRange() also call ImmutableList<T>.Empty.AddRange().
Finally, the builder is the only one that has some key difference, although you're using it wrong: you should be using ToImmutable(), because it involves doing fewer unecessary copies. ToImmutableList() just uses the above extension method, and therefore also uses ImmutableList<T>.Empty.AddRange().
Really, if you're building a possibly large immutable list as you go, you should use a builder, and then freeze it with ToImmutable() when you're done modifying it. Otherwise, all the other methods are functionally identical, so pick whichever is clearest to you.
This question already has answers here:
How to find all combinations of coins when given some dollar value [closed]
(37 answers)
Closed 2 years ago.
I have a task to write a function(n) that finds all the sequences that consists of only 1, 3, 4 (not sure how to explain it in english but i have example below)
For example if n is equal to five then i should print out
5 = 1+1+1+1+1
5 = 1+1+3
5 = 1+3+1
5 = 3+1+1
5 = 1+4
5 = 4+1
I'm not sure how should i go about writing it.
Just finding the right terminology might be an answer:
"You are trying to get all the permutations of the values 1, 3 and 4, that sum up to 5."
Unlike combinations, with permutation the order maters. Wich is why {1,4} and {4,1} are both seperate, valid answers. Same with the {1,1,3} variations. I would say this is a hard recursive problem. I am a bit a a loss for the function signature right now, as the reference nature of arras could become a big issue.
just finding all the combinations and then creating all the permutations of each set could work.
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 4 years ago.
Improve this question
Sorry if this isn't appropriate, but I'm really try to work on my algorithm skills.
static public ListNode RemoveNthNodeFromEndOf(ListNode head, int n)
{
if (head == null || n == 0) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode p = dummy , q = dummy.next;
int i = 0;
while (q.next != null)
{
if (i >= n - 1) p = p.next;
q = q.next;
i++;
}
p.next = p.next.next;
return dummy.next;
}
I completely understand what they did here, I just don't understand how you get to that answer and I would honestly like to figure out that skill. Basically, how do you approach this and think, i >= n-1 then p should start to follow q. Steps to the thought to get there I guess? Not
You want a pointer n from the end. But you can't move backwards, and you can't see forwards. What to do?
Suppose you're in a dark corridor and someone tells you the light switch is three meters from the end. It's too dark to see three meters, so you pick up the proverbial ten-foot pole, hold it pointing forward, and walk down the corridor until the pole touches the end.
Here, we don't have a pole handy, but we can build one by making a second pointer and stepping it n times. Now, we have two pointers n steps apart and we can advance both in parallel until the advance pointer hits the wallend of the list. Then the other pointer is n steps from the end.
How to come up with these solutions? Both come about from the same thought process: "I could do this if only I had something walking ahead of me." I guess practice helps. Really wanting to solve the problem helps, as does believing the problem has a solution. Add a bit of creativity and maybe a bit of seeing the world as you would like it to be. In short, the same way you solve any problem.
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 9 years ago.
Improve this question
My teacher gave me today this mission, to built a code block in c# that input data to a two-dimensional array, in only one loop.
What is the easiest way to do so? I tried some thing with While and they didn't work at all.
You can create a counter that is the length of the first array, a counter for the length of the second array. Then increment each as you seed the arrays appropriately. I'm not sure you want actual code since this is an assignment.
You can use a while loop that checks for the counters to be a certain length to know when the arrays are finished being loaded with data.
I suspect your teacher wants to learn about modulo division operator (%). If your two dimensions are of sizes X & Y respectively, then you have a total of X*Y items in your 2D array. So you can always translate item's count into it's position in 2D array. E.g. (in pseudo code):
for(int i = 0; i < X*Y; ++i)
{
myArray[i%x, i/x] = i;
}
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 9 years ago.
Improve this question
First a bit of background information. I am playing an online campaign of D&D and am playing a druid. I already build an animal-tracker, for all my summoned animals, but now I want to speed things up a bit more by building a character tracker, for my own character, Duncan, as well as for my Mighty Eddy (dire-wolf animal companion).
The thing I am now working on, is keeping track of my spells. In D&D spells and spell levels are based on the level of the character and bonus stats.
For Druid: http://www.dandwiki.com/wiki/SRD:Druid
For Bonusses: http://www.dandwiki.com/wiki/SRD:Ability_Scores
Scroll down for Ability modifiers, so the more wisdom, the more spells.
Now, I am thinking of making tab-controlled pages, for spells ranging from level0 to level9 and then displaying ComboBoxes to select the spell(s) you want to prepare.
Innitially, I was planning on hiding the vast majority of comboboxes and only unhiding them with simple If statement, so if wisdom is high enough, unhide x amount of combo boxes....but that will mean creating loads of IF statements.
Is there a way to say, IF Wisdom is high enough for 10 spells, display 10 combo boxes, if wisdom is high enough for only 5, display only 5?
Or does anyone else have a good alternative idea on how to do this? I am open for suggestions.
NOTE: since you didn't specify a language or a platform I'm going to use C# and Windows Forms.
Sure, I'm not sure how you determine the wisdom level, but let's say it's stored in a variable named _wisdomLevel:
private int _wisdomLevel;
now you just need a Dictionary to handle that:
private Dictionary<int, int> _wisdomLevelSpells = new Dictionary<int, int>
{
{ 1, 5 },
{ 2, 5 },
{ 3, 10 },
}
Now, the values I put in there are random, they are so you can get the idea. Now to display those combo boxes I might do something like this:
for (int i = 0; i < _wisdomLevelSpells[_wisdomLevel]; i++)
{
this.Controls.Add(new ComboBox()
{
DataSource = ...,
ValueMember = ...,
DisplayMember = ...,
}
}