How to display last 4 numbers in an int? - jsf

i have an int field that i want to display its last 4 numbers in jsf page, please advise how to accomplish that.

You can use the modulo operation for that, this is also supported in EL using the modulus operator %.
<p>Last 4 numbers: #{bean.number % 10000}</p>

If you need the last four digits of a number, use the modulus operator:
int someInteger = 23421984;
int last4digits = someInteger % 10000;
// last4digits = 1948;

Related

convert string to list of int in kotlin

I have a string = "1337" and I want to convert it to a list of Int, I tried to get every element in the string and convert it to Int like this string[0].toInt but I didn't get the number I get the Ascii value, I can do it with this Character.getNumericValue(number), How I do it without using a built it function? with good complexity?
What do you mean "without using a built in function"?
string[0].toInt gives you the ASCII value of the character because the fun get(index: Int) on String has a return type of Char, and a Char behaves closer to a Number than a String. "0".toInt() == 0 will yield true, but '0'.toInt() == 0 will yield false. The difference being the first one is a string and the second is a character.
A oneliner
string.split("").filterNot { it.isBlank() }.map { it.toInt() }
Explanation: split("") will take the string and give you a list of every character as a string, however, it will give you an empty string at the beginning, which is why we have filterNot { it.isBlank() }, we then can use map to transform every string in our list to Int
If you want something less functional and more imperative that doesn't make use of functions to convert there is this
val ints = mutableListOf<Int>() //make a list to store the values in
for (c: Char in "1234") { //go through all of the characters in the string
val numericValue = c - '0' //subtract the character '0' from the character we are looking at
ints.add(numericValue) //add the Int to the list
}
The reason why c - '0' works is because the ASCII values for the digits are all in numerical order starting with 0, and when we subtract one character from another, we get the difference between their ASCII values.
This will give you some funky results if you give it a string that doesn't have only digits in it, but it will not throw any exceptions.
As in Java and by converting Char to Int you get the ascii equivalence.
You can instead:
val values = "1337".map { it.toString().toInt() }
println(values[0]) // 1
println(values[1]) // 3
// ...
Maybe like this? No-digits are filtered out. The digits are then converted into integers:
val string = "1337"
val xs = string.filter{ it.isDigit() }.map{ it.digitToInt() }
Requires Kotlin 1.4.30 or higher and this option:
#OptIn(ExperimentalStdlibApi::class)

Convert string S to another string T by performing exactly K operations (append to / delete from the end of the string S)

I am trying to solve a problem. But I am missing some corner case. Please help me. The problem statement is:
You have a string, S , of lowercase English alphabetic letters. You can perform two types of operations on S:
Append a lowercase English alphabetic letter to the end of the string.
Delete the last character in the string. Performing this operation on an empty string results in an empty string.
Given an integer, k, and two strings, s and t , determine whether or not you can convert s to t by performing exactly k of the above operations on s.
If it's possible, print Yes; otherwise, print No.
Examples
Input Output
hackerhappy Yes
hackerrank
9
5 delete operations (h,a,p,p,y) and 4 append operations (r,a,n,k)
aba Yes
aba
7
4 delete operations (delete on empty = empty) and 3 append operations
I tried in this way (C language):
int sl = strlen(s); int tl = strlen(t); int diffi=0;
int i;
for(i=0;s[i]&&t[i]&&s[i]==t[i];i++); //going till matching
diffi=i;
((sl-diffi+tl-diffi<=k)||(sl+tl<=k))?printf("Yes"):printf("No");
Please help me to solve this.
Thank You
You also need the remaining operations to divide in 2, because you need to just add and remove letters to waste the operations.
so maybe:
// c language - strcmp(s,t) returns 0 if s==t.
if(strcmp(s,t))
((sl-diffi+tl-diffi<=k && (k-(sl-diffi+tl-diffi))%2==0)||(sl+tl<=k))?printf("Yes"):printf("No");
else
if(sl+tl<=k||k%2==0) printf("Yes"); else printf("No");
You can do it one more way using binary search.
Take the string of smaller length and take sub-string(pattern) of length/2.
1.Do a binary search(by character) on both of the string if u get a match append length/4 more character to the pattern if it matches add more by length/2^n else append one character to the original(pattern of length/2) and try .
2.If u get a mismatch for pattern of length/2 reduce length of the pattern to length/4 and if u get a match append next character .
Now repeat the steps 1 and 2
If n1+n2 <= k then the answer is Yes
else the answer is no
Example:
s1=Hackerhappy
s2=Hackerrank
pattern=Hacker // length = 10 (s2 is smaller and length of s2=10 length/2 =5)
//Do a binary search of the pattern you will get a match by steps 1 and 2
n1 number of mismatched characters is 5
n2 number of mismatched characters is 4
Now n1+n2<k // its because we will need to do these much operation to make these to equal.
So Yes
This should work for all cases:
int sl = strlen(s); int tl = strlen(t); int diffi=0;
int i,m;
for(i=0;s[i]&&t[i]&&s[i]==t[i];i++); //going till matching
diffi=i;
m = sl+tl-2*diffi;
((k>=m&&(k-m)%2==0)||(sl+tl<=k))?printf("Yes"):printf("No");

Always find the middle of a string with elements grouped by 3 characters

I've a series of strings that represent airline's itineraries:
FLROTP
MADFCOFCOFLR
BLQMADMADUIOUIOMADMADBLQ
MXPJFKJFKMCOJFKMXP
WAWPSAPSAWAW
FLRFRAFRASGNSGNBKKBKKVIEVIEFLR
FLRMUCMUCDELDXBDXBZRHZRHFLR
FLRFRAFRASINSINMELMELSINSINFRAFRAFLR
FLRCDGCDGCANCANJJNZHACANCANCDGCDGFLRWNZCANCANZHAHKGAMSFLR
JFKMTYMTYMEXMEXPTYMDEMDEBOGBOGLIM
PSAISTISTICNICNNRTNRTISTISTPSANRTISTISTPSA
MXPDXBDXBPERPERADLADLMELMELASPASPAYQAYQASPASPSYDSYDDXBDXBMXP
FLRFRAFRAORDORDLASLASBNACLTCLTMUCMUCPSA
FLRCDGCDGBOGBOGBAQBAQBOGBOGCUCCUCBOGBOGMDEMDEBOGBOGUIOGYELIMLIMHAVHAVCDGCDGFLR
FLRFRAFRALAXLAXSEASEAORDORDICTICTORDORDCMHCMHBOSBOSMIAMIAFRAFRAFLR
PSAMUCMUCIADIADGSOGSOCLTCLTMIAMIADFWDFWICTICTDFWDFWCMHCMHPHLPHLALBALBIADIADFRAFRAFLR
FLRFRAFRAEZEEZESCLSCLGRUCGHSDUSDUPOAPOAGRUGRULIMLIMUIOUIOBOGBOGPTYPTYPOSPOSMIAMIAFRAFRAFLR
PSACDGCDGHAVHAVPTYPTYUIOUIOMDEMDEBOGBOGBAQBAQBOGBOGCUCCUCBOGBOGCDGCDGFLR
FLRCDGCDGMEXMEXSJOSJOMEXBJXBJXMEXMEXCDGCDGPSA
I'd like to always be able to find the "middle" of the string (that 90% of the cases is the passenger's destination) but i'm short of ideas. Any help? :)
What you want is not the index at the exact middle of the string, but the closest index to the middle that is a multiple of 3, to index the start of a valid 3-letter code.
You didn't specify a language so I'll just use C++ to illustrate.
std::string code = "MXPJFKJFKMCOJFKMXP";
Find the length of the string:
int length = code.size();
Count how many codes you have:
int codecount = length / 3;
Find the middle code, using integer arithmetic (rounding down), with the codes numbered from zero:
int middlecode = codecount / 2;
Find the start index of your middle code:
int index = middlecode * 3;
Get the middle code:
std::string destination = code.substr(index, 3);
For strings with an even number of codes, this will give the first code in the second half of the string, e.g:
MXPJFKJFKMCOJFKMXP
For strings with an odd number of codes, this will give the middle code, e.g:
FLRFRAFRAORDORDLASLASBNACLTCLTMUCMUCPSA
(which in the above case looks wrong, but you did say only 90%!)

Converting between a number to a string without num2str

For example, input is a=5678. How do you make b='5678'? (b is a String).
Not allowed to use str2num or any casting.
Is it possible to use log10? (I know how to do the reverse action).
[This is how I did the opposite (from string to num):
s = input('Enter a number: ','s');
x = sum(10.^(length(s-'0')-1:-1:0).*(s-'0'));
This looks like homework, so first here are some hints:
log10 may be useful to determine the number of digits.
mod can help to obtain each digit.
From your code for the reverse action: using successive powers of 10, as well as +'0' / -'0' to convert between digits and ASCII codes, may also be of help here.
And here's a possible approach using these hints (hover the mouse to find out):
b = char(mod(floor(a./10.^((ceil(log10(a))-1):-1:0)),10) + '0'):

Convert a string to number without using any java conversion functions/utility classes

I was asked in a Java interview to write a program that would convert a string for example "123" into number 123 without using any of java's conversion functions/utility classes.
I am still confused if that would be possible. Any ideas ?
Thanks
Break the string into individual characters, map each to its numeric value, and combine by multiplying each by its place value.
I should have made my comment an answer so I can get the answer credit :)
"what counts as a utility class? Could you have a map of strings to numbers, iterate over the string, look up the number by the string, and construct the number?"
in pseudocode:
Map<String, Integer> = {
"0":0,
"1":1,
"2":2,
... etc }
int number = 0;
for(i=string.length-1; i>=0; i--){
String substr = string.substring(i, i+1);
int digit = map.get(substr);
number += 10^(string.length-i)*digit;
}
return number

Resources