Skip to content

Commit

Permalink
Data Structures, Java, Regex
Browse files Browse the repository at this point in the history
  • Loading branch information
h-ssiqueira committed Aug 6, 2022
1 parent b61ff84 commit 72733a7
Show file tree
Hide file tree
Showing 25 changed files with 692 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

.vscode
*.o
*.class
228 changes: 228 additions & 0 deletions Data_Structures/c/Left_Rotation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
56 changes: 56 additions & 0 deletions Data_Structures/java/Arrays_DS.java
Original file line number Diff line number Diff line change
@@ -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<Integer> reverseArray(List<Integer> 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<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());

List<Integer> res = Result.reverseArray(arr);

bufferedWriter.write(
res.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);

bufferedReader.close();
bufferedWriter.close();
}
}
49 changes: 49 additions & 0 deletions Data_Structures/javascript/Arrays_DS.js
Original file line number Diff line number Diff line change
@@ -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();
}
Binary file added Data_Structures/pdf/array-left-rotation.pdf
Binary file not shown.
32 changes: 32 additions & 0 deletions Data_Structures/python/Arrays_DS.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit 72733a7

Please sign in to comment.