dolphinalt / APCSA-Pages

A minimal, responsive and feature-rich Jekyll theme for technical writing.
https://chirpy.cotes.page
MIT License
1 stars 0 forks source link

Review #5

Closed dolphinalt closed 9 months ago

dolphinalt commented 10 months ago

Fibonacci!

I created the whole of the Fibonacci frontend and backend.

At first, I thought this part of the project would be pretty easy, as it was just creating algorithms to generate a fibonacci list. However, as I progressed, I realized that I needed to learn a lot of things I previously didn't know about Java. For example streams:

Streams learning

public class FibonacciStream extends Fibonacci {
    public ArrayList<Integer> FibonacciStream(int nth) {
        return Stream.iterate(new int[]{1, 1}, fib -> new int[]{fib[1], fib[0] + fib[1]})
                .limit(nth)
                .map(fib -> fib[0])
                .collect(Collectors.toCollection(ArrayList::new));
    }
}

Before this project, I would not be able to tell you what this code did. However, now I can explain the whole thing. We are returning an ArrayList of length nth, where we generate the ArrayList with two integers in the 0 and 1 slots. These will be initialized as 1 and 1. We then generate new items in the list by creating additional ArrayLists that combine the elements of the previous list by adding them with each other. At the end, we collect all the arrays together into one big ArrayList, returning it to the API.

Inheritence within fibo

Here's the fibonacci parent class

package com.nighthawk.spring_portfolio.mvc.fib;
import java.util.ArrayList;

public class Fibonacci {
    public String toStringArr(ArrayList fib) {
        int n = fib.size();
        String array = "";
        for (int i = 0; i < n; i++) {
             array = array + fib.get(i) + " ";
        }
        return array;
    }
}

and an example of one of the individual methods, inheriting the toStringArr abstraction method

package com.nighthawk.spring_portfolio.mvc.fib.fiboAlgorithms;

import java.util.ArrayList;

import com.nighthawk.spring_portfolio.mvc.fib.Fibonacci;

public class FibonacciWhile extends Fibonacci {
    public ArrayList<Integer> FibonacciWhile(int nth) {
        ArrayList<Integer> fib = new ArrayList<>();
        int i=0;
        while (i<nth) {
            if (i == 0) {
                fib.add(1);
            } else if (i == 1) {
                fib.add(1);
            } else {
                fib.add(fib.get(i - 2) + fib.get(i - 1));
            }
            i++;
        }

        return fib;
    }
}

Request API objects

The Fibonacci request controller

package com.nighthawk.spring_portfolio.mvc.fib;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class FibRequest {
    private int length;
}

The Fibonacci API controller

package com.nighthawk.spring_portfolio.mvc.fib;

import java.util.ArrayList;

import org.json.simple.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nighthawk.spring_portfolio.mvc.fib.fiboAlgorithms.FibonacciFor;
import com.nighthawk.spring_portfolio.mvc.fib.fiboAlgorithms.FibonacciRecursive;
import com.nighthawk.spring_portfolio.mvc.fib.fiboAlgorithms.FibonacciStream;
import com.nighthawk.spring_portfolio.mvc.fib.fiboAlgorithms.FibonacciWhile;

@RestController
@RequestMapping("/api/fib")
public class FibonacciApiController {
    private JSONObject body; // last run result
    private HttpStatus status; // last run status
    String last_run = null; // last run day of month

    @GetMapping("/fibFor")
    public ResponseEntity<ArrayList<Integer>> getFibonacciFor(@RequestBody FibRequest request) {
        int startTime = (int)System.currentTimeMillis();

        int length = request.getLength();
        FibonacciFor fibonacciFor = new FibonacciFor();
        ArrayList<Integer> fibonacciSeries = fibonacciFor.FibonacciFor(length);
        System.out.println(fibonacciFor.toStringArr(fibonacciSeries));
        int endTime = (int)System.currentTimeMillis();
        int elapsedTime = endTime - startTime;
        fibonacciSeries.add(elapsedTime);

        return new ResponseEntity<>(fibonacciSeries, HttpStatus.OK);
    }

    @GetMapping("/fibRecursive")
    public ResponseEntity<ArrayList<Integer>> getFibonacciRecursive(@RequestBody FibRequest request) {
        int startTime = (int)System.currentTimeMillis();

        int length = request.getLength();
        FibonacciRecursive fibonacciRecursive = new FibonacciRecursive();
        ArrayList<Integer> fibonacciSeries = fibonacciRecursive.FibonacciRecursive(0, length);
        System.out.println(fibonacciRecursive.toStringArr(fibonacciSeries));
        int endTime = (int)System.currentTimeMillis();
        int elapsedTime = endTime - startTime;
        fibonacciSeries.add(elapsedTime);

        return new ResponseEntity<>(fibonacciSeries, HttpStatus.OK);
    }

