YChalk

DNA Health.2

Mar 25th, 2022 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.function.*;
  8. import java.util.regex.*;
  9. import java.util.stream.*;
  10. import static java.util.stream.Collectors.joining;
  11. import static java.util.stream.Collectors.toList;
  12. import java.util.concurrent.atomic.AtomicInteger;
  13.  
  14.  
  15.  
  16. public class Solution {
  17.     public static void main(String[] args) throws IOException {
  18.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
  19.  
  20.         int n = Integer.parseInt(bufferedReader.readLine().trim());
  21.  
  22.         List<String> genes = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  23.             .collect(toList());
  24.  
  25.         List<Integer> health = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
  26.             .map(Integer::parseInt)
  27.             .collect(toList());
  28.  
  29.         int s = Integer.parseInt(bufferedReader.readLine().trim());
  30.        
  31.         //Map<String, Integer> mappings = mapping(genes, health);
  32.        
  33.        
  34.         AtomicInteger min = new AtomicInteger(Integer.MAX_VALUE);
  35.         AtomicInteger max = new AtomicInteger(0);
  36.  
  37.         IntStream.range(0, s).forEach(sItr -> {
  38.             try {
  39.                 String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
  40.  
  41.                 int first = Integer.parseInt(firstMultipleInput[0]);
  42.  
  43.                 int last = Integer.parseInt(firstMultipleInput[1]);
  44.  
  45.                 String d = firstMultipleInput[2];
  46.                
  47.                 int weight = weight(genes, health, first, last, d);
  48.                 if (weight < min.intValue()){
  49.                     min.set(weight);
  50.                 }
  51.                 if (weight > max.intValue()){
  52.                     max.set(weight);
  53.                 }
  54.                
  55.             } catch (IOException ex) {
  56.                 throw new RuntimeException(ex);
  57.             }
  58.         });
  59.  
  60.         bufferedReader.close();
  61.        
  62.        
  63.         System.out.println(min + " " + max);
  64.     }
  65.    
  66.     public static int geneOccurence(String strand, String gene){
  67.         int result = 0;
  68.        
  69.         /*if (gene.length() == 1){
  70.             char[] gChar = gene.toCharArray();
  71.             return (int)strand.chars().filter(c -> c == gChar[0]).count();
  72.         }*/
  73.        
  74.         int index = 0;
  75.        
  76.         while (true) {
  77.             index = strand.indexOf(gene, index);
  78.             if (index == -1){
  79.                 break;
  80.             }
  81.             result++;
  82.             index++;
  83.         }
  84.         return result;
  85.     }
  86.    
  87.  
  88.     public static int weight(List<String> genes, List<Integer> health, int first, int last, String strand){
  89.        
  90.         /*
  91.         *create wrapper class for health list
  92.         *initilizes an IntStream with an intitial range between first and last
  93.         *stream genes and set range between first and last
  94.         *iterate over stream with forEach
  95.         *retrieve first element of HealthWrapper intStream
  96.         *remove first element of HealthWrapper intStream using skip()
  97.         *
  98.         */
  99.         AtomicInteger result = new AtomicInteger(0);
  100.         HealthWrapper healthWrapper = new HealthWrapper(health, first, last);
  101.        
  102.         genes.stream().skip(first).limit(last-first+1).forEach(gene ->{
  103.             try {
  104.                 int occurence = geneOccurence(strand, gene);
  105.                 int weight = healthWrapper.get();
  106.                 healthWrapper.skip();
  107.                 result.addAndGet(weight*occurence);    
  108.             } catch (NullPointerException e){
  109.                 System.out.println(result);
  110.             }          
  111.         });      
  112.  
  113.        
  114.         return result.intValue();
  115.     }
  116.    
  117.     public static class HealthWrapper{
  118.         IntStream healthStream;
  119.        
  120.         public HealthWrapper(List<Integer> health, int first, int last){
  121.             healthStream = health.stream().skip(first).limit(last-first+1).mapToInt(Integer::intValue);
  122.         }
  123.        
  124.         public int get(){
  125.             return healthStream.findFirst().getAsInt();
  126.         }
  127.        
  128.         public void skip(){
  129.             healthStream = healthStream.skip(1);
  130.         }
  131.     }
  132. }
  133.  
Add Comment
Please, Sign In to add comment