logic

Iterators at critical logic

In a recent interview one peculiar question has been asked a[]= { 1,2,3,4,5,6,7,8,9,10} When an array is given with specified starting index i have to iterate it till i traverse all elements. I mean suppose the starting index is "5" i have to start from 6,7,8,9,10,5,4,3,2,1.Please carefully look at the sequence ,how can one create ...

Writing your own square root function.

How do you write your own function for finding the most accurate square root of an integer? After googling it, I found this, but firstly, I didn't get it completely, and secondly, it is approximate too. Assume square root as nearest integer (to the actual root) or a float. ...

Best resize and or crop logic

Hey guys, I've come across this a few times and thought it would be good to put it out there. What's your best image resize and/or crop logic. The idea being that some method is called with a target image, dimensions and a crop flag - this would return or save off or whatever the desired image. Mine is below. Converted from VB to C#...

Why are default arguments evaluated at definition time in Python?

I had a very difficult time with understanding the root cause of a problem in an algorithm. Then, by simplifying the functions step by step I found out that evaluation of default arguments in Python doesn't behave as I expected. The code is as follows: class Node(object): def __init__(self, children = []): self.children = c...

Applications of Unification?

What are (practical) applications of Unification ? Where it is been used in real world? I couldn't get the whole idea of what it is really about and why its considered as a part of Artificial Intelligence. ...

how to create a parser for search queries

for example i'd need to create something like google search query parser to parse such expressions as: flying hiking or swiming -"walking in boots " author:hamish author:reid or house in new york priced over $500000 with a swimming pool how would i even go about start building something like it? any good resources? c# re...

Pragmatically adding give-aways/freebies to an online store.

Our business currently has an online store and recently we've been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the extra free items to their order after they checkout. Of course, it'd be nice to automate this entire process. I've been mulling over a fe...

Proving with floor and ceiling functions formally for computer scientists

Hi there, This is indeed a sort of exercise I have to complete but a little direction would be wonderful. I have to determine if I should prove or disprove these three statements... The definition I have of floor and ceil are pretty basic. I wont bother placing them here. Once I determine if they need proof/disproof I have to get to wo...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [H|T], [H|Tnew]) :- insert_pq(State, T, Tnew). precedes(X, Y) :- X < Y. % < needs to be defined depending on problem the function qui...

RoR: "belongs_to_many"? Association headache...

I can't seem to wrap my head around this, so I thought I'd post and see if anyone could help me out (please pardon the question if it's insultingly simple: it's complicated to me right now!) I have these models: order service customer I think they speak for themselves: a service is what the customer buys when they place an order. O...

php riddle - interesting result

I have the following code: <?php $cups = array(); for($i=0; $i<500; $i++){ $cups[$i] = 0; } for($x=1; $x<500; $x++){ for($y=$x; $y<500; $y+=$x){ $cups[$y] = !$cups[$y]; } } foreach($cups as $key => $value){ if($value == 1){ echo "{$key}, "; } } ?> As you can see, I fill up an array with 500 zeroes, lo...

prolog - why this strange trace

here is the prolog code (which i sort of follow). len([],0). len([_|T],N) :- len(T,X), N is X+1. and here is the trace for it (im running linux, swi) [trace] ?- len([d,f,w,c],X). Call: (7) len([d, f, w, c], _G314) ? Call: (8) len([f, w, c], _L182) ? Call: (9) len([w, c], _L201) ? Call: (10) len([c], _L220) ? C...

PHP/Flash advice

Looking to start a project that would require me to use Flash or Flex (I have not worked with either of these yet, yikes!!!). Flash would be the front end user interface that needs to display items pulled from a MySQL Database (I was thinking ajax via jQuery but open to suggestions). My question is, What would be the best approach for so...

Python nested lists and recursion problem

Hi guys, I'm trying to process a first order logic formula represented as nested lists and strings in python so that that its in disjunctive normal form, i.e ['&', ['|', 'a', 'b'], ['|', 'c', 'd']] turns into ['|' ['&', ['&', 'a', 'c'], ['&', 'b', 'c']], ['&', ['&', 'a', 'd'], ['&', 'b', 'd']]] where | is 'or' and & is 'and'. cu...

Python: Analyzing complex statements during execution

I am wondering if there is any way to get some meta information about the interpretation of a python statement during execution. Let's assume this is a complex statement of some single statements joined with or (A, B, ... are boolean functions) if A or B and ((C or D and E) or F) or G and H: and I want to know which part of the state...

PLC Ladder Logic Outputs

On a single ladder rung how many outputs can you have. If you have more than one. Would it be AND Logic, or OR Logic. Series, or parallel. I'm trying to make six lights flash using timer on delay instructions with a closed input instruction. I will using an Allen Bradley SLC 500 series PLC. ...

Logic / Probability Question: Picking from a bag

I'm coding a board game where there is a bag of possible pieces. Each turn, players remove randomly selected pieces from the bag according to certain rules. For my implementation, it may be easier to divide up the bag initially into pools for one or more players. These pools would be randomly selected, but now different players would be...

What is the best way to process multiple transactions one at a time using ajax calls?

Note: Sorry for the amount of pseudo code below, but I didn't know how else to show what I'm trying. There is actually a lot more code than that in my solution that manages ajax icon's and status, but I've dumbed it down to show the root of my problem. Basically, I'm trying to create a process loop on a web page and I'm finding it to be...

Django edit form based on add form?

I've made a nice form, and a big complicated 'add' function for handling it. It starts like this... def add(req): if req.method == 'POST': form = ArticleForm(req.POST) if form.is_valid(): article = form.save(commit=False) article.author = req.user # more processing ... Now I don'...

How can I implement scripting in my game?

I'm trying to write a game and implement scripting so that later on in development I won't have to recompile everything when I want to change numbers. My problem is that I don't know how scripts should interface with the game. The scripting language I'm using is angelscript. Right now, I have a state: the intro state, which I'm using a...