Search found 51 matches

by greysquare
Tue Nov 16, 2010 1:45 am UTC
Forum: Coding
Topic: [Unnecessarily-] Compact coding tricks
Replies: 112
Views: 17569

Re: [Unnecessarily-] Compact coding tricks

A few weeks ago I compacted a full 16-bit RISC CPU emulator onto one 2748 character line of python (one statement, including two classes!): https://github.com/tomjnixon/Stumpy/raw/master/stumpy_line.py I *think* that all python programs can be reduced like this, using nested lambda (I think this has...
by greysquare
Mon May 10, 2010 6:40 pm UTC
Forum: Coding
Topic: Stupid python question...
Replies: 20
Views: 2723

Re: Stupid python question...

In this case, you can actually leave the brackets off, like: print '\n'.join('|%s|' % ' '.join(map(str, row)) for row in x) , which has the same effect as using ()s, ie. a generator. My personal preference is to use generators unless a list is actually required, for speed reasons, but it can be a bi...
by greysquare
Sat Jan 09, 2010 2:37 pm UTC
Forum: Coding
Topic: Python: PyWin32 problems
Replies: 6
Views: 973

Re: Python: PyWin32 problems

A '.' in a path means the current directory, which is the directory that the python program was run from, not the directory containing the script. This is actually really useful when passing file names to the program on the command line. The path to the script from the current directory can be found...
by greysquare
Wed Jan 06, 2010 4:01 pm UTC
Forum: Coding
Topic: Why are HDLs so extremely different from everything!?
Replies: 13
Views: 1781

Re: Why are HDLs so extremely different from everything!?

I'm not sure if this is right (i have little experience with verilog, and no way to test it), but i believe your verilog example could be shortened to something like: module OR2gate (input wire A, input wire B, output wire F); assign F = A | B; endmodule , which imho isn't too bad, assuming that it ...
by greysquare
Sun Nov 08, 2009 7:05 pm UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 10016
Views: 2034524

Re: Coding: Fleeting Thoughts

InkL0sed wrote:Is there something wrong with liking C strings?


No, as long as you like them in the same way that you like programming in assembly, or brainfuck.
by greysquare
Sun Nov 08, 2009 1:15 am UTC
Forum: Coding
Topic: Coding: Fleeting Thoughts
Replies: 10016
Views: 2034524

Re: Coding: Fleeting Thoughts

I often get a feeling that something is not right with my current approach, and then I stop making progress because I don't want to do things the wrong way. Which means I rarely finish my free time projects. If you don't already have your code in some kind of repository, it's a really good idea. If...
by greysquare
Fri Oct 30, 2009 6:32 pm UTC
Forum: Coding
Topic: After AP Compuer Science
Replies: 18
Views: 2934

Re: After AP Compuer Science

If you're after something that complements Java well, and makes programming more interesting and thought-provoking, you might want to check out Scala. It's a relatively new object-functional language, with the main selling point that it runs on the Java virtual machine, and can therefore access all ...
by greysquare
Wed Oct 07, 2009 8:45 pm UTC
Forum: Coding
Topic: What's a good first programming language, without install?
Replies: 32
Views: 3048

Re: What's a good first programming language, without install?

How about using a linux liveCD? If her laptop is crappy because it's running windows, then it'll solve that problem without ever touching the hard drive. Most liveCDs will at least have python and a text editor installed, which is all you really want. If you're really lucky, she might even prefer li...
by greysquare
Wed Jun 17, 2009 10:52 pm UTC
Forum: Coding
Topic: Shooting yourself in the foot: Part two
Replies: 50
Views: 5440

Re: Shooting yourself in the foot: Part two

regex:

Code: Select all

s/stump/leg/


It's easy when you use the right tools.
by greysquare
Wed Jun 03, 2009 12:44 am UTC
Forum: Coding
Topic: Python - return values?
Replies: 30
Views: 3669

Re: Python - return values?

imho, when coding in python it's best to think "Could someone who doesn't know python tell what this means?", which is why using the slice operator to reverse a list isn't a good idea - reversing a list isn't a very common operation, so you'd be better off just telling the reader what you'...
by greysquare
Tue Jun 02, 2009 5:14 pm UTC
Forum: Coding
Topic: Python - return values?
Replies: 30
Views: 3669

Re: Python - return values?

