hungarian BBAN validation - iban

can anybody tell me how to validate Hungarian BBAN account numbres ?
on the internet i have only found that it is 24 numbers length
And in format
bbbs sssk cccc cccc cccc cccx
b = National bank code
s = Branch code
c = Account number
x = National check digit
but how to calculate x = National check digit ?
I have tried to remove last char and modulo rest by 97 but it does not work
(the result is not 1 for valid account numbers)
thanks in advance for any help

I just finished Hungarian account validation function. This is the first version of this function but it's work well.
public string sprawdzWegierskitempAccountNumber(string _accountNumberToCheck, bool _iban) //if iban is true then function result will be always IBAN (even if _accountNumberToCheck will be BBAN)
{
string _accountNumberCorrected = _accountNumberToCheck.Replace(" ", "");
_accountNumberCorrected = _accountNumberCorrected.Replace("-", "");
_accountNumberCorrected = _accountNumberCorrected.Replace("//", "");
_accountNumberCorrected = _accountNumberCorrected.Replace("\", "");
string _accountNumberCorrectedFirst = _accountNumberCorrected;
if (_accountNumberCorrected.Length == 16 || _accountNumberCorrected.Length == 24 || _accountNumberCorrected.Length == 28)
{
if (_accountNumberCorrected.Length == 28) //IBAN Account number
{
_accountNumberCorrected = _accountNumberCorrected.Substring(4, _accountNumberCorrected.Length - 4); //we don't need first four digits (HUxx)
}
string digitToMultiply = "9731";
int checkResult = 0;
//checking first part of account number
for (int i = 0; i <= 6; i++)
{
checkResult = checkResult + int.Parse(_accountNumberCorrected.ToCharArray()[i].ToString()) * int.Parse(digitToMultiply.ToCharArray()[i % 4].ToString());
}
checkResult = checkResult % 10;
checkResult = 10 - checkResult;
if (checkResult == 10)
{
checkResult = 0;
}
if (checkResult.ToString() != _accountNumberCorrected.ToCharArray()[7].ToString())
{
throw new Exception("Wrong account number");
}
else
{
//if first part it's ok then checking second part of account number
checkResult = 0;
for (int i = 8; i <= _accountNumberCorrected.Length-2; i++)
{
checkResult = checkResult + int.Parse(_accountNumberCorrected.ToCharArray()[i].ToString()) * int.Parse(digitToMultiply.ToCharArray()[i % 4].ToString());
}
checkResult = checkResult % 10;
checkResult = 10 - checkResult;
if (checkResult == 10)
{
checkResult = 0;
}
if (checkResult.ToString() != _accountNumberCorrected.ToCharArray()[_accountNumberCorrected.Length-1].ToString())
{
throw new Exception("Wrong account number");
}
}
string tempAccountNumber = _accountNumberCorrected + "173000";
var db = 0; var iban = 0;
var maradek = 0;
string resz = "", ibanstr = "", result = "";
while (true)
{
if (db == 0)
{
resz = tempAccountNumber.Substring(0, 9);
tempAccountNumber = tempAccountNumber.Substring(9, (tempAccountNumber.Length - 9));
}
else
{
resz = maradek.ToString();
resz = resz + tempAccountNumber.Substring(0, (9 - db));
tempAccountNumber = tempAccountNumber.Substring((9 - db), (tempAccountNumber.Length - 9 + db));
}
maradek = int.Parse(resz) % 97;
if (maradek == 0)
db = 0;
else
if (maradek < 10)
db = 1;
else
db = 2;
if ((tempAccountNumber.Length + db) <= 9)
break;
}
if (maradek != 0)
{
resz = maradek.ToString();
resz = resz + tempAccountNumber;
}
else
resz = tempAccountNumber;
maradek = int.Parse(resz) % 97; ;
iban = 98 - maradek;
if (iban < 10)
ibanstr = "0" + iban.ToString();
else ibanstr = iban.ToString();
if (_accountNumberCorrected.Length == 16)
{
_accountNumberCorrected = _accountNumberCorrected + "00000000";
_accountNumberCorrectedFirst = _accountNumberCorrectedFirst + "00000000";
}
if (_iban)
{
result = "HU" + ibanstr + _accountNumberCorrected;
}
else
{
result = _accountNumberCorrectedFirst;
}
return result;
}
else
{
throw new Exception("Wrong length of account number");
}
}

Related

How to escape HTML special characters in Solidity?

I am trying the approach below. It is based on some of Uniswap's code.
I am curious as to whether it will work at reasonable cost or whether there is a better way.
function escapeHTML(string memory input)
public
pure
returns (string memory)
{
bytes memory inputBytes = bytes(input);
uint extraCharsNeeded = 0;
for (uint i = 0; i < inputBytes.length; i++) {
bytes1 currentByte = inputBytes[i];
if (currentByte == "&") {
extraCharsNeeded += 4;
} else if (currentByte == "<") {
extraCharsNeeded += 3;
} else if (currentByte == ">") {
extraCharsNeeded += 3;
}
}
if (extraCharsNeeded > 0) {
bytes memory escapedBytes = new bytes(
inputBytes.length + extraCharsNeeded
);
uint256 index;
for (uint i = 0; i < inputBytes.length; i++) {
if (inputBytes[i] == "&") {
escapedBytes[index++] = "&";
escapedBytes[index++] = "a";
escapedBytes[index++] = "m";
escapedBytes[index++] = "p";
escapedBytes[index++] = ";";
} else if (inputBytes[i] == "<") {
escapedBytes[index++] = "&";
escapedBytes[index++] = "l";
escapedBytes[index++] = "t";
escapedBytes[index++] = ";";
} else if (inputBytes[i] == ">") {
escapedBytes[index++] = "&";
escapedBytes[index++] = "g";
escapedBytes[index++] = "t";
escapedBytes[index++] = ";";
} else {
escapedBytes[index++] = inputBytes[i];
}
}
return string(escapedBytes);
}
return input;
}

Google Code Jam: My solution to Train Timetable problem is failing

I'm trying to solve this problem from Google's Code Jam 2008:
The problem is called Train Timetable and you can find the full explanation here:
Code Jam - Train Timetable
Note: I've decided to solve the problem with Node.js.
My code is the next:
function timeToMinutes(time) {
const timeArray = time.split(":");
const hours = parseInt(timeArray[0]);
const minutes = parseInt(timeArray[1]);
const hoursInMinutes = hours * 60;
const total = hoursInMinutes + minutes;
return total;
}
function timetableFiller(NAB, NBA, array) {
let timetable = {
departuresFromA: [],
arrivalsToB: [],
departuresFromB: [],
arrivalsToA: [],
};
for (let i = 0; i < NAB + NBA; i++) {
let tempArr = [];
tempArr = array[i].split(" ");
if (i < NAB) {
timetable.departuresFromA.push(tempArr[0]);
timetable.arrivalsToB.push(tempArr[1]);
} else {
timetable.departuresFromB.push(tempArr[0]);
timetable.arrivalsToA.push(tempArr[1]);
}
}
return timetable;
}
function timetableToMinutes(timetable) {
let timetableMinutes = {
departuresFromA: [],
arrivalsToB: [],
departuresFromB: [],
arrivalsToA: [],
};
for (const property in timetable) {
timetable[property].map((element) =>
timetableMinutes[property].push(timeToMinutes(element))
);
}
return timetableMinutes;
}
function trainsNeededCounter(arrivalsFromDestiny, departuresFromOrigin, tat) {
let trainsNeeded = departuresFromOrigin.length;
for (let i = 0; i < arrivalsFromDestiny.length; i++) {
for (let j = 0; j < departuresFromOrigin.length; j++) {
if (arrivalsFromDestiny[i] + tat <= departuresFromOrigin[j]) {
trainsNeeded = trainsNeeded - 1;
departuresFromOrigin.splice(j, 1);
}
}
}
return trainsNeeded;
}
function responseGenerator(inputA, inputB, caseNumber) {
return `Case #${caseNumber}: ${inputA} ${inputB}`;
}
function problemSolution(input) {
const numberOfCases = parseInt(input[0]);
input.shift();
let response = [];
let caseNumber = 0;
let NAB;
let NBA;
for (let i = 0; i < input.length; i = i + NAB + NBA + 2) {
caseNumber = caseNumber + 1;
const tat = parseInt(input[i]);
const arrayNTrips = input[i + 1].split(" ");
NAB = parseInt(arrayNTrips[0]);
NBA = parseInt(arrayNTrips[1]);
const arraySchedule = input.slice(i + 2, i + 2 + NAB + NBA);
const timetable = timetableFiller(NAB, NBA, arraySchedule);
const timetableMinutes = timetableToMinutes(timetable);
const trainsNeededAB = trainsNeededCounter(
timetableMinutes.arrivalsToA,
timetableMinutes.departuresFromA,
tat
);
const trainsNeededBA = trainsNeededCounter(
timetableMinutes.arrivalsToB,
timetableMinutes.departuresFromB,
tat
);
response.push(
responseGenerator(trainsNeededAB, trainsNeededBA, caseNumber)
);
}
return response;
}
function readInput() {
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false,
});
let problem = [];
rl.on("line", (line) => {
problem.push(line);
}).on("close", () => {
const solution = problemSolution(problem);
solution.map((response) => console.log(response));
});
}
readInput();
How to replicate the issue
You should login into Code Jam with your Google account.
Paste into the code area on the right side and activate the Test run mode.
As input you can copy paste the sample input provided in the problem and you can see that the output is exactly as the sample output.
I've tried with my own variations of the input and the responses seems correct but when I run the real attempt the platform says WA or Wrong Answer.
Thank you so much for your help!
I made a video about this recently. You should check it out.
I think you can understand the logic flow from it. We are both doing the same thing basically.
https://youtu.be/_Cp51vMDZAs
-check this out
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
void solve(int t)
{
int NA, NB;
float T;
cin >> T >> NA >> NB;
cin.ignore();
vector<string> ASchedule, BSchedule;
if (NA > 0)
for (int i = 0; i < NA; i++)
{
string s;
getline(cin, s);
ASchedule.push_back(s);
}
if (NB > 0)
for (int i = 0; i < NB; i++)
{
string s;
getline(cin, s);
BSchedule.push_back(s);
}
int alength, blength;
alength = (int)ASchedule.size();
blength = (int)BSchedule.size();
if (alength == 0 || blength == 0)
{
cout << "Case #" << t << ": " << alength << " " << blength << endl;
return;
}
float TT = T / 10;
string val, value;
int d;
float ADH, ADM, AAH, AAM, BDH, BDM, BAH, BAM;
vector<float> AD, AA, BD, BA;
for (int i = 0; i < alength; i++)
{
val = ASchedule[i];
ADH = stof(val.substr(0, 2));
AAH = stof(val.substr(6, 2));
ADM = stof(val.substr(3, 2));
AAM = stof(val.substr(9, 2));
if (val.at(9) == '0')
{
AAM /= 10;
AAM += TT;
AAM *= 10;
}
else
AAM += T;
if (AAM > 59)
{
d = -1;
while (AAM != 59)
{
AAM -= 1;
d++;
}
AAH++;
AAM = 0;
AAM += d;
}
// if (ADH > 23)
// ADH = 0;
// if (AAH > 23)
// AAH = 0;
ADM /= 100;
ADH += ADM;
AAM /= 100;
AAH += AAM;
AD.push_back(ADH);
AA.push_back(AAH);
}
for (int j = 0; j < blength; j++)
{
value = BSchedule[j];
BDH = stof(value.substr(0, 2));
BDM = stof(value.substr(3, 2));
BAH = stof(value.substr(6, 2));
BAM = stof(value.substr(9, 2));
if (value.at(9) == '0')
{
BAM /= 10;
BAM += TT;
BAM *= 10;
}
else
BAM += T;
if (BAM > 59)
{
d = -1;
while (BAM != 59)
{
BAM -= 1;
d++;
}
BAH++;
BAM = 0;
BAM += d;
}
// if (BDH > 23)
// BDH = 0;
// if (BAH > 23)
// BAH = 0;
BDM /= 100;
BDH += BDM;
BAM /= 100;
BAH += BAM;
BA.push_back(BAH);
BD.push_back(BDH);
}
int no1 = alength, no2 = blength;
sort(BD.begin(), BD.end());
sort(BA.begin(), BA.end());
sort(AA.begin(), AA.end());
sort(AD.begin(), AD.end());
for (int i = 0; i < alength; i++)
for (int j = 0; j < blength; j++)
if (AD[i] >= BA[j])
{
no1--;
BA[j] = 50;
break;
}
for (int i = 0; i < blength; i++)
for (int j = 0; j < alength; j++)
if (AA[j] <= BD[i])
{
no2--;
AA[j] = 50;
break;
}
cout << "Case #" << t << ": " << no1 << " " << no2 << endl;
}
int main()
{
int N;
cin >> N;
cin.ignore();
for (int t = 1; t <= N; t++)
solve(t);
}

