Commit 6b29d962 authored by NAVNEET's avatar NAVNEET

lab8

parents
import java.io.DataInputStream;
import java.io.IOException;
public class q1 {
public static void main(String[] args) throws IOException{
DataInputStream k=new DataInputStream(System.in);
int i;
int i1=0;
int i2=3;
String s="";
char c;
while((i = k.read())!=-1) {
// converts integer to character
if (48<=i&&i<=57) {
i1=((int)i)-48;
while((i=k.read())!=-1){
if (48<=i&&i<=57) {
i2=((int)i)-47;
break;}
}
break;
}
c = (char)i;
s=s+c;
}
s=s.substring(i1, i2);
System.out.println(s);
if(k!=null)
k.close();
}
}
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
public class q2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n;
Scanner mysc = new Scanner(System.in);
HashMap<Integer, Integer> mymap = new HashMap<Integer, Integer>();
n = mysc.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;i++) {
arr[i] = mysc.nextInt();
if(mymap.containsKey(arr[i]) == true) {
mymap.put(arr[i],mymap.get(arr[i]) + 1);
}
else {
mymap.put(arr[i], 1);
}
}
Iterator<Map.Entry<Integer,Integer>> iterator = mymap.entrySet().iterator();
int max = 0;
while(iterator.hasNext()) {
Map.Entry<Integer, Integer> pair = iterator.next();
if(pair.getValue() > max) {
max = pair.getValue();
}
}
mysc.close();
System.out.println(max);
}
}
import java.util.regex.*;
import java.util.*;
public class q3 {
public boolean fun1(String myStr) {
// write your code here
return (((myStr.length()<=5) && (Pattern.matches("^[a-zA-Z0-9 ]+$", myStr))) ? true : false);
}
public boolean fun2(String myStr) {
// write your code here
return ((myStr.length()>=2) && (Pattern.matches("^a*+b+b*+c$", myStr))) ? true : false;
}
public boolean fun3(String myStr) {
// write your code here
return (Pattern.compile("(?x)(?:a(?= a*(\\1?+b)))+\\1").matcher(myStr).matches());
}
public ArrayList<String> fun4(String myStr, String patt) {
// write your code here
Pattern p = Pattern.compile(patt);
Matcher mtch = p.matcher(myStr);
ArrayList<String> ips = new ArrayList<String>();
while(mtch.find())
{
ips.add(mtch.group());
}
return ips;
}
}
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.*;
public class q4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread mythread = new Thread(new q4().new currTime());
mythread.start();
}
private class currTime implements Runnable{
public void run() {
while (true)
{
Date date = new Date();
String timeFormat = "HH:mm:ss";
DateFormat myformat = new SimpleDateFormat(timeFormat);
String finalTime = myformat.format(date);
System.out.println(finalTime);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class q5 {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int arraySize = reader.nextInt();
ArrayList<int[]> arrays = new ArrayList<int[]>(arraySize);
for (int index=0; index < arraySize; index++){
int[] array = new int[reader.nextInt()];
for (int index2=0; index2 < array.length; index2++){
array[index2] = reader.nextInt();
}
arrays.add(array);
}
int querySize = reader.nextInt();
ArrayList<int[]> queries = new ArrayList<int[]>(querySize);
for (int index=0; index < querySize; index++){
int[] array = new int[2];
array[0] = reader.nextInt();
array[1] = reader.nextInt();
queries.add(array);
}
for (int index=0; index < querySize; index++){
int row = queries.get(index)[0] - 1;
int col = queries.get(index)[1] - 1;
if(!(row < arrays.size() && col < arrays.get(row).length)) {
System.out.println("ERROR!");
}
else {
System.out.println(arrays.get(row)[col]);
}
}
}
}
public class Matrix {
float m[][];
Matrix(int a, float v){
m=new float[a][a];
int m1=m.length;
int n1=m[0].length;
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
m[i][j]=v;
}
}
}
Matrix(int a, int b,float v){
m=new float[a][b];
int m1=m.length;
int n1=m[0].length;
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
m[i][j]=v;
}
}
}
Matrix(int a, int b){
m=new float[a][b];
int m1=m.length;
int n1=m[0].length;
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
m[i][j]=0.0f;
}
}
}
Matrix(int a){
m=new float[a][a];
int m1=m.length;
int n1=m[0].length;
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
m[i][j]=0.0f;
}
}
}
float[][] add(Matrix k){
int m1=m.length;
int n1=m[0].length;
int m2=k.m.length;
int n2=k.m[0].length;
float mr[][]=new float[1][1];
mr[0][0]=0;
if(m1!=m2){
System.out.println("Matrices cannot be added");
return mr;
}
if(n1!=n2){
System.out.println("Matrices cannot be added");
return mr;
}
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
m[i][j]=k.m[i][j]+m[i][j];
}
}
return m;
}
float[][] matmul(Matrix k){
int m1=m.length;
int n1=m[0].length;
int m2=k.m.length;
int n2=k.m[0].length;
float t=0;
float mr2[][]=new float[1][1];
mr2[0][0]=0;
if(n1!=m2){
System.out.println("Matrices cannot be multiplied");
return mr2;
}
float mr[][]=new float[m1][n2];
for(int i=0;i<m1;i++){ //rows of a
for(int j=0;j<n2;j++){ //col of b
t=0.0f;
for(int f=0;f<n1;f++){ //col of a
t+=m[i][f]*k.m[f][j];
}
mr[i][j]=t;
}
}
return mr;
}
void scalarmul(int s){
int m1=m.length;
int n1=m[0].length;
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
m[i][j]=m[i][j]*s;}
}
}
int getrows(){
int m1=m.length;
return m1;
}
int getcols(){
int n1=m[0].length;
return n1;
}
float getelem(int i,int j){
int m1=m.length;
int n1=m[0].length;
if(i>=m1||j>=n1){
System.out.println("Index out of bound");
return -100;
}
return m[i][j];
}
void setelem(int i ,int j,float n){
int m1=m.length;
int n1=m[0].length;
if(i>=m1||j>=n1){
System.out.println("Index out of bound");
return ;
}
m[i][j]=n;
}
void printmatrix(){
int m1=m.length;
int n1=m[0].length;
for(int i=0;i<m1;i++){
for(int j=0;j<n1;j++){
System.out.printf("%f ",m[i][j]);
}
System.out.printf("\n");
}
}
}
import java.nio.file.*;
import java.util.*;
public class q7 {
public static void main(String[] args) throws Exception {
String myStr = args[0];
String data = readFileAsString(myStr); //reading file as string into data
data = data.replace("\u2026", ""); //Removing horizontal ellipsis
data = data.replace("\u002D", " "); //Replacing - with " "
data = data.replaceAll("\\p{Punct}", ""); //Removing Puctuations from the string
List<String> wordsList = tokeningString(data); //Creating Tokens
List<String> stopWords = Arrays.asList("and", "the", "is", "in", "at", "of", "his", "her", "him");
wordsList.removeAll(stopWords); //Removing the Stop Words from tokens
//Creating TreeHashMap for lexicographical sorting
Map<String, Integer> resultMap = new TreeMap<String, Integer>();
for(String i : wordsList) {
Integer j = resultMap.get(i);
resultMap.put(i, (j == null) ? 1 : j+1);
}
//Sorting by frequency of values
Map<String, Integer> finalList = sortByValue(resultMap);
//Printing Output to screen
for(Map.Entry<String, Integer> val : finalList.entrySet()) {
System.out.println(val.getKey() + "," + val.getValue());
}
}
//Method for Reading file as String
public static String readFileAsString(String fileName) throws Exception {
String data = "";
data = new String(Files.readAllBytes(Paths.get(fileName)));
return data;
}
//Method for tokening the String
public static List<String> tokeningString(String datafile) {
StringTokenizer st = new StringTokenizer(datafile);
List<String> elements = new ArrayList<String>();
while(st.hasMoreTokens()) {
elements.add(st.nextToken());
}
return elements;
}
//Method for sorting by frequency of tokens
public static HashMap<String, Integer> sortByValue(Map<String, Integer> resultMap) {
//Creting new list for storing compared values
List<Map.Entry<String, Integer>> afterCmpList = new LinkedList<Map.Entry<String, Integer>>(resultMap.entrySet());
//Sorting the compared list
Collections.sort(afterCmpList, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String,Integer> obj1, Map.Entry<String, Integer> obj2) {
return (obj2.getValue()).compareTo(obj1.getValue());
}
});
//Moving data from sorted list to fianlList
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for(Map.Entry<String, Integer> aa : afterCmpList) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
}
Looking back on a childhood filled with events and memories, I find it rather difficult to pick one that leaves me with the fabled "warm and fuzzy feelings."
As the daughter of an Air Force major, I had the pleasure of traveling across America in many moving trips.
I have visited the monstrous trees of the Sequoia National Forest, stood on the edge of the Grand Canyon and have jumped on the beds at Caesar's Palace in Lake Tahoe."
"The day I picked my dog up from the pound was one of the happiest days of both of our lives.
I had gone to the pound just a week earlier with the idea that I would just "look" at a puppy.
Of course, you can no more just look at those squiggling little faces so filled with hope and joy than you can stop the sun from setting in the evening.
I knew within minutes of walking in the door that I would get a puppy… but it wasn't until I saw him that I knew I had found my puppy."
"Looking for houses was supposed to be a fun and exciting process.
Unfortunately, none of the ones that we saw seemed to match the specifications that we had established.
They were too small, too impersonal, too close to the neighbors.
After days of finding nothing even close, we began to wonder: was there really a perfect house out there for us?"
"The afternoon grew so glowering that in the sixth inning the arc lights were turned on--always a wan sight in the daytime, like the burning headlights of a funeral procession.
Aided by the gloom, Fisher was slicing through the Sox rookies, and Williams did not come to bat in the seventh.
He was second up in the eighth.
This was almost certainly his last time to come to the plate in Fenway Park, and instead of merely cheering, as we had at his three previous appearances, we stood, all of us, and applauded."
Navneet Ranjan(203050106)- Problem 3(c,d),5
Bandana Raviteja(203050084) - Problem 1,6
Abhishek Chugh(203050046) - Problem 2,3(b),4
Vara Prasad Kolli(203050114)-Problem 3(a),7
Git Link
https://git.cse.iitb.ac.in/navneetranjan/outlab8
References:
https://www.java2novice.com/java-collections-and-util/regex/extract-capture/-question n0 -3(d)
https://stackoverflow.com/questions/3644266/how-can-we-match-an-bn-with-java-regex-question n0 -3(c)
https://www.hackerrank.com/challenges/java-arraylist/problem -question n0 -5
https://www.geeksforgeeks.org/how-to-check-if-a-key-exists-in-a-hashmap-in-java/-question n0 -4
https://www.geeksforgeeks.org/arrays-in-java/-question n0 -4
https://www.geeksforgeeks.org/runnable-interface-in-java/#:~:text=java.-,lang.,run()%20method%20of%20Runnable%20.-question n0 -4
https://dzone.com/articles/getting-current-date-time-in-java-question n0 -4
https://www.geeksforgeeks.org/count-occurrences-elements-list-java/-question n0 -7
https://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html-question n0 -7
https://www.geeksforgeeks.org/different-ways-reading-text-file-java/-question n0 -7
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment