Groovy Turkish tax number verification - groovy

I need to calculate Turkish Tax number via Groovy.. Basically it's 10 digit number..
Here is the math behind it..
First 9 digit number (d), last 10 digit control ( c )value
Tr tax number = d1 d2 d3 d4 d5 d6 d7 d8 d9 c1
So d1..d9 >> d[i]
p[i] = (d[i] + 10 — i) mod 10
p[i] = 9 => q[i] = 9
p[i] != 9 => q[i] = (p[i] * 2^(10 — i)) mod 9
c1 = (10 — (Σ q[i] mod 10)) mod 10
Let’s say tax number : 018273645x
p[1] = ( d[1] + 10–1 ) mod 10 = (0 + 10–1) mod 10 = 9
p[2] = ( d[2] + 10–2) mod 10 = (1 + 10–2) mod 10 = 9
p[3] = ( d[3] + 10–3 ) mod 10 = (8 + 10–3) mod 10 = 5
p[4] = ( d[4] + 10–4 ) mod 10 = (2 + 10–4) mod 10 = 8
p[5] = ( d[5] + 10–5 ) mod 10 = (7 + 10–5) mod 10 = 2
p[6] = ( d[6] + 10–6 ) mod 10 = (3 + 10–6) mod 10 = 7
p[7] = ( d[7] + 10–7 ) mod 10 = (6 + 10–7) mod 10 = 9
p[8] = ( d[8] + 10–8 ) mod 10 = (4 + 10–8) mod 10 = 6
p[9] = ( d[9] + 10–9 ) mod 10 = (5 + 10–9) mod 10 = 6
q[1] = 9
q[2] = 9
q[3] = (p[3] * 2^(10–3)) mod 9 = (5 * 2⁷) mod 9 = 1
q[4] = (p[4] * 2^(10–4)) mod 9 = (8 * 2⁶) mod 9 = 8
q[5] = (p[5] * 2^(10–5)) mod 9 = (2 * 2⁵) mod 9 = 1
q[6] = (p[6] * 2^(10–6)) mod 9 = (7 * 2⁴) mod 9 = 4
q[7] = 9
q[8] = (p[8] * 2^(10–8)) mod 9 = (6 * 2²) mod 9 = 6
q[9] = (p[9] * 2^(10–9)) mod 9 = (6 * 2¹) mod 9 = 3
i = 1..9 >> Σ q[i] = 9 + 9 + 1 + 8 + 1 + 4 + 9 + 6 + 3 = 50
c1 = (10 — (Σ q[i] mod 10)) mod 10 = (10–50 mod 10) mod 10
c1 = (10–0) mod 10 = 0
So it would be >> 0182736450
Here is my code.
boolean isTurkishVID(String idnum) {
d=idnum;
int v1 = 0;
int v2 = 0;
int v3 = 0;
int v4 = 0;
int v5 = 0;
int v6 = 0;
int v7 = 0;
int v8 = 0;
int v9 = 0;
int v11 = 0;
int v22 = 0;
int v33 = 0;
int v44 = 0;
int v55 = 0;
int v66 = 0;
int v77 = 0;
int v88 = 0;
int v99 = 0;
int v_last_digit = 0;
int total = 0;
if (d.size()==10) {
v1 = (d[0].toInteger() + 9) % 10;
v2 = (d[1].toInteger() + 8) % 10;
v3 = (d[2].toInteger() + 7) % 10;
v4 = (d[3].toInteger() + 6) % 10;
v5 = (d[4].toInteger() + 5) % 10;
v6 = (d[5].toInteger() + 4) % 10;
v7 = (d[6].toInteger() + 3) % 10;
v8 = (d[7].toInteger() + 2) % 10;
v9 = (d[8].toInteger() + 1) % 10;
v11 = (v1 * 512) % 9;
v22 = (v2 * 256) % 9;
v33 = (v3 * 128) % 9;
v44 = (v4 * 64) % 9;
v55 = (v5 * 32) % 9;
v66 = (v6 * 16) % 9;
v77 = (v7 * 8) % 9;
v88 = (v8 * 4) % 9;
v99 = (v9 * 2) % 9;
if (v1 != 0 && v11 == 0) v11 = 9;
if (v2 != 0 && v22 == 0) v22 = 9;
if (v3 != 0 && v33 == 0) v33 = 9;
if (v4 != 0 && v44 == 0) v44 = 9;
if (v5 != 0 && v55 == 0) v55 = 9;
if (v6 != 0 && v66 == 0) v66 = 9;
if (v7 != 0 && v77 == 0) v77 = 9;
if (v8 != 0 && v88 == 0) v88 = 9;
if (v9 != 0 && v99 == 0) v99 = 9;
total = v11 + v22 + v33 + v44 + v55 + v66 + v77 + v88 + v99;
if (total % 10 == 0) total = 0;
else total = (10 - (total % 10));
if (total == v_last_digit) {
return true;
} else return false;
} else return false;
}
It returns error when I use it like
isTurkishVI("1234567891");
Returns;
groovy.lang.MissingMethodException: No signature of method: ConsoleScript20.isTurkishVI() is applicable for argument types: (java.lang.Integer) values: [1234567891]
Possible solutions: isTurkishVID(java.lang.String)
at ConsoleScript20.run(ConsoleScript20:74)

Related

Node isometric tile map render second layer problem

I'm building an isometric map tile image in Node and I'm stuck at the second layer rendering, I can't figure out how to adjust items in the y axis
Here's my code so far:
let i = 0;
for (let layer of map) {
const xTiles = layer.length;
const yTiles = layer[0].length;
for (let Xi = xTiles - 1; Xi >= 0; Xi--) {
for (let Yi = 0; Yi < yTiles; Yi++) {
const imgIndex = layer[Xi][Yi];
if (imgIndex == null || imgIndex == -1) {
continue;
}
const tile = tiles[imgIndex];
const offX = (Xi * tileColOffset / 2 + Yi * tileColOffset / 2 + originX) + (i * ((tileColOffset - tile.width) / 2));
const offY = (Yi * tileRowOffset / 2 - Xi * tileRowOffset / 2 + originY) - (i * ((tileRowOffset / 2)));
ctx.drawImage(tile, offX, offY);
}
}
i++;
}
I can center the sprite on the x axis but not on the y one, probably because sprites have different heights. The code above reproduces this
As you can see the taller sprites are quite centered, but the small ones are not. Any suggestion?
Thanks!
Just found out that I have to calculate the difference between the half height of the ground tile and the height of the sprite:
let i = 0;
for (let layer of map) {
const xTiles = layer.length;
const yTiles = layer[0].length;
for (let Xi = xTiles - 1; Xi >= 0; Xi--) {
for (let Yi = 0; Yi < yTiles; Yi++) {
const imgIndex = layer[Xi][Yi];
if (imgIndex == null || imgIndex == -1) {
continue;
}
const tile = tiles[imgIndex];
let offX, offY
if (i === 0) {
offX = (Xi * tileColOffset / 2 + Yi * tileColOffset / 2 + originX);
offY = (Yi * tileRowOffset / 2 - Xi * tileRowOffset / 2 + originY);
} else {
offX = (Xi * tileColOffset / 2 + Yi * tileColOffset / 2 + originX) + (i * ((tileColOffset - tile.width) / 2));
offY = (Yi * tileRowOffset / 2 - Xi * tileRowOffset / 2 + originY) + ((i * (tileRowOffset / 2 - tile.height)));
}
ctx.drawImage(tile, offX, offY);
}
}
i++;
}

CS50 Wk4 Blur Pset

void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int red_total = 0;
int blue_total = 0;
int green_total = 0;
int number_counted = 0;
for (int k = -1; k <= 1; k++)
{
for (int l = -1; l <= 1; l++)
{
if (i + k <= height && i + k >= 0 && j + l <= width && j + l >= 0)
{
blue_total += image[i+k][j+l].rgbtBlue;
red_total += image[i+k][j+l].rgbtRed;
green_total += image[i+k][j+l].rgbtGreen;
number_counted ++;
}
}
}
image[i][j].rgbtBlue = blue_total / number_counted;
image[i][j].rgbtRed = red_total / number_counted;
image[i][j].rgbtGreen = green_total / number_counted;
}
}
return;
}
Why is that section && operators?
if (i + k <= height && i + k >= 0 && j + l <= width && j + l >= 0)
I ran it with || operators because my understanding is that under the guise of the problem IF any of those conditions are satisfied there is no block to add. Yet why is it that when I run it under || it returns segmentation fault whereas if I run it with && the problem works out?
Thank you for answering!
All of those conditions have to be true or else the array operations will be invalid.
e.g. if i+k > height then image[i+k] is invalid.
Also I think you have some "off by one problems. image is [height][width] so valid values are [0..height-1] and [0..width-1] so the checks should be more like if (i + k < height && i + k >= 0 && j + l < width && j + l >= 0)

Calculate Values of Variables Found in an Interval with Java

