Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/main/java/Lecture4Exercises.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import java.lang.reflect.Array;
import java.util.Locale;

public class Lecture4Exercises {

/*
* implement a function that returns factorial of given n
* lecture 4 page 15
*/
public long factorial(int n) {
return 0L;
int ans = 1,i=n;

while (i!=0){
ans*=i;
i--;
}

return ans;
}

/*
Expand All @@ -14,15 +24,35 @@ public long factorial(int n) {
* lecture 4 page 19
*/
public long fibonacci(int n) {
return 0;
Long a= Long.valueOf(1),b= Long.valueOf(1),i= Long.valueOf(2);

while(i<n){
a+=b;
b+=a;
i+=2;
}

if(i==n){
return b;
}

else{
return a;
}
}

/*
* implement a function that return reverse of a given word
* lecture 4 page 19
*/
public String reverse(String word) {
return null;
String reversed="";

for (int i=word.length()-1;i>=0;i--){
reversed = reversed + word.charAt(i);
}

return reversed;
}

/*
Expand All @@ -32,7 +62,10 @@ public String reverse(String word) {
* lecture 4 page 19
*/
public boolean isPalindrome(String line) {
return false;
String line1 = line;
line1 = line1.replaceAll("\\s", "");
line1 = line1.toLowerCase(Locale.ROOT);
return reverse(line1).equals(line1);
}

/*
Expand All @@ -47,6 +80,18 @@ public boolean isPalindrome(String line) {
* lecture 4 page 26
*/
public char[][] dotPlot(String str1, String str2) {
return null;
char [][] ans = new char[str1.length()][str2.length()];
for (int i=0;i<str1.length();i++){
for (int j=0;j<str2.length();j++){
if (str1.charAt(i)==str2.charAt(j)){
ans[i][j]='*';
}
else{
ans[i][j]=' ';
}
}
}

return ans;
}
}
97 changes: 94 additions & 3 deletions src/main/java/Lecture5Exercises.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.Random;

public class Lecture5Exercises {

/*
Expand All @@ -6,7 +9,11 @@ public class Lecture5Exercises {
* lecture 5 page 14
*/
public String weakPassword(int length) {
return null;
String password = "";
for (int i = 0; i < length; i++) {
password = password + (char) randomNum(97, 122);
}
return password;
}

/*
Expand All @@ -15,7 +22,26 @@ public String weakPassword(int length) {
* lecture 5 page 14
*/
public String strongPassword(int length) throws Exception {
return null;
String password;
ArrayList <Character> opts = new ArrayList<>();
optMaker(opts);

while (true) {
password = "";


for (int i = 0; i < length; i++) {
password = password + opts.get(randomNum(0, opts.size() - 1));
}

System.out.println(password);

String par ="([a-z]+)(\\d+)([+*?^$(){}&|]+)";

if (password.matches(par)){
return password;
}
}
}

/*
Expand All @@ -27,6 +53,71 @@ public String strongPassword(int length) throws Exception {
* lecture 5 page 17
*/
public boolean isFiboBin(int n) {
return false;
int i=0;
while (fibonacci(i) + binSum(i) < n){
i++;
}

return fibonacci(i) + binSum(i) == n;
}

public int randomNum(int a, int b) {
Random r = new Random();
int result = r.nextInt(b - a + 1) + a;
return result;
}



static int binSum(int n) {
int[] binaryNum = new int[1000];

int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}

int sum = 0;
for (int value : binaryNum) {
sum += value;
}

return sum;
}

public long fibonacci(int n) {
Long a= Long.valueOf(1),b= Long.valueOf(1),i= Long.valueOf(2);

while(i<n){
a+=b;
b+=a;
i+=2;
}

if(i==n){
return b;
}

else{
return a;
}
}

public static void optMaker(ArrayList<Character> opts){
for(int i=48;i<58;i++){
opts.add((char) i);
}

for(int i=97;i<123;i++){
opts.add((char) i);
}

String specials = ".+*?^$(){}|";

for (int i=0;i<specials.length();i++){
opts.add(specials.charAt(i));
}
}
}
66 changes: 60 additions & 6 deletions src/main/java/Lecture6Exercises.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Lecture6Exercises {
Expand All @@ -8,7 +10,12 @@ public class Lecture6Exercises {
* lecture 6 page 16
*/
public long calculateEvenSum(int[] arr) {
return 0L;
long sum=0;
for(int i=0;i<arr.length;){
sum+=arr[i];
i+=2;
}
return sum;
}

/*
Expand All @@ -17,15 +24,33 @@ public long calculateEvenSum(int[] arr) {
* lecture 6 page 16
*/
public int[] reverseArray(int[] arr) {
return null;
int[] reversed = new int[arr.length];
int j=0;
for(int i=arr.length - 1;i>=0;i--){
reversed[j]=arr[i];
j++;
}
return reversed;
}

/*
* implement a function that calculate product of two 2-dim matrices
* lecture 6 page 21
*/
public double[][] matrixProduct(double[][] m1, double[][] m2) throws RuntimeException {
return null;
int row = m1.length;
int coloumn_row = m1[0].length;
int coloumn = m2[0].length;
double[][] r = new double[row][coloumn];
for (int i=0;i<row;i++){
for(int j=0;j<coloumn;j++){
r[i][j]=0;
for(int k=0;k<coloumn_row;k++){
r[i][j]+=m1[i][k]*m2[k][j];
}
}
}
return r;
}

/*
Expand All @@ -34,7 +59,11 @@ public double[][] matrixProduct(double[][] m1, double[][] m2) throws RuntimeExce
* lecture 6 page 30
*/
public List<List<String>> arrayToList(String[][] names) {
return null;
List<List<String>> ans = new ArrayList<>();
for (int i=0;i< names.length;i++){
ans.add(new ArrayList<String>(List.of(names[i])));
}
return ans;
}

/*
Expand All @@ -43,14 +72,39 @@ public List<List<String>> arrayToList(String[][] names) {
* lecture 6 page 30
*/
public List<Integer> primeFactors(int n) {
return null;
List<Integer> primes = new ArrayList<Integer>();
for (int i=2;i<=n;i++){
if (isPrime(i)){
if (n%i==0){
primes.add(i);
}
}
}

return primes;
}

/*
* implement a function that return a list of words in a given string
* lecture 6 page 30
*/
public List<String> extractWord(String line) {
return null;
line = line.replaceAll("[$&+,:;=?@#|'<>.-^*()%!]" , "");
List<String> myList = new ArrayList<String>(Arrays.asList(line.split(" ")));
return myList;
}

public boolean isPrime(int num)
{
if(num<=1)
{
return false;
}
for(int i=2;i<=num/2;i++)
{
if((num%i)==0)
return false;
}
return true;
}
}