Sunday, 1 September 2013

Wrong number of arguments when a calling function from class in Python

Wrong number of arguments when a calling function from class in Python

I'm trying to write an implementation of a genetic algorithm in python. It
says there I am calling it with two arguments when only one is allowed,
but I'm sure I'm not.
Here is the relevant code:
class ga:
def __init__(self, best, pops=100, mchance=.07, ps=-1):
import random as r
self.pop = [[] for _ in range(pops)]
if ps == -1:
ps = len(best)
for x in range(len(self.pop)): #Creates array of random characters
for a in range(ps):
self.pop[x].append(str(unichr(r.randint(65,122))))
def mutate(array):
if r.random() <= mchance:
if r.randint(0,1) == 0:
self.pop[r.randint(0, pops)][r.randint(0, ps)] +=1
else:
self.pop[r.randint(0, pops)][r.randint(0, ps)] -=1
This is the code when I initialize and call from the class:
a = ga("Hello",10,5)
a.mutate(a.pop)
which returns the following error from IDLE:
TypeError: mutate() takes exactly 1 argument (2 given)
How can I fix this?

Go basics: What is the diference between calling a method on struct and calling it on a pointer to that struct?

Go basics: What is the diference between calling a method on struct and
calling it on a pointer to that struct?

Supoose that I have a Vertex type
type Vertex struct {
X, Y float64
}
and I've defined a method
func (v *Vertex) Abs() float64 {
return math.Sqrt(v.X*v.X + v.Y*v.Y)
}
What's the difference between those two calls ? (both of them return the
same result)
v1 := Vertex{3, 4}
fmt.Println(v1.Abs())
v2 := &Vertex{3, 4}
fmt.Println(v2.Abs())

REFACTORING in order to avoid duplication of fields and code netbeans

REFACTORING in order to avoid duplication of fields and code netbeans

I have created Logic gate in Net beans I have following classes
AND
OR
XOR
NOT
So I have used same input variables ie input1 and input2 and each class
has its own getter and setter for returning and setting. but i want create
a abstract base class and use REFACTORING in order to avoid duplication of
fields and code. So that each of the gate class should only include code
that is unique to them that is the method for computing outputs.
So I would like to know how to refactor the code
thanks in advance

Saturday, 31 August 2013

Mention 2 modifications that can be done to a linked list data structure so that it is converted into a binary tree data structure?

Mention 2 modifications that can be done to a linked list data structure
so that it is converted into a binary tree data structure?

It's an A level question. I looked it out on google but only programs are
found. I need theory explanation.

Strange behavior of @RequestMapping on a maven war overlay

Strange behavior of @RequestMapping on a maven war overlay

I've some strange issues with @RequestMapping behavior. Here is the scenario
I have a web project with a @Controller class and some @RequestMapping
methods. They work fine. One mapping is, for example,
@RequestMapping("/jpm/{param}/add")
I have another web project with a war overlay on the first project. Every
thing works fine, I inherite the controller and it methods. No problem so
far.
Now I want a new @Controller in the 2nd project. I add it just like the
other one but with a diferent @RequestMapping ("/jpm/{param}/activate")
method.
@Controller and @RequestMapping are readed fine by Spring as I can see
them in logs but, while "/jpm/{param}/add" works, "/jpm/{param}/activate"
does not, it says 404 resource not found.
Both classes are in different packages but both are loaded, I have two
context:component-scan, one on each project. I've find that if both
classes are in the same package (name), they work fine (even in diferents
projects).
Is that normal? What I'm missing? Why the controller and mapping get
readed but just doesn't work?
I'm getting mad with this, any advice is welcome!.
web.xml (on 1st project)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/applicationContext.xml,
WEB-INF/spring-locale.xml,
WEB-INF/spring-security.xml,
WEB-INF/spring-datasource.xml,
WEB-INF/spring-hibernate.xml,
WEB-INF/spring-jpm.xml
</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<servlet>
<servlet-name>jpm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jpm</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

Make method fewer lines of code

Make method fewer lines of code

I have a method like below, is there any way to refactor, cleaner way, so
that i can make it in fewer lines of code for e.g removal of if / for
loops someting like that
public void CheckProductExistThenAddToCart(CartItem item)
{
if (CartItems.Count == 0) AddToCart(item);
bool itemFound = false;
foreach (var cartItem in CartItems)
{
if (cartItem.ProductId.Equals(item.ProductId))
{
itemFound = true;
cartItem.Qty += item.Qty;
break;
}
}
if (!itemFound)
{
AddToCart(item);
}
}

Application name changes on AWS - how to retrieve resources, which URL or method to use?

Application name changes on AWS - how to retrieve resources, which URL or
method to use?

For example:
applicationSuperName - using this local server name requests build like:
applicationSuperName/home
This web app is deployed into cloud and has:
Public DNS: ec2-23-213-211-f222.eu-west-1.compute.amazonaws.com/home
Some_elastic_IP/home
- elastic ip address
mysuperdomain.com - My domain name attached to elastic IP
My resources(css, js, images) are under applicationSuperName/resources
(locally) , in cloud this folder will be resolved by using elastic IP
address or domain name, but I do no want to include either of these two
parameters into links in my app. (so it would be context independent)
So I can't use:
<link href="/applicationSuperName/resources/css/bootstrap.min.css
rel="stylesheet">
(because application name becomes purchased domain name or IP)
or I can't use:
<link href=".resources/css/bootstrap.min.css" rel="stylesheet">
(because this is request relevant with this part building on top thus the
location for any other request except for root page becomes
applicationSuperName/home/resources/css/bootstrap.min.css which is totally
incorrect)
How to make url request which would go on second level after any
application name and goes to my resources folder?
I came up solution namely using request .getServerName(), but I strongly
believe this is overkill and may has some negative impact, since I have to
call it for each request and page, or check for this variable(like in
session) if I want to use say spring:url tag(to substitute app name and do
usual style like "spring:url/resources").
Please advice if the latter is acceptable or suggest other way to resolve
resources correctly regardless of the app name.