Last active
January 24, 2018 08:44
-
-
Save Brutt/057dbeba43f9e77c75254552fe9a521d to your computer and use it in GitHub Desktop.
5. принимает массив байт, если в массиве есть повторяющиеся елементы, возвращает тру
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DecToBinary { | |
String getBinary(int number, int outputLength){ | |
if (number==0){ | |
return "0"; | |
} | |
String strNumber = ""; | |
while (!((number/2==0)&&(number%2==1))){ | |
strNumber = (number % 2) + strNumber; | |
number = number / 2; | |
} | |
strNumber = (number % 2) + strNumber; | |
//strNumber = String.format("%16s", Integer.toBinaryString(strNumber)).replace(' ', '0') | |
return strNumber; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
class EmployeeService { | |
Employee[] users; | |
void generateEmployees(int size) { | |
String[] maleNames = {"Anatoliy","Oleg","Vladimir","Yuriy"}; | |
String[] femaleNames = {"Anna","Olha","Vlada","Yulia"}; | |
Random random = new Random(); | |
users = new Employee[size]; | |
// generate random users | |
// [1, "Tolik", 1000, 'M'] | |
for (int i = 0; i < users.length; i++) { | |
users[i] = new Employee(); | |
users[i].age = random.nextInt(65-18+1)+18; | |
users[i].gender = users[i].age % 2==0 ? 'M' : 'F'; | |
users[i].salary = random.nextInt(3000-500+1)+500; | |
String personName = maleNames[random.nextInt(maleNames.length)]; | |
if (users[i].gender == 'F') { | |
personName = femaleNames[random.nextInt(femaleNames.length)]; | |
} | |
users[i].name = personName; | |
} | |
} | |
// print all users in array | |
void print() { | |
for (int i = 0; i < users.length; i++) { | |
System.out.println( "Name: "+users[i].name+ | |
"; Gender: " + users[i].gender + | |
"; Age: " + users[i].age + | |
"; Salary: " + users[i].salary); | |
} | |
} | |
// ASC | |
void sortByName() { | |
for (int i = 0; i < users.length; i++) { | |
for (int i = 0; i < users.length; i++) { | |
} | |
} | |
// if names are equal, compare by salary | |
void sortByNameAndSalary() { | |
} | |
// return sum of all salaries | |
double calculateSalary() { | |
return 0; | |
} | |
Employee[] searchByName(String name) { | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Duplicates { | |
public static void main(String[] args) { | |
byte[] startArray = {-10,-128,127,1,5,55,-10}; | |
boolean isDuplicates = isDuplictesInArray(startArray); | |
System.out.println(isDuplicates); | |
} | |
static boolean isDuplictesInArray(byte[] arrayVal){ | |
byte[] additionalArray = new byte[256]; | |
for (int i=0;i<arrayVal.length;i++){ | |
if (additionalArray[arrayVal[i]+128] == 1){ | |
return true; | |
} | |
else{ | |
additionalArray[arrayVal[i]+128] = 1; | |
} | |
} | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Fridge { | |
public int[][] tumblers = new int[4][4]; | |
void initialFill(String variant){ | |
int x = 0, y = 0; | |
for (int i = 0; i < variant.length(); i++) { | |
tumblers[x][y] = Integer.parseInt(variant.substring(i,i+1)); | |
if (((i+1)%4==0)&&(i!=0)){ | |
x++; | |
y=0; | |
} | |
else { | |
y++; | |
} | |
} | |
} | |
void printArray(){ | |
for (int i = 0; i < tumblers.length; i++) { | |
for (int j = 0; j < tumblers[i].length; j++) { | |
System.out.print(tumblers[i][j]); | |
} | |
System.out.println(); | |
} | |
} | |
void turnLinked(int x, int y){ | |
int newValue = tumblers[x][y]==0?1:0; | |
for (int i = 0; i < tumblers.length; i++) { | |
for (int j = 0; j < tumblers[i].length; j++) { | |
if ((i == x) || (j == y)) { | |
tumblers[i][j] = newValue; | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LinkedStack { | |
Node head; | |
int size; | |
Node getLastNode(Node node){ | |
if (node.next != null){ | |
return getLastNode(node.next); | |
} | |
return node; | |
} | |
// same methods as in ArrayStack | |
void push(Object value) { | |
//getLastNode(head).value = value; | |
//getLastNode(head).next = new Node(); | |
if (head == null){ | |
head = new Node(); | |
head.value = value; | |
} | |
else { | |
Node bufferNode = head; | |
Node parentNode = head; | |
for (int i = 0; i < size; i++) { | |
parentNode = bufferNode; | |
bufferNode = bufferNode.next; | |
} | |
bufferNode = new Node(); | |
bufferNode.value = value; | |
parentNode.next = bufferNode; | |
} | |
size++; | |
} | |
Object poll() { | |
size--; | |
Node lastNode = getLastNode(head); | |
Node parentNode = head; | |
for (int i = 0; i < size-1; i++) { | |
parentNode = parentNode.next; | |
} | |
parentNode.next = null; | |
return lastNode.value; | |
} | |
Object peek() { | |
return getLastNode(head).value; | |
} | |
int size() { | |
return size; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) { | |
LinkedStack linkedStack = new LinkedStack(); | |
linkedStack.push("A"); | |
linkedStack.push(32); | |
linkedStack.push(8.0); | |
linkedStack.push('R'); | |
System.out.println("Size:" + linkedStack.size()); | |
System.out.println("Value peek:" + linkedStack.peek()); | |
System.out.println("Value pool:" + linkedStack.poll()); | |
System.out.println("Value peek:" + linkedStack.peek()); | |
linkedStack.push('C'); | |
System.out.println("Size:" + linkedStack.size()); | |
System.out.println("Value peek:" + linkedStack.peek()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) { | |
//DecToBinary dtb = new DecToBinary(); | |
/*for (int i = 1; i < 65536; i++) { | |
String newVariant = String.format("%16s", Integer.toBinaryString(i)).replace(' ', '0'); | |
Fridge fridge = new Fridge(); | |
fridge.initialFill(newVariant); | |
fridge.printArray(); | |
System.out.println("----"+i+"-----"); | |
}*/ | |
Fridge fridge = new Fridge(); | |
fridge.initialFill("0000000000000001"); | |
fridge.printArray(); | |
fridge.turnLinked(0,0); | |
System.out.println("---------"); | |
fridge.printArray(); | |
fridge.turnLinked(0,0); | |
System.out.println("---------"); | |
fridge.printArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment