0% found this document useful (0 votes)
2 views9 pages

codingame problem solving (1)

The document contains various code snippets in Java and TypeScript, showcasing different functionalities such as components for displaying votes, methods for calculating sums, finding the largest number, and SQL queries for retrieving customer data. It includes implementations of algorithms for searching, concatenating strings, and handling transactions. Additionally, it demonstrates the use of Java Streams and Executors for parallel processing and accumulator patterns.

Uploaded by

hmidialaa92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

codingame problem solving (1)

The document contains various code snippets in Java and TypeScript, showcasing different functionalities such as components for displaying votes, methods for calculating sums, finding the largest number, and SQL queries for retrieving customer data. It includes implementations of algorithms for searching, concatenating strings, and handling transactions. Additionally, it demonstrates the use of Java Streams and Executors for parallel processing and accumulator patterns.

Uploaded by

hmidialaa92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 9

@Component({

selector: 'display-component',
template: `
<div id="lastVote">{{ answer }}</div>
<voter-component
[question]="question"
[yesAnswer]="yesAnswer"
[noAnswer]="noAnswer"
(output)="setVote($event)"
></voter-component>
`
})
export class DisplayComponent {
public question = 'Too easy?';
public yesAnswer = 'Yes';
public noAnswer = 'no';
public answer: boolean;
setVote(event: boolean) {
this.answer = event ? this.yesAnswer : this.noAnswer;
}
}
-----------------------------------------------------------------
selector:'transaction-component',
template: `
<div id="fee">
{{fee | percent : '2.2-3'}}
</div>
<div id="amount">
{{amount | currency : currency :'symbol' : '9.2-2'}}
</div>
<div id="time">
{{timeOfTransaction | date : 'ww: yyyy MMMMM dd hh-mm-ss'}}
</div>
`
* Implémentez la méthode boolean A.exists(int[] ints, int k) afin qu’elle retourne true si k est
présent dans ints, sinon la méthode devra retourner false.

static boolean exists(int[] ints, int k) {

boolean b = Arrays.stream(ints).parallel().anyMatch(value -> value == k);


if(ints.length == 0){
return false;
}
boolean trouve = false;
int id = 0;
int ifin = ints.length;
int im;

while (!trouve && ((ifin - id) > 1)) {


im = (id + ifin) / 2;
trouve = (ints[im] == k);
if (ints[im] > k) {
ifin = im;
} else {
id = im;
}
}
return (ints[id] == k);
}
}

----------------------------------------------------------------------
public class Concatenation {

public static void main(String[] args ){


System.out.println(concat(new String[]{"f","o","o","bar"}));
}

static String concat(String[] strings){


//StringBuffer sb = new StringBuffer();
String join = String.join("", strings);
/*for(int i = 0 ; i <strings.length - 1;i++){
sb.append(strings[i]);
}*/
return join;
}
}
-----------------------------------------------------

public class CompareThreeInt {


public static int solve(int weight0, int weight1, int weight2) {
Map<Integer,Integer> weights = new HashMap<>();
weights.put(0,weight0);
weights.put(1,weight1);
weights.put(2,weight2);
return Collections.max(weights.entrySet(),
Comparator.comparingInt(Map.Entry::getValue)).getKey();
}

public static void main(String args[]) {


int action = solve(110, 120, 90);
System.out.println(action);
}
}
-----------------------------------------------------------------------------
public class LargestNumber {

static int findLargest (int[] numbers){


Arrays.sort(numbers); //return numbers[numbers.length -1];
int max1 = Arrays.stream(numbers).max().getAsInt();
if(numbers.length > 1){
int max = 0;
for(int i = 0;i<numbers.length;i++){
if(numbers[i] > max){
max = numbers[i];
if(max == Integer.MAX_VALUE){
return max;
}
}
}
return max;
} else {
return numbers[0];
}
}

public static void main(String[] args){


int [] numbers = {100,50,150};
System.out.println(LargestNumber.findLargest(numbers));
}

}
------------------------------------------------------------------------

class Solution {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n == 0){
System.out.println("0");
return;
}
in.nextLine();
String[] tempL = in.nextLine().split(" ");
int min = Integer.parseInt(tempL[0]);
for (String strTemp : tempL) {
int temp = Integer.parseInt(strTemp);
if(Math.abs(temp) < Math.abs(min) || (0 < temp && -min == temp)){
min = temp;
}
}
System.out.println(min);
}
}

