|
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 November 28 2016 02:16 travis wrote: What does this do:
int i = 0
for(int j = 0: j < 10; j++) { for(i++; i < 10; i++) { print hi } }
I guess you meant there to be "print i" not "print hi". It would simply print out 123456789.
|
naw the print statement was in there just so that there would be something inside the question was more about the looping I was actually trying to do a basic bubble sort, but I figured it out
outer loop needs to be:
for(int i=0; i < array length; i++)
and then inner loop is:
for(int j=1; j < (array length - i); j++)
|
On November 27 2016 18:51 Aerisky wrote:Show nested quote +On November 27 2016 03:44 Nesserev wrote:On November 27 2016 03:32 phar wrote: Anyways, how about that bike shed? First quality post in a while EDIT: Anyone got any recommended reads that really blew them away recently? Just read "Story of Your Life" by Ted Chiang (short story on which Arrival was based) which was really great. JK but I actually read https://learnpythonthehardway.org/book/nopython3.html recently, and found it pretty interesting since I generally like python 3. I was blown away as I really felt some of his concerns were over, uh, blown, and didn't realize it was that big of an issue (is python actually a dying language)? Not sure whence this rant came, tbh. What do you guys think? I don't feel like I'm experienced enough to have an informed opinion, but other than better error messages and what I've heard was rather bungled handling of the transition from python 2 to 3, I don't really feel like there's that much doom and gloom to be had regarding python 3. Maybe the guy has had lots of problems with string encoding in his own work, but based on my own personal experience, I have to disagree with the thrust of his argument, and most of his conclusions.
Firstly, I don't know enough about the Python VM/compiler internals to be able say how hard it would have been to include backwards-compatibility from the start, versus only supporting Python 3. As for the conversion argument, from my own experience, porting projects to Python 3 has been pretty easy, and most of the work is in auditing the code to take advantage of new Python 3 features.
Secondly, I don't think implicit conversions between byte sequences and Unicode strings are safe in general. This only works if you're operating entirely in ASCII, or if you didn't need a string in the first place. In Unicode, a byte != a code point, so turning your Unicode string into a sequence of bytes and then trying to print it is going to work, but will result in garbled messages. String and byte sequences are semantically different, and I think it makes sense to separate the two and force explicit conversions when needed.
I agree that having multiple string formatting choices as we do now is a smell, and goes against the "there should be one obvious way to do it" principle of Python.
Lastly, I would question how many people have to actually work with multiple encodings and converting between them on a regular basis. Most of my professional work with Python is with building/supporting a large distributed, automated, test framework, and I basically never had to touch strings. I also concurrently used Python 2 and 3, and found very little mental switching needed between them, besides missing some features from Python 3.
On another note, this sentence made me burst out laughing:
Now, I fear that everyone who currently codes Python 2 is simply going to move to a more stable language like Go, Rust, Clojure, or Elixir. I could maybe see some Python programmers moving to Go, but when his argument is that having a couple of static string formats is confusing enough to kill Python and drive beginners from the language, saying they'll go to one of these other languages is hilarious.
I mean, Rust doesn't even let you index into Strings, because they're always valid UTF-8, and the semantics of indexing are unclear: https://doc.rust-lang.org/std/string/struct.String.html . You have to convert the string using either as_bytes() or chars() to get iterators over the bytes or characters. There's also no implicit conversion the other way, from a u8[] to a std::String. This is just strings too, before you get into some of the more confusing to grok things like the borrow checker and lifetimes.
Never mind that these languages aren't even necessarily targeting the same design space as Python is either. It's like saying, "Now, I fear that everyone who currently codes Python 2 is simply going to move to a more stable language like Ruby, C, Haskell, or Erlang.". It's just listing languages, I don't see how there's some logical transition to any of these.
If I was a Python language contributor, who was worried about someone else coming and gobbling up my users, I'd probably be most scared of Go, or else Julia from the scientific side of things. The reasons to be scared also aren't related to having multiple static string types either.
|
|
28078 Posts
I like Zed Shaw and what he's done (I've read LPTHW for example), but sometimes you just have to ignore him
|
|
|
United Kingdom14103 Posts
am i good to ask why a bug is happening in my code here even though the program its in is part of my coursework? im not asking for a solution to my coursework, just cant work out why the bug is happening
|
On November 28 2016 08:11 Targe wrote: am i good to ask why a bug is happening in my code here even though the program its in is part of my coursework? im not asking for a solution to my coursework, just cant work out why the bug is happening
I don't think most people here really care (in my experience), so it's up to your personal integrity. I ask questions about my coursework sometimes, when I do it's generally because I think it isn't really cheating or that my school's requirements are being constricting to my growth.
|
United Kingdom14103 Posts
On November 28 2016 08:26 travis wrote:Show nested quote +On November 28 2016 08:11 Targe wrote: am i good to ask why a bug is happening in my code here even though the program its in is part of my coursework? im not asking for a solution to my coursework, just cant work out why the bug is happening I don't think most people here really care (in my experience), so it's up to your personal integrity. I ask questions about my coursework sometimes, when I do it's generally because I think it isn't really cheating or that my school's requirements are being constricting to my growth. great, thanks
considering the end result of the coursework is to produce a simple c compiler, one bug isnt the biggest detriment to me doing it all (i often search stackoverflow for the solutions to bugs)
spoilered the relevant code here (i think its enough?), language is C
basically the variables in two instances of mainClosure seem to be different when called from set_up_global() and main() even though im acquiring them the same way through get_func_binding() in both cases and im returning the TOKEN* used to call get_func_binding() in set_up_global() to main() in order to call get_func_binding() again
+ Show Spoiler + TOKEN* set_up_global(NODE* tree, FRAME* theFrame) { TOKEN* main; if (tree->left-> type == '~') { printf("got BRANCH\n"); set_up_global(tree->right, theFrame); set_up_global(tree->left, theFrame); } else if (tree->type == 'D') { printf("got DECLARATION\n"); //print_tree(tree->left->left);//return type //print_tree(tree->left->right->left);//function name TOKEN* nameToken = (TOKEN*)tree->left->right->left->left; char testString[5]; sprintf(testString, nameToken->lexeme); if (strcmp(testString, "main") == 0) { printf("MAIN FOUND\n"); main = nameToken; } printf("nameToken name is: "); printf(nameToken->lexeme); printf("\n"); //printf("name: %s\n", nameToken->lexeme); //print_tree(tree->left->right->right);//formals //print_tree(tree->right);//body add_function_binding(theFrame, nameToken, (NODE *)tree->left->right->right, (NODE *)tree->right); printf("making mainClosure\n"); struct closure mainClosure = get_func_binding(theFrame, main); print_tree(mainClosure.body); } else if (tree->type == '~') { printf("got VAR\n"); TOKEN* val = (TOKEN*)tree->right->right->left; printf("bindingtest=%d\n", val->value); int theVal = val->value; print_tree(tree->right->right);
if (tree->right->right->type == '+' || tree->right->right->type == '-' || tree->right->right->type == '*' || tree->right->right->type == '/') { printf("PLUS\n"); int l = interpret(tree->right->right, theFrame); printf("l=%d\n", l); theVal = l; }
add_binding(theFrame, (TOKEN*)tree->right->left->left, theVal); //print_tree(tree->right->right);//value //print_tree(tree->right->left);//var name }
return main; }
int main(int argc, char** argv) { if (argc>1 && strcmp(argv[1],"-d")==0) yydebug = 1; init_symbtable();
// head = (listnode*)malloc(sizeof(listnode)); // FRAME* main = (FRAME*)malloc(sizeof(FRAME)); //setup main scope // main->next = global; //link main and global frames // TOKEN* testtoken = lookup_token("z"); //testing to see if frames work // add_binding(main,testtoken,5); NODE* tree;
int r; printf("--C COMPILER\n"); yyparse(); tree = ans; printf("parse finished with %p\n", tree); print_tree(tree); printf("\n\n-----\n\n"); print_tree(tree->right->right);
FRAME* g = (FRAME*)malloc(sizeof(FRAME)); TOKEN* mainToken = set_up_global(tree ,g); //printf(mainToken->lexeme); struct closure mainClosure = get_func_binding(g, mainToken); printf("PRINTING MAIN\n"); //print_tree(mainClosure.formals); //print_tree(mainClosure.body); //r = interpret(,g); // printf("%d\n",r); //compile(tree->right); return 0; }
EDIT: i think i found why its not working? its due to the fact the function recursively calls itself so the TOKEN* main that gets returned might not necessarily have been assigned a value?
|
Hyrule18968 Posts
The homework rule is to avoid situations like
I have this homework problem:
>homework problem<
please give me code for it
If you've already got an answer, or if you want direction in how to approach the homework/quiz/test/schoolwork/whatever, that's different than just asking for the answer to a problem. We don't mind helping people find how to solve a problem, or even giving loads of examples on how problems can be approached. Don't be scared
|
United Kingdom14103 Posts
guess i got worried after seeing stuff like "homework thread" in blog comments lol
i dont think its even possible for me to ask questions like the example you gave at this stage in my degree tbh, all my coursework now is a bit beyond simple solutions
|
@Nesserev and Mr. Wiggles whoa, thanks for the really detailed responses. I agree, yeah. The late start of Python 3 was definitely in part the fault of the maintainers/designers to an extent, but it overall seems to be headed in the right direction. I also got the impression that Zed has some beef based on some conversations with the Python team that didn't go very well w.r.t. backwards compatibility and the other points mentioned.
|
On November 28 2016 08:33 Targe wrote:Show nested quote +On November 28 2016 08:26 travis wrote:On November 28 2016 08:11 Targe wrote: am i good to ask why a bug is happening in my code here even though the program its in is part of my coursework? im not asking for a solution to my coursework, just cant work out why the bug is happening I don't think most people here really care (in my experience), so it's up to your personal integrity. I ask questions about my coursework sometimes, when I do it's generally because I think it isn't really cheating or that my school's requirements are being constricting to my growth. great, thanks considering the end result of the coursework is to produce a simple c compiler, one bug isnt the biggest detriment to me doing it all (i often search stackoverflow for the solutions to bugs) spoilered the relevant code here (i think its enough?), language is C basically the variables in two instances of mainClosure seem to be different when called from set_up_global() and main() even though im acquiring them the same way through get_func_binding() in both cases and im returning the TOKEN* used to call get_func_binding() in set_up_global() to main() in order to call get_func_binding() again + Show Spoiler + TOKEN* set_up_global(NODE* tree, FRAME* theFrame) { TOKEN* main; if (tree->left-> type == '~') { printf("got BRANCH\n"); set_up_global(tree->right, theFrame); set_up_global(tree->left, theFrame); } else if (tree->type == 'D') { printf("got DECLARATION\n"); //print_tree(tree->left->left);//return type //print_tree(tree->left->right->left);//function name TOKEN* nameToken = (TOKEN*)tree->left->right->left->left; char testString[5]; sprintf(testString, nameToken->lexeme); if (strcmp(testString, "main") == 0) { printf("MAIN FOUND\n"); main = nameToken; } printf("nameToken name is: "); printf(nameToken->lexeme); printf("\n"); //printf("name: %s\n", nameToken->lexeme); //print_tree(tree->left->right->right);//formals //print_tree(tree->right);//body add_function_binding(theFrame, nameToken, (NODE *)tree->left->right->right, (NODE *)tree->right); printf("making mainClosure\n"); struct closure mainClosure = get_func_binding(theFrame, main); print_tree(mainClosure.body); } else if (tree->type == '~') { printf("got VAR\n"); TOKEN* val = (TOKEN*)tree->right->right->left; printf("bindingtest=%d\n", val->value); int theVal = val->value; print_tree(tree->right->right);
if (tree->right->right->type == '+' || tree->right->right->type == '-' || tree->right->right->type == '*' || tree->right->right->type == '/') { printf("PLUS\n"); int l = interpret(tree->right->right, theFrame); printf("l=%d\n", l); theVal = l; }
add_binding(theFrame, (TOKEN*)tree->right->left->left, theVal); //print_tree(tree->right->right);//value //print_tree(tree->right->left);//var name }
return main; }
int main(int argc, char** argv) { if (argc>1 && strcmp(argv[1],"-d")==0) yydebug = 1; init_symbtable();
// head = (listnode*)malloc(sizeof(listnode)); // FRAME* main = (FRAME*)malloc(sizeof(FRAME)); //setup main scope // main->next = global; //link main and global frames // TOKEN* testtoken = lookup_token("z"); //testing to see if frames work // add_binding(main,testtoken,5); NODE* tree;
int r; printf("--C COMPILER\n"); yyparse(); tree = ans; printf("parse finished with %p\n", tree); print_tree(tree); printf("\n\n-----\n\n"); print_tree(tree->right->right);
FRAME* g = (FRAME*)malloc(sizeof(FRAME)); TOKEN* mainToken = set_up_global(tree ,g); //printf(mainToken->lexeme); struct closure mainClosure = get_func_binding(g, mainToken); printf("PRINTING MAIN\n"); //print_tree(mainClosure.formals); //print_tree(mainClosure.body); //r = interpret(,g); // printf("%d\n",r); //compile(tree->right); return 0; }
EDIT: i think i found why its not working? its due to the fact the function recursively calls itself so the TOKEN* main that gets returned might not necessarily have been assigned a value?
I'm not sure exactly if this is your problem, but g isn't initialised before it's passed to set_up_global. Also, in set_up_global, anything that changes the pointer theFrame is only going to act locally, and not change the object pointed to by g. I'm not sure what your problem is, but I would recommend initialising g.
|
I think that the first thing you should work on is structuring your data properly:
tree->left->right->left->left
Holy hell...
Also, may I ask why do you want to introduce closures in C? C is not a functional language and trying to make it so isn't going to help you.
Edit: I assume you have seen this already?
|
ungraded lab exercise at my school we are doing threading
the idea in this excersize is to replace all the values in a 2 dimensional array with the square root of each value
but in this class, we are doing it by making height number threads, and each thread will square root every width value
so, I made ProcessRow, which has the runnable for my thread behavior
but what do I put into my main method and "//do stuff here"? The directions say I need to create an "array of threads". I know I need to do something like.. new thread. which I guess I will put in every index. and then I need to go through the indexes and do that index.start. Also I then need to "join" the threads before it gets to the timer.
I know I am basically asking someone to do this part of the work for me, but yeah, it's a bit confusing.
+ Show Spoiler + public class MathWithMultipleThreads {
private static Random random = new Random(); private static double[][] makeArray(int height, int width) { double[][] answer = new double[height][width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { answer[row][col] = random.nextDouble(); } } return answer; } public static void main(String[] args) throws InterruptedException { double[][] array = makeArray(1000, 1000); long start = System.nanoTime(); //do stuff here long end = System.nanoTime(); System.out.println((end - start)/1000000000.0); } class ProcessRow implements Runnable { double[] arrayRow; public ProcessRow(double[] rows) { arrayRow = rows; }
@Override public void run() { for (int col = 0; col < arrayRow.length; col++){ arrayRow[col] = Math.sqrt(arrayRow[col]); } }
} }
|
Hyrule18968 Posts
On November 28 2016 09:31 Targe wrote: guess i got worried after seeing stuff like "homework thread" in blog comments lol
i dont think its even possible for me to ask questions like the example you gave at this stage in my degree tbh, all my coursework now is a bit beyond simple solutions Just follow the thread for a few days and you'll see how it works. It's a lot of Travis along for homework help :p
|
Was all that code provided? I feel like the Thread should be setup in a different way, but it may just make more sense in my mind that way.
|
On November 28 2016 12:20 tofucake wrote:Show nested quote +On November 28 2016 09:31 Targe wrote: guess i got worried after seeing stuff like "homework thread" in blog comments lol
i dont think its even possible for me to ask questions like the example you gave at this stage in my degree tbh, all my coursework now is a bit beyond simple solutions Just follow the thread for a few days and you'll see how it works. It's a lot of Travis along for homework help :p
I like to think of it as I am a driving force of the thread.
On November 28 2016 12:26 Blitzkrieg0 wrote: Was all that code provided? I feel like the Thread should be setup in a different way, but it may just make more sense in my mind that way.
This is what was provided
+ Show Spoiler + public class MathWithMultipleThreads {
private static Random random = new Random(); private static double[][] makeArray(int height, int width) { double[][] answer = new double[height][width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { answer[row][col] = random.nextDouble(); } } return answer; } public static void main(String[] args) throws InterruptedException { double[][] array = makeArray(1000, 1000); long start = System.nanoTime(); //do stuff here long end = System.nanoTime(); System.out.println((end - start)/1000000000.0); } }
but they also basically told us to create an inner class that implements runnable
|
so what you wrote doesn't actually access 'array' that was created so you're doing the square root on different data.
|
|
|
|