    @GetMapping("/fibStream")
    public ResponseEntity<ArrayList<Integer>> getFibonacciStream(@RequestBody FibRequest request) {
        int startTime = (int)System.currentTimeMillis();

        int length = request.getLength();
        FibonacciStream fibonacciStream = new FibonacciStream();
        ArrayList<Integer> fibonacciSeries = fibonacciStream.FibonacciStream(length);
        System.out.println(fibonacciStream.toStringArr(fibonacciSeries));
        int endTime = (int)System.currentTimeMillis();
        int elapsedTime = endTime - startTime;
        fibonacciSeries.add(elapsedTime);

        return new ResponseEntity<>(fibonacciSeries, HttpStatus.OK);
    }

    @GetMapping("/fibWhile")
    public ResponseEntity<ArrayList<Integer>> getFibonacciWhile(@RequestBody FibRequest request) {
        int startTime = (int)System.currentTimeMillis();

        int length = request.getLength();
        FibonacciWhile fibonacciWhile = new FibonacciWhile();
        ArrayList<Integer> fibonacciSeries = fibonacciWhile.FibonacciWhile(length);
        System.out.println(fibonacciWhile.toStringArr(fibonacciSeries));
        int endTime = (int)System.currentTimeMillis();
        int elapsedTime = endTime - startTime;
        fibonacciSeries.add(elapsedTime);

        return new ResponseEntity<>(fibonacciSeries, HttpStatus.OK);
    }
}

Sorting!

I worked on selection sort for the sorting. I only worked on one sort due to me doing the other half of the project completely by my self. Using the animation methods Alex wrote, I built my selection sort around it. This project allowed me to gain a deeper understanding of the innerworkings of Selection sort, allowing me to understand this sorting algorithm and allow for future application into my projects. In addition, this entire project helped me to gain a much deeper understanding of Java and will definitely prepare me well for the AP test!

package com.nighthawk.spring_portfolio.mvc.sorting.SortingAlgorithms;

import java.util.ArrayList;
import java.util.HashMap;

import com.nighthawk.spring_portfolio.mvc.sorting.SortingAnimationGenerator;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class SelectionSort extends SortingAnimationGenerator {
    private ArrayList<HashMap<String, ArrayList<Integer>>> animations;
    public boolean sorted = false;

    public SelectionSort(int length, ArrayList<Integer> array) {
        super(length, array);
        animations = new ArrayList<>();
        long start = System.nanoTime();
        addAnimationEntry(new ArrayList<>(arr), -1, -1);
        this.SelectionSortAnimation();
        addAnimationEntry(new ArrayList<>(arr), -1, -1);
        long end = System.nanoTime();
        Integer elapsedTime = (int) (end - start);
        HashMap<String, ArrayList<Integer>> animationTime = new HashMap<>();
        ArrayList<Integer> timeValue = new ArrayList<>();
        HashMap<String, ArrayList<Integer>> animationSwaps = new HashMap<>();
        ArrayList<Integer> swapsValue = new ArrayList<>();
        swapsValue.add(this.swaps);
        animationSwaps.put("swaps", swapsValue);
        animations.add(animationSwaps);
        timeValue.add(elapsedTime);
        animationTime.put("time", timeValue);
        animations.add(animationTime);
    }

    public void addAnimationEntry(ArrayList<Integer> sortedArr, int num, int move) {
        HashMap<String, ArrayList<Integer>> animationEntry = new HashMap<>();
        ArrayList<Integer> integer = new ArrayList<>();
        animationEntry.put("arr", new ArrayList<>(sortedArr));
        integer.add(num);
        integer.add(move);
        animationEntry.put("int", integer);
        animations.add(animationEntry);
        this.swaps++;
    }

    public ArrayList<HashMap<String, ArrayList<Integer>>> getAnimations() {
        return animations;
    }

    public void SelectionSortAnimation() {
        if (!sorted) {
            selection();
        }
    }

    public void selection() {
        int n = arr.size();
        int i, j, temp;
        boolean swapped;

        for (i=0; i < n - 1; i++) {
            swapped = false;
            int min_idx = i;
            for (j = 0; j < n - i - 1; j++) {
                if (arr.get(j) > arr.get(min_idx)) {
                    min_idx = j;
                }
                temp = arr.get(min_idx);
                arr.set(min_idx, arr.get(i));
                arr.set(i, temp);
            }

            if (!swapped) {
                addAnimationEntry(arr, i, j + 1);
                break;
            }
        }
        sorted = true;
    }
}

Request controller:

package com.nighthawk.spring_portfolio.mvc.sorting;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import java.util.ArrayList;

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data
public class SelectionRequest {
    private int length;
    private ArrayList<Integer> array;
}

API controller

@PostMapping("/selection")
    public ResponseEntity<ArrayList<HashMap<String, ArrayList<Integer>>>> getHeapAnimations(@RequestBody SelectionRequest request) {
        int length = request.getLength();
        ArrayList<Integer> array = request.getArray();
        SelectionSort selectionSort = new SelectionSort(length, array);
        System.out.println(selectionSort.toStringArr());
        return new ResponseEntity<>(selectionSort.getAnimations(), HttpStatus.OK);
    }
github-actions[bot] commented 9 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.