|
Hi,
I'm trying to solve 100 doors Kata from provided link "http://www.tddbuddy.com/katas/100%20Doors.pdf[^]"
I've solved Open and Closed door status but stuck with Bonus part where I have to show Holding state as well. Can someone please help me to solve that. Below is the code for Open and Close door.
List<bool> listDoorStatus= new List<bool>();
for (int i = 1; i <= 100; i++)
{
for (int j = i; j <= numberOfDoors; j += i)
{
listDoorStatus[j - 1] = !listDoorStatus[j - 1];
}
}
StringBuilder sb = new StringBuilder();
foreach (var item in listDoorStatus)
{
if (item)
{
sb.Append("@");
}
else
{
sb.Append("#");
}
} return sb.toString()
|
|
|
|
|
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I am able to solve till two status of doors which are open and close. but in next step have to Add a third state which is holding (use H). By adding this state, you must toggle between open, holding and closed when visiting the doors.
I am just trying to learn this kind of problems which we generally found on Hacker Rank just like FizzBuzz.
So Till now what I solved:
If pass=1 for 10 doors then output will be @@@@@@@@@@
If Pass=2 for 10 doors then output will be @#@#@#@#@#
If Pass=3 for 10 doors then output will be @###@@@###
Where @ represents Open, and # represents close.
till this place I have solved it. But not when I've to show holding status as well. i am bit confused. I tried to create a loop on output and place H (represent Holding State) but it didn't work for me.
Like for Pass 2 output should be @H#@H#@H#@
|
|
|
|
|
So what have you tried?
It's obvious why you're having difficulty, but your code doesn't show any attempt to implement a "third state" at all.
And that implies "I haven't tried at all" - so you need to show us that you have. We aren;t here to do it all for you!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
How do you identify whether a door is open or closed, and what do you mean by "holding status"?
|
|
|
|
|
You started with 2 "states", you now have 3: open "@", closed "#", and holding "H".
Change your logic to handle 3 states, instead of just 2 (i.e. if item is a "bool", it should be a numeric or an "enum").
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I am trying to put together an application in Visual Studio using C sharp for the first time. I have a database on my SQL server but can not seem to connect using the string provided by Visual Studio nor the one below. Can anyone please help?
SqlConnection con = new SqlConnection(@"Data Source=\=DAVIES-SERVER\SQLEXPRESS;database=Artemis Data;Integrated Security=True");
Many thanks
|
|
|
|
|
Your connectionstring seems off.
Member 14812582 wrote: @"Data Source=\=DAVIES-SERVER\SQLEXPRESS;database=Artemis Data;Integrated Security=True"
"=DAVIES-SERVER" is hardly a computer-name. Drop the equals, and it may work. Use an IP if it doesn't.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
|
|
Use the Server Explorer under Tools | Connect to Database / Server and test your connection. All your attempts will fail if you're not available to your server.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
the goal: use interfaces to enable creating a collection of different types of objects: and, then, at run-time cast back to the generic type without hard-coding specific types.
a. without using 'dynamic
b. without using reflection
c. without cloning/copying (using something like 'Activator.CreateInstance)
note: you might think you could:
a. use 'Convert.ChangeType : no, it is designed for conversion to a variety of primitive types, requires implementing IConvertible, and it returns an object.
b. use a 'ComponentModel.TypeConverter implementation per class: returns an object; internal method overrides cannot have return type modified
background:
A common pattern in creating heterogenous collections of different Type objects is making the various objects inherit from a generic Interface that itself inherits from a non-generic interface: by casting the instances of the generic interface to the non-generic interface, you then can create a collection you can compile.
when, at run-time, you have an instance of the non-generic interface, you can, then, "upcast" to the generic interface to expose fields, properties, methods unique to the generic type ... but ...
to upcast you need to:
1. know the Type
2. or, use a switch statement in which you use explicitly coded possible type matches
if you have a variable of type 'Type, there is no way to use its run-time value to upcast the non-generic interface instance !
public interface IBeing
{
string BName { get; set; }
Type BType { get; set; }
}
public interface IBeing<T> : IBeing where T : class
{
T Value { get; set; }
}
public class Being<T> : IBeing where T : class
{
public Being(string name, T value)
{
Value = value;
BName = name;
}
public T Value { get; set; }
public string BName { get; set; }
public Type BType { get; set; }
}
public class Human : IBeing
{
public Human(string name)
{
BName = name;
}
public Type BType { get; set; }
public string BName { get; set; }
}
public class Dog : IBeing
{
public Dog(string name)
{
BName = name;
}
public Type BType { get; set; }
public string BName { get; set; }
}
public class BeingCache
{
public BeingCache()
{
Beings = new List<IBeing>();
}
public List<IBeing> Beings { get; }
public IBeing AddBeing<T>(string name, T value) where T : class
{
Being<T> newbeing = new Being<T>(name, value);
newbeing.BType = typeof(T);
Beings.Add(newbeing);
return newbeing;
}
}
BeingCache beingCache = new BeingCache();
Human h1 = new Human("h1");
Dog d1 = new Dog("d1");
IBeing being1 = beingCache.AddBeing<Human>("Jack", h1);
IBeing being2 = beingCache.AddBeing<Dog>("Rover", d1);
IBeing being1human = beingCache.Beings[0];
IBeing being2dog = beingCache.Beings[1]; At this point, to convert the interface instance back to its generic form: IBeing<Human> backtogeneric1 = (IBeing<Human>) being1Human; will work.
And, a typical pattern is to have a 'switch statement that hard codes the conversion for each interface instancee Type.
But, consider: hard-coded conversion requires design-time "world knowledge" of each Type.
What is desired is a run-time conversion method that uses the Type stored in the interface instance to get the generic interface instance with it "payload" of other data.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
modified 24-Apr-20 10:50am.
|
|
|
|
|
If I understand the goal correctly (which maybe I don't), then it is inherently impossible, because even if you arranged to get an element casted to the right type, there is no way to represent the result. Any single type (this excludes dynamic because it is not a "single type") that you choose for a method that performs that conversion would be the wrong type - the only things that works are "useless types" (non-generic IBeing and object ). If you already knew the resulting type then you could do it, but then we're back to requiring that knowledge to be built in statically which you didn't want.
|
|
|
|
|
Hi Harold,
I embed an instance of the generic Type in the newly created interface instance. At run-time, I can pull that reference out: given I know the current interface instance type, and the destination generic type ... you'd think you could "rehydrate" (convert) without reflection, use of reflection, 'dynamic, etc.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
The problem is that you don't know at compile-time what the run-time type of the instance will be.
As Harold said, there isn't a compile-time type you can use which would give you access to the properties or methods of the run-time type.
Consider:
public abstract class Being
{
public string Name { get; set; }
}
public class Dog : Being
{
public void Bark() { ... }
}
public class Human : Being
{
public void ComposeMusic() { ... }
}
List<Being> beings = LoadBeings();
Being aBeing = beings[0];
Type beingType = aBeing.GetType();
{ What could possibly go here? } myBeing = BeingAsType(aBeing, beingType);
myBeing.{ Which method could you call here? }();
Perhaps the visitor pattern might help you?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks, Richard, if you look at the code I posted, you will see that as the generic instance is created, the generic Type is injected into the instance.
public IBeing AddBeing<T>(string name, T value) where T : class
{
Being<T> newbeing = new Being<T>(name, value);
newbeing.BType = typeof(T);
Beings.Add(newbeing);
return newbeing;
} when the generic Type is added to the interface instance collection, it is "downcast" to the interface, but the Type variable is there, and accessible from the interface instance at run time.
Your code example has no relevance to the issue here.
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I disagree.
You have a variable with the compile-time type of IBeing , and the run-time type of IBeing<T> for some T . You can find the T at run-time, either by accessing a property on the instance, or by examining the run-time type of the instance.
What compile-time type could you possibly use for the variable to make it match the unknown run-time type? And what properties or methods could you possibly access on that variable that aren't declared on the base type?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
BillWoodruff wrote: But, consider: hard-coded conversion requires design-time "world knowledge" of each Type.
What is desired is a run-time conversion method t
I doubt that is what you want.
When you use the case statement, as you stated, the reason you do that is not because you want the specific type but rather because you want to do/access something that is specific to that type.
So given that you had the type then how are you going to do something specific with it if you don't code it in the first place?
I will note that I do know of one way to get what I suspect what you want but it still depends on what how would then subsequently use that type (the question above.)
|
|
|
|
|
|
Either:
1) You assumed - wrongly - this is FarceBok.
2) You are a spam tester.
I'm guessing the later...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I've worked a lot to try to get this working, but after several errors and trials, i've tried two things without error, but it doesn't work
here is my customer list load code
SQLiteParameter[] param = {};
customerList.Rows.Clear();
DataTable searchinfo = database.query_search("SELECT id, firstname, lastname FROM customers", param);
foreach (DataRow row in searchinfo.Rows)
{
customerList.Rows.Add(row[0].ToString(), row[1].ToString() + " " + row[2].ToString());
}
statusLabel.Text = searchinfo.Rows.Count.ToString() + " row(s) returned";
double clicking or clicking an 'edit' button successfully raises a child form
private void customerList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int index = e.RowIndex;
DataGridViewRow row = customerList.Rows[index];
String cid = row.Cells[0].Value.ToString();
editcustomer editdiag = new editcustomer();
editdiag.id = cid;
editdiag.ShowDialog();
}
I used a timer to filter results from a textbox that starts and stops based on textchanged
private void search_TextChanged(object sender, EventArgs e)
{
if(queryInterval.Enabled == false)
queryInterval.Start();
}
here is the 1000ms tick that stops itself after an idle match
public void queryInterval_Tick(object sender, EventArgs e)
{
if (lastQuery == search.Text)
{
queryInterval.Stop();
}
else
{
if (search.Text.Trim() != "" && search.Text.Length >= 3)
{
customerList.Rows.Clear();
SQLiteParameter[] param = {
new SQLiteParameter("@search", "%" + search.Text + "%")
};
DataTable searchinfo = database.query_search("SELECT id, firstname, lastname FROM customers WHERE firstname LIKE @search OR lastname LIKE @search", param);
foreach (DataRow row in searchinfo.Rows)
{
customerList.Rows.Add(row[0].ToString(), row[1].ToString() + " " + row[2].ToString());
}
statusLabel.Text = searchinfo.Rows.Count.ToString() + " row(s) returned";
}
else if (search.Text.Trim() == "")
{
SQLiteParameter[] param = { };
customerList.Rows.Clear();
DataTable searchinfo = database.query_search("SELECT id, firstname, lastname FROM customers", param);
foreach (DataRow row in searchinfo.Rows)
{
customerList.Rows.Add(row[0].ToString(), row[1].ToString() + " " + row[2].ToString());
}
statusLabel.Text = searchinfo.Rows.Count.ToString() + " row(s) returned";
}
else if (search.Text.Length < 3 && search.Text.Trim() != "")
{
}
lastQuery = search.Text;
}
}
i've tried directly running the load from the parent form on the child close
private void editcustomer_FormClosed(object sender, FormClosedEventArgs e)
{
customers custForm = new customers();
custForm.refresh(sender, e);
custForm.customers_Load(sender, e);
}
or blanked out the match text and started the timer
private void editcustomer_FormClosed(object sender, FormClosedEventArgs e)
{
customers custForm = new customers();
custForm.lastQuery = "";
custForm.queryInterval.Start();
}
I've done messageboxes in both the interval and the load and it does execute it, but the datagridview refuses to update. Is the parent form marked disabled till the form totally closes? Is there a better way to do this i'm not seeing?
yeah point is close the edit form and update the database and close the child form, have the parent update (name information updated, irrelevant if other data not seen is updated)
would iterating through the parent datagridview with the ID and upadting it manually work? I somehow doubt it if the other two is running, but not clearing the data and updating the new rows with the database data
thanks, sorry about the complexity of this
|
|
|
|
|
Your problem is to do with the way you display the child form: ShowDialog is a modal function: it does not return until the form it displays is closed. So the UI for Form1 is "busy" and doesn't get updated while the child form still exists.
Instead of ShowDialog, you can use Show - which allows both forms to continue working - but in any case you are going at this the wrong way:
1) Creating a new instance of the "parent" form and calling it's methods will not affect the existing one: it's a separate instance and does not share a DataGridView with any other. In order to directly update the parent, you would need to use the actual instance that opened the child, not a new one.
2) But the child form shouldn't even know the parent form exists, much less what type it is, or what methods it contains. Instead, the child should raise events that the parent handles and let teh parent do what it wants with the updates.
That probably sounds complicated, but it actually solves both your problems very neatly. Have a look here:
Transferring information between two forms, Part 2: Child to Parent[^]
It will show you how to do it properly.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
lol I actually ran across that page during my research. It sounds real complicated. a little blood just dropped from my nose
|
|
|
|
|
It's not difficult at all, not really - you've got the full code, and all it's doing is using the same event mechanism that everything else does, just you are creating and raising the event as well as handling it.
And creating an event is almost trivial, I do it so often I created a Visual Studio shortcut to to make it even simpler: A Simple Code Snippet to Add an Event[^]. Now I just type "evh" and press TAB and it fills out the event, just like "prop"+TAB creates a property.
Raising the event is just a case of calling a method!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I tried, and got errors, even downloaded the zip and it's pretty confusing. Keep in mind i've been doing c# for like, two weeks? Gotten fairly far using just google searches. Some specific things do elude me. Like your code. Still have a lot of specific stuff to figure out. like things like seeing {get; set;} at top and things like that I just don't understand. Mind you this program is going to be used by just me, nor will I be doing any business-level programming. I did some of that with PHP but never continued and never learned ajax/dhtml, which a lot of web sites require. My specialty is repair/support (which this program is for me to keep track of that)
aaaanyways, I found a solution, that I think I did put in my original post, but just using .show allowed my original form call to create a FormClosedEventHandler from the parent, which works. So now the child is doing nothing aware from the parent...sort of
|
|
|
|
|