Encode string "aaa" to "3[a]"

give a string s, encode it by the format: "aaa" to "3[a]". The length of encoded string should the shortest.
example: "abbabb" to "2[a2[b]]"
update: suppose the string only contains lowercase letters
update: here is my code in c++, but it's slow. I know one of the improvement is using KMP to compute if the current string is combined by a repeat string.
// this function is used to check if a string is combined by repeating a substring.
// Also Here can be replaced by doing KMP algorithm for whole string to improvement
bool checkRepeating(string& s, int l, int r, int start, int end){
if((end-start+1)%(r-l+1) != 0)
return false;
int len = r-l+1;
bool res = true;
for(int i=start; i<=end; i++){
if(s[(i-start)%len+l] != s[i]){
res = false;
break;
}
}
return res;
}
// this function is used to get the length of the current number
int getLength(int l1, int l2){
return (int)(log10(l2/l1+1)+1);
}
string shortestEncodeString(string s){
int len = s.length();
vector< vector<int> > res(len, vector<int>(len, 0));
//Initial the matrix
for(int i=0; i<len; i++){
for(int j=0; j<=i; j++){
res[j][i] = i-j+1;
}
}
unordered_map<string, string> record;
for(int i=0; i<len; i++){
for(int j=i; j>=0; j--){
string temp = s.substr(j, i-j+1);
/* if the current substring has showed before, then no need to compute again
* Here is a example for this part: if the string is "abcabc".
* if we see the second "abc", then no need to compute again, just use the
* result from first "abc".
**/
if(record.find(temp) != record.end()){
res[j][i] = record[temp].size();
continue;
}
string ans = temp;
for(int k=j; k<i; k++){
string str1 = s.substr(j, k-j+1);
string str2 = s.substr(k+1, i-k);
if(res[j][i] > res[j][k] + res[k+1][i]){
res[j][i] = res[j][k]+res[k+1][i];
ans = record[str1] + record[str2];
}
if(checkRepeating(s, j, k, k+1, i) == true && res[j][i] > 2+getLength(k-j+1, i-k)+res[j][k]){
res[j][i] = 2+getLength(k-j+1, i-k)+res[j][k];
ans = to_string((i-j+1)/(k-j+1)) + '[' + record[str1] +']';
}
}
record[temp] = ans;
}
}
return record[s];
}
With very little to start with in terms of a question statement, I took a quick stab at this using JavaScript because it's easy to demonstrate. The comments are in the code, but basically there are alternating stages of joining adjacent elements, run-length checking, joining adjacent elements, and on and on until there is only one element left - the final encoded value.
I hope this helps.
function encode(str) {
var tmp = str.split('');
var arr = [];
do {
if (tmp.length === arr.length) {
// Join adjacent elements
arr.length = 0;
for (var i = 0; i < tmp.length; i += 2) {
if (i < tmp.length - 1) {
arr.push(tmp[i] + tmp[i + 1]);
} else {
arr.push(tmp[i]);
}
}
tmp.length = 0;
} else {
// Swap arrays and clear tmp
arr = tmp.slice();
tmp.length = 0;
}
// Build up the run-length strings
for (var i = 0; i < arr.length;) {
var runlength = runLength(arr, i);
if (runlength > 1) {
tmp.push(runlength + '[' + arr[i] + ']');
} else {
tmp.push(arr[i]);
}
i += runlength;
}
console.log(tmp);
} while (tmp.length > 1);
return tmp.join();
}
// Get the longest run length from a given index
function runLength(arr, ind) {
var count = 1;
for (var i = ind; i < arr.length - 1; i++) {
if (arr[i + 1] === arr[ind]) {
count++;
} else {
break;
}
}
return count;
}
<input id='inp' value='abbabb'>
<button type="submit" onClick='javascript:document.getElementById("result").value=encode(document.getElementById("inp").value)'>Encode</button>
<br>
<input id='result' value='2[a2[b]]'>

