diff --git a/.gitignore b/.gitignore index 7393e46..7380e67 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ - +.vscode *.o +*.class \ No newline at end of file diff --git a/Data_Structures/c/Left_Rotation.c b/Data_Structures/c/Left_Rotation.c new file mode 100644 index 0000000..fea7167 --- /dev/null +++ b/Data_Structures/c/Left_Rotation.c @@ -0,0 +1,228 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char* readline(); +char* ltrim(char*); +char* rtrim(char*); +char** split_string(char*); + +int parse_int(char*); + +/* + * Complete the 'rotateLeft' function below. + * + * The function is expected to return an INTEGER_ARRAY. + * The function accepts following parameters: + * 1. INTEGER d + * 2. INTEGER_ARRAY arr + */ + +/* + * To return the integer array from the function, you should: + * - Store the size of the array to be returned in the result_count variable + * - Allocate the array statically or dynamically + * + * For example, + * int* return_integer_array_using_static_allocation(int* result_count) { + * *result_count = 5; + * + * static int a[5] = {1, 2, 3, 4, 5}; + * + * return a; + * } + * + * int* return_integer_array_using_dynamic_allocation(int* result_count) { + * *result_count = 5; + * + * int *a = malloc(5 * sizeof(int)); + * + * for (int i = 0; i < 5; i++) { + * *(a + i) = i + 1; + * } + * + * return a; + * } + * + */ +int* rotateLeft(int d, int arr_count, int* arr, int* result_count) { + *result_count = arr_count; + if(arr_count == d) + return arr; + int x,y,i; + while(d > 0){ + x = arr[0]; + for(i = arr_count-1; i >= 0; i--){ + y = arr[i]; + arr[i] = x; + x = y; + } + d--; + } + return arr; +} + +int main() +{ + FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w"); + + char** first_multiple_input = split_string(rtrim(readline())); + + int n = parse_int(*(first_multiple_input + 0)); + + int d = parse_int(*(first_multiple_input + 1)); + + char** arr_temp = split_string(rtrim(readline())); + + int* arr = malloc(n * sizeof(int)); + + for (int i = 0; i < n; i++) { + int arr_item = parse_int(*(arr_temp + i)); + + *(arr + i) = arr_item; + } + + int result_count; + int* result = rotateLeft(d, n, arr, &result_count); + + for (int i = 0; i < result_count; i++) { + fprintf(fptr, "%d", *(result + i)); + + if (i != result_count - 1) { + fprintf(fptr, " "); + } + } + + fprintf(fptr, "\n"); + + fclose(fptr); + + return 0; +} + +char* readline() { + size_t alloc_length = 1024; + size_t data_length = 0; + + char* data = malloc(alloc_length); + + while (true) { + char* cursor = data + data_length; + char* line = fgets(cursor, alloc_length - data_length, stdin); + + if (!line) { + break; + } + + data_length += strlen(cursor); + + if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { + break; + } + + alloc_length <<= 1; + + data = realloc(data, alloc_length); + + if (!data) { + data = '\0'; + + break; + } + } + + if (data[data_length - 1] == '\n') { + data[data_length - 1] = '\0'; + + data = realloc(data, data_length); + + if (!data) { + data = '\0'; + } + } else { + data = realloc(data, data_length + 1); + + if (!data) { + data = '\0'; + } else { + data[data_length] = '\0'; + } + } + + return data; +} + +char* ltrim(char* str) { + if (!str) { + return '\0'; + } + + if (!*str) { + return str; + } + + while (*str != '\0' && isspace(*str)) { + str++; + } + + return str; +} + +char* rtrim(char* str) { + if (!str) { + return '\0'; + } + + if (!*str) { + return str; + } + + char* end = str + strlen(str) - 1; + + while (end >= str && isspace(*end)) { + end--; + } + + *(end + 1) = '\0'; + + return str; +} + +char** split_string(char* str) { + char** splits = NULL; + char* token = strtok(str, " "); + + int spaces = 0; + + while (token) { + splits = realloc(splits, sizeof(char*) * ++spaces); + + if (!splits) { + return splits; + } + + splits[spaces - 1] = token; + + token = strtok(NULL, " "); + } + + return splits; +} + +int parse_int(char* str) { + char* endptr; + int value = strtol(str, &endptr, 10); + + if (endptr == str || *endptr != '\0') { + exit(EXIT_FAILURE); + } + + return value; +} diff --git a/Data_Structures/java/Arrays_DS.java b/Data_Structures/java/Arrays_DS.java new file mode 100644 index 0000000..29119c2 --- /dev/null +++ b/Data_Structures/java/Arrays_DS.java @@ -0,0 +1,56 @@ +import java.io.*; +import java.math.*; +import java.security.*; +import java.text.*; +import java.util.*; +import java.util.concurrent.*; +import java.util.function.*; +import java.util.regex.*; +import java.util.stream.*; +import static java.util.stream.Collectors.joining; +import static java.util.stream.Collectors.toList; + +class Result { + + /* + * Complete the 'reverseArray' function below. + * + * The function is expected to return an INTEGER_ARRAY. + * The function accepts INTEGER_ARRAY a as parameter. + */ + + public static List reverseArray(List a) { + // Write your code here + Integer[] list = new Integer[a.size()]; + int j = a.size()-1; + for(Integer i : a) + list[j--] = i; + return Arrays.asList(list); + } + +} + +public class Arrays_DS { + public static void main(String[] args) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH"))); + + int arrCount = Integer.parseInt(bufferedReader.readLine().trim()); + + List arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" ")) + .map(Integer::parseInt) + .collect(toList()); + + List res = Result.reverseArray(arr); + + bufferedWriter.write( + res.stream() + .map(Object::toString) + .collect(joining(" ")) + + "\n" + ); + + bufferedReader.close(); + bufferedWriter.close(); + } +} diff --git a/Data_Structures/javascript/Arrays_DS.js b/Data_Structures/javascript/Arrays_DS.js new file mode 100644 index 0000000..6606a6d --- /dev/null +++ b/Data_Structures/javascript/Arrays_DS.js @@ -0,0 +1,49 @@ +'use strict'; + +const fs = require('fs'); + +process.stdin.resume(); +process.stdin.setEncoding('utf-8'); + +let inputString = ''; +let currentLine = 0; + +process.stdin.on('data', function(inputStdin) { + inputString += inputStdin; +}); + +process.stdin.on('end', function() { + inputString = inputString.split('\n'); + + main(); +}); + +function readLine() { + return inputString[currentLine++]; +} + +/* + * Complete the 'reverseArray' function below. + * + * The function is expected to return an INTEGER_ARRAY. + * The function accepts INTEGER_ARRAY a as parameter. + */ + +function reverseArray(a) { + // Write your code here + return a.reverse(); +} + +function main() { + const ws = fs.createWriteStream(process.env.OUTPUT_PATH); + + const arrCount = parseInt(readLine().trim(), 10); + + const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10)); + + const res = reverseArray(arr); + + ws.write(res.join(' ') + '\n'); + + ws.end(); +} diff --git a/Data_Structures/pdf/array-left-rotation.pdf b/Data_Structures/pdf/array-left-rotation.pdf new file mode 100644 index 0000000..7d431b0 Binary files /dev/null and b/Data_Structures/pdf/array-left-rotation.pdf differ diff --git a/Data_Structures/python/Arrays_DS.py b/Data_Structures/python/Arrays_DS.py new file mode 100644 index 0000000..80349ea --- /dev/null +++ b/Data_Structures/python/Arrays_DS.py @@ -0,0 +1,32 @@ +#!/bin/python3 + +import math +import os +import random +import re +import sys + +# +# Complete the 'reverseArray' function below. +# +# The function is expected to return an INTEGER_ARRAY. +# The function accepts INTEGER_ARRAY a as parameter. +# + +def reverseArray(a): + # Write your code here + return a[::-1] + +if __name__ == '__main__': + fptr = open(os.environ['OUTPUT_PATH'], 'w') + + arr_count = int(input().strip()) + + arr = list(map(int, input().rstrip().split())) + + res = reverseArray(arr) + + fptr.write(' '.join(map(str, res))) + fptr.write('\n') + + fptr.close() diff --git a/Java/java/Covariant_Return_Types.java b/Java/java/Covariant_Return_Types.java new file mode 100644 index 0000000..1503284 --- /dev/null +++ b/Java/java/Covariant_Return_Types.java @@ -0,0 +1,63 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +//Complete the classes below +class Flower { + public String whatsYourName() { + return "I have many names and types."; + } +} + +class Jasmine extends Flower { + @Override + public String whatsYourName() { + return "Jasmine"; + } +} + +class Lily extends Flower { + @Override + public String whatsYourName() { + return "Lily"; + } +} + +class Region { + public Flower yourNationalFlower() { + return new Flower(); + } +} + +class WestBengal extends Region { + @Override + public Jasmine yourNationalFlower() { + return new Jasmine(); + } +} + +class AndhraPradesh extends Region { + @Override + public Lily yourNationalFlower() { + return new Lily(); + } +} + + +public class Covariant_Return_Types { + public static void main(String[] args) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + String s = reader.readLine().trim(); + Region region = null; + switch (s) { + case "WestBengal": + region = new WestBengal(); + break; + case "AndhraPradesh": + region = new AndhraPradesh(); + break; + } + Flower flower = region.yourNationalFlower(); + System.out.println(flower.whatsYourName()); + } +} \ No newline at end of file diff --git a/Java/java/Stack.java b/Java/java/Stack.java new file mode 100644 index 0000000..c450948 --- /dev/null +++ b/Java/java/Stack.java @@ -0,0 +1,33 @@ +import java.util.*; +class Solution{ + + public static void main(String []argh) + { + Scanner sc = new Scanner(System.in); + + while (sc.hasNext()) { + String input=sc.next(); + //Complete the code + Deque stack = new ArrayDeque<>(); + if(input.length() % 2 != 0) + System.out.println("false"); + else{ + for(int i = 0; i < input.length(); i++){ + if(stack.isEmpty()) + stack.addFirst(input.charAt(i)); + else{ + if((input.charAt(i) == ')' && stack.peek() == '(') || (input.charAt(i) == '}' && stack.peek() == '{') || (input.charAt(i) == ']' && stack.peek() == '[')) + stack.pop(); + else + stack.addFirst(input.charAt(i)); + } + } + System.out.println(stack.isEmpty() ? "true" : "false");; + } + } + + } +} + + + diff --git a/Java/java/Varargs_Simple_Addition.java b/Java/java/Varargs_Simple_Addition.java new file mode 100644 index 0000000..7dd6869 --- /dev/null +++ b/Java/java/Varargs_Simple_Addition.java @@ -0,0 +1,55 @@ +import java.io.*; +import java.lang.reflect.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +//Write your code here +class Add { + public void add(int... args) { + int sum = 0; + for(int i = 0; i < args.length; i++){ + sum += args[i]; + System.out.print(i+1 != args.length ? args[i] + "+" : args[i]); + } + System.out.println("=" + sum); + } +} + + +public class Varargs_Simple_Addition { + + public static void main(String[] args) { + try{ + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + int n1=Integer.parseInt(br.readLine()); + int n2=Integer.parseInt(br.readLine()); + int n3=Integer.parseInt(br.readLine()); + int n4=Integer.parseInt(br.readLine()); + int n5=Integer.parseInt(br.readLine()); + int n6=Integer.parseInt(br.readLine()); + Add ob=new Add(); + ob.add(n1,n2); + ob.add(n1,n2,n3); + ob.add(n1,n2,n3,n4,n5); + ob.add(n1,n2,n3,n4,n5,n6); + Method[] methods=Add.class.getDeclaredMethods(); + Set set=new HashSet<>(); + boolean overload=false; + for(int i=0;i ## [Java](Java) ### Advanced * [Can_You_Access](Java/pdf/can-you-access.pdf) - [Solution](Java/java/Can_You_Access.java) +* [Covariant_Return_Types](Java/pdf/java-covariance.pdf) - [Solution](Java/java/Covariant_Return_Types.java) * [Factory_pattern](Java/pdf/java-factory.pdf) - [Solution](Java/java/Factory_pattern.java) +* [Varargs_Simple_Addition](Java/pdf/simple-addition-varargs.pdf) - [Solution](Java/java/Varargs_Simple_Addition.java) ### Big Number * [Bigdecimal](Java/pdf/java-bigdecimal.pdf) - [Solution](Java/java/Bigdecimal.java) @@ -145,6 +153,7 @@ Using: * [Generics](Java/pdf/java-generics.pdf) - [Solution](Java/java/Generics.java) * [Hashsets](Java/pdf/java-hashset.pdf) - [Solution](Java/java/Hashsets.java) * [Lists](Java/pdf/java-list.pdf) - [Solution](Java/java/Lists.java) +* [Stack](Java/pdf/java-stack.pdf) - [Solution](Java/java/Stack.java) * [Subarray](Java/pdf/java-negative-subarray.pdf) - [Solution](Java/java/Subarray.java) ### Exception Handling @@ -228,6 +237,13 @@ Problem | Java | Javascript | Python [Matching_Whitespace_Non_Whitespace_Character](Regex/pdf/matching-whitespace-non-whitespace-character.pdf) | [Solution](Regex/java/Matching_Whitespace_Non_Whitespace_Character.java) | [Solution](Regex/javascript/Matching_Whitespace_Non_Whitespace_Character.js) | [Solution](Regex/python/Matching_Whitespace_Non_Whitespace_Character.py) [Matching_Word_Non_Word_Character](Regex/pdf/matching-word-non-word.pdf) | [Solution](Regex/java/Matching_Word_Non_Word_Character.java) | [Solution](Regex/javascript/Matching_Word_Non_Word_Character.js) | [Solution](Regex/python/Matching_Word_Non_Word_Character.py) +### Character Class +Problem | Java | Javascript | Python +:---: | :---: | :---: | :---: +[Excluding_Specific_Characters](Regex/pdf/excluding-specific-characters.pdf) | [Solution](Regex/java/Excluding_Specific_Characters.java) | [Solution](Regex/javascript/Excluding_Specific_Characters.js) | [Solution](Regex/python/Excluding_Specific_Characters.py) +[Matching_Character_Ranges](Regex/pdf/matching-range-of-characters.pdf) | [Solution](Regex/java/Matching_Character_Ranges.java) | [Solution](Regex/javascript/Matching_Character_Ranges.js) | [Solution](Regex/python/Matching_Character_Ranges.py) +[Matching_Specific_Characters](Regex/pdf/matching-specific-characters.pdf) | [Solution](Regex/java/Matching_Specific_Characters.java) | [Solution](Regex/javascript/Matching_Specific_Characters.js) | [Solution](Regex/python/Matching_Specific_Characters.py) + ## [SQL](SQL) diff --git a/Regex/java/Excluding_Specific_Characters.java b/Regex/java/Excluding_Specific_Characters.java new file mode 100644 index 0000000..538e174 --- /dev/null +++ b/Regex/java/Excluding_Specific_Characters.java @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Excluding_Specific_Characters { + + public static void main(String[] args) { + + Regex_Test tester = new Regex_Test(); + tester.checker("^[^\\d][^aeiou][^DFbc][^\\r\\n\\t\\f\\s][^AEIOU][^,.]$"); // Use \\ instead of using \ + + } +} + +class Regex_Test { + + public void checker(String Regex_Pattern){ + + Scanner Input = new Scanner(System.in); + String Test_String = Input.nextLine(); + Pattern p = Pattern.compile(Regex_Pattern); + Matcher m = p.matcher(Test_String); + System.out.println(m.find()); + } + +} \ No newline at end of file diff --git a/Regex/java/Matching_Character_Ranges.java b/Regex/java/Matching_Character_Ranges.java new file mode 100644 index 0000000..03019b8 --- /dev/null +++ b/Regex/java/Matching_Character_Ranges.java @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Matching_Character_Ranges { + + public static void main(String[] args) { + + Regex_Test tester = new Regex_Test(); + tester.checker("^[a-z][1-9][^a-z][^A-Z][A-Z]"); // Use \\ instead of using \ + + } +} + +class Regex_Test { + + public void checker(String Regex_Pattern){ + + Scanner Input = new Scanner(System.in); + String Test_String = Input.nextLine(); + Pattern p = Pattern.compile(Regex_Pattern); + Matcher m = p.matcher(Test_String); + System.out.println(m.find()); + } + +} \ No newline at end of file diff --git a/Regex/java/Matching_Specific_Characters.java b/Regex/java/Matching_Specific_Characters.java new file mode 100644 index 0000000..3113246 --- /dev/null +++ b/Regex/java/Matching_Specific_Characters.java @@ -0,0 +1,28 @@ +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Matching_Specific_Characters { + + public static void main(String[] args) { + + Regex_Test tester = new Regex_Test(); + tester.checker("^[123][120][xs0][30aA][xsu][\\.\\,]$"); // Use \\ instead of using \ + + } +} + +class Regex_Test { + + public void checker(String Regex_Pattern){ + + Scanner Input = new Scanner(System.in); + String Test_String = Input.nextLine(); + Pattern p = Pattern.compile(Regex_Pattern); + Matcher m = p.matcher(Test_String); + System.out.println(m.find()); + } + +} \ No newline at end of file diff --git a/Regex/javascript/Excluding_Specific_Characters.js b/Regex/javascript/Excluding_Specific_Characters.js new file mode 100644 index 0000000..3222fa2 --- /dev/null +++ b/Regex/javascript/Excluding_Specific_Characters.js @@ -0,0 +1,18 @@ +var Regex_Pattern = /^[^\d][^aeiou][^DFbc][^\r\n\t\f\s][^AEIOU][^,.]$/; //Do not delete '/'. Replace __________ with your regex. + +function processData(Test_String) { + //Enter your code here + + console.log(!!Test_String.match(Regex_Pattern)); +} + +process.stdin.resume(); +process.stdin.setEncoding("ascii"); +_input = ""; +process.stdin.on("data", function (input) { + _input += input; +}); + +process.stdin.on("end", function () { + processData(_input); +}); diff --git a/Regex/javascript/Matching_Character_Ranges.js b/Regex/javascript/Matching_Character_Ranges.js new file mode 100644 index 0000000..f0ee19b --- /dev/null +++ b/Regex/javascript/Matching_Character_Ranges.js @@ -0,0 +1,18 @@ +var Regex_Pattern = /^[a-z][1-9][^a-z][^A-Z][A-Z]/; //Do not delete '/'. Replace __________ with your regex. + +function processData(Test_String) { + //Enter your code here + + console.log(!!Test_String.match(Regex_Pattern)); +} + +process.stdin.resume(); +process.stdin.setEncoding("ascii"); +_input = ""; +process.stdin.on("data", function (input) { + _input += input; +}); + +process.stdin.on("end", function () { + processData(_input); +}); diff --git a/Regex/javascript/Matching_Specific_Characters.js b/Regex/javascript/Matching_Specific_Characters.js new file mode 100644 index 0000000..61d2d81 --- /dev/null +++ b/Regex/javascript/Matching_Specific_Characters.js @@ -0,0 +1,18 @@ +var Regex_Pattern = /^[1-3][0-2][x0s][30Aa][xus][.,]$/; //Do not delete '/'. Replace __________ with your regex. + +function processData(Test_String) { + //Enter your code here + + console.log(!!Test_String.match(Regex_Pattern)); +} + +process.stdin.resume(); +process.stdin.setEncoding("ascii"); +_input = ""; +process.stdin.on("data", function (input) { + _input += input; +}); + +process.stdin.on("end", function () { + processData(_input); +}); diff --git a/Regex/pdf/excluding-specific-characters.pdf b/Regex/pdf/excluding-specific-characters.pdf new file mode 100644 index 0000000..be2ad4e Binary files /dev/null and b/Regex/pdf/excluding-specific-characters.pdf differ diff --git a/Regex/pdf/matching-range-of-characters.pdf b/Regex/pdf/matching-range-of-characters.pdf new file mode 100644 index 0000000..38cb59f Binary files /dev/null and b/Regex/pdf/matching-range-of-characters.pdf differ diff --git a/Regex/pdf/matching-specific-characters.pdf b/Regex/pdf/matching-specific-characters.pdf new file mode 100644 index 0000000..8985685 Binary files /dev/null and b/Regex/pdf/matching-specific-characters.pdf differ diff --git a/Regex/python/Excluding_Specific_Characters.py b/Regex/python/Excluding_Specific_Characters.py new file mode 100644 index 0000000..bb706ce --- /dev/null +++ b/Regex/python/Excluding_Specific_Characters.py @@ -0,0 +1,5 @@ +Regex_Pattern = r'^[^\d][^aeiou][^DFbc][^\r\n\t\f\s][^AEIOU][^,.]$' # Do not delete 'r'. + +import re + +print(str(bool(re.search(Regex_Pattern, input()))).lower()) \ No newline at end of file diff --git a/Regex/python/Matching_Character_Ranges.py b/Regex/python/Matching_Character_Ranges.py new file mode 100644 index 0000000..99031b4 --- /dev/null +++ b/Regex/python/Matching_Character_Ranges.py @@ -0,0 +1,5 @@ +Regex_Pattern = r'^[a-z][1-9][^a-z][^A-Z][A-Z]' # Do not delete 'r'. + +import re + +print(str(bool(re.search(Regex_Pattern, input()))).lower()) \ No newline at end of file diff --git a/Regex/python/Matching_Specific_Characters.py b/Regex/python/Matching_Specific_Characters.py new file mode 100644 index 0000000..2f158af --- /dev/null +++ b/Regex/python/Matching_Specific_Characters.py @@ -0,0 +1,5 @@ +Regex_Pattern = r'^[1-3][0-2][sx0][30Aa][sux][.,]$' # Do not delete 'r'. + +import re + +print(str(bool(re.search(Regex_Pattern, input()))).lower()) \ No newline at end of file