how do I write a code that´s applying letters to numbers and prints names into a sum of numbers? - numbers

For example A = 1 , Z = 26 , Aron = 48
Non English-letter character like space or apostrophe can be ignored
Lowercase and uppercase letters are equal
I am new to programming and try some practices with java in order to learn from this.

Related

How to check is the text is in right format? Flutter

I'm having a condition as the particular text must be in the particular format where in I'm getting the text from the scanner and need to check if it is in the right format.
The format comes like
The starting letter must start with e or E, next letter might be any letter from a to z or A to Z alphabets, next 9 characteres must be numbers and the last two characters must be from anything within a to z or A to Z alphabets
I tried something like
if (_scannedCode.startsWith('e|E') && _scannedCode[1].startsWith('a-zA-Z') && _scannedCode.substring(2, 10))
but got struck.
Seeing the answers did get the condtions correctly but was struck up with one, so just wanted to get a clarification if its right or not
RegExp emo = new RegExp(r'[0-9]{6}(EM|em|eM|Em){2}[0-9]{10}$');
As i needed the first 6 characters to be numbers the next two characters be alphabet(em) and the remaining 10 characters be numbers.
final regex = RegExp(r'(e|E)[a-zA-z]\d{9}[a-zA-z]{2}');
if (_scannedCode.length == 13 && regex.hasMatch(_scannedCode) ) {
// your code
}

Matlab: Function that returns a string with the first n characters of the alphabet

I'd like to have a function generate(n) that generates the first n lowercase characters of the alphabet appended in a string (therefore: 1<=n<=26)
For example:
generate(3) --> 'abc'
generate(5) --> 'abcde'
generate(9) --> 'abcdefghi'
I'm new to Matlab and I'd be happy if someone could show me an approach of how to write the function. For sure this will involve doing arithmetic with the ASCII-codes of the characters - but I've no idea how to do this and which types that Matlab provides to do this.
I would rely on ASCII codes for this. You can convert an integer to a character using char.
So for example if we want an "e", we could look up the ASCII code for "e" (101) and write:
char(101)
'e'
This also works for arrays:
char([101, 102])
'ef'
The nice thing in your case is that in ASCII, the lowercase letters are all the numbers between 97 ("a") and 122 ("z"). Thus the following code works by taking ASCII "a" (97) and creating an array of length n starting at 97. These numbers are then converted using char to strings. As an added bonus, the version below ensures that the array can only go to 122 (ASCII for "z").
function output = generate(n)
output = char(97:min(96 + n, 122));
end
Note: For the upper limit we use 96 + n because if n were 1, then we want 97:97 rather than 97:98 as the second would return "ab". This could be written as 97:(97 + n - 1) but the way I've written it, I've simply pulled the "-1" into the constant.
You could also make this a simple anonymous function.
generate = #(n)char(97:min(96 + n, 122));
generate(3)
'abc'
To write the most portable and robust code, I would probably not want those hard-coded ASCII codes, so I would use something like the following:
output = 'a':char(min('a' + n - 1, 'z'));
...or, you can just generate the entire alphabet and take the part you want:
function str = generate(n)
alphabet = 'a':'z';
str = alphabet(1:n);
end
Note that this will fail with an index out of bounds error for n > 26, so you might want to check for that.
You can use the char built-in function which converts an interger value (or array) into a character array.
EDIT
Bug fixed (ref. Suever's comment)
function [str]=generate(n)
a=97;
% str=char(a:a+n)
str=char(a:a+n-1)
Hope this helps.
Qapla'

Unicode character transformation in SPSS

I have a string variable. I need to convert all non-digit characters to spaces (" "). I have a problem with unicode characters. Unicode characters (the characters outside the basic charset) are converted to some invalid characters. See the code for example.
Is there any other way how to achieve the same result with procedure which would not choke on special unicode characters?
new file.
set unicode = yes.
show unicode.
data list free
/T (a10).
begin data
1234
5678
absd
12as
12(a
12(vi
12(vī
12āčž
end data.
string Z (a10).
comp Z = T.
loop #k = 1 to char.len(Z).
if ~range(char.sub(Z, #k, 1), "0", "9") sub(Z, #k, 1) = " ".
end loop.
comp Z = normalize(Z).
comp len = char.len(Z).
list var = all.
exe.
The result:
T Z len
1234 1234 4
5678 5678 4
absd 0
12as 12 2
12(a 12 2
12(vi 12 2
12(vī 12 � 6
>Warning # 649
>The first argument to the CHAR.SUBSTR function contains invalid characters.
>Command line: 1939 Current case: 8 Current splitfile group: 1
12āčž 12 �ž 7
Number of cases read: 8 Number of cases listed: 8
The substr function should not be used on the left hand side of an expression in Unicode mode, because the replacement character may not be the same number of bytes as the character(s) being replaced. Instead, use the replace function on the right hand side.
The corrupted characters you are seeing are due to this size mismatch.
How about instead of replacing non-numeric characters, you cycle though and pull out the numeric characters and rebuild Z? (Note my version here is pre CHAR. string functions.)
data list free
/T (a10).
begin data
1234
5678
absd
12as
12(a
12(vi
12(vī
12āčž
12as23
end data.
STRING Z (a10).
STRING #temp (A1).
COMPUTE #len = LENGTH(RTRIM(T)).
LOOP #i = 1 to #len.
COMPUTE #temp = SUBSTR(T,#i,1).
DO IF INDEX('0123456789',#temp) > 0.
COMPUTE Z = CONCAT(SUBSTR(Z,1,#i-1),#temp).
ELSE.
COMPUTE Z = CONCAT(SUBSTR(Z,1,#i-1)," ").
END IF.
END LOOP.
EXECUTE.

Algorithm for finding all possible key combinations in given range

Last time I got curious about how long would it take to break my password using brute force attack. I'd like to check it.
So, how should I implement algorithm to find all possible key combinations in given range (for eg. 15 letters)? I found algorithms for permutations around but they all swap letters for given word, it's not what I'm looking for.
Assuming that passwords can consist of combinations of 89 possible characters (a-z, A-z, 0-9, space, and all the different symbol keys on a Windows keyboard), a there there are 82 the the 15th power different combinations of 15 characters (82 * 82 * 82 ... ). In other words, a lot.
If you want to use just letters, and you differentiate between upper and lower case, there would be 52 ** 15 possible 15-letter combinations. If you want to take in the possibility of shorter strings as well you could write something like (pseudocode):
long combos = 0
for i = 6 TO 20 -- legal password lengths
combos = combos + POW(52, i)
print "there are " + combos.ToString()
+ " possible passwords between 6 and 20 characters"
To actually enumerate and print the permutations in C# you could do:
void AddNextCharAndPrintIfDone(string pwd, int maxLen)
{
for (char c = 'a'; c < 'Z'; c++)
{
pwd = pwd + c;
if (pwd.Length >= maxLen)
System.Console.WriteLine(pwd);
else AddNextCharAndPrintIfDone(pwd, maxLen)
}
}
Main()
{
for (int i=6; i < 20; i++)
AddNextCharAndPrintIfDone("", i);
}
Not really written for efficiency, but if you have enough memory and time, you'll get every possible permutation.
You can download php pear project math combinatoric to generate those passwords.

Convert numbers 1-26 to A-Z?

How can I convert the numbers in the range 1 through 26 to their respective letter position in the alphabet?
1 = A
2 = B
...
26 = Z
CHR(#) will give you the ASCII character, you just need to offset it based on the ASCII table:
e.g. A = 65, so you will need to add 64 to 1:
CHR(64 + #) = A if # is 1
ASCII code is the numerical representation of a character such as 'a' or 'Z'. Therefore by looking at the table one can see that capital A has a value of 65 and Z has a value of 90. Adding 64 from each value in the range 1-26 will give you their corresponding letter.