math4tots

1

votes
1

answer
374

views

How do I find the Subsets of given Set in Sage?

I have code that looks like this: def Z(m,n): return CartesianProduct(IntegerRange(m),IntegerRange(n)) for v in Subsets(Z(2,2)): print v However, when I try to run it, I get the following error: Traceback (most recent call last): File 'x.py', line 13, in for v in Subsets(Z(_sage_const_2 ,_sage_cons...
math4tots
1

votes
1

answer
1.2k

views

Writing a notification program using Tweepy

In trying to learn the Python-tweepy api, I put together a little bit of code that tells me if there has been any new tweets within the most recent ~10 second interval (of course, in practice it is more than 10 seconds because code aside from sleep(10) also takes some time to run): from getApi impor...
math4tots
1

votes
1

answer
326

views

Windows batch script stops working after a few iterations

I have a windows batch script that just calls vcavars32 for a C compiler, deduces the name of my C program based on the working directory name, saves some content from the user from the command-line, then compiles and runs my program from the commandline (if you know what USACO is, I was hoping this...
math4tots
1

votes
2

answer
147

views

How is Callback handling implemented?

In the past, I have used libraries that would allow me to register a callback so that the library can call my method when some event happens (e.g. it is common to see in code that use GUI libraries to look like button.onClick(clickHandler)). Naively, I suppose the library's handling mechanism could...
math4tots
1

votes
1

answer
669

views

Java persistence with java.util.prefs.Preferences

It seems as though I can save some user preferences using java.util.prefs.Preferences. The preferences are tied to the specific classes -- I get a Preferences instance by passing a class Object to some static method, e.g. Preferences.userNodeForPackage. However, what happens if I delete my class and...
math4tots
1

votes
1

answer
1.3k

views

Binding Ctrl (-) in Python Tkinter

I have an application with a Text widget, and I want my font to get smaller when the user presses the Control key and the minus key (-). the code self.bind_all('', self.increaseFont) seems to work fine for getting the font to increase when the user presses the control key and the equals key, but the...
math4tots
1

votes
1

answer
1.9k

views

HTML Table with thousands of rows

I have a server that will supply a list of objects in json, and I would like to display them in a table on the client side. My initial instinct was to edit the DOM after I got the results from the server. Create the entire table DOM element from the json, and then call replaceChild on the element th...
math4tots
1

votes
1

answer
82

views

Efficiently manage handle for an array binary heap in C++?

Is there a way to efficiently keep track of handles in an array binary heap? Since there's no fast lookups built into traditional binary heaps, users need a 'handle_type' for delete or decrease_key on an arbitrary element in the heap. You can't use a pointer or an index into the array, because heap...
math4tots
1

votes
6

answer
17.8k

views

What might cause an IOException in Java when I make a call to Reader.read()?

I have code that looks something like: public static void func(Reader r){ int c = r.read(); ... } and the compiler tells me that r.read() might throw an IOException. Under what circumstances might this happen? It seems clear that something like FileNotFoundException is thrown when a file is not foun...
math4tots
1

votes
1

answer
1k

views

How do I label expression alternatives with same precedence level?

With antlr4 I can label rule alternatives like this: e : e '*' e # Mult | e '+' e # Add | INT # Int ; From what I understand, in the rule above, Mult has higher precedence over Add because Mult comes before Add in the list of alternatives. So for instance, if I wrote: e : e '*' e # Mult | e ('...
math4tots
6

votes
1

answer
123

views

Template class method does not cause error — is this part of the standard?

When I compile the following with g++ --std=c++98 -Wall -Werror -Wpedantic Test.cc, there's no error. template struct TemplateClass { T *ptr; TemplateClass(T *p): ptr(p) {} int foo() { return ptr->bar(); } }; struct ExampleClass { }; int main() { TemplateClass x(new ExampleClass()); } I expected th...
math4tots
4

votes
2

answer
358

views

O(1) extra space lookup data structure

I was wondering if there was a simple data structure that supports amortized log(n) lookup and insertion like a self balancing binary search tree but with constant memory overhead. (I don't really care about deleting elements). One idea I had was to store everything in one contiguous block of memory...
math4tots
5

votes
2

answer
1.6k

views

Is there an unsynchronized java.util.Stack?

Is there an unsynchronized version of java.util.Stack? ArrayList almost has everything I want, except there doesn't seem to be a method that removes last element in amortized constant time. I want something that can act like a stack, but still let me have constant random access to the elements. If t...
math4tots
5

votes
2

answer
601

views

How are golang select statements implemented?

In particular, I have some blocking queues in C++, and I want to wait until any one of them has some item I can pop. The only mechanism I can think of is to spawn a separate thread for each queue that pops from its input queue and feeds into a master queue that the original thread can wait on. It se...
math4tots
2

votes
2

answer
170

views

How does gcc know where the source is coming from?

I was playing with flex and bison today, and something kind of eerie came to my attention. localhost:c math4tots$ lex c.l localhost:c math4tots$ yacc -d c.y localhost:c math4tots$ rm c.l c.y localhost:c math4tots$ gcc c.c lex.yy.c y.tab.c c.y: In function ‘opr’: c.y:120: error: ‘nodeType’ ha...
math4tots
2

votes
3

answer
1.8k

views

Getting the best fit Instance Method in Java

If I run the following program: class Runit{ public static void main(String[] argsWut) throws Exception { String arg = 'what?'; Class[] parameters = { new Object().getClass() }; Object[] args = { arg }; System.out.println(''.getClass().getMethod('equals',parameters).invoke('what?',args)); } }; I get...
math4tots
5

votes
2

answer
10.2k

views

Visual Studio “illegal use of this type as an expression”

I've just started using Visual Studio (I got VS 2012 from dreamspark, and it hasn't been long since I've started using Windows again) and I'm having some trouble. I have a single file named 'main.c' under my Source Files folder that looks like this: #include typedef struct S_s S; struct S_s { void*...
math4tots
5

votes
2

answer
2.4k

views

Limiting scope of Python import

I have some code that looks like this: from pyparsing import Word, alphas, Optional, ... # Do stuff ... # And at the end, save a result to the outside world. parser = ... # Now use parser but don't use anything else from pyparsing again. I like having the convenience of calling from import , but I...
math4tots
2

votes
3

answer
966

views

parser generator that generates stand-alone C++ code

Is there a LALR parser generator that produces stand-alone C++ code? I am hoping that it would generate two files named something like 'Parser.cpp' and 'Parser.hpp,' and the generated parser is implemented in a single class (that I can wrap in whatever namespace) that I can use for my parsing needs....
math4tots
2

votes
1

answer
8.5k

views

Setting the security policy file in Eclipse

From what I understand from searching around on the internet, if I want to run a java program with a security policy, I would have to type in something like java -Djava.security.policy=java.policy MainClass Where java.policy is the file describing the security policy. How do I do something like tha...
math4tots
5

votes
1

answer
710

views

C++ template template parameter with default parameters

I have a class that needs to use some sort of map. By default, I want to use std::map, but I also want to give the user the ability to use something different if they want (e.g. std::unordered_map or maybe even a user created one). So I have code that looks like #include template class MyClass { };...
math4tots
4

votes
1

answer
4.7k

views

Running Java Security Manager without the default java.policy file

I don't want to modify anything in my java home directory, however, I am afraid that sometimes my default java.policy file may be too permissive. Is there a way for me to use a specified policy file as the only policy file when I run java with the -Djava.security.manager option? If I add a -Djava.se...
math4tots
22

votes
6

answer
35k

views

Is there a way to guarantee an interface extends a class in Java?

Suppose I have the following situation: public abstract class Vehicle { public void turnOn() { ... } } public interface Flier { public void fly(); } Is there a way that I can guarantee that any class that implements Flier must also extend Vehicle? I don't want to make Flier an abstract class because...
math4tots
4

votes
1

answer
88

views

Does GAE offer default quantitative abuse protection?

If I were to say, upload the sample application written in Python, would Google protect me from malicious bots trying to eat up my resources? DoS attacks? Exactly how much security can I expect from Google? background: I've read this article and it looks like you have the option to manually request...
math4tots
3

votes
2

answer
1.2k

views

Copying a java.util.Stack in Java [duplicate]

This question already has an answer here: How do I copy a stack in Java? 4 answers I'm looking for a proper way to copy java.util.Stack in Java. Stack doesn't seem to have a copy constructor, and the only thing I have managed to find in the documentation that might do what I want is the clone metho...
math4tots
3

votes
2

answer
125

views

Create a new instance of a Class given an instance in Python

In Python, if I have an object, say x, then how do I create a new object y so that x and y are of the same class? In Java, the code I want would look something like this: Object y = x.getClass().newInstance();
math4tots
3

votes
1

answer
148

views

“Or” operator in emacs regexp with `M-x occur`

I've wanted to get an overview of my Python program, so I ran: M-x occur and for the regexp I've supplied (def)|(class) which failed to match anything. I've also looked at this post, and tried (def)\\|(class) but this failed to match anything either... How do I get M-x occur to match class or def?
math4tots
3

votes
1

answer
7.1k

views

Wrapper class in Python [duplicate]

This question already has an answer here: How can I intercept calls to python's “magic” methods in new style classes? 4 answers I want to have a wrapper class that behaves exactly like the object it wraps except that it adds or overwrites a few select methods. My code currently looks like this:...
math4tots
1

votes
3

answer
1.9k

views

Getting window dimensions in a Metro app

How do I get the dimensions of the window in a windows 8 metro app? I want to fill the screen with a canvas element, and currently my default.js file looks something like this // ... some autogenerated code ... app.onactivated = function (args) { if (args.detail.kind === activation.ActivationKind.la...
math4tots
6

votes
6

answer
16.5k

views

Slick way to reverse the (binary) digits of a number in Python?

I am looking for a slick function that reverses the digits of the binary representation of a number. If f were such a function I would have int(reversed(s),2) == f(int(s,2)) whenever s is a string of zeros and ones starting with 1. Right now I am using lambda x: int(''.join(reversed(bin(x)[2:])),2)...
math4tots
3

votes
3

answer
791

views

C++ class implementation namespace

I have files MyClass.hpp and MyClass.cpp MyClass.hpp class MyClass { public: void method1(); void method2(); }; MyClass.cpp #include 'MyClass.hpp' void MyClass::method1() { } void MyClass::method2() { } I find it a little silly that I have to write out the MyClass:: every time I have to write a meth...
math4tots
2

votes
1

answer
817

views

Custom RGB colors with Python curses

I'm writing a program in Python using the curses module in the standard library. I want my program to just exit if it can't use custom colors I specify with RGB triples. So I have some starter code that looks like: import curses def main(stdscr): if not curses.can_change_color(): raise Exception('Ca...
math4tots
4

votes
2

answer
323

views

How does dict.pop() detect if an optional argument has been passed?

d = dict() d.pop('hello', None) # No exception thrown d.pop('hello', 0) # No exception thrown d.pop('hello') # KeyError I had thought that in Python we usually tested whether a default argument was passed by testing the argument with some sort of default value. I can't think of any other 'n...
math4tots
5

votes
1

answer
213

views

Intercepting heapq

I want to use Python's heapq module. However, I need to keep track of which index every value is set to. So I wrote class heap(list): def __init__(self,xs): super(heap,self).__init__(xs) self._index_table = {x:i for i,x in enumerate(self)} def __setitem__(self,i,v): print(i,v) super(heap,self...
math4tots
30

votes
2

answer
7.3k

views

How do I declare that a computed property 'throws' in Swift 2?

class SomeClass { var someProperty: Int { throw Err('SNAFU') } } For code like the above, the swift binary complains 'error is not handled because the enclosing function is not declared 'throws'. How do I declare that 'someProperty' 'throws' in the above? class SomeClass { var someProperty throws: I...
math4tots
2

votes
2

answer
390

views

Circular Prolog

I have the following prolog code: equiAngularTriangle(T) :- equiLateralTriangle(T). equiLateralTriangle(T) :- equiAngularTriangle(T). Is there a way to keep the interpreter from asking the same question twice? For instance, if I ask equiAngularTriangle(t), then it's going to ask equiLateralTriangle(...
math4tots
3

votes
2

answer
5.7k

views

C++ vector adding elements efficiently

Suppose I have the following code: #include #include using namespace std; class X { public: int x[1000]; X(int y) { for (int i = 0; i < 1000; i++) x[i] = y; } }; int main() { vector v; X x0(0); X x1(1); X x2(2); v.push_back(x0); v.push_back(x1); v.push_back(x2); cout
math4tots
31

votes
1

answer
3.1k

views

How does Python's cmp_to_key function work?

I came across this function here. I am baffled as to how this would be implemented -- how does the key function generated by cmp_to_key know what 'position' a given element should be without checking how the given element compares with every other element of interest?
math4tots
2

votes
2

answer
114

views

C++ stream iterators vs container iterators

From what I understand, in C++, for container types (e.g. vector, deque, map), I can get the iterator type using syntax like vector::iterator map::const_iterator And get an actual iterator to first element through a method like stuff.begin() However, for streams (e.g. ifstream), the interface seems...
math4tots
1

votes
5

answer
96

views

Is it possible for me to know if I have cleaned up properly in C?

I have code that looks something like, #include #include char** someArray = NULL; size_t numberOfEntriesInArray = 0; void addToArray(char* someString){ someArray = realloc(someArray, (numberOfEntriesInArray+1) * sizeof(char*)); someArray[numberOfEntriesInArray] = malloc( (strlen(someString) + 1) * s...
math4tots

View additional