0% found this document useful (0 votes)
23 views5 pages

21BCS7586 PriyanshuMalik Day 5

The document outlines a worksheet for a Java programming assignment for a student named Priyanshu Malik. It includes objectives to demonstrate programming algorithms such as adding strings, regular expression matching, insertion sort, and quicksort. The document provides code implementations for each of these algorithms.

Uploaded by

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

21BCS7586 PriyanshuMalik Day 5

The document outlines a worksheet for a Java programming assignment for a student named Priyanshu Malik. It includes objectives to demonstrate programming algorithms such as adding strings, regular expression matching, insertion sort, and quicksort. The document provides code implementations for each of these algorithms.

Uploaded by

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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Worksheet Day 5
Student Name: Priyanshu Malik UID: 21BCS7586
Branch: CSE Section/Group: 21BCS_CC-652-A
Subject Name: Java Date of Performance: 03/06/24

Aim:
To demonstrate the concept of programming algorithms.

Objective:
1) Add Strings - Given two non-negative integers, num1 and num2 represented as string, return
the sum of num1 and num2 as a string. (Level – Medium)
2) Regular Expression Matching - Given an input string s and a pattern p, implement regular
expression matching with support for '.' and '*' where: '.' Matches any single character. '*'
Matches zero or more of the preceding element. The matching should cover the entire input
string (not partial). (Level – Medium)
3) Insertion Sort - Part 1 - One common task for computers is to sort data. For example, people
might want to see all their files on a computer sorted by size. Since sorting is a simple problem
with many different possible solutions, it is often used to introduce the study of algorithms.
(Level – Medium)
4) Quicksort 1 – Partition - This challenge is a modified version of the algorithm that only
addresses partitioning. (Level – Medium)

Code:
1)
class Solution {
public String addStrings(String num1, String num2) {
StringBuilder str = new StringBuilder(); int i = num1.length() -
1, j = num2.length() - 1, carry = 0, sum; while (i >= 0 || j >= 0
|| carry > 0) { int digit1 = (i >= 0) ? num1.charAt(i--) -
'0' : 0; int digit2 = (j >= 0) ? num2.charAt(j--) - '0' : 0;
sum = digit1 + digit2 + carry; str.append(sum % 10);
carry = sum / 10;
}
return str.reverse().toString();
}
}
2)
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

class Solution { public boolean isMatch(String s, String


p) { int dp[][] = new int[s.length() + 1][p.length()
+ 1]; return sol(s, p, dp);
} public boolean sol(String s, String p, int[][] dp) { int m =
s.length(), n = p.length(); if (n == 0) return m == 0;
if (dp[m][n] != 0) return dp[m][n] == 1; boolean f = (s.length()
> 0 && (s.charAt(0) == p.charAt(0) || p.charAt(0) ==
'.')); boolean ans = false; if (n > 1 && p.charAt(1) == '*') {
ans = (sol(s, p.substring(2), dp) || (f && sol(s.substring(1), p, dp)));
} else { // Yuvraj Singh ans = (f &&
sol(s.substring(1), p.substring(1), dp));
} if (ans)
dp[m][n] = 1; else
dp[m][n] = -1;
return ans;
}
} 3) class Result { public static void insertionSort1(int n,
List<Integer> arr) { int temp = arr.get(n - 1);
int i;
for (i = n - 2; i >= 0; i--) {
if (arr.get(i) > temp) {
arr.set(i + 1, arr.get(i)); for
(Integer num : arr) {
System.out.print(num + " ");
}
System.out.println();
} else {
break;
}
} arr.set(i + 1, temp);
for (Integer num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}

4) public static List<Integer> quickSort(List<Integer>


arr) { int pivot=arr.get(0);
List<Integer> left = new ArrayList<>();
List<Integer> right = new ArrayList<>();
List<Integer> equal = new ArrayList<>();
for (int num : arr) { if (num <
pivot) { left.add(num);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

} else if (num > pivot) {


right.add(num);
} else {
equal.add(num);
}
}
List<Integer> result = new ArrayList<>(left.size() + equal.size() + right.siz
e()); result.addAll(left); result.addAll(equal);
result.addAll(right); return result;
}
}

Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

You might also like