-------------------------------------------------------------------------------
class ClosestToZero {

static int closestToZero(int[] ints) {

if (ints == null || ints.length == 0) {


return 0;
}
int closestToZero = ints[0];
for (int i =1;i<ints.length;i++) {
int abs = Math.abs(ints[i]);
if ( abs < Math.abs(closestToZero)) {
closestToZero = ints[i];
} else if (abs == Math.abs(closestToZero) && closestToZero < 0 && ints[i] > 0) {
closestToZero = abs;
}
}
return closestToZero;
}
----------------------------------------------------------------------

public class TestAccumulator {


public static void main(String... args){
LongAccumulator accumulator = new LongAccumulator(Long::sum,0);
ExecutorService executorService = Executors.newFixedThreadPool(4);
Arrays.stream(new int[] {1,2,3,4,5,6,7,8,9})
.forEach(value -> executorService.execute(() -> accumulator.accumulate(value)));
executorService.shutdown();
while(!executorService.isTerminated()) {}
System.out.println(accumulator.getThenReset());
System.out.println(accumulator.get());
}
}

-------------------------------------------------------------------------------------

public class SommeTableau {


public static int calc(int[] array, int n1, int n2) {
int sum = 0;
for(int i = n1;i<=n2;i++){
sum+= array[i];
}
return sum;
}

public static void main(String... args){


int[] array = new int[] {0,1,2,3,4,5,3};
System.out.println(SommeTableau.calc(array, 0, 1)); // 1
System.out.println(SommeTableau.calc(array, 0, 5)); // 15
System.out.println(SommeTableau.calc(array, 0, 0)); // 0
System.out.println(SommeTableau.calc(array, 0, 6)); // 18
}
}
-----------------------------------------------------------------------------------

public class CalculateTotalPrice {

public static void main(String[] args) {


int[] prices= {101,30,2,80,10};
System.out.println(calculateTotalPrice1(prices, 20));
}

public static int calculateTotalPrice1(int[] prices,int discount) {


int total1=0;
int len=prices.length;
Arrays.sort(prices);
for(int i=0;i<len-1;i++) {
total1=total1+prices[i];
}
return (int) ( (total1+(float) ((prices[len-1])-((prices[len-1])*discount/100f))));

}
}
--------------------------------------------------------------------

isTwin(a , b) ?

public static boolean isTwin(String a , String b) {


boolean result = false;
char[] a1 = a.toLowerCase().toCharArray();
char[] b1 = b.toLowerCase().toCharArray();
Arrays.sort(a1);
Arrays.sort(b1);
return new String(a1).equals(new String(b1));
}
}
------------------------------------------------------------------------------------

- Echo HELLO YOU ! ---- >


public static void main(String[] args) {
for(String arg : args) {
}
sys out (args);
}

--------------------------------------------------------------------------------

class StreamPrinter {

void print(Reader reader) throws IOException {


try {
int code = reader.read();
while ( code != -1) {
System.out.println((char) code);
code = reader.read();
}
} catch (IOException e) {
} finally {
reader.close();
}
}}

--------------------------------ORR-******
try {
int code = reader.read();
while (code != -1) {
System.out.print((char) code);
code = reader.read();
}
} finally {
try {
reader.close();
} catch(IOException e) {
// Log here.
}
}
-----------------------------------------------------------
A.a( int I , int j)
Class A {
static boolean a ( int I , int j) {
if(i ==1 | | j ==1 ))
return true;
else
return false;
}
Node : implement a new Node’s methode named find(int v) which return the node holding the
value of v if the node doesn’t exist then find should return null .

Class Node {
Node left , right;
int value ;
Node current = null;
public Node find(int v) {

Node current = this;


while ( current != null && current.value !=v) {
current = v < current.value ? Current.left : current.right;

}
return current;
}

}
SQL

Vehicle_part **
-------------------------------------------------
Select vehicle_part.vehicle_part_id , RFID

from vehicle_part inner join Vehicule_part_location

on vehicle_part.vehicle_part_id = vehicle_part_location.vehicle_part_id

Where arrived_timestamp is not null

and left_timestamp is not null

and left_timestamp > arrived_timestmp

order by vehicle_part.vehicle_part_id , RFID

------------------------------------------------------
Modifier la requete pour selectionner uniquement les clients ayant passe au moins deux
commandes .
Select lastname as customer_lastname , Count(purchase_order.customer_id ) as purchase_count

From customer inner join purchase_order

on customer.customer_id = purchase_order.customer_id

group by purchase_order.customer_id

having Count(purchase_order.customer_id ) > 1

order by lastname

-------------------------------------------------------
n’est pas afficher de valeur null pour la catégorie.
select Product.name as PRODUCT_NAME , Product_category.name as CATEGORY_NAME

from Product left outer join product_category

ON Product.product_category_id = Product_category.product_category_id

Order by Product.name , Product_category.name

----------------------------------------------------------------------
Problem : Select only the customer IDs (Not duplicates and sorted in ascending) who
purchased at least one product in "Books" or "Garden" category.

SELECT DISTINCT c.customer_id FROM customer c, purchase_order po, order_product op,


product p, product_category pc
WHERE c.customer_id=po.customer_id AND po.order_id=op.order_id AND
op.product_id=p.product_id AND p.product_category_id=pc.product_category_id AND pc.name
IN ('Books', 'Garden')
ORDER BY c.customer_id

You might also like