|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On January 05 2013 13:26 Saracen wrote:Just started on ProjectEuler... Is it cheating to use Python? It feels like it's cheating, since Python has pretty much infinite integer precision afaik... For example, for number 16... + Show Spoiler +reduce(lambda x, y: int(x) + int(y), list(str(2 ** 1000))) ...seems just unfair when it seems like they're looking for a clever/elegant way to deal with large numbers. Almost all languages have support for large numbers. They are looking for elegant solutions (well, sometimes :p) but dealing with large numbers is not part of that. Rather, I'd say Python is a good choice because it makes you focus on the problems, instead of dealing with large numbers.
|
On January 05 2013 14:35 Shenghi wrote:Show nested quote +On January 05 2013 13:26 Saracen wrote:Just started on ProjectEuler... Is it cheating to use Python? It feels like it's cheating, since Python has pretty much infinite integer precision afaik... For example, for number 16... + Show Spoiler +reduce(lambda x, y: int(x) + int(y), list(str(2 ** 1000))) ...seems just unfair when it seems like they're looking for a clever/elegant way to deal with large numbers. Almost all languages have support for large numbers. They are looking for elegant solutions (well, sometimes :p) but dealing with large numbers is not part of that. Rather, I'd say Python is a good choice because it makes you focus on the problems, instead of dealing with large numbers. They've got to be looking for something other than what I've posted above, though. Problem 20 can be done the exact same way, and I'm pretty sure that's not what they want...
EDIT: Nevermind, that seems to be how everyone is doing it on the problem thread...
|
On January 05 2013 15:14 Saracen wrote:Show nested quote +On January 05 2013 14:35 Shenghi wrote:On January 05 2013 13:26 Saracen wrote:Just started on ProjectEuler... Is it cheating to use Python? It feels like it's cheating, since Python has pretty much infinite integer precision afaik... For example, for number 16... + Show Spoiler +reduce(lambda x, y: int(x) + int(y), list(str(2 ** 1000))) ...seems just unfair when it seems like they're looking for a clever/elegant way to deal with large numbers. Almost all languages have support for large numbers. They are looking for elegant solutions (well, sometimes :p) but dealing with large numbers is not part of that. Rather, I'd say Python is a good choice because it makes you focus on the problems, instead of dealing with large numbers. They've got to be looking for something other than what I've posted above, though. Problem 20 can be done the exact same way, and I'm pretty sure that's not what they want... EDIT: Nevermind, that seems to be how everyone is doing it on the problem thread...
I treated problem 16 as pretty much 'can you make your own way to deal with big numbers'.
Sure you can just use whatever large number support (BigIntger for Java~), but if you have the time (aka bored) might as well give it a shot and see if you can solve it without that stuff. I ended up using an Array of arbitrary size, though if I were to try again I'd probably see if it would be cleaner in an ArrayList.
Then for future problems that require large numbers copy/paste/tweak your original solution... or just use the support in the language for lazy o/
|
Using the cheesy bigInt solution kinda defeats the purpose of the problem though... since you can just multiply manually and store the 50~ digit number on string
|
Hi fellow TLers,
im starting to learn Java and have a specific problem to solve (interfaces and abstract classes / beginner stuff). Is any expert for Java out there who I could pm for a few questions? :D
Thanks in advance
Regards
|
Just ask in here, lots of the people who hang out in this thread are good Java programmers (though not me)
|
EDIT: AAAAAAAAAAAAAAAAAAAARGH
It was so simple
My inital commits used the format of My Name (SgtCoDFish) <my_email_address <at> domain <dot> com>
But apparently, you're not allowed to use an invalid email address. I changed to my_email_address@domain.com and it pushed first time
aaaaaaaaaaaaaaaaaaargh don't make the same mistake as me
EDIT: Typical, got an email from them just after I posted this. I'll edit again if I manage to get it fixed, but atm it looks like a problem on their end.
OK guys, I figure I might aswell ask for help here since I've sent a request to GitHub and so far got nothing, and the internet has drawn blanks. I've googled like hell and nothing I've found has worked/been relevant.
I've committed to GitHub from this comp before. Then I didn't commit anything for like 5 months, didn't really change anything about my dev environment/my git setup, and created a new project. I coded it almost entirely before I made the repo on GitHub (stupid, but I was motivated and didn't want to stop for anything). Now I made some local commits, got to version 1.0 and tried to push using Eclipse...
"Can't connect to any repository: <my repo> (<my repo's address>: error occurred during unpacking on the remote end: index-pack abnormal exit)"
I figured it was a bug with Eclipse, so I tried it on the command line. Same error: "index-pack abnormal exit"
I updated my eclipse to latest, and updated all git stuff on my machine (well, I reinstalled to latest version)
I've tried every fix that I've found online and nothing has changed. I've set sharedRepository = true, I've updated some openssl libs used by cygwin (doubted it would be this since the git package comes compiled on Windows, but I was desperate). I've made a new local repo and tried to push from that and got the same. I saw something about incorrect permissions, but that was server-side.
Everything seems to point to it being a problem on their end. I can only imagine that my local "pack" is corrupted and they're bugging out with it, or else it's a problem for them. If it's their problem, obviously you guys can't do anything, but is there anything I can perhaps do to get a more descriptive message/fix it? Anyone had anything similar?
|
Bisutopia19248 Posts
Can anyone recommend a place online that does a thorough job of explaining in C++ char pointers? I need a good review for upcoming interview questions. Thanks!
|
On January 06 2013 02:53 BisuDagger wrote: Can anyone recommend a place online that does a thorough job of explaining in C++ char pointers? I need a good review for upcoming interview questions. Thanks!
Sorry, but if you need a review of something that basic, you should fail those interview questions. I hope you didn't say that you have experience working with C++, or knowledge of C++, or something like that in your application, because that would be a lie.
Anyways, here's my explanation:
char *chararray = "hello"; //this is just a short way of writing the following code: //char *chararray = new char[6]; //chararray[0] = h; chararray[1] = e; same for l,l and o; chararray[5] = '\0'; (add escape sequence, so whoever uses this string knows where it ends //at this point, the value of chararray is just some random number. that number is the address of where the pointer points to. if that random number happens to be "42", then the first letter of the string, 'h', is located at memory location "42"
chararray[3] = 's'; //the entire string is now "helso"
char value = *chararray; //value = h; the little star is just a way to tell the compiler "don't give me the value of chararray, but instead interpret the value of chararray as a memory address, then look at what's actually at that memory address, and give it to me" value = *(chararray+2) //value = l, charrarray+2 is just an expression, the result of which yields a temporary value, which has the same type as chararray but points to two elements "behind" chararray
char *secondarray = chararray; //secondarray points to the same string as chararray chararray++; //now it's changed for real; the entire string is now "elso". notice that 'h' hasn't disappeared. it's still where it was before, you just don't use it anymore. value = *chararray; //value = e
//fortunately, we backed the old point up with secondarray, so we can still print hello cout<<secondarray<<endl; //prints hello and newline
|
Bisutopia19248 Posts
On January 06 2013 03:09 heishe wrote:Show nested quote +On January 06 2013 02:53 BisuDagger wrote: Can anyone recommend a place online that does a thorough job of explaining in C++ char pointers? I need a good review for upcoming interview questions. Thanks! Sorry, but if you need a review of something that basic, you should fail those interview questions. I hope you didn't say that you have experience working with C++, or knowledge of C++, or something like that in your application, because that would be a lie. Anyways, here's my explanation: + Show Spoiler + char *chararray = "hello"; //this is just a short way of writing the following code: //char *chararray = new char[6]; //chararray[0] = h; chararray[1] = e; same for l,l and o; chararray[5] = '0'; (add escape sequence, so whoever uses this string knows where it ends //at this point, the value of chararray is just some random number. that number is the address of where the pointer points to. if that random number happens to be "42", then the first letter of the string, 'h', is located at memory location "42"
chararray[3] = 's'; //the entire string is now "helso"
char value = *chararray; //value = h; the little star is just a way to tell the compiler "don't give me the value of chararray, but instead interpret the value of chararray as a memory address, then look at what's actually at that memory address, and give it to me" value = *(chararray+2) //value = l, charrarray+2 is just an expression, the result of which yields a temporary value, which has the same type as chararray but points to two elements "behind" chararray
char *secondarray = chararray; //secondarray points to the same string as chararray chararray++; //now it's changed for real; the entire string is now "elso". notice that 'h' hasn't disappeared. it's still where it was before, you just don't use it anymore. value = *chararray; //value = e
//fortunately, we backed the old point up with secondarray, so we can still print hello cout<<secondarray<<endl; //prints hello and newline
Thanks for the reply.
I've been working in TorqueScript for over two years with a simulations company. Scripting greatly depreciated my skills in C++ because I don't have to deal with pointers in torque. I guess I was looking for a more advanced guide then just basic assignment or memory access. I should have been way more clear just doing a lot of things right now. There are many questions that ask for output to the screen and its been a while since I've dealt with pointer manipulation in general. I guess maybe I just need a better heads up on what trick questions to look out for. Like I said, over 2 years is a long time and it makes you forget what type of questions companies are looking for.
For example finding the output of this code:
class A { public: A() : m_x(0) {} public: static ptrdiff_t member_offset(const A &a) { const char *p = reinterpret_cast<const char*>(&a); const char *q = reinterpret_cast<const char*>(&a.m_x);
return q - p; }
private: int m_x; };
class B :public A { public: B() : m_x('a') {} public: static int m_n; public: static ptrdiff_t member_offset(const B &b) { const char *p = reinterpret_cast<const char*>(&b); const char *q = reinterpret_cast<const char*>(&b.m_x);
return q - p; }
private: char m_x; };
int B::m_n = 1;
class C { public: C() : m_x(0) {} virtual ~C() {}
public: static ptrdiff_t member_offset(const C &c) { const char *p = reinterpret_cast<const char*>(&c); const char *q = reinterpret_cast<const char*>(&c.m_x);
return q - p; }
private: int m_x; };
int main() { A a; B b; C c; cout << ((A::member_offset(a) == 0) ? 0 : 1); cout << ((B::member_offset(b) == 0) ? 0 : 2); cout << ((A::member_offset(b) == 0) ? 0 : 3); cout << ((C::member_offset(c) == 0) ? 0 : 4); cout << endl;
system("pause"); return 0; }
|
On January 06 2013 03:09 heishe wrote:
Sorry, but if you need a review of something that basic, you should fail those interview questions. I hope you didn't say that you have experience working with C++, or knowledge of C++, or something like that in your application, because that would be a lie.
That's pretty harsh - he's looking for a review of something that shouldn't really be used in modern C++ because of how icky it is.
char *chararray = "hello"; //this is just a short way of writing the following code: //char *chararray = new char[6]; chararray[3] = 's'; //the entire string is now "helso"
For example, I think this is actually wrong.
IIRC "hello" is a string literal - a const char* stored statically, not in memory allocated with new. This means that trying to change a character like that is actually undefined behaviour...
|
On January 05 2013 23:31 AmericanUmlaut wrote: Just ask in here, lots of the people who hang out in this thread are good Java programmers (though not me)
Alright here goes The Task is this: I have this Code by default: + Show Spoiler + interface Conscience {
double value(int firings); }
interface Shareholder {
double value(int firings, double sharevalue); }
abstract class Employee {
double salary;
Employee(double salary) { this.salary = salary; }
@Override public abstract String toString();
public abstract int getIndex();
boolean striking(double salarycut, int firings, double sharevalue) {
}
}
The first task is to implement the "striking" method which calculates if an employee is on strike or not. The "cost" of an employee is a product of salary*salarycut.
If an employee is supporting the interface "Conscience" its cost decreases by the value calculated trough this method (more on that later)
If an employee is supporting the interface "Shareholder" its cost increases by the value calculated through its method (again more on that later)
Finally if the cost is < 40.0 the employee is on strike and isnt if its >= 40.0.
There are two types of employees: 1.workers and 2. bosses
The 2nd Task involves creating a class "worker" which implements the "Conscience" interface: -workers are constructed via salary and a "consciencesvalue" -getIndex() returns 0 for workers -toString() returns "worker" + the salary -The method value(int firings) from the Conscious interface returns the value of firings*consciencevalue (this is what lowers the "cost" value in the boolean method)
I implemented this like this:
+ Show Spoiler +public static class Worker extends Employee implements Conscience {
static double consciencevalue; static double value;
public Worker(double salary, double consciencevalue) { super(salary); this.consciencevalue = consciencevalue;
}
@Override public String toString() { String s = "Worker " + salary; return s; }
@Override public int getIndex() { return 0; }
@Override public double value(int firings) { value = firings * consciencevalue return value; }
}
Now my first problem is this. I cant seem to get
@Override public double value(int firings) { value = firings * consciencevalue return value; }
this part right. I tested it in main printing out Worker.value and it always returns 0.0.
The same problem occurs in the 3rd Task which asks me to implement the "Boss" class.
- getIndex() for bosses is 1 - toString() returns "Boss" + salary - Boss implements Conscience AND Shareholder - value(int firings) returns firings*0.2 for bosses - value(int firings, double sharevalue) returns firings*sharevalue
I implemented it like this:
+ Show Spoiler + public static class Boss extends Employee implements Conscience, Shareholder {
static double value; static double value2;
public Boss(double salary) { super(salary); }
@Override public String toString() { String s = "Boss " + salary; return s; }
@Override public int getIndex() { return 1; }
@Override public double value(int firings) { value = 0.2 * firings; return value; }
@Override public double value(int firings, double sharevalue) { value2 = firings * sharevalue; return value2; }
} Again I have the same problem, the double value(...) methods dont return anything. There is also a fourth task which I can only start to work on after i got this running :D Finally, how can i implement the boolean method so it calculates the costs with the right formula according to if its a "Boss" or a "Worker". As i said I'm a beginner hope you dont mind.
Thanks in advance.
Regards
|
Google about the keyword static & don't be stupid (following the suggestions of your ide without thinking about it).
|
On January 06 2013 03:39 netherh wrote:Show nested quote +On January 06 2013 03:09 heishe wrote:
Sorry, but if you need a review of something that basic, you should fail those interview questions. I hope you didn't say that you have experience working with C++, or knowledge of C++, or something like that in your application, because that would be a lie.
That's pretty harsh - he's looking for a review of something that shouldn't really be used in modern C++ because of how icky it is. Show nested quote + char *chararray = "hello"; //this is just a short way of writing the following code: //char *chararray = new char[6]; chararray[3] = 's'; //the entire string is now "helso"
For example, I think this is actually wrong. IIRC "hello" is a string literal - a const char* stored statically, not in memory allocated with new. This means that trying to change a character like that is actually undefined behaviour...
Well, it compiles like that. I'm not even sure if the standard says that changing the value of a constant (e.g. via const_cast) is undefined behavior. Either way, on all machines that you can get this code to compile on, it will run without problems or unexpected behavior, because statically stored things don't get stored any differently than stuff that is created on the heap. It's still just some address in memory space.
Of course if you were to create the array using new, the locations of the two arrays would be different: One would be "above the stack" where static stuff like that is usually allocated, and the other would be somewhere in heap memory. I just wanted to illustrate what "hello" does in terms of functionality.
|
I think CecilSunkre(?) has some exercises in a blog that actually makes you understand pointers. Too lazy to search but its there!
|
On January 06 2013 02:53 BisuDagger wrote: Can anyone recommend a place online that does a thorough job of explaining in C++ char pointers? I need a good review for upcoming interview questions. Thanks! How about you write a memory pool implementation? You can use char buffers to hold data and placement new things in the char buffers.
Here's a snippet I wrote a little while ago:
template <typename T> void FastList<T>::Node::Assign( const T& _data ) { new (data) T( _data ); // placement copy }
data is just a char array of the sizeof( T ).
Then you can also worry about memory alignment since a char alignment is of (usually) just one byte. I'd use a union and a double along with your char buffer for this.
|
On January 06 2013 04:19 flexgd wrote: ...snip...
As far as I'm aware, the problem is that you're using the static variable "value" inside the function "value". I would recommend using a different name for the method and the variable, because the "value" inside your class could either call the method or the variable called "value". This may be completely wrong because I'm not a Java programmer.
Also, I'm not sure if static is used properly here, or if you mean public.
|
|
On January 06 2013 17:12 cersdfsd wrote: I am too fat My boyfriend will left me. Because I am too fat. 70kg,165cm.So I want to buy a bicycle. Mountain bike or road bike ?Just for lose weight. Heard that this is well. What do you think? *links omitted*
Gotta watch out for dem boyfriends leaving you because you're too fat in the big programming thread??
This is golden.
|
yeah fat people need carbon fibre bikes to lose weight
|
|
|
|