Advertisement
Guest User

Full Code of First Index of String in Infinite String

a guest
Aug 17th, 2017
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package newcodewarsgen;
  7.  
  8. import java.math.BigInteger;
  9. import java.util.stream.Collectors;
  10. import java.util.stream.LongStream;
  11.  
  12. /**
  13.  *
  14.  * @author Dazai Osamu
  15.  */
  16. public class PositionOfDigitalStringMySolution {
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21. //        try {
  22. //            BufferedWriter out = new BufferedWriter(new FileWriter("numberfuck.txt"));
  23. //            out.write(generateNum("53635"));  //Replace with the string
  24. //                                                     //you are trying to write  
  25. //            out.close();
  26. //        }
  27. //        catch (IOException e)
  28. //        {
  29. //            System.out.println("Exception "+e);
  30. //
  31. //        }
  32.         System.out.println(findPosition("229"));
  33.        
  34. //        System.out.println(BigInteger.valueOf(187).xor(BigInteger.valueOf(991)));
  35. //        System.out.println(BigInteger.valueOf(6).xor(BigInteger.valueOf(4)));
  36.     }
  37. //    public static String generateNum(String s){
  38. //        while(!num.contains(s)){
  39. //            num+=start.toString();
  40. //            start= start.add(BigInteger.ONE);
  41. //        }
  42. //        return num;
  43. //    }
  44.     public static long findPosition(final String s) {
  45.         BigInteger start = new BigInteger("13");      
  46.         String num = "123456789101112";
  47.         Long index = 0L;
  48.         System.out.println("The Num: "+s);
  49.         if(!num.contains(s)){
  50.             if (Long.valueOf(s).compareTo(Long.valueOf("12345678913"))>=0) {
  51.                 num = "12345678913";
  52.                 index=generateIndex(num);
  53.                 return index+((Long.valueOf(s)-Long.valueOf(num))*num.length());
  54.             }else if(Long.valueOf(s).compareTo(Long.valueOf("1234567892"))>=0){
  55.                 num = "1234567892";
  56.                 index=generateIndex(num);
  57.                 return index+((Long.valueOf(s)-Long.valueOf(num))*num.length());
  58.             }else if(Long.valueOf(s).compareTo(Long.valueOf("123456790"))>=0){
  59.                 num = "123456790";
  60.                 index=generateIndex(num);
  61.                 return index+((Long.valueOf(s)-Long.valueOf(num))*num.length());
  62.             }
  63.         }
  64.         while(!num.contains(s)){
  65. //            System.out.println("Num: "+num);
  66.             num +=start.toString();
  67.             start = start.add(BigInteger.ONE);
  68.         }
  69.         return num.indexOf(s)+index;
  70.     }
  71.     public static Long generateIndex(String num){
  72.         return Long.parseLong(LongStream.generate(()->9).limit(num.length()).mapToObj(String::valueOf).collect(Collectors.joining("")));
  73.     }
  74.     public static BigInteger newStart(String num){
  75.         return new BigInteger(num).add(BigInteger.ONE);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement