Any ways to find out "beautiful numbers" [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
A beautiful number is a number containing only one type of digit, such as these: 0, 4, 44, 55555, 3333.
I would like to write an android app that finds out if a number is beautiful or not.

public static boolean isBeautiful(int n) {
char targetDigit = String.valueOf(n).toCharArray()[0];
for (char currentDigit : String.valueOf(n).toCharArray()) {
if (targetDigit != currentDigit) {
return false;
}
}
return true;
}
Bonus (sorry for the late addition of the bonus, but here it is), here's how to get all beautifuls up to x:
public static List<Integer> getBeautifuls(int upTo) {
List<Integer> beautifuls = new ArrayList<>();
int next = 0;
while (upTo >= next) {
beautifuls.add(next);
if (String.valueOf(next).toCharArray()[0] == '9') {
next = Integer.valueOf(new String(new char[String.valueOf(next).length() + 1]).replace("\0", "1"));
} else {
next = next + Integer.valueOf(new String(new char[String.valueOf(next).length()]).replace("\0", "1"));
}
}
return beautifuls;
}
Repeted string algorithm based on user102008's excellent answer on this question: Simple way to repeat a String in java
Note: Using Strings to create ints is a bit hack-ish and not so fast.

Try to check how many digits the number has, for example the number has 5 digits(between 10000-99999 and >= 11111)
if(num % 11111 == 0){
//it is beautiful number
}else{
//it is not beautiful
}
This is only an example for 5 digit numbers. You can develop an algorithm for checking other num of digits. I don't know is there any algorithm created before.

Related

java regex questions [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Given that I have a string of number, like "12354789556", I need to check that string whether has digits from 0 to 9 at least once.
Can anyone tell me whether i can express this in regex please?
If your strings contains only digits for example "123548955664789556" then try:
System.out.println(myString.chars().distinct().count() == 10);
if your string can also contain letters for example sth like "bbb1235489556fhjerfs64789556"
System.out.println(myString.replaceAll("[^\\d]", "").chars().distinct().count() == 10);
With lookaheads :
^(?=.*0)(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5)(?=.*6)(?=.*7)(?=.*8)(?=.*9)
If you want to restrict the string to digits only in addition to making sure it contains every digit :
^(?=.*0)(?=.*1)(?=.*2)(?=.*3)(?=.*4)(?=.*5)(?=.*6)(?=.*7)(?=.*8)(?=.*9)\d+$
Note that a version without lookaheads would be technically possible, but would realistically have to be crafted by code as it would have to enumerate all possible orders between digits (10! = 3628800 enumerations).
You can also do it in Java like this:
boolean containsAll = true;
for (int i = 0; i < 10; i++) {
if (!str.contains("" + i)) {
containsAll = false;
}
}
return containsAll;
A non-regex way would be to loop through the String and return false if the indexOf returns -1:
static boolean checkAll(String s, char[] allNums) {
for (int i = 0; i < allNums.length; i++) {
if (s.indexOf(allNums[i]) == -1) {
return false;
}
}
return true;
}
Example

Given a string of integers find out all the possible words that can made out of it in continuous order. [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Given a string of integers how to find out all the possible words that can made out of it in continuous order. Eg: 11112
ans: AAAAB
AKAB
AAKB
AAAL etc.
public static void main(String[] args) {
String str="11111124";
char strChar[]=str.toCharArray();
String target="";
for(int i=0;i<strChar.length;i++)
{
target=target+(char)Integer.parseInt(""+(16+strChar[i]));
}
System.out.println(target);
}
i am trying to find the solution for this but not able to find all combination
Combining the comments saying that 163 can be 1,6,3 or 16,3, but not 1,63, and user3437460's suggestion of using recursion:
Take first digit and convert to letter. Make recursive call using letter and remaining digits.
Take first two digits. If <=26, convert to letter and make recursive call using letter and remaining digits.
Here is the code. Since I have no clue what to call the method, I'm going with x.
public static void main(String[] args) {
x("11112", "");
System.out.println("------");
x("163", "");
}
private static final void x(String digits, String word) {
if (digits.isEmpty())
System.out.println(word);
else {
int num = Integer.parseInt(digits.substring(0, 1));
x(digits.substring(1), word + (char)('A' + num - 1));
if (digits.length() >= 2 && (num = Integer.parseInt(digits.substring(0, 2))) <= 26)
x(digits.substring(2), word + (char)('A' + num - 1));
}
}
Output
AAAAB
AAAL
AAKB
AKAB
AKL
KAAB
KAL
KKB
------
AFC
PC

about comparing two strings in java and expluding repeats [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
OK lets say the secret pin is:
2236 (resident evil 2 reference)
if a person type 2200
I get a match count of 2;
But:
If the person type: 2000 or 0200, or 0020, or 0002
results says that it still matches 2 numbers.
It should only match one of the 2 number but I still get a match count of 2.
I'm currently using :
if(currentPin.indexOf(secretPin.charAt(i)) != -1)
on a loop.
UPDATE:
Assuming all the the necessary variables are declared and initialized and all scopes are closed
heres the code that does the matching
for (int i = 0; i < secretPin.length(); i++)
{
if (currentPin.indexOf(secretPin.charAt(i)) != -1)
{
System.out.println("Found");
match++;
}
}
//output:
2236 //SECRET NUMBER display
0002 //USER GUESS
Found
Found
MATCH: 2 // should only match 1 if USER GUESS only contain one number 2
Assuming the number of digits is always the same between secret pin and current pin, this simple algorithm would do the trick to retrieve the number of match :
public static void main(String[] args) {
String secretPin = "2236";
String currentPin = "6322";
int match = 0;
for (int i = 0; i < currentPin.length(); i++) {
if (secretPin.contains(String.valueOf(currentPin.charAt(i)))) {
secretPin = secretPin.replaceFirst(String.valueOf(currentPin.charAt(i)), "");
match++;
}
}
System.out.println(match);
}
"if(currentPin.indexOf(secretPin.charAt(i) != -1)" is getting a character, checks if it's != -1, and passes the boolean result to indexOf. You also have a bracket mismatch. As given this will not compile, let alone produce inaccurate output. Please paste the code you say you are able to run if you would like it reviewed.

How do I create a loop which checks these values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
If I had a numberical idno value, how would I find the ones >=5000 and < 5000? Would i use idno.substring(7,11) and then check the conditions
if >=5000 print Male
if <5000 print Female
How would I perform this?
Check idno % 10000 to get the value of the last four digits.
Given that your ID number is stored as an integer:
int idno = ...; // some ID number
You can use this comparison:
if (idno % 10000 >= 5000) {
System.out.println("Male");
} else {
System.out.println("Female");
}
The if statement is a branching operator that allows your code to branch to one block or another based on the result of the comparison. This is different from a loop.
A loop is code that will execute repeatedly until a condition is met. For example, if you had an array of integer ID numbers:
int[] idArray = { .... , .... , .... }; // some array of IDs
You could loop through them like this:
for (int idno: idArray) {
if (idno % 10000 >= 5000) {
System.out.println(idno+" is Male");
} else {
System.out.println(idno+" is Female");
}
}
First convert your String to integer using
int idno1=Integer.parseInt(idno);
Then simple check idno1<=5000 or idno1>5000

How to substract two strings [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have started learning Java and have some across some difficulties. I'm trying to subtract two strings.
for example, with these strings;"032"&&"100". I want to be able to subtract each number individually so that the answer would be "032".
I have tried using substring, and parsing the two values to ints, but don't know what to do next. I have also tries using a for loop, to go through each arrays of the strings.
I do not expect for anyone to do this for me, but I would love to get some insight,or to tell me that i'm headed in the right direction
thanks
public static String appliquerCoup( String combinaison, String coup ) {
String nouveauCoup="";
if(combinaison!=null&&coup!=null){
for(int i=0;i>combinaison.length();i++){
int a = Integer.parseInt(combinaison.substring(i, i + 1));
int b = Integer.parseInt(coup.substring(i, i + 1));
nouveauCoup=String.valueOf(a-b);
if(a-b<0){
nouveauCoup=0;
}
}
} // main
return nouveauCoup;
}
If I understand you question correctly. you want to subtract each digit individually.
So (0-1), (3-0), (2-0). The following program does this (yields -132):
public static void main(String[] args) {
String A = "032";
String B = "100";
String str = "";
for(int i = 0; i < A.length(); i++)
{
int a = Integer.parseInt(A.substring(i, i + 1));
int b = Integer.parseInt(B.substring(i, i + 1));
int c = a - b;
str += String.valueOf(c < 0 ? 0 : c);
}
System.out.println(str);
}
Essentially, extract the i-th character of each string, convert them to integers, then do the subtraction. Convert the result back to a string and append it to the result string.

Categories