I am trying to find the values x and y may take so the following inequalities hold:
1/24 < 1/15*y < 1/10*x < 2/24 < 2/15*y < 3/24
Is there a way to formulate such a problem in Java?
Constraint Programming would probably solve such a problem but is there an alternative way?
If Constraint Programming is the only way, how does this look like?
The following is what I tried with constraint programming using or-tools. How to formulate strict inequalities?
MPSolver solver = new MPSolver(
"SimpleMipProgram", MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
// [END solver]
// [START variables]
double infinity = java.lang.Double.POSITIVE_INFINITY;
// x and y are float/double variables.
MPVariable x = solver.makeNumVar(0,1,"x"); //makeIntVar(0.0, infinity, "x");
MPVariable y = solver.makeNumVar(0,1,"y"); //makeIntVar(0.0, infinity, "y");
System.out.println("Number of variables = " + solver.numVariables());
// [END variables]
// [START constraints]
// x + 7 * y <= 17.5.
/*MPConstraint c0 = solver.makeConstraint(-1, 17.5, "c0");
c0.setCoefficient(x, 1);
c0.setCoefficient(y, 7);
// x <= 3.5.
MPConstraint c1 = solver.makeConstraint(-infinity, 3.5, "c1");
c1.setCoefficient(x, 1);
c1.setCoefficient(y, 0);*/
// 1/24 < 1/15*y ---> -1/15 * y < -1/24
MPConstraint c0 = solver.makeConstraint(-1000,-1/24.0,"c0");
c0.setCoefficient(y,-1/15.0);
// 1/15*y < 1/10*x ---> 1/15*y - 1/10*x < 0
MPConstraint c1 = solver.makeConstraint(-1000,0,"c1");
c1.setCoefficient(y,1/15.0);
c1.setCoefficient(x,-1/10.0);
// 1/10*x < 2/24 ---> 1/10*x < 2/24
MPConstraint c2 = solver.makeConstraint(-1000,2/24.0,"c2");
c2.setCoefficient(x,1/10.0);
// 2/24 < 2/15*y ---> -2/15*y < -2/24
MPConstraint c3 = solver.makeConstraint(-1000, -2/24.0);
c3.setCoefficient(y,-2/15.0);
// 2/15*y < 3/24 ---> 2/15*y < 3/24
MPConstraint c4 = solver.makeConstraint(-1000,3/24.0);
c4.setCoefficient(y,2/15.0);
Here is a working code using the integer solver
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from ortools.sat.python import cp_model
model = cp_model.CpModel()
scale = 1000
x = model.NewIntVar(0, scale, 'x')
y = model.NewIntVar(0, scale, 'y')
# 1/24 < 1/15*y < 1/10*x < 2/24 < 2/15*y < 3/24
model.Add(5 * scale < 8 * y)
model.Add(8 * y < 12 * x)
model.Add(12 * x < 10 * scale)
model.Add(10 * scale < 16 * y)
model.Add(16 * y < 15 * scale)
solver = cp_model.CpSolver()
solver.parameters.log_search_progress = True
status = solver.Solve(model)
if status == cp_model.FEASIBLE:
print('x =', solver.Value(x) * 1.0 / scale)
print('y =', solver.Value(y) * 1.0 / scale)
With scale = 1000, it outputs:
x = 0.418
y = 0.626
With scale = 100, it outputs:
x = 0.43
y = 0.63
With scale = 10, it outputs
x = 0.5
y = 0.7
I found the solution by writing down a loop that produces random values until all the statements are fulfilled.
Now I am interested in how wolfram alpha solves such problems so quickly.
public class inequalities {
private static double x;
private static double y;
private static double Ratio3 = 1/24.0;
private static double Ratio2 = 1/15.0;
private static double Ratio1 = 1/10.0;
public static void main(String[] args) {
x = Math.random();
y = Math.random();
boolean loop = true;
while (loop) {
loop = calculatingTheInequalities();
if (loop) {
x = Math.random();
y = Math.random();
}
}
System.out.println("x value: " + x);
System.out.println("y value: " + y);
}
public static boolean calculatingTheInequalities() {
if (Ratio3<Ratio2*y && Ratio2*y<Ratio1*x &&
Ratio1*x<2*Ratio3 && 2*Ratio3<2*Ratio2*y &&
2*Ratio2*y<3*Ratio3) {
return false;
} else {
return true;
}
/*if (Ratio3 < Ratio2 *y) {
if (Ratio2 *y < Ratio1 *x) {
if (Ratio1 *x<2* Ratio3) {
if (2* Ratio3 < 2* Ratio2 *y) {
if (2* Ratio2 *y < 3* Ratio3) {
return false;
} else {
return true;
}
} else {
return true;
}
} else {
return true;
}
} else {
return true;
}
} else {
return true;
}*/
}
}

Flipping algorithm

I have a string s containing different types of brackets : () and [] . How can I balance a string of this type with the minimum possible number of reversals ? I can replace any bracket with any other one.
For example : Cost for [)(] is 2, it becomes [()]. Cost for [](( is 1, it becomes []() . [(]) is not balanced.
A more complex example : )[)([)())] can be turned to ([])[(())] in 4 changes, but can also be turned to [()(()())] in 3 steps, which is the least number of modifications to make it balanced.
How can I solve the problem ?
First approach I came with is O(n^3) dynamic programming.
Let match(i, j) be the number of replaces you have to make in order to make s[i] and s[j] as () or []. So match(i, j) can be either 0, 1 or 2.
Consider dp[i][j] = the minimum cost to balance the subsequence from i to j in your brackets array. Now you will define dp[i][i + 1] as:
dp[i][i + 1] = match(i, i + 1)
Now the general rule is that we take the overall minimum between dp[i + 1][j - 1] + match(i, j) and min(dp[i][j], dp[i][p] + dp[p + 1][j]) for any i < p < j. Obviously, the result will be held in dp[1][n]. There is a C++ solution (I'll also upload a python program in about 15 minutes when I'll be done with it - not so strong with python :P).
#include <iostream>
#include <string>
using namespace std;
int dp[100][100];
string s;
int n;
int match(char a, char b) {
if (a == '(' && b == ')') {
return 0;
}
if (a == '[' && b == ']') {
return 0;
}
if ((a == ')' || a == ']') && (b == '(' || b == '[')) {
return 2;
}
return 1;
}
int main() {
cin >> s;
n = s.length();
s = " " + s;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= n; ++j) {
dp[i][j] = 0x3f3f3f3f;
}
}
for (int i = 1; i < n; ++i) {
dp[i][i + 1] = match(s[i], s[i + 1]);
}
for (int k = 3; k <= n; k += 2) {
for (int i = 1; i + k <= n; ++i) {
int j = i + k;
dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + match(s[i], s[j]));
for (int p = i + 1; p <= j; p += 2) {
dp[i][j] = min(dp[i][j], dp[i][p] + dp[p + 1][j]);
}
}
}
cout << dp[1][n] << '\n';
/*for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cout << dp[i][j] << ' ';
}
cout << '\n';
}*/
return 0;
}
Edit:
Here you go Python :)
s = input()
n = len(s)
inf = 0x3f3f3f3f
def match(x, y):
if x == '(' and y == ')':
return 0
if x == '[' and y == ']':
return 0
if (x == ')' or x == ']') and (y == '(' or y == '['):
return 2
return 1
# dp[i][j] = min. cost for balancing a[i], a[i + 1], ..., a[j]
dp = [[inf for j in range(n)] for i in range(n)]
for i in range(n - 1):
dp[i][i + 1] = match(s[i], s[i + 1])
for k in range(3, n, 2):
i = 0
while i + k < n:
j = i + k
dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + match(s[i], s[j]))
for p in range(i + 1, j, 2):
dp[i][j] = min(dp[i][j], dp[i][p] + dp[p + 1][j])
i += 1
print(dp[0][n - 1])
#for i in range(n):
# for j in range(n):
# print(dp[i][j], end = ' ')
# print()

(computer graphics) radial image distortion

I need to create an effect, that radially distorts a bitmap, by stretching or shrinking its "layers of pixels" radially (as shown on the image):
http://i.stack.imgur.com/V6Voo.png
by colored circles (their thickness) is shown the transform, that is applied to the image
What approach should I take? I have a bitmap (array of pixels) and an another bitmap, that should be the result of such a filter applied (as a result, there should be some kind of a round water ripple on the bitmap).
Where could I read about creating such effects?
Thank you.
Try to look here
http://www.jhlabs.com/ip/blurring.html
Zoom and Spin Blur
it is Java but nevertheless it could be fit to your request.
Well, the most accurate results would come from mapping the euclidean coordinates to a polar matrix. Then you would very easily be able to stretch them out. Then just translate them back to a euclidean representation and save. I'll write and edit with some code in a second.
Alright I got a bit carried away but here's my code. It will take a bitmap, convert it to and from polar coordinates and save it. now, radial based distortion should be a breeze.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#define PI 3.141592654
#define C_R 1000
#define C_S 1000
#define C_M 2000
typedef struct{ int r,g,b; } color;
typedef struct{ int t; color* data; int w, h; } bitmap;
typedef struct{ int t; color* data; int r, s, w, h; } r_bitmap;
bitmap* bmp_load_from_file( const char* fname ){
FILE* b = fopen( fname, "rb" );
if( b <= 0 ) return 0;
int num;
fscanf( b, "BM%n", &num );
if( num < 2 ) return 0;
struct{ int size, reserved, offset;
int hsize, wid, hig, planes:16, bpp:16, comp, bmpsize, hres, vres, colors, important; } head;
fread( &head, 13, 4, b );
bitmap* bmp = malloc( sizeof( bitmap ) );
bmp->data = malloc( head.wid * head.hig * sizeof( color ) );
bmp->w = head.wid;
bmp->h = head.hig;
for( int y = head.hig - 1; y >= 0; --y ){
int x;
for( x = 0; x < head.wid; ++x ){
color t;
t.r = fgetc( b );
t.g = fgetc( b );
t.b = fgetc( b );
bmp->data[x+y*bmp->w] = t;
}
x*=3;
while( x%4 != 0 ){
++x;
fgetc( b );
}
}
bmp->t = 0;
fclose( b );
return bmp;
}
void bmp_save( const char* fname, bitmap* bmp ){
FILE* b = fopen( fname, "wb" );
if( b <= 0 ) return 0;
struct{ int size, reserved, offset;
int hsize, wid, hig, planes:16, bpp:16, comp, bmpsize, hres, vres, colors, important; } head;
fprintf( b, "BM" );
head.size = 3 * (bmp->w+4)/4*4 * bmp->h + 54;
head.offset = 54;
head.hsize = 40;
head.wid = bmp->w;
head.hig = bmp->h;
head.planes = 1;
head.bpp = 24;
head.comp = 0;
head.bmpsize = 3 * (bmp->w+4)/4*4 * bmp->h;
head.hres = 72;
head.vres = 72;
head.colors = 0;
head.important = 0;
fwrite( &head, 13, 4, b );
for( int y = bmp->h - 1; y >= 0; --y ){
int x;
for( x = 0; x < bmp->w; ++x ){
fputc( bmp->data[x + y * bmp->w].r, b );
fputc( bmp->data[x + y * bmp->w].g, b );
fputc( bmp->data[x + y * bmp->w].b, b );
}
x*=3;
while( x % 4 != 0 ){
++x;
fputc(0, b);
}
}
fclose( b );
}
color color_mix( color a, color b, int offset ){ /*offset is a value between 0 and 255 to determine the weight. the lower it is the more color a gets*/
//if( offset > 255 || offset < 0)
//printf("%i\t", offset);
a.r += ( b.r - a.r ) * offset / 255;
a.g += ( b.g - a.g ) * offset / 255;
a.b += ( b.b - a.b ) * offset / 255;
return a;
}
r_bitmap* bmp_to_r( bitmap* b ){
r_bitmap* r = malloc( sizeof( r_bitmap ) );
r->t = 1;
int radius = sqrt( b->w * b->w + b->h * b->h ) / 2 * C_R / C_M + 2;
int step = C_S * ( b->w + b->h ) / C_M;
r->data = malloc( radius * step * sizeof( color ) );
r->r = radius;
r->s = step;
r->w = b->w;
r->h = b->h;
color black = {0, 0, 0};
for( double i = 0; i < radius; ++ i ){
for( double j = 0; j < step; ++j ){
double x = i * C_M * cos( 2 * PI * j / step ) / C_R + b->w / 2;
double y = i * C_M * sin( 2 * PI * j / step ) / C_R + b->h / 2;
int ix = x;
int iy = y;
if( x < 0 || x >= b->w || y < 0 || y >= b->h )
r->data[(int)(j + i * step)] = black;
else{
color tmp = b->data[ix + iy * b->w];
if( iy < b->h - 1 ){
int off = 255 * (y - iy);
tmp = color_mix( tmp, b->data[ix + (iy+1) * b->w], off );
}
if( ix < b->w - 1 ){
int off = 255 * ( x - ix );
tmp = color_mix( tmp, b->data[ix +1 + iy * b->w], off );
}
r->data[(int)(j + i * step)] = tmp;
}
}
}
return r;
}
bitmap* bmp_from_r( r_bitmap* r ){
bitmap* b = malloc( sizeof( bitmap ) );
b->t = 0;
b->data = malloc( r->w * r->h * sizeof( color ) );
b->w = r->w;
b->h = r->h;
for( int y = 0; y < b->h; ++y ){
for( int x = 0; x < b->w; ++x ){
int tx = x - b->w/2;
int ty = y - b->h/2;
double rad = sqrt( tx*tx+ty*ty ) * C_R / C_M;
double s = atan2( ty, tx );
if( s < 0 ) s += 2 * PI;
s *= r->s / ( 2 * PI );
int is = s;
int irad = rad;
color tmp = r->data[(int)(is + irad * r->s)];
/*if( x > 0 && x < r->w - 1 && y > 0 && y < r->h - 1 ){
tmp = color_mix(tmp, r->data[((int)(is+1)%r->s + irad * r->s)], abs(255* rad - floor(rad)));
tmp = color_mix(tmp, r->data[(is + (irad + 1) * r->s)], abs(255* s - floor(s)));
}*/
b->data[x+y*b->w] = tmp;
}
}
return b;
}
int main( ) {
bitmap* b = bmp_load_from_file( "foo.bmp" );
r_bitmap* r = bmp_to_r( b );
bitmap* c = bmp_from_r( r );
bmp_save( "lol.bmp", c );
}

Resources