Kevin
BAN USER- 3of 3 votes
AnswersImplement LookAndSay function. For example, first, let user input a number, say 1. Then, the function will generate the next 10 numbers which satisfy this condition:
- Kevin in United States
1, 11,21,1211,111221,312211...
explanation: first number 1, second number is one 1, so 11. Third number is two 1(previous number), so 21. next number one 2 one 1, so 1211 and so on...| Report Duplicate | Flag | PURGE
Epic Systems Software Engineer / Developer Algorithm
import java.util.HashSet;
import java.util.Set;
public class ColorfulNumber {
public static boolean isColorful(int number) {
String num = String.valueOf(number);
int length = num.length();
int size = 1;
Set<Integer> set = new HashSet<Integer>();
while (size <= length) {
for (int i = 0; i <= length - size; i++) {
int j = size;
int index = 0;
int value = 1;
while (j > 0) {
value *= Integer.parseInt(String.valueOf(num.charAt(i
+ index)));
j--;
index++;
}
if (set.contains(value)) {
return false;
} else {
set.add(value);
}
}
size++;
}
return true;
}
public static void main(String[] args) {
System.out.println(isColorful(263));
}
}
public class VerifyPasswd {
public static void verifyPasswd(String passwd) {
if (!checkLen(passwd))
return;
if (!checkChar(passwd))
return;
if (!checkSeq(passwd))
return;
System.out.println("Valid Passwd");
}
private static boolean checkSeq(String passwd) {
for (int index = 0; index < passwd.length(); index++) {
char c1 = passwd.charAt(index);
int index2 = passwd.indexOf(c1, index + 1);
if (index2 != -1) {
String str1 = passwd.substring(index, index2);
String str2 = passwd.substring(index2, 2 * index2 - index);
if(str1.equals(str2)){
return false;
}
}
}
return true;
}
private static boolean checkChar(String passwd) {
char[] chars = passwd.toCharArray();
boolean num_flag = false;
boolean lowercase_flag = false;
for (int i = 0; i < chars.length; i++) {
if (chars[i] >= '0' && chars[i] <= '9') {
num_flag = true;
}
if (chars[i] >= 'a' && chars[i] <= 'z') {
lowercase_flag = true;
}
}
if (num_flag && lowercase_flag) {
return true;
}
return false;
}
private static boolean checkLen(String passwd) {
if (passwd.length() < 5 || passwd.length() > 12) {
return false;
}
return true;
}
public static void main(String[] args) {
verifyPasswd("1123123qs");
}
}
import java.util.Arrays;
public class Transpose {
public static void transpose(char[] source, char[] destination) {
// detect whether two array could be transposed to the other
char[] src_copy = new char[source.length];
char[] dest_copy = new char[destination.length];
int i = 0;
for (char n : source) {
src_copy[i++] = n;
}
i = 0;
for (char n : destination) {
dest_copy[i++] = n;
}
Arrays.sort(src_copy);
Arrays.sort(dest_copy);
if (!Arrays.equals(src_copy, dest_copy)) {
System.out.println("Couldn't transpose!");
return;
}
for (i = 0; i < source.length; i++) {
if (source[i] == destination[i]) {
continue;
} else {
change(source, i, destination[i]);
}
}
}
private static void change(char[] source, int i, char target) {
int index;
for (index = i; i < source.length; index++) {
if (target == source[index]) {
break;
}
}
swap_helper(source, i, index);
}
private static void swap_helper(char[] source, int start, int end) {
for (int index = end; index > start; index--) {
char tmp = source[index - 1];
source[index - 1] = source[index];
source[index] = tmp;
}
}
public static void main(String[] args) {
char[] src = {'a','f','d','c','e','b'};
char[] dest = {'a','b','c','d','e','f'};
transpose(src,dest);
System.out.println(src);
}
}
public class PerfectValidNumber {
public static void generateNum(int start, int end) {
for (int num = start; num <= end; num++) {
printValidNum(String.valueOf(num));
}
}
private static void printValidNum(String num) {
if (num == null || num.length() < 3) {
return;
}
for (int init_size = 1; init_size <= num.length() / 2; init_size++) {
if (isValid(num, init_size,
Integer.parseInt(num.substring(0, init_size)),
Integer.parseInt(num.substring(init_size, 2 * init_size)))) {
System.out.println(num);
break;
}
}
}
private static boolean isValid(String num, int init_size, int n1, int n2) {
int n3 = n1 + n2;
if (num.length() == String.valueOf(n1).length()
+ String.valueOf(n2).length() + String.valueOf(n3).length()) {
if (num.substring(
String.valueOf(n1).length() + String.valueOf(n2).length())
.equals(String.valueOf(n3))) {
return true;
} else {
return false;
}
} else if (num.length() < String.valueOf(n1).length()
+ String.valueOf(n2).length() + String.valueOf(n3).length()) {
return false;
}else{
if (num.substring(String.valueOf(n1).length() + String.valueOf(n2).length(),String.valueOf(n1).length() + String.valueOf(n2).length()+String.valueOf(n3).length())
.equals(String.valueOf(n3))) {
return isValid(num.substring(String.valueOf(n1).length()),init_size,n2,n3);
} else {
return false;
}
}
}
public static void main(String[] args) {
generateNum(123,123);
}
}
public class SnakeSequence {
int M;
int N;
int[][] matrix;
int[][] table;
public SnakeSequence(int m, int n) {
super();
N = n;
M = m;
matrix = new int[M][N];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = (int) (Math.random() * 8);
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
public void longestSSDB() {
// define and initialize table
table = new int[M][N];
for (int j = 0; j < N; j++) {
if (matchNeighbor(0, j)) {
table[0][j] = table[0][j - 1] + 1;
} else {
table[0][j] = 1;
}
}
for (int i = 0; i < M; i++) {
if (matchNeighbor(i, 0)) {
table[i][0] = table[i - 1][0] + 1;
} else {
table[i][0] = 1;
}
}
// fill table
for (int i = 1; i < M; i++) {
for (int j = 1; j < N; j++) {
if (matchNeighbor(i, j)) {
table[i][j] = getLargest(table, i, j) + 1;
} else {
table[i][j] = 1;
}
}
}
// print table
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(table[i][j] + "\t");
}
System.out.println();
}
}
private int getLargest(int[][] table, int i, int j) {
int max = 1;
if (i - 1 >= 0) {
if (Math.abs(matrix[i][j] - matrix[i - 1][j]) == 1 && table[i - 1][j] > max) {
max = table[i - 1][j];
}
}
if (Math.abs(matrix[i][j] - matrix[i][j - 1]) == 1 && j - 1 >= 0) {
if (table[i][j - 1] > max) {
max = table[i][j - 1];
}
}
return max;
}
public void printLargestLength() {
int max = 1;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (table[i][j] > max) {
max = table[i][j];
}
}
}
System.out.println("The longest length is " + max);
}
private boolean matchNeighbor(int i, int j) {
if (i - 1 >= 0) {
if (Math.abs(matrix[i][j] - matrix[i - 1][j]) == 1) {
return true;
}
}
if (j - 1 >= 0) {
if (Math.abs(matrix[i][j] - matrix[i][j - 1]) == 1) {
return true;
}
}
return false;
}
public static void main(String[] args) {
SnakeSequence ss = new SnakeSequence(3, 5);
System.out.println();
ss.longestSSDB();
ss.printLargestLength();
}
}
public class ValidPhoneNumber {
public static void printValidPhoneNum(int n) {
if (n <= 0 || n == 7 || n == 2 || n == 9) {
System.out.println("No such phone number");
return;
}
for (int start = (int) Math.pow(10.0, (double) (n - 1)); start < (int) Math
.pow(10.0, (double) (n)); start++) {
if (checkDoesContainFour(start) && checkConsecutive(start)) {
System.out.println(start);
}
}
}
private static boolean checkConsecutive(int num) {
String str = String.valueOf(num);
for (int i = 0; i < str.length() - 1; i++) {
// 0 1 2 3 4 5 6 7 8 9
if (str.charAt(i) + 1 == str.charAt(i + 1)
|| str.charAt(i) - 1 == str.charAt(i + 1)) {
return false;
}
}
return true;
}
private static boolean checkDoesContainFour(int num) {
String str = String.valueOf(num);
if (str.contains("4")) {
if (str.charAt(0) == '4') {
return true;
}else{
return false;
}
}
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
printValidPhoneNum(3);
}
}
import java.util.ArrayList;
public class PrintLLReverse {
public static void print(ArrayList<Integer> list) {
print_helper(list, 0);
}
private static void print_helper(ArrayList<Integer> list, int position) {
if (position >= list.size() - 1) {
System.out.print(list.get(position) + "\t");
return;
}
print_helper(list, position + 1);
System.out.print(list.get(position) + "\t");
}
/**
* @param args
*/
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(6);
list.add(2);
list.add(1);
list.add(9);
list.add(0);
print(list);
}
}
Question:
1. {0,0} How many equilibrium points does it have?
2. {4} What's the equilibrium point?
3. Is it possible to have more than 1 equilibrium point in a normal integer array?
public class Equillibrium {
public static void equi(int[] array) {
if (array.length == 0) {
return;
}
if (array.length == 1) {
System.out.println("Equilibrium Point:" + "0:" + array[0]);
return;
}
int left_sum = 0;
int right_sum = 0;
for (int i = 0; i < array.length; i++) {
left_sum = sum(array, 0, i);
right_sum = sum(array, i, array.length - 1);
if (left_sum == right_sum) {
System.out.println("Equilibrium Point:" + i + "-" + array[i]);
}
}
}
private static int sum(int[] array, int start, int end) {
int sum = 0;
for (int i = start; i <= end; i++) {
sum += array[i];
}
return sum;
}
public static void main(String[] args) {
// int[] array = { 1, 3, 4, 10, 18, 3, 1, 6, 3, 1, 4 };
// int[] array = {3,4,5,2,5};
// int[] array = {0,0};
int[] array = { -3, 0, 3, -3, 0, 3 };
equi(array);
}
}
Could somebody help me finish the negative part?
public class Convert {
public static String convert(double num) {
if (num >= 0) {
String n = String.valueOf(num);
int int_rep = Integer.parseInt(n.substring(0, n.indexOf(".")));
double dec_rep = Double.parseDouble(n.substring(n.indexOf(".")));
StringBuilder sb_int = new StringBuilder();
while (int_rep > 0) {
sb_int.append(int_rep % 2);
int_rep /= 2;
}
StringBuilder sb_rep = new StringBuilder();
while (dec_rep > 0) {
if (sb_rep.length() > 32) {
return "Error";
}
if (dec_rep == 1) {
sb_rep.append(1);
break;
}
double r = dec_rep * 2;
if (r >= 1) {
sb_rep.append(1);
dec_rep = r - 1;
} else {
sb_rep.append(0);
dec_rep = r;
}
}
String tail = sb_rep.toString().equals("") ? "0" : sb_rep.toString();
return sb_int.reverse().toString() + "." + tail;
} else {
//negative
}
return null;
}
public static void main(String[] args) {
System.out.println(Double.MAX_VALUE);
System.out.println(convert(3));
}
}
RepPacheTanley, Associate at Adobe
Hi , I am Conference service coordinator for the KB Toys company. I Build and maintain relationships with meeting planners and ...
RepHi, I’m Jamie from the Portsmouth USA and I am working as an account manager. I work for a ...
RepErmanStern, Applications Developer at ADP
I am Erman . I am metal finishing-, plating- and coating-machine operators operate and monitor equipment which finishes, plates and coats ...
Repammiwilson5, Personnel at BMO Harris Bank
Hi I am Ammy from Served on a research team for improved customer satisfaction survey process,Moderated focus groups to ...
RepJanie Margreta, Android Engineer at Achieve Internet
JanieMargreta works as a plant operator, an employee who supervises the operation of an industrial plant. Where I have to ...
Repjanicepdaniels1, Backend Developer at Accenture
I decided to become an entrepreneur and work for myself because I wasn't making the money I wanted to ...
RepMariaHobbs, Consultant at Adobe
Hi, I am Maria Hobbs from NewYork.Teach career development courses for designated areas. Develop, evaluate and revise course materials ...
RepShirleyMHansen, Project Leader at Chelsio Communications
I am Physical Therapy Aide with 3 years experience in hospital. I like to build up my knowledge of various ...
Repanneabush, AT&T Customer service email at Accolite software
Hello, I am Anne and I live in Tampa, USA. I am working in a company as a caretaker. I ...
Repmerrittmonique9, Android Engineer at AMD
I am Monique and working in high court from last 20 years.Handel many cases in my career.Build and ...
Repsarahchannah745, Android Engineer at ASAPInfosystemsPvtLtd
Hello, I am an information records clerk.We are responsible for maintaining their company records in a complete and orderly ...
Repritahmixon, Analyst at ADP
Hi, I am a physiotherapy to help people handle everyday problems. I also assist peoples who have issues caused by ...
RepFanieGoode, Consultant at Achieve Internet
I am a Media Library Assistant that provides integrated support for two popular “Multi Language/ Multilingual/ Internationalization” plugins; WPML and ...
Repgoamgivheler, Accountant at Lava
I am working as a nurse in Bali Boer Cafarius. I enjoy relationships with patients and their families. It has ...
- Kevin February 19, 2013