sum and substract using char and string only

I have to make a code that sums and subtracts two or more numbers using the + and - chars
I managed to make the sum, but I have no idea on how to make it subtract.
Here is the code (I'm allowed to use the for and while loops only):
int CM = 0, CR = 0, A = 0, PS = 0, PR = 0, LC = 0, D;
char Q, Q1;
String f, S1;
f = caja1.getText();
LC = f.length();
for (int i = 0; i < LC; i++) {
Q = f.charAt(i);
if (Q == '+') {
CM = CM + 1;
} else if (Q == '-') {
CR = CR + 1;
}
}
while (CM > 0 || CM > 0) {
LC = f.length();
for (int i = 0; i < LC; i++) {
Q = f.charAt(i);
if (Q == '+') {
PS = i;
break;
} else {
if (Q == '-') {
PR = i;
break;
}
}
}
S1 = f.substring(0, PS);
D = Integer.parseInt(S1);
A = A + D;
f = f.substring(PS + 1);
CM = CM - 1;
}
D = Integer.parseInt(f);
A = A + D;
salida.setText("resultado" + " " + A + " " + CR + " " + PR + " " + PS);
The following program will solve your problem of performing addition and subtraction in a given equation in String
This sample program is given in java
public class StringAddSub {
public static void main(String[] args) {
//String equation = "100+500-20-80+600+100-50+50";
//String equation = "100";
//String equation = "500-900";
String equation = "800+400";
/** The number fetched from equation on iteration*/
String b = "";
/** The result */
String result = "";
/** Arithmetic operation to be performed */
String previousOperation = "+";
for (int i = 0; i < equation.length(); i++) {
if (equation.charAt(i) == '+') {
result = performOperation(result, b, previousOperation);
previousOperation = "+";
b = "";
} else if (equation.charAt(i) == '-') {
result = performOperation(result, b, previousOperation);
previousOperation = "-";
b = "";
} else {
b = b + equation.charAt(i);
}
}
result = performOperation(result, b, previousOperation);
System.out.println("Print Result : " + result);
}
public static String performOperation(String a, String b, String operation) {
int a1 = 0, b1 = 0, res = 0;
if (a != null && !a.equals("")) {
a1 = Integer.parseInt(a);
}
if (b != null && !b.equals("")) {
b1 = Integer.parseInt(b);
}
if (operation.equals("+")) {
res = a1 + b1;
}
if (operation.equals("-")) {
res = a1 - b1;
}
return String.valueOf(res);
}
}

Text version compare in FCKEditor

Am using Fck editor to write content. Am storing the text as versions in db. I want to highlight those changes in versions when loading the text in FCK Editor.
How to compare the text....
How to show any text that has been deleted in strike through mode.
Please help me/...
Try google's diff-patch algorithm http://code.google.com/p/google-diff-match-patch/
Take both previous and current version of the text and store it into two parameters. Pass the two parameters to the following function.
function diffString(o, n) {
o = o.replace(/<[^<|>]+?>| /gi, '');
n = n.replace(/<[^<|>]+?>| /gi, '');
var out = diff(o == "" ? [] : o.split(/\s+/), n == "" ? [] : n.split(/\s+/));
var str = "";
var oSpace = o.match(/\s+/g);
if (oSpace == null) {
oSpace = ["\n"];
} else {
oSpace.push("\n");
}
var nSpace = n.match(/\s+/g);
if (nSpace == null) {
nSpace = ["\n"];
} else {
nSpace.push("\n");
}
if (out.n.length == 0) {
for (var i = 0; i < out.o.length; i++) {
str += '<span style="background-color:#F00;"><del>' + escape(out.o[i]) + oSpace[i] + "</del></span>";
}
} else {
if (out.n[0].text == null) {
for (n = 0; n < out.o.length && out.o[n].text == null; n++) {
str += '<span style="background-color:#F00;"><del>' + escape(out.o[n]) + oSpace[n] + "</del></span>";
}
}
for (var i = 0; i < out.n.length; i++) {
if (out.n[i].text == null) {
str += '<span style="background-color:#0C0;"><ins>' + escape(out.n[i]) + nSpace[i] + "</ins></span>";
} else {
var pre = "";
for (n = out.n[i].row + 1; n < out.o.length && out.o[n].text == null; n++) {
pre += '<span style="background-color:#F00;"><del>' + escape(out.o[n]) + oSpace[n] + "</del></span>";
}
str += " " + out.n[i].text + nSpace[i] + pre;
}
}
}
return str;
}
this returns an html in which new text is marked green and deleted text as red + striked out.

Resources