Another way, which isn't as pythonic, but can be seen in a lot of python code is: y = x[:] This uses the list slice operator, and while it is quick and easy, it's best avoided for new code. Also, rather than comparing two lists number by number, you would be best just using ==, it will always be fas...
by greysquare
Tue May 19, 2009 10:47 pm UTC
Forum: Coding
Topic: Personal Game Project in PyGlet
Replies: 17
Views: 7089

Re: Personal Game Project in PyGlet

I don't really know pyglet, but from what i understand, whenever the screen needs to be redrawn, the on_draw function is called, and all your drawing is done from this function. If you draw outside this function, it is not 'expecting' to be drawn on, and so it probably won't work. Also, looking at y...
by greysquare
Mon May 18, 2009 11:22 pm UTC
Forum: Coding
Topic: Personal Game Project in PyGlet
Replies: 17
Views: 7089

Re: Personal Game Project in PyGlet

Glad to hear you've fixed it. If you're going to try threads anywhere, it might as well be python, it really does make it super simple to use. To make your life easier, I would strongly recommend only drawing form the on_draw function, and just having the dialogue function queue the message up, thou...
by greysquare
Mon May 18, 2009 6:03 pm UTC
Forum: Coding
Topic: Personal Game Project in PyGlet
Replies: 17
Views: 7089

Re: Personal Game Project in PyGlet

If it's not thread safe, then that means that you shouldn't access it at the same time from two threads, but you should be okay if only one of your threads draws to the screen at once. You could draw from more than one thread, using a lock around all the drawing code, but I can't imagine any reason ...
by greysquare
Thu May 14, 2009 7:23 pm UTC
Forum: Hardware
Topic: DC Jack Polarity Question
Replies: 11
Views: 4635

Re: DC Jack Polarity Question

I know you've worked it out, but i wonder if you could spin it by hand, them measure the voltage produced on the jack. It probably wouldn't do the mechanism any good, but it might just work.
by greysquare
Thu May 14, 2009 5:24 pm UTC
Forum: Coding
Topic: Personal Game Project in PyGlet
Replies: 17
Views: 7089

Re: Personal Game Project in PyGlet

That depends on how the dialogue is being shown on the screen. One way might be: import time while dialogue_on_screen: time.sleep(0.01) which will just wait until the flag is false. edit: You could create a queue for everything, not just messages, though that depends on how you want your game to wor...
by greysquare
Tue May 12, 2009 5:42 pm UTC
Forum: Coding
Topic: Compiling a java file from a java program.
Replies: 7
Views: 863

Re: Compiling a java file from a java program.

Personally, I agree with vatar, doing things like this can make things go very wrong very fast, however if you must, why not use Janino, which compiles and executes java in the same virtual machine, without using external files. It looks easy to use, too.
by greysquare
Sun Apr 12, 2009 4:16 pm UTC
Forum: Coding
Topic: Contest #3
Replies: 97
Views: 8601

Re: Contest #3

Version two: #!/usr/bin/env python3.0 import subprocess as sp import re import urllib.request as ul import random eval(r"""pr%s%ss,f:(%sa,b:f(b,'+',1) if a==0%sf(f(a,'-',1),'?',1) if a>0 and b==0%sf(f(a,'-',1),'?',f(a,'?',f(b,'-',1))))(*map(int,s%s?'))))if '?' in s%s%sr: ("%%s r%...
by greysquare
Sat Apr 11, 2009 4:16 pm UTC
Forum: Coding
Topic: Contest #3
Replies: 97
Views: 8601

Re: Contest #3

<snip> ok I might have to break down and actually go download python 3 No need! I've converted back to python2. I think i've got everything. It turns out that section of code with the problem (map returning a list rather than an iterable) wasn't needed anyway. The code: #!/usr/bin/env python import...
by greysquare
Sat Apr 11, 2009 3:06 pm UTC
Forum: Coding
Topic: Contest #3
Replies: 97
Views: 8601

Re: Contest #3

I think i'm done with this: #!/usr/bin/env python3.0 import subprocess as sp import re import urllib.request as ul print((lambda s,f:((lambda a,b:f(b,'+',1) if a==0 else f(f(a,'-',1),'?',1) if a>0 and b==0 else f(f(a,'-',1),'?',f(a,'?',f(b,'-',1))))(*map(int,s.split('?'))))if '?' in s else (lambda r...
by greysquare
Sat Apr 11, 2009 12:53 am UTC
Forum: Coding
Topic: Contest #3
Replies: 97
Views: 8601

Re: Contest #3

