Browsing posts in: Stackoverflow.com

## maytham-ɯɐɥʇʎɐɯ on Karatsuba multiplication java recursion code not working?

Aug 20, 2017    |      Comments Off on maytham-ɯɐɥʇʎɐɯ on Karatsuba multiplication java recursion code not working?

First of all I tried look at your code, it gets a programmer to get lost, few things before we go into solution.

General advice. It is not good practice to convert string to value and back and forward like you do, it does not work like this. I tried as well to debug your code, it is just devil circle.

So I would start with check if value length and the maximum one.

Than if one of the values is less than 2 of length mean every thing less than 10 do multiplication otherwise do karatsuba recursion algorithm.

Here is the solution:

``````public static long karatsuba(long num1, long num2) {

int m = Math.max(
String.valueOf(num1).length(),
String.valueOf(num2).length()
);

if (m < 2)
return num1 * num2;

m = (m / 2) + (m % 2);

long b = num1 >> m;
long a = num1 - (b << m);
long d = num2 >> m;
long c = num2 - (d << m);

long ac = karatsuba(a, c);
long bd = karatsuba(b, d);
long abcd = karatsuba(a + b, c + d);

return ac + (abcd - ac - bd << m) + (bd << 2 * m);
}
``````

Some test;

``````public static void main(String[] args) {
System.out.println(karatsuba(1, 9));
System.out.println(karatsuba(1234, 5678));
System.out.println(karatsuba(12345, 6789));
}
``````

The output would be

``````9
7006652
83810205
``````

It is less pain than your Stringish code. Btw, the solution is inspired from the pesudo in wiki and this class.

## maytham-ɯɐɥʇʎɐɯ on How to solve Instant Run on Android Studio Canary 5, 6 and 7

Jul 26, 2017    |      Comments Off on maytham-ɯɐɥʇʎɐɯ on How to solve Instant Run on Android Studio Canary 5, 6 and 7

I solved this way:

What I did is creating an empty new project in Android Studio 3.0 Canary 7, it does not have this behavior. I compared the new empty project default `build.gradle (Module: app)` with my existing project that has the mentioned problem.

Off course this can differs from case to case, but in my case, I used a variable to update `versionName` and it worked previously with out any issues, but not on Andriod Studio 3. Therefore I corrected the line, `Sync` and `Rebuild` the problem was gone.

## maytham-ɯɐɥʇʎɐɯ on How to enable Jack (Java Android Compiler Kit) in android studio

Jul 26, 2017    |      Comments Off on maytham-ɯɐɥʇʎɐɯ on How to enable Jack (Java Android Compiler Kit) in android studio

Java 8 is supported on Android Studio 3 versions, all this `jackOptions` is not required longer.

Jack is no longer supported, and you should first disable Jack to use
the improved Java 8 support built into the default toolchain.

https://developer.android.com/studio/write/java8-support.html

## maytham-ɯɐɥʇʎɐɯ on Unable to run a Scala class in Intellij 15

Jul 22, 2017    |      Comments Off on maytham-ɯɐɥʇʎɐɯ on Unable to run a Scala class in Intellij 15

As @Clashsoft says you can not initialize the class like this. You can do some thing simple like this for testing:

``````class Rational(n: Int, d: Int) {
def oneHalf: Int =
n * d
}

object MyProgram {
def main(args: Array[String]) {
val rational = new Rational(1, 2)
println(rational.oneHalf)

}
}
``````

It is also possible to use `App` trait (`extends App`) then you do not need to have main method:

``````object MyProgram extends App {
val rational = new Rational(1, 2)
println(rational.oneHalf)
}
``````

All depends on how you want to implement your solution at the end. Regarding different between main and `App` trait please read more.

Thx to @tzachzohar for following addition:

In Scala, just like in Java, only a static `main` method (with appropriate argument and return types) can serve as a program’s entry point. For convenience, IntelliJ IDEA provides a Scala Worksheet as a way to easily test your code, but that’s no magic either – it’s a just nice wrapper – behind the scenes, a worksheet has its own `main` method calling your code.

## maytham-ɯɐɥʇʎɐɯ on JGrapT setting undirected graph

Jun 1, 2017    |      Comments Off on maytham-ɯɐɥʇʎɐɯ on JGrapT setting undirected graph

I have made few manipulations to your code and this should works.

The part that removes arrows is following part

``````// This part to remove arrow from edge
mxUtils.setCellStyles(graphComponent.getGraph().getModel(),
cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE);
``````

So the full code would be like this, and it is just an example and the rest is left to your fantasy:

``````import com.mxgraph.layout.mxCircleLayout;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxUtils;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;

import javax.swing.*;
import java.awt.*;
import java.util.Collection;

public class UndirectedGraphClass extends JFrame {

public static void main(String[] args) {
new UndirectedGraphClass();
}

private UndirectedGraphClass() {

UndirectedGraph g =
new SimpleGraph(DefaultEdge.class);

mxGraphModel graphModel = (mxGraphModel) graphComponent.getGraph().getModel();
Collection cells = graphModel.getCells().values();
// This part to remove arrow from edge
mxUtils.setCellStyles(graphComponent.getGraph().getModel(),
cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE);

this.setTitle(" some undirected graph ");
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setPreferredSize(new Dimension(400, 400));
this.pack();
this.setVisible(true);
}
}

The solution is inspired from this answer.

What does the “Unrecognised SQL statement” mean when I am trying to use IF NOT EXISTS?

May 28, 2017							   |
Comments Off on What does the “Unrecognised SQL statement” mean when I am trying to use IF NOT EXISTS?

This is how to do it, I have test it and it works:
\$sql = "
SELECT * FROM (SELECT '\$user', '\$phash') AS tmp
WHERE NOT EXISTS (
) LIMIT 1;
";

This solution is inspired from this answer.
The problem is that you can not combine PHP and MySQL statement like you did, you need to encapsulate all MySQL statements in quote ".
What comes RAISERROR, it is not MySQL function, it belongs to Microsoft.
You could easily make php if statement that checks if \$sql contain valid username and return your message. That part is left to your fantasy.
XAMPP has no thing to do with the error, it just a software that provides an Apache and MySQL installation for Windows.

Note: P.S. please learn to use parameterized queries, because your
code is vulnerable to SQL injection. thanks to @BillKarwin for mentioning this.

May 21, 2017							   |

I have had a similar scenario in Laravel and solved it in the following way.
The password contains characters from at least three of the following five categories:

English uppercase characters (A – Z)
English lowercase characters (a – z)
Base 10 digits (0 – 9)
Non-alphanumeric (For example: !, \$, #, or %)
Unicode characters

First, we need to create a regular expression and validate it.
Your regular expression would look like this:
^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dX])(?=.*[!\$#%]).*\$

I have tested and validated it on this site. Yet, perform your own in your own manner and adjust accordingly.
So your final Laravel code should be like this:
min:6|
regex:/^.*(?=.{3,})(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[dX])(?=.*[!\$#%]).*\$/|
confirmed',

Note:

I have tested and validated it on both the regular expression site and a Laravel 5 test environment and it works.
I have used min:6, this is optional but it is always a good practice to have a security policy that reflects different aspects, one of which is minimum password length.
I suggest you to use password confirmed to ensure user typing correct password.
Within the 6 characters our regex should contain at least 3 of a-z or A-Z and number and special character.
Always test your code in a test environment before moving to production.

Some online references

http://regex101.com
http://regexr.com (another regex site taste)
http://www.regular-expressions.info/refquick.html
https://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspx
http://php.net/manual/en/function.preg-match.php
http://laravel.com/docs/5.1/validation#rule-regex

Regarding your custom validation message for the regex rule in Laravel, here are a few links to look at:

Laravel Validation custom message
Custom validation message for regex rule in Laravel?
Laravel custom validation messages
How to add regex to attribute name in custom validations in Laravel?

open big URL from a batch file

May 20, 2017							   |
Comments Off on open big URL from a batch file

This would work

Command line progress bar in Java

Apr 13, 2017							   |
Comments Off on Command line progress bar in Java

I call the method periodically in my file download to check the total file-size and remaining and present that in %.
It can be used for other task purpose as well.
Test and output example
progressPercentage(0, 1000);
[----------] 0%

progressPercentage(10, 100);
[*---------] 10%

progressPercentage(500000, 1000000);
[*****-----] 50%

progressPercentage(90, 100);
[*********-] 90%

progressPercentage(1000, 1000);
[**********] 100%

Test with for loop
for (int i = 0; i <= 200; i = i + 20) {
progressPercentage(i, 200);
try {
} catch (Exception e) {
}
}

The method can be easily modified:
public static void progressPercentage(int remain, int total) {
if (remain > total) {
throw new IllegalArgumentException();
}
int maxBareSize = 10; // 10unit for 100%
int remainProcent = ((100 * remain) / total) / maxBareSize;
char defaultChar = '-';
String icon = "*";
String bare = new String(new char[maxBareSize]).replace('', defaultChar) + "]";
StringBuilder bareDone = new StringBuilder();
bareDone.append("[");
for (int i = 0; i < remainProcent; i++) {
bareDone.append(icon);
}
String bareRemain = bare.substring(remainProcent, bare.length());
System.out.print("r" + bareDone + bareRemain + " " + remainProcent * 10 + "%");
if (remain == total) {
System.out.print("n");
}
}

Export Postgres table to CSV file with headings

Apr 9, 2017							   |

For version 9.5 I use, it would like:
COPY products_273 TO '/tmp/products_199.csv' WITH (FORMAT CSV, HEADER);

Pages:12345678›

Search for:

Recent Posts

maytham-ɯɐɥʇʎɐɯ on Karatsuba multiplication java recursion code not working?

Wet extractor 3 release!

maytham-ɯɐɥʇʎɐɯ on How to solve Instant Run on Android Studio Canary 5, 6 and 7

maytham-ɯɐɥʇʎɐɯ on How to enable Jack (Java Android Compiler Kit) in android studio

maytham-ɯɐɥʇʎɐɯ on Unable to run a Scala class in Intellij 15

Categories
Article

Portfolio

Projects

Research

Stackoverflow.com

Uncategorized

Created by Site5 WordPress Themes. Experts in WordPress Hosting

{"vars": {"account": "UA-52839102-1"},"triggers": {"trackPageview": {"on": "visible","request": "pageview"}}}

.icon-set-lsi_widget-2 a,
.icon-set-lsi_widget-2 a:visited,
.icon-set-lsi_widget-2 a:focus {
background: #1E72BD !important;
color: #FFFFFF !important;
font-size: 20px !important;
}
.icon-set-lsi_widget-2 a:hover {
background: #777777 !important;
color: #FFFFFF !important;
}

/* <![CDATA[ */