Try this:
- Code: Select all
if mode == '1':
and
- Code: Select all
elif mode == '2':
Moderators: phlip, Moderators General, Prelates
if mode == '1':elif mode == '2':

0 1 2
0 1 1 1
1 1 1 1
2 1 1 1
def matrix_mult(matrix, n, m_len):
matrix_new = [[1 for x in range(m_len)] for x in range(m_len)]
for x in range(n):
for i in range(m_len):
for j in range(m_len):
#to get matrix(i, j), compute sum of the products in the ith row in the first matrix, and the jth row in the 2nd
matrix_new[i][j] = sum(map(lambda x : x[0]*x[1], zip(matrix[i], matrix[j])))
return matrix_new
n = graph[0][num_cities-1]
m = matrix_mult(graph, n, num_cities)
print m[0][num_cities-1]
def matrix_transpose(matrix, m_len):
matrix_result = [[0 for x in range(m_len)] for x in range(m_len)]
for i in range(m_len):
for j in range(m_len):
matrix_result[i][j] = matrix[j][i]
return matrix_result
def matrix_mult(left, right, m_len):
right = matrix_transpose(right, m_len)
matrix_result = [[0 for x in range(m_len)] for x in range(m_len)]
for i in range(m_len):
for j in range(m_len):
#to get matrix(i, j), compute sum of the products in the ith row in the first matrix, and the jth row in the 2nd
matrix_result[i][j] = sum(map(lambda x : x[0]*x[1], zip(left[i], right[j])))
return matrix_result
def matrix_pow(matrix, n, m_len):
matrix_result = [[0 for x in range(m_len)] for x in range(m_len)]
for i in range(m_len):
# identity matrix! Handles n=0 case
matrix_result[i][i] = 1
for unused in range(n):
matrix_result = matrix_mult( matrix_result, matrix, m_len )
return matrix_result
void SortedList::freeList(ListNode* L){
ListNode* next;
while (L != 0){
next = L -> next;
delete L -> student;
delete L;
L = next;
}
}
ListNode* SortedList::copyList(ListNode* L){
if (L == 0){
return 0;
}
ListNode* n, temp;
n = temp = new ListNode;
n -> student = L -> student;
L = L -> next;
while (L != 0){
temp = new ListNode;
temp -> student = L -> student;
L = L -> next;
}
return n;
}
struct ListNode {
Student *student;
ListNode *next;
};
static ListNode* copyList(ListNode* L);
static void freeList(ListNode* L);jawdisorder wrote:
- Code: Select all
ListNode* n, temp;
ListNode* n;
ListNode temp;ListNode* n;
ListNode* temp;ListNode* n, * temp;SortedList::ListNode * SortedList::copyList(ListNode *n)
{
//code
}freeglut ([project file path]): failed to open display ''
#include <iostream>
#include <glew.h>
#include <GL/glut.h>
using namespace std;
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1920, 1080);
glutCreateWindow("TEST");
GLenum err = glewInit();
if(err != GLEW_OK)
{
cout <<"ERROR: could not load GLEW. " <<glewGetErrorString(err) <<endl;
return 0;
}
cout <<"GLEW loaded, running version " <<glewGetString(GLEW_VERSION) <<endl;
return 0;
}
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.


Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
while the program is running:
if there are any events:
e = get an event
handle_event(e)
else:
sleep a little bitdef buttonClick():
do stuff
time.sleep(2000)
do more stuffdef buttonClick():
do stuff
tk_root.after(2000, delayCallback)
def delayCallback():
do more stuffphlip wrote:The "yield" statement is completely unrelated with what you're trying to do... it's a way of progressively generating a list of numbers, returning them one at a time. Nothing to do with sleeping.
def function():
while health > 0:
time.sleep(4) #sleep time in seconds
text.insert(END, "Health is " + str(health))
do some stuff
Jof16's wrote:The real problem is that not even the x button in the upper left works (This is obviously Windows).
Jplus wrote:Also, since when does Windows have an X button in the upper left?
Jof16's wrote:Also, I need it to sleep inside a while loop inside the function.
Something like this:
- Code: Select all
def function():
while health > 0:
time.sleep(4) #sleep time in seconds
text.insert(END, "Health is " + str(health))
do some stuff
The reason I need to do it this way is the function only outputs text to the textbox in the GUI. Unfortunately, it spits all of it out at once and I need to slow it down a bit.
The yield statement in python doesn't mean the same thing as the yield function in other languages.Jof16's wrote:I actually found the yield from an example code and it was used to pause the program for a short time.phlip wrote:The "yield" statement is completely unrelated with what you're trying to do... it's a way of progressively generating a list of numbers, returning them one at a time. Nothing to do with sleeping.
Xanthir wrote:You need to do what was described before - refactor your code to use callbacks with delays.
Evented programming ends up looking kinda like recursive programming in these circumstances. Instead of a loop, you just check the condition with an if(), do your work, and set yourself as a callback again after a certain amount of time. The event loop will call you when your timeout is up.
roband wrote:Mav is a cow.
Jof16's wrote:Xanthir wrote:You need to do what was described before - refactor your code to use callbacks with delays.
Evented programming ends up looking kinda like recursive programming in these circumstances. Instead of a loop, you just check the condition with an if(), do your work, and set yourself as a callback again after a certain amount of time. The event loop will call you when your timeout is up.
Okay, I get it now.
One question though. does the rest of the code continue running while waiting for the delay time to finish?
Because I keep getting an error for a line that shouldn't run until the tk_root.after() has.
If so, how would I stop this?
function foo() {
bar();
settimeout(foo, 5000);
baz(); // error from this line
}
function foo() {
bar();
settimeout(foo2, 5000);
}
function foo2() {
baz(); // error from this line
settimeout(foo3, 5000);
}
function foo3() {
bez(); // error from this line
}Xanthir wrote:...
Do you mean something like (JS here, because I dunno how tkinter works):
- Code: Select all
function foo() {
bar();
settimeout(foo, 5000);
baz(); // error from this line
}
If so, then yes, the rest of the code keeps running after your timeout. Setting a timeout just schedules some later code; it doesn't interfere with the current code that's running. This is *unlike* recursive code, where you can count on the recursive call to run before the code continues on.
I need to pause the code for a pre-determined amount of time rather than schedule some code to run later.
function foo() {
bar()
pause_for_5_seconds()
baz()
}
function foo() {
bar()
call_in_5_seconds(foo2);
}
function foo2() {
baz()
}
Jof16's wrote:Either I'm not understanding or I'm not making myself clear (probably the former).
Here's the code for the fight function:Spoiler:
Using root.after() would require me to write multiple functions.
Unless I called the first function after the second one is finished. But, how would I return totalHealth to the fightButton function.. Plus, I'd be writing the same function twice, just with a very slightly different name.
if( playerHealth > 0 && zombieHealth > 0 ) {
settimeout(function(){fight(playerHealth, zombieHealth);}, 1000);
} else {
doSomethingWhenTheFightIsOver(totalHealth);
}
function foo() {
A();
while(needToRun) {
B();
sleep(delay);
}
C();
}
function foo() {
A();
B();
}
function B() {
stuff();
if( needToRunAgain )
setTimeout(B,delay);
else
C();
}
Users browsing this forum: ViKing and 7 guests