print (lambda s:"%sr%s"%divmod(*map(int,s.split('/')) if '/' in s else 42 if '?' in s else eval(s.replace("^","**")))(raw_input()) handles division & the mystery operator. What is the difference between using raw_input and sys.stdin.readline? edit: changed quote to...
by greysquare
Fri Apr 10, 2009 9:49 pm UTC
Forum: Coding
Topic: Contest #3
Replies: 97
Views: 8601

Re: Contest #3

Hmm... 99999^99999 is a pretty big number... windows calculator just says 'invalid input for function', and google calculator doesn't even try. Handling all cases correctly might be a bit of a trick. Putting this into python takes a few minutes, but eventually yields an answer with 499995 digits. T...
by greysquare
Mon Apr 06, 2009 9:34 pm UTC
Forum: Coding
Topic: Import problem with Python
Replies: 24
Views: 2020

Re: Import problem with Python

Many things changed between python 2.x (the version your book was written for) and 3.0, most notably print statements. To make this work, you need to change print x into print(x) , and print "This program illustrates a chaotic function" into print("This program illustrates a chaotic f...
by greysquare
Mon Apr 06, 2009 11:09 am UTC
Forum: Coding
Topic: xkcd coding contest #2: NOPscotch
Replies: 87
Views: 6191

Re: xkcd coding contest #2: NOPscotch

Thanks for the prize, and for running a 100% awesome competition. Also, dean.menezes, that was quite sneaky.
by greysquare
Sun Apr 05, 2009 10:58 pm UTC
Forum: Coding
Topic: xkcd coding contest #2: NOPscotch
Replies: 87
Views: 6191

Re: xkcd coding contest #2: NOPscotch

Just thought I'd post some code. This generated a 700. NOPscotch.cpp #include "NOPscotch.h" struct _node { bool used; struct _node *next[8]; struct _node *next_noSort[8]; struct _node *right; uint inputs; uint val; uint x; uint y; float cost; }; typedef struct _node node; node* nodeGrid[he...
by greysquare
Fri Apr 03, 2009 8:06 pm UTC
Forum: Coding
Topic: Python compiler?
Replies: 29
Views: 2656

Re: Python compiler?

Actually, only imported modules are precompiled for next time. If you invoke a python script from the commandline of the interpreter, no .pyc file is created. Why does it do this? Could it be because you might want to store your main script in /usr/bin, where it probably dosen't have write permissi...
by greysquare
Fri Apr 03, 2009 4:31 pm UTC
Forum: Coding
Topic: Python compiler?
Replies: 29
Views: 2656

Re: Python compiler?

pythonw -c "import py_compile; py_compile.compile(r'<FILEPATH>')" replace <FILEPATH> with the full pathname for your file and run it in the command line... This makes the interpreter compile the file into bytecode to allow faster interpretation That does compile to bytecode, but dosen't m...
by greysquare
Fri Mar 27, 2009 9:02 pm UTC
Forum: Religious Wars
Topic: What Media Player (software) Do you use?
Replies: 74
Views: 12341

Re: What Media Player (software) Do you use?

I usually use Exaile for music, and GtkPod for transferring to my iPod, but over the past few days i've been trying mpd. It runs as a daemon, and you use a client to control it. I've been mostly using mpc, the command line client, just because it's kind of fun to use output redirection just to add s...
by greysquare
Sat Feb 28, 2009 11:58 pm UTC
Forum: Coding
Topic: Nested loops
Replies: 25
Views: 1774

Re: Nested loops

why not do: for(int i = 0; i < 100; i++) { for(int j = 0; j < 100; j++) { // Some code. if(condition_met) i=j=100; } } No extra variables, no gotos, and no raptor attacks. It doesn't work for all loops, but for ones like this it should be fine. Is this clearer than a flag? I'm not sure. edit: also, ...
by greysquare
Mon Feb 23, 2009 6:28 pm UTC
Forum: The Help Desk
Topic: New Notebook Not Starting Up
Replies: 3
Views: 509

Re: New Notebook Not Starting Up

Can you boot off a live-cd? If not then there's something wrong with the hardware. I'd take it back and try to get it replaced. If it's only two days old then there should be no trouble. Be glad it broke now, not in a year, just before you were about to do a backup (that ALWAYS happens :roll: ). Hav...
by greysquare
Mon Feb 23, 2009 4:27 pm UTC
Forum: Computer Science
Topic: Is there a fast way to find each elem thats in all of n sets
Replies: 19
Views: 2258

Re: Is there a fast way to find each elem thats in all of n sets

Well, O(mn) is the data size, so there should be no possible way you could go lower than that. Well I was thinking there was a way to not look at all the elements. For example if "A" is not in set #1, then you don't need to look for "A" in any of the other sets. You still need t...
by greysquare
Sun Feb 15, 2009 3:12 pm UTC
Forum: Religious Wars
Topic: VB
Replies: 87
Views: 12042

Re: VB

So basically, you're saying that it's possible to write bad code in C++, and therefore C++ is bad. I would argue that it's possible to write dodgy code in any language, and that that's the problem with who ever wrote the code, not the language. You don't have to use every feature, just because it's ...
by greysquare
Sat Feb 14, 2009 1:49 pm UTC
Forum: Hardware
Topic: Finding the perfect A/D converter
Replies: 38
Views: 3271

Re: Finding the perfect A/D converter

the ratio of Z 1 to Z 2 actually has to be 9 to 1 in order to get the scale factor right. Ouch. That hurt. I just had one of those "what the hell was i thinking" moments. Depending on what resistors you have access to, you might be best getting a potentiometer and tuning it yourself with ...
by greysquare
Fri Feb 13, 2009 3:20 pm UTC
Forum: Hardware
Topic: Finding the perfect A/D converter
Replies: 38
Views: 3271

Re: Finding the perfect A/D converter

If you still want to ude this A/D converter, then you could use a potential divider to get it into range. For the range you want, you can't use the first digit (it only shows 0 or 1), so the voltage needs dividing by 10 for degrees. For example: http://upload.wikimedia.org/wikipedia/en/f/f4/Impedanc...
by greysquare
Thu Feb 05, 2009 5:28 pm UTC
Forum: Coding
Topic: Looking for a portable IDE
Replies: 12
Views: 4640

Re: Looking for a portable IDE

For portable python development, i like http://www.portablepython.com/ mainly because it's actually free. It comes with the SciTE editor, which is passable. For c(++) you could use http://sourceforge.net/projects/devcpp-portable . I don't know whether it includes the compiler (usually MinGW) or not.
by greysquare
Fri Jan 30, 2009 5:51 pm UTC
Forum: Coding
Topic: ...wait, what? (malicious code at fannie mae)
Replies: 10
Views: 1752

Re: ...wait, what? (malicious code at fannie mae)

Maybe it was a form feed (redirects to page break). These can usually be added by pressing Control-L. Still sounds a bit dubious though.
by greysquare
Tue Jan 20, 2009 7:40 pm UTC
Forum: Hardware
Topic: Recommend a good magnetic card reader/writer.
Replies: 10
Views: 7442

Re: Recommend a good magnetic card reader/writer.

Thanks Shadowpawn, the link was correct, but i get a 404. Here's the page on archive.org: http://web.archive.org/web/20080120003050/http://www.sephail.net/articles/magstripe/ . There's no pictures, but it's pretty self explanatory. Hopefully this link will work for more than a day :roll: .
by greysquare
Mon Jan 19, 2009 5:55 pm UTC
Forum: Hardware
Topic: Recommend a good magnetic card reader/writer.
Replies: 10
Views: 7442

Re: Recommend a good magnetic card reader/writer.

I've heard of ordinary VCRs used to do this. It's the same mechanism; a magnetic stripe and a read/write head. I don't think a VCR would work, as they use a helical scan system, where the data is written in lines at an angle. Rather than trying to write to the card, just read from it. The data on y...
by greysquare
Mon Jan 19, 2009 4:08 pm UTC
Forum: Coding
Topic: Linux: Detect keypress in background
Replies: 5
Views: 2031

Re: Linux: Detect keypress in background

Could you not use the keyboard shortcuts that your window manager provides to run a script that clears this flag (in a file?) and then calls xset to turn the display back on? That way you don't have to fiddle with X stuff, and it's one less thing intercepting your keyboard input. EDIT: Oops, i thoug...
by greysquare
Sun Jan 18, 2009 6:17 pm UTC
Forum: Coding
Topic: Python and mySQL
Replies: 7
Views: 987

Re: Python and mySQL

It sounds like your problem is more with generic SQL than the MySQLdb module. A quick datatype reference: http://www.w3schools.com/sql/sql_datatypes.asp (scroll down to the mysql bit). Creating tables: http://www.w3schools.com/sql/sql_create_table.asp So, to use this in python: c.execute(""...

